using HslCommunication;
|
using Newtonsoft.Json;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
|
using Quartz.Util;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Security.Claims;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Common;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_DTO.SquareCabin;
|
using WIDESEA_IWMsInfoServices;
|
using WIDESEA_Model.Models;
|
using static WIDESEA_DTO.SquareCabin.AlarmDto;
|
using static WIDESEA_DTO.SquareCabin.TowcsDto;
|
|
namespace WIDESEA_WMsInfoServices
|
{
|
public class ContainerService : ServiceBase<Dt_Container, IRepository<Dt_Container>>, IContainerService
|
{
|
private readonly IMessageInfoService _messageInfoService;
|
public ContainerService(IRepository<Dt_Container> BaseDal, IMessageInfoService messageInfoService) : base(BaseDal)
|
{
|
_messageInfoService = messageInfoService;
|
}
|
|
public IRepository<Dt_Container> Repository => BaseDal;
|
|
public WebResponseContent Sensor()
|
{
|
try
|
{
|
var url = "http://172.16.1.4:8082/api/environment/sensor";
|
var result = HttpHelper.Get(url);
|
var response = JsonConvert.DeserializeObject<CollResponse<CoolDto>>(result);
|
|
if (response.code != "000" || response.data == null)
|
{
|
return new WebResponseContent { Status = false, Message = "接口返回数据报错" };
|
}
|
|
// 使用批量操作提高性能
|
var updateList = new List<Dt_Container>();
|
var addList = new List<Dt_Container>();
|
var currentTime = DateTime.Now;
|
|
// 先查询所有设备,避免循环中多次查询数据库
|
var deviceNames = response.data.Select(x => x.name).Distinct().ToList();
|
var existingDevices = BaseDal.QueryData(x => deviceNames.Contains(x.deviceName))
|
.ToDictionary(x => x.deviceName, x => x);
|
|
foreach (var item in response.data)
|
{
|
if (existingDevices.TryGetValue(item.name, out var cool))
|
{
|
// 更新现有记录
|
cool.CurrentTemperature = item.temperature;
|
cool.Humidity = item.humidity;
|
cool.Alarm = item.alarm;
|
cool.AlarmInformation = item.message;
|
cool.ModifyDate = currentTime;
|
updateList.Add(cool);
|
if (cool.Alarm!="正常")
|
{
|
_messageInfoService.AddMessageInfo(MessageGroupByEnum.EquipmentAlarm, $"冷柜{item.alarm}", item.message, MessageStatusEnum.Undisposed);
|
}
|
}
|
else
|
{
|
// 创建新记录
|
var dto = new Dt_Container
|
{
|
deviceName = item.name,
|
CurrentTemperature = item.temperature,
|
Humidity = item.humidity,
|
Alarm = item.alarm,
|
AlarmInformation = item.message,
|
CreateDate = currentTime,
|
Creater = "System",
|
ModifyDate = currentTime,
|
Modifier = "System"
|
};
|
addList.Add(dto);
|
if (item.alarm != "正常")
|
{
|
_messageInfoService.AddMessageInfo(MessageGroupByEnum.EquipmentAlarm, $"冷柜{item.alarm}", item.message, MessageStatusEnum.Undisposed);
|
}
|
}
|
}
|
|
// 开始事务
|
Db.Ado.BeginTran();
|
|
try
|
{
|
// 批量插入新记录
|
if (addList.Any())
|
{
|
BaseDal.Db.Insertable(addList).ExecuteCommand();
|
}
|
|
// 批量更新现有记录
|
if (updateList.Any())
|
{
|
BaseDal.Db.Updateable(updateList).ExecuteCommand();
|
}
|
|
// 提交事务
|
Db.Ado.CommitTran();
|
|
return new WebResponseContent
|
{
|
Status = true,
|
Message = $"同步成功:新增{addList.Count}条,更新{updateList.Count}条"
|
};
|
}
|
catch (Exception ex)
|
{
|
Db.Ado.RollbackTran();
|
return new WebResponseContent
|
{
|
Status = false,
|
Message = $"数据保存失败:{ex.Message}"
|
};
|
}
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent
|
{
|
Status = false,
|
Message = $"接口调用失败:{ex.Message}"
|
};
|
}
|
}
|
}
|
}
|