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 { /// /// 机柜格子管理服务 /// [ApiDescriptionSettings(Tag = "机柜格子管理服务", Name = "UavDeviceCell", Order = 200)] [Route("api/Extend/[controller]")] public class UavDeviceCellService : IUavDeviceCellService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _uavDeviceCellRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public UavDeviceCellService( ISqlSugarRepository uavDeviceCellRepository, IUserManager userManager) { _uavDeviceCellRepository = uavDeviceCellRepository; _db = _uavDeviceCellRepository.Context; _userManager = userManager; } #region 获取机柜格子管理 /// /// 获取机柜格子管理 /// /// 参数 /// [HttpGet("{id}")] public async Task GetInfo(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); var output = entity.Adapt(); return output; } #endregion #region 获取机柜格子管理列表 /// /// 获取机柜格子管理列表 /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] UavDeviceCellListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; var data = await _db.Queryable() .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().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.SqlSugarPageResult(data); } #endregion #region 新建机柜格子管理 /// /// 新建机柜格子管理 /// /// 参数 /// [HttpPost("")] public async Task Create([FromBody] UavDeviceCellCrInput input) { var userInfo = await _userManager.GetUserInfo(); var entity = input.Adapt(); 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 更新机柜格子管理 /// /// 更新机柜格子管理 /// /// 主键 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] UavDeviceCellUpInput input) { var entity = input.Adapt(); var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001); } #endregion #region 删除机柜格子管理 /// /// 删除机柜格子管理 /// /// [HttpDelete("{id}")] public async Task Delete(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005); var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002); } #endregion #region 调整格位修改时间 /// /// 调整格位修改时间 /// /// 调整时间输入参数 /// [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().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 } }