using Admin.NET.Application; using iWareCommon.Common.Globle; using iWareCommon.Utils; using iWareModel; using iWareSql.WmsDBModel; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WZ.Useful.Commons.Collections; namespace iWareCC { public class DeviceWarningHandler { /// /// 获取配置报警信息列表 /// /// public static DeviceWarningOutput GetConfigWarningList() { DeviceWarningOutput output = new DeviceWarningOutput(); System.Collections.Generic.List retList = new System.Collections.Generic.List(); System.Collections.Generic.List dbList = new System.Collections.Generic.List(); System.Collections.Generic.List real_dbList = new System.Collections.Generic.List(); try { using (WmsDBModel edm = new WmsDBModel()) { //注意:只查询 安全报警、硬件报警信息 var query1 = (int)DeviceWarningTypeEnum.安全报警; var query2 = (int)DeviceWarningTypeEnum.硬件报警; dbList = edm.wms_config_device_warning.Where(x => x.DeviceWarningType == query1 || x.DeviceWarningType == query2).ToList(); //每种区域的每种类型,只取前20条记录 var groupedData = dbList.GroupBy(x => new { x.DeviceAreaCode, x.DeviceWarningType }).Select(group => new { GroupKey = group.Key, Top20Items = group.ToList().OrderBy(x => x.Id).Take(20) }); foreach (var item in groupedData) { real_dbList.AddRange(item.Top20Items); } } foreach (var item in real_dbList) { retList.Add(new StationService.WmsConfigDeviceWarning() { WarningCode = item.WarningCode }); } } catch (Exception ex) { } output.wmsConfigDeviceWarnings = retList; output.wms_config_device_warning_list = real_dbList; return output; } /// /// 新建报警信息 /// /// /// /// /// public static void SaveWarning(EDevice device, LogType _LogType, string warningCode, int deviceAreaCode, string warningContent, int deviceWarningType) { Task.Run(() => { try { wms_record_device_warning request = new wms_record_device_warning() { Id = Yitter.IdGenerator.YitIdHelper.NextId(), DeviceWarningStatus = 0, WarningCode = warningCode, WarningContent = warningContent, WarningTime = DateTime.Now, CreateUserName = SysGloble.WCSSystem, DeviceAreaCode = deviceAreaCode, DeviceWarningType = deviceWarningType, DurationTime = "", CreateTime = DateTime.Now, }; using (WmsDBModel edm = new WmsDBModel()) { var data = edm.wms_record_device_warning.Where(x => x.DeviceWarningStatus == 0 && x.WarningCode == warningCode).FirstOrDefault(); if (data == null) { edm.wms_record_device_warning.Add(request); edm.SaveChanges(); } } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, "保存报警出现异常:" + ex.Message, ex); } }); } /// /// 自动关闭报警信息 /// /// 设备区域 /// /// public static void AutoCloseWarning(EDevice device, LogType _LogType, System.Collections.Generic.List warningCodeList) { Task.Run(() => { try { using (WmsDBModel edm = new WmsDBModel()) { var dataList = edm.wms_record_device_warning.Where(x => x.DeviceWarningStatus == 0).ToList(); if (dataList != null && dataList.Count > 0) { var msg = ""; var nowDate = DateTime.Now; foreach (var detail in dataList) { if (!warningCodeList.Contains(detail.WarningCode)) { nowDate = DateTime.Now; detail.DeviceWarningStatus = 1; //状态(0:新建 1:已处理) detail.FinishTime = nowDate; DateTimeHelper.GetTimeDiffer(detail.CreateTime, nowDate, ref msg); detail.DurationTime = msg; } } edm.SaveChanges(); } } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(_LogType, "自动关闭报警出现异常:" + ex.Message, ex); } }); } } public class DeviceWarningOutput { public System.Collections.Generic.List wmsConfigDeviceWarnings { get; set; } public System.Collections.Generic.List wms_config_device_warning_list { get; set; } } }