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 { /// /// 设备串口日志服务 /// [ApiDescriptionSettings(Tag = "设备串口日志服务",Name = "UavDeviceLog", Order = 200)] [Route("api/Extend/[controller]")] public class UavDeviceLogService : IUavDeviceLogService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _uavDeviceLogRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public UavDeviceLogService( ISqlSugarRepository uavDeviceLogRepository, IUserManager userManager) { _uavDeviceLogRepository = uavDeviceLogRepository; _db = _uavDeviceLogRepository.Context; _userManager = userManager; } /// /// 获取设备串口日志 /// /// 参数 /// [HttpGet("{id}")] public async Task GetInfo(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); var output = entity.Adapt(); return output; } /// /// 获取设备串口日志列表 /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] UavDeviceLogListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; var data = await _db.Queryable() .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.SqlSugarPageResult(data); } /// /// 新建设备串口日志 /// /// 参数 /// [HttpPost("")] [AllowAnonymous] public async Task Create([FromBody] UavDeviceLogCrInput input) { var entity = input.Adapt(); 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); } /// /// 更新设备串口日志 /// /// 主键 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] UavDeviceLogUpInput input) { var entity = input.Adapt(); var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001); } /// /// 删除设备串口日志 /// /// [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); } } }