1
dengjunjie
8 天以前 5621e29bd415c85fd719e0e6366eae813b970ea2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 (cool.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}"
                };
            }
        }
    }
}