add
zongzhibin
2024-11-26 327dbcbe3e6ee5fea207422c7792fc35a1027638
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
using iWareCommon.Common.Globle;
using iWareCommon.Utils;
using iWareModel;
using iWareSql.DBModel;
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();
                    request.Id = Guid.NewGuid().ToString();
                    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.CreateTime = request.ModifyTime = DateTime.Now;
                    request.CreateBy = request.ModifyBy = SysGloble.WCSSystem;
 
                    request.OperationRemark = "添加";
                    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();
                        }
                    }
                }
                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();
                       }
                   }
               }
               catch (Exception ex)
               {
                   Log4NetHelper.WriteErrorLog(_LogType, "自动关闭报警出现异常:" + ex.Message, ex);
               }
           });
        }
    }
}