UavDeviceCellService.cs
7.18 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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.UavDeviceCell;
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.UavDeviceCell;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
namespace NCC.Extend.UavDeviceCell
{
/// <summary>
/// 机柜格子管理服务
/// </summary>
[ApiDescriptionSettings(Tag = "机柜格子管理服务", Name = "UavDeviceCell", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavDeviceCellService : IUavDeviceCellService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<UavDeviceCellEntity> _uavDeviceCellRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
/// <summary>
/// 初始化一个<see cref="UavDeviceCellService"/>类型的新实例
/// </summary>
public UavDeviceCellService(
ISqlSugarRepository<UavDeviceCellEntity> uavDeviceCellRepository,
IUserManager userManager)
{
_uavDeviceCellRepository = uavDeviceCellRepository;
_db = _uavDeviceCellRepository.Context;
_userManager = userManager;
}
#region 获取机柜格子管理
/// <summary>
/// 获取机柜格子管理
/// </summary>
/// <param name="id">参数</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
var entity = await _db.Queryable<UavDeviceCellEntity>().FirstAsync(p => p.Id == id);
var output = entity.Adapt<UavDeviceCellInfoOutput>();
return output;
}
#endregion
#region 获取机柜格子管理列表
/// <summary>
/// 获取机柜格子管理列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] UavDeviceCellListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable<UavDeviceCellEntity>()
.WhereIF(!string.IsNullOrEmpty(input.deviceId), p => p.DeviceId.Equals(input.deviceId))
.WhereIF(!string.IsNullOrEmpty(input.cellCode), p => p.CellCode.Contains(input.cellCode))
.Select(it => new UavDeviceCellListOutput
{
id = it.Id,
deviceId = it.DeviceId,
deviceName = SqlFunc.Subqueryable<UavDeviceEntity>().Where(u => u.Id == it.DeviceId).Select(u => u.DeviceName),
cellCode = it.CellCode,
status = it.Status,
rfid1 = it.Rfid1,
rfid2 = it.Rfid2,
uavCode = it.UavCode,
createTime = it.CreateTime,
updateTime = it.UpdateTime,
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UavDeviceCellListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 新建机柜格子管理
/// <summary>
/// 新建机柜格子管理
/// </summary>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPost("")]
public async Task Create([FromBody] UavDeviceCellCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt<UavDeviceCellEntity>();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 更新机柜格子管理
/// <summary>
/// 更新机柜格子管理
/// </summary>
/// <param name="id">主键</param>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavDeviceCellUpInput input)
{
var entity = input.Adapt<UavDeviceCellEntity>();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除机柜格子管理
/// <summary>
/// 删除机柜格子管理
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable<UavDeviceCellEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable<UavDeviceCellEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
#region 调整格位修改时间
/// <summary>
/// 调整格位修改时间
/// </summary>
/// <param name="input">调整时间输入参数</param>
/// <returns></returns>
[HttpPost("AdjustUpdateTime")]
public async Task AdjustUpdateTime([FromBody] AdjustUpdateTimeInput input)
{
try
{
if (input == null)
{
throw NCCException.Oh(ErrorCode.COM1000, "输入参数不能为空");
}
if (string.IsNullOrEmpty(input.CellId))
{
throw NCCException.Oh(ErrorCode.COM1000, "格位ID不能为空");
}
var entity = await _db.Queryable<UavDeviceCellEntity>().FirstAsync(p => p.Id == input.CellId);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005, "格位不存在");
// 记录原始时间用于调试
var originalTime = entity.UpdateTime;
// 更新修改时间
entity.UpdateTime = input.NewUpdateTime;
var isOk = await _db.Updateable(entity)
.UpdateColumns(it => new { it.UpdateTime })
.ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001, "更新失败");
// 这里可以添加日志记录
// TODO: 添加操作日志记录
}
catch (Exception ex)
{
// 记录异常日志
throw NCCException.Oh(ErrorCode.COM1000, $"调整修改时间失败: {ex.Message}");
}
}
#endregion
}
}