You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<EquipInfo>
{
EquipInfoRepository _equipInfoRepository;
EquipNetInfoRepository _equipNetInfoRepository;
public EquipInfoService(EquipInfoRepository service,EquipNetInfoRepository repository)
{
_equipInfoRepository = service;
_equipNetInfoRepository = repository;
this.BaseDal = service;
}
/// <summary>
/// 获取设备信息分页 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PageResult<EquipInfoResp>> Page(PageEquipInfoReq input)
{
RefAsync<int> total = 0;
List<EquipInfo> 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<EquipNetInfo> equipNetInfoList = await _equipNetInfoRepository.QueryAsync();
Dictionary<string,EquipNetInfo> equipNetInfoDict = equipNetInfoList.ToDictionary(e => e.Code);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EquipInfoResp, EquipInfo>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EquipInfoResp> respList = mapper.Map<List<EquipInfoResp>>(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<EquipInfoResp>
{
PageNum = input.PageNum,
PageSize = input.PageSize,
ToTal = total,
Rows = respList
};
}
/// <summary>
/// 增加设备信息🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Result<string>> 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<string>.Fail("已存在同名或同编码设备信息");
}
if (isNetInfoExist != null)
{
return Result<string>.Fail("编码在通信设备配置表已存在");
}
var equipInfo = input.Adapt<EquipInfo>();
var equipNetInfo = input.Adapt<EquipNetInfo>();
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<string>.Success("新增id" + insertAsync.Id);
}
return Result<string>.Fail("新增失败");
}
/// <summary>
/// 更新设备信息🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Result<string>> 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<string>.Fail("已存在同名或同编码设备信息");
}
var config = input.Adapt<EquipInfo>();
var configNetInfo = input.Adapt<EquipNetInfo>();
EquipInfo equipInfo = await _equipInfoRepository.QueryByClauseAsync(u => u.Code == input.Code);
if (equipInfo != null && isExistNetInfo == null)
{
var equipNetInfo = input.Adapt<EquipNetInfo>();
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<string>.Success("修改成功");
}
return Result<string>.Fail("修改失败");
}
/// <summary>
/// 删除设备信息 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Result<string>> DeleteEquipInfo(DeleteEquipInfoReq input)
{
// 查询 EquipInfo
var equipInfo = await _equipInfoRepository.QueryByClauseAsync(u => u.Id == input.Id);
if (equipInfo == null)
{
return Result<string>.Fail("id不存在");
}
// 查询 EquipNetInfo
var equipNetInfo = await _equipNetInfoRepository.QueryByClauseAsync(u => u.Code == equipInfo.Code);
if (equipNetInfo == null)
{
return Result<string>.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<string>.Success("删除设备信息成功");
}
return Result<string>.Fail("删除失败");
}
}
}