using System.ComponentModel; using Admin.Bodk.Device.Entities.equipment; using Admin.Bodk.Device.Entities.TaskChain; using Admin.NET.Core; using Furion.DatabaseAccessor; using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace Admin.Bodk.Device.Services; /// /// 设备管理服务 /// [ApiDescriptionSettings(Order = 2)] public class SysEquipmentService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _repository; private readonly SqlSugarRepository _taskChainRepository; public SysEquipmentService(SqlSugarRepository repository, SqlSugarRepository taskChainRepository) { _repository = repository; _taskChainRepository = taskChainRepository; } /// /// 获取设备列表 /// /// /// [DisplayName("获取设备列表")] public async Task> PostEquipmentList(EquipmentInpt input) { return await _repository.AsQueryable() // .LeftJoin((u, a) => u.Id == a.EquipmentId) .WhereIF(!string.IsNullOrWhiteSpace(input.EquipmentName), m => m.EquipmentName != null && m.EquipmentName.Contains(input.EquipmentName)) .WhereIF(!string.IsNullOrWhiteSpace(input.Type), m => m.Type == input.Type) .Select((u) => new EquipmentDto { Id = u.Id, EquipmentName = u.EquipmentName, Type = u.Type, Position = u.Position, Remark = u.Remark, Code = u.Code, Ip = u.Ip, Status = u.Status, CreateTime = u.CreateTime, TaskChainList =SqlFunc.Subqueryable().Where(tc => tc.EquipmentId == u.Id).ToList() }) .ToPagedListAsync(input.Page, input.PageSize); } /// /// 增加设备 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Add"), HttpPost] [DisplayName("增加设备")] public async Task AddEquipment(EquipmentDto input) { if(input is null) throw Oops.Oh("参数不能为空"); var equipment = input.Adapt(); // if( a is double b) // { // Console.WriteLine(b); // } var newEquipment = await _repository.AsInsertable(equipment).ExecuteReturnEntityAsync(); if (input?.TaskChainList is { Count: > 0 }) { foreach (var taskChain in input.TaskChainList) { taskChain.EquipmentId = newEquipment.Id; await _taskChainRepository.AsInsertable(taskChain).ExecuteReturnEntityAsync(); } } input.Id = newEquipment.Id; return newEquipment.Id; } /// /// 更新设备 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新设备")] public async Task Update(EquipmentDto input) { _taskChainRepository.AsDeleteable().Where(tc => tc.EquipmentId == input.Id).ExecuteCommand(); if (input?.TaskChainList is { Count: > 0 }) { foreach (var taskChain in input.TaskChainList) { taskChain.EquipmentId = input.Id; await _taskChainRepository.AsInsertable(taskChain).ExecuteReturnEntityAsync(); } } await _repository.AsUpdateable(input.Adapt()) .UpdateColumns(m => new { m.Type, m.EquipmentName, m.Remark, m.Code }) .ExecuteCommandAsync(); } /// /// 删除设备 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除设备")] public async Task Delete(DeleteEquipmentInput input) { var user = await _repository.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh("设备不存在"); await _repository.DeleteAsync(user); } }