Blame view

netcore/src/Modularity/Extend/NCC.Extend/UavDeviceLogService.cs 4.62 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  using NCC.Common.Core.Manager;
  using NCC.Common.Enum;
  using NCC.Common.Extension;
  using NCC.Common.Filter;
  using NCC.Dependency;
  using NCC.DynamicApiController;
  using NCC.FriendlyException;
  using NCC.Extend.Interfaces.UavDeviceLog;
  using Mapster;
  using Microsoft.AspNetCore.Mvc;
  using SqlSugar;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using NCC.Extend.Entitys;
  using NCC.Extend.Entitys.Dto.UavDeviceLog;
  using Yitter.IdGenerator;
  using NCC.Common.Helper;
  using NCC.JsonSerialization;
  using Microsoft.AspNetCore.Authorization;
  
  namespace NCC.Extend.UavDeviceLog
  {
      /// <summary>
      /// 设备串口日志服务
      /// </summary>
      [ApiDescriptionSettings(Tag = "设备串口日志服务",Name = "UavDeviceLog", Order = 200)]
      [Route("api/Extend/[controller]")]
      public class UavDeviceLogService : IUavDeviceLogService, IDynamicApiController, ITransient
      {
          private readonly ISqlSugarRepository<UavDeviceLogEntity> _uavDeviceLogRepository;
          private readonly SqlSugarScope _db;
          private readonly IUserManager _userManager;
  
          /// <summary>
          /// 初始化一个<see cref="UavDeviceLogService"/>类型的新实例
          /// </summary>
          public UavDeviceLogService(
              ISqlSugarRepository<UavDeviceLogEntity> uavDeviceLogRepository,
              IUserManager userManager)
          {
              _uavDeviceLogRepository = uavDeviceLogRepository;            
              _db = _uavDeviceLogRepository.Context;
              _userManager = userManager;
          }
  
          /// <summary>
          /// 获取设备串口日志
          /// </summary>
          /// <param name="id">参数</param>
          /// <returns></returns>
          [HttpGet("{id}")]
          public async Task<dynamic> GetInfo(string id)
          {
              var entity = await _db.Queryable<UavDeviceLogEntity>().FirstAsync(p => p.Id == id);
              var output = entity.Adapt<UavDeviceLogInfoOutput>();
              return output;
          }
  
          /// <summary>
  		/// 获取设备串口日志列表
  		/// </summary>
  		/// <param name="input">请求参数</param>
  		/// <returns></returns>
          [HttpGet("")]
          public async Task<dynamic> GetList([FromQuery] UavDeviceLogListQueryInput input)
          {
              var sidx = input.sidx == null ? "id" : input.sidx;
              var data = await _db.Queryable<UavDeviceLogEntity>()
                  .WhereIF(!string.IsNullOrEmpty(input.deviceCode), p => p.DeviceCode.Contains(input.deviceCode))
                  .Select(it=> new UavDeviceLogListOutput
                  {
                      id = it.Id,
                      deviceCode=it.DeviceCode,
                      content=it.Content,
                  }).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
                  return PageResult<UavDeviceLogListOutput>.SqlSugarPageResult(data);
          }
  
          /// <summary>
          /// 新建设备串口日志
          /// </summary>
          /// <param name="input">参数</param>
          /// <returns></returns>
          [HttpPost("")]
          [AllowAnonymous]
          public async Task Create([FromBody] UavDeviceLogCrInput input)
          {
              var entity = input.Adapt<UavDeviceLogEntity>();
              entity.Id = YitIdHelper.NextId().ToString();
              entity.CreatTIme = DateTime.Now;
              var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
          }
  
          /// <summary>
          /// 更新设备串口日志
          /// </summary>
          /// <param name="id">主键</param>
          /// <param name="input">参数</param>
          /// <returns></returns>
          [HttpPut("{id}")]
          public async Task Update(string id, [FromBody] UavDeviceLogUpInput input)
          {
              var entity = input.Adapt<UavDeviceLogEntity>();
              var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
          }
  
          /// <summary>
          /// 删除设备串口日志
          /// </summary>
          /// <returns></returns>
          [HttpDelete("{id}")]
          public async Task Delete(string id)
          {
              var entity = await _db.Queryable<UavDeviceLogEntity>().FirstAsync(p => p.Id == id);
              _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
              var isOk = await _db.Deleteable<UavDeviceLogEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
              if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
          }
      }
  }