ke_junjie
2025-06-04 84620534eb627e95811b971a4b552b6a177829bf
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using iWareCommon.Common.Globle;
using iWareCommon.Utils;
using iWareModel;
using iWareSql.DbOrm;
using iWareSql.Orm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareSql.DataAccess
{
    public class DeviceWarningHandler
    {
        /// <summary>
        /// 新建报警信息
        /// </summary>
        /// <param name="deviceCode"></param>
        /// <param name="deviceName"></param>
        /// <param name="warningCode"></param>
        /// <param name="warningContent"></param>
        public static void SaveWarning(EDevice device, LogType _LogType, string warningCode, string warningDbAddress, string warningContent)
        {
            Task.Run(() =>
            {
                try
                {
                    EDeviceType deviceType = BusinessHelper.GetEDeviceTypeByEDevice(device);
 
                    string deviceCode = ((int)device).ToString();
                    string deviceName = device.ToString();
                    //Device_Warning request = new Device_Warning();
                    ware_device_wareing request = new ware_device_wareing();
                    request.Id = Convert.ToInt64(DateTime.Now.ToString("yyMMddhhmmssfff"));
                    request.DeviceCode = deviceCode;
                    request.DeviceName = deviceName;
 
                    request.DeviceType = (int)deviceType;
                    request.DeviceTypeName = deviceType.ToString();
 
                    request.WarningDbAddress = warningDbAddress;
                    request.WarningCode = warningCode;
                    request.WarningContent = warningContent;
                    request.WarningTime = DateTime.Now;
                    //状态(0:新建 1:已处理)
                    request.Status = 0;
                    request.StatusName = "新建";
 
                    request.CreatedTime = DateTime.Now;
                    request.CreatedUserName = SysGloble.WCSSystem;
 
                    request.OperationRemark = "添加";
                    request.IsDeleted = false;
                    //using (DbModel edm = new DbModel())
                    //{
                    //    var data = edm.Device_Warning.Where(x => x.DeviceCode == deviceCode && x.Status == 0 && x.WarningCode == warningCode).FirstOrDefault();
                    //    if (data == null)
                    //    {
                    //        edm.Device_Warning.Add(request);
                    //        edm.SaveChanges();
                    //    }
                    //}
                    using (DbOrm.DbOrm dbOrm = new DbOrm.DbOrm())
                    {
                        var data = dbOrm.ware_device_wareing.Where(x => x.DeviceCode == deviceCode && x.Status == 0 && x.WarningCode == warningCode).FirstOrDefault();
                        if (data == null)
                        {
                            dbOrm.ware_device_wareing.Add(request);
                            dbOrm.SaveChanges();
                        }
                    }
 
                }
                catch (Exception ex)
                {
                    Log4NetHelper.WriteErrorLog(_LogType, "保存报警出现异常:" + ex.Message, ex);
                }
            });
 
        }
 
        /// <summary>
        /// 自动关闭报警信息
        /// </summary>
        /// <param name="deviceId">设备区域</param>
        /// <param name="_LogType"></param>
        /// <returns></returns>
        public static void AutoCloseWarning(EDevice device, LogType _LogType, List<string> warningAddressList)
        {
            Task.Run(() =>
            {
                try
                {
                    string deviceCode = ((int)device).ToString();
                    //using (DbModel edm = new DbModel())
                    //{
                    //    var dataList = edm.Device_Warning.Where(x => x.DeviceCode == deviceCode && x.Status == 0).ToList();
                    //    if (dataList != null && dataList.Count > 0)
                    //    {
                    //        var msg = "";
                    //        var nowDate = DateTime.Now;
                    //        foreach (var detail in dataList)
                    //        {
                    //            if (!warningAddressList.Contains(detail.WarningCode))
                    //            {
                    //                nowDate = DateTime.Now;
                    //                detail.ModifyTime = nowDate;
                    //                detail.ModifyBy = SysGloble.WCSSystem;
                    //                detail.Status = 1; //状态(0:新建 1:已处理)
                    //                detail.StatusName = "已处理";
                    //                detail.CloseContent = SysGloble.WCSSystem + "关闭";
                    //                detail.FinishTime = nowDate;
                    //                detail.OperationRemark = "关闭报警";
                    //                DateTimeHelper.GetTimeDiffer(detail.CreateTime, nowDate, ref msg);
                    //                detail.DurationTime = msg;
                    //            }
                    //        }
                    //        edm.SaveChanges();
                    //    }
                    //}
                    using (DbOrm.DbOrm edm = new DbOrm.DbOrm())
                    {
                        var dataList = edm.ware_device_wareing.Where(x => x.DeviceCode == deviceCode && x.Status == 0).ToList();
                        if (dataList != null && dataList.Count > 0)
                        {
                            var msg = "";
                            var nowDate = DateTime.Now;
                            foreach (var detail in dataList)
                            {
                                if (!warningAddressList.Contains(detail.WarningCode))
                                {
                                    nowDate = DateTime.Now;
                                    detail.UpdatedTime = nowDate;
                                    detail.UpdatedUserName = SysGloble.WCSSystem;
                                    detail.Status = 1; //状态(0:新建 1:已处理)
                                    detail.StatusName = "已处理";
                                    detail.CloseContent = SysGloble.WCSSystem + "关闭";
                                    detail.FinishTime = nowDate;
                                    detail.OperationRemark = "关闭报警";
                                    DateTimeHelper.GetTimeDiffer(detail.WarningTime, nowDate, ref msg);
                                    detail.DurationTime = msg;
                                }
                            }
                            edm.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.WriteErrorLog(_LogType, "自动关闭报警出现异常:" + ex.Message, ex);
                }
            });
        }
    }
}