using Entity.Ammeter; using Entity.DbModel; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac.Attribute; using Repository.Ammeter; using Repository.Station; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using Entity.Dto.Req; using Entity.Dto.Resp; using HybirdFrameworkCore.Entity; using Mapster; using SqlSugar; namespace Service.Station { [Scope("SingleInstance")] public class EquipInfoService : BaseServices { EquipInfoRepository _equipInfoRepository; EquipNetInfoRepository _equipNetInfoRepository; public EquipInfoService(EquipInfoRepository service,EquipNetInfoRepository repository) { _equipInfoRepository = service; _equipNetInfoRepository = repository; this.BaseDal = service; } /// /// 获取设备信息分页 �� /// /// /// public async Task> Page(PageEquipInfoReq input) { RefAsync total = 0; List items = await _equipInfoRepository.EquipInfoQueryPageAsync( !string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name), !string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code), input.Status.HasValue, u => u.Status == input.Status, input.PageNum, input.PageSize, total, input); List equipNetInfoList = await _equipNetInfoRepository.QueryAsync(); Dictionary equipNetInfoDict = equipNetInfoList.ToDictionary(e => e.Code); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); List respList = mapper.Map>(items); foreach (var equipInfo in respList) { if (equipNetInfoDict.TryGetValue(equipInfo.Code, out var equipNetInfo)) { equipInfo.NetAddr = equipNetInfo.NetAddr; equipInfo.NetPort = equipNetInfo.NetPort; equipInfo.DestAddr = equipNetInfo.DestAddr; } } return new PageResult { PageNum = input.PageNum, PageSize = input.PageSize, ToTal = total, Rows = respList }; } /// /// 增加设备信息�� /// /// /// public async Task> AddEquipInfo(AddEquipInfoReq input) { var existingEquipInfoTask = _equipInfoRepository.QueryByClauseAsync(u => u.Name == input.Name || u.Code == input.Code); var existingNetInfoTask = _equipNetInfoRepository.QueryByClauseAsync(u => u.Code == input.Code); await Task.WhenAll(existingEquipInfoTask, existingNetInfoTask); var isExist = existingEquipInfoTask.Result; var isNetInfoExist = existingNetInfoTask.Result; if (isExist != null) { return Result.Fail("已存在同名或同编码设备信息"); } if (isNetInfoExist != null) { return Result.Fail("编码在通信设备配置表已存在"); } var equipInfo = input.Adapt(); var equipNetInfo = input.Adapt(); var insertEquipInfoTask = _equipInfoRepository.InsertAsync(equipInfo); var insertEquipNetInfoTask = _equipNetInfoRepository.InsertAsync(equipNetInfo); await Task.WhenAll(insertEquipInfoTask, insertEquipNetInfoTask); var insertAsync = insertEquipInfoTask.Result; var insertAsyncNetInfo = insertEquipNetInfoTask.Result; if (insertAsync.Id > 0 && insertAsyncNetInfo.Id > 0) { return Result.Success("新增id:" + insertAsync.Id); } return Result.Fail("新增失败"); } /// /// 更新设备信息�� /// /// /// public async Task> UpdateEquipInfo(UpdateEquipInfoReq input) { var equipInfoTask = _equipInfoRepository.QueryByClauseAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id); var equipNetInfoTask = _equipNetInfoRepository.QueryByClauseAsync(u => u.Code == input.Code); await Task.WhenAll(equipInfoTask, equipNetInfoTask); var isExist = equipInfoTask.Result; var isExistNetInfo = equipNetInfoTask.Result; if (isExist != null) { return Result.Fail("已存在同名或同编码设备信息"); } var config = input.Adapt(); var configNetInfo = input.Adapt(); EquipInfo equipInfo = await _equipInfoRepository.QueryByClauseAsync(u => u.Code == input.Code); if (equipInfo != null && isExistNetInfo == null) { var equipNetInfo = input.Adapt(); var insertEquipNetInfoTask = await _equipNetInfoRepository.InsertAsync(equipNetInfo); configNetInfo.Id = insertEquipNetInfoTask.Id; } else { configNetInfo.Id = isExistNetInfo.Id; } var updateEquipInfoTask = _equipInfoRepository.UpdateAsync(config, true); var updateEquipNetInfoTask = _equipNetInfoRepository.UpdateAsync(configNetInfo, true); await Task.WhenAll(updateEquipInfoTask, updateEquipNetInfoTask); var updateResult = updateEquipInfoTask.Result; var updateResultNetInfo = updateEquipNetInfoTask.Result; if (updateResult > 0 && updateResultNetInfo > 0) { return Result.Success("修改成功"); } return Result.Fail("修改失败"); } /// /// 删除设备信息 �� /// /// /// public async Task> DeleteEquipInfo(DeleteEquipInfoReq input) { // 查询 EquipInfo var equipInfo = await _equipInfoRepository.QueryByClauseAsync(u => u.Id == input.Id); if (equipInfo == null) { return Result.Fail("id不存在"); } // 查询 EquipNetInfo var equipNetInfo = await _equipNetInfoRepository.QueryByClauseAsync(u => u.Code == equipInfo.Code); if (equipNetInfo == null) { return Result.Fail("设备通信列表不存在此编码"); } var deleteEquipInfoTask = _equipInfoRepository.DeleteAsync(equipInfo); var deleteEquipNetInfoTask = _equipNetInfoRepository.DeleteAsync(equipNetInfo); await Task.WhenAll(deleteEquipInfoTask, deleteEquipNetInfoTask); var deleteResult = deleteEquipInfoTask.Result; var deleteResultNetInfo = deleteEquipNetInfoTask.Result; if (deleteResult && deleteResultNetInfo) { return Result.Success("删除设备信息成功"); } return Result.Fail("删除失败"); } } }