using MaterialTraceability.Common; using MaterialTraceability.Entity.DAO; using MaterialTraceability.Entity.DTO; using MaterialTraceability.Entity.UpLoad; using MaterialTraceability.SqlSugar; using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; namespace MaterialTraceability.Business { /// /// 数据上传逻辑 /// public sealed class UpLoadBusiness { #region 单例模式 private static readonly Lazy lazy = new Lazy(() => new UpLoadBusiness()); public static UpLoadBusiness Instance { get { return lazy.Value; } } private UpLoadBusiness() { } #endregion private SqlSugarClient _db = SqlGenerator.GetMySqlInstance(); private AppConfigDto appConfig = AppConfigDto.Instance; /// /// 放卷记录 /// /// public void SaveUpRecord(ProUpRecord upRecord) { try { UpRecord info = new UpRecord() { clientId = upRecord.Id, machineId = upRecord.MachineId, resource = appConfig.resource, positionId = upRecord.PositionId, rfidStr = upRecord.Rfid, sfcStr = upRecord.Sfc, eaValue = upRecord.eaValue, isProduction = upRecord.IsProduction, isFinish = upRecord.isFinish, recordTime = upRecord.RecordTime, beginTime = upRecord.beginTime, endTime = upRecord.endTime, upMaterialId = upRecord.UpMaterialId, }; LogHelper.Info("上传放卷记录:" + JsonChange.ModeToJson(info)); int result = _db.Insertable(info).ExecuteCommand(); if (result > 0) { LogHelper.Info("放卷记录上传成功"); } else { LogHelper.Info("放卷记录上传失败"); } }catch(Exception ex) { LogHelper.Error("UpRecord数据上传异常", ex); } } /// /// 修改上料记录 /// /// public void UpdateUpRecord(ProUpRecord upRecord) { try { UpRecord info = new UpRecord() { clientId = upRecord.Id, machineId = upRecord.MachineId, resource = appConfig.resource, positionId = upRecord.PositionId, rfidStr = upRecord.Rfid, sfcStr = upRecord.Sfc, eaValue = upRecord.eaValue, isProduction = upRecord.IsProduction, isFinish = upRecord.isFinish, recordTime = upRecord.RecordTime, beginTime = upRecord.beginTime, endTime = upRecord.endTime, upMaterialId = upRecord.UpMaterialId, }; LogHelper.Info("修改放卷记录:" + JsonChange.ModeToJson(info)); int result = _db.Updateable() .Where(it => it.clientId == upRecord.Id && it.positionId == upRecord.PositionId && it.resource == appConfig.resource) .SetColumns(it=>it.rfidStr == upRecord.Rfid) .SetColumns(it => it.sfcStr == upRecord.Sfc) .SetColumns(it => it.eaValue == upRecord.eaValue) .SetColumns(it => it.isProduction == upRecord.IsProduction) .SetColumns(it => it.isFinish == upRecord.isFinish) .SetColumns(it => it.recordTime == upRecord.RecordTime) .SetColumns(it => it.beginTime == upRecord.beginTime) .SetColumns(it => it.endTime == upRecord.endTime) .SetColumns(it => it.upMaterialId == upRecord.UpMaterialId) .ExecuteCommand(); if (result > 0) { LogHelper.Info("放卷记录修改成功"); } else { LogHelper.Info("放卷记录修改失败"); } } catch (Exception ex) { LogHelper.Error("UpdateUpRecord处理异常", ex); } } /// /// 收卷记录 /// /// public void SaveDownRecord(ProDownRecord downRecord) { try { DownRecord info = new DownRecord() { clientId = downRecord.Id, machineId = downRecord.MachineId, resource = appConfig.resource, positionId = downRecord.PositionId, rfidStr = downRecord.Rfid, sfcStr = downRecord.Sfc, eaValue = downRecord.eaValue, isProduction = downRecord.IsProduction, isFinish = downRecord.isFinish, recordTime = downRecord.RecordTime, beginTime = downRecord.beginTime, endTime = downRecord.endTime, downMaterialId = downRecord.DownMaterialId, productUser = ConfigHelper.GetConfig("userName"), }; LogHelper.Info("上传收卷记录:" + JsonChange.ModeToJson(info)); int result = _db.Insertable(info).ExecuteCommand(); if (result > 0) { LogHelper.Info("收卷记录上传成功"); } else { LogHelper.Info("收卷记录上传失败"); } } catch (Exception ex) { LogHelper.Error("DownRecord数据上传异常", ex); } } /// /// 修改收卷记录 /// /// public void UpdateDownRecord(ProDownRecord downRecord) { try { DownRecord info = new DownRecord() { clientId = downRecord.Id, machineId = downRecord.MachineId, resource = appConfig.resource, positionId = downRecord.PositionId, rfidStr = downRecord.Rfid, sfcStr = downRecord.Sfc, eaValue = downRecord.eaValue, isProduction = downRecord.IsProduction, isFinish = downRecord.isFinish, recordTime = downRecord.RecordTime, beginTime = downRecord.beginTime, endTime = downRecord.endTime, downMaterialId = downRecord.DownMaterialId, productUser = ConfigHelper.GetConfig("userName"), }; LogHelper.Info("修改收卷记录:" + JsonChange.ModeToJson(info)); int result = _db.Updateable() .Where(it => it.clientId == downRecord.Id && it.positionId == downRecord.PositionId && it.resource == appConfig.resource) .SetColumns(it => it.rfidStr == downRecord.Rfid) .SetColumns(it => it.sfcStr == downRecord.Sfc) .SetColumns(it => it.eaValue == downRecord.eaValue) .SetColumns(it => it.isProduction == downRecord.IsProduction) .SetColumns(it => it.isFinish == downRecord.isFinish) .SetColumns(it => it.recordTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) .SetColumns(it => it.beginTime == downRecord.beginTime) .SetColumns(it => it.endTime == downRecord.endTime) .SetColumns(it => it.downMaterialId == downRecord.DownMaterialId) .ExecuteCommand(); if (result > 0) { LogHelper.Info("收卷记录修改成功"); } else { LogHelper.Info("收卷记录修改失败"); } } catch (Exception ex) { LogHelper.Error("UpdateDownRecord处理异常", ex); } } /// /// 模切特征数据上传 /// /// public void SaveMqUpLoad(MqUpLoad mqUpLoad) { try { LogHelper.Info("上传模切重量特征数据:" + JsonChange.ModeToJson(mqUpLoad)); int result = _db.Insertable(mqUpLoad).ExecuteCommand(); if (result > 0) { LogHelper.Info("模切重量特征数据上传成功"); } else { LogHelper.Info("模切重量特征数据上传失败"); } } catch (Exception ex) { LogHelper.Error("SaveMqUpLoad数据上传异常", ex); } } /// /// 模切特征数据修改 /// /// public void UpdateMqUpLoad(MqUpLoad mqUpLoad) { try { LogHelper.Info("修改模切重量特征数据:" + JsonChange.ModeToJson(mqUpLoad)); int result = _db.Updateable() .Where(it => it.clientId == mqUpLoad.clientId && it.sfcStr == mqUpLoad.sfcStr) .SetColumns(it => it.is_success == mqUpLoad.is_success) .SetColumns(it => it.recordTime == mqUpLoad.recordTime) .ExecuteCommand(); if (result > 0) { LogHelper.Info("模切重量特征数据修改成功"); } else { LogHelper.Info("模切重量特征数据修改失败"); } } catch (Exception ex) { LogHelper.Error("UpdateMqUpLoad处理异常", ex); } } /// /// 冷压重量特征数据上传 /// /// public void SaveLyUpLoad(LyUpLoad lyUpLoad) { try { LogHelper.Info("上传模切重量特征数据:" + JsonChange.ModeToJson(lyUpLoad)); int result = _db.Insertable(lyUpLoad).ExecuteCommand(); if (result > 0) { LogHelper.Info("冷压重量特征数据上传成功"); } else { LogHelper.Info("冷压重量特征数据上传失败"); } } catch (Exception ex) { LogHelper.Error("SaveLyUpLoad数据上传异常", ex); } } /// /// 读取记录 /// /// public void SaveReadRecord(ProReadRecord readRecord) { try { ReadRecord info = new ReadRecord() { machineId = readRecord.MachineID, resource = appConfig.resource, equipId = readRecord.EquipID, positionId = readRecord.PositionID, ant = readRecord.Ant, rfidStr = readRecord.ReadEPC, is_success = StringChange.ParseToInt(readRecord.Result), recordTime = readRecord.ReadTime }; LogHelper.Info("上传读取记录:" + JsonChange.ModeToJson(info)); int result = _db.Insertable(info).ExecuteCommand(); if (result > 0) { LogHelper.Info("读取记录上传成功"); } else { LogHelper.Info("读取记录上传失败"); } } catch (Exception ex) { LogHelper.Error("ReadRecord数据上传异常", ex); } } /// /// 通过Card获取用户登录信息 /// /// /// public SysUser GetSysUserInfoByCard(string card) { SysUser sysUser = new SysUser(); if (StringExtension.IsNotBlank(card)) { DataTable info = _db.Queryable().Where(x => x.clientKey == card).ToDataTable(); if (info.Rows.Count > 0) { DataRow dataRow = info.Rows[0]; sysUser.id = Convert.ToInt32(dataRow["user_id"]); sysUser.loginName = dataRow["login_name"].ToString(); sysUser.userName = dataRow["user_name"].ToString(); sysUser.clientKey = dataRow["client_key"].ToString(); sysUser.clientRole = dataRow["client_role"].ToString(); } } return sysUser; } /// /// 判断当前机台是否为190品种,190品种上传重量特征数据 /// /// public bool JudgeResourceIsUplpadData() { try { DeviceInfo deviceInfo = new DeviceInfo(); string resource = appConfig.resource; DataTable info = _db.Queryable().Where(x => x.resource == resource).ToDataTable(); if (info.Rows.Count > 0) { DataRow dataRow = info.Rows[0]; deviceInfo.deviceId = Convert.ToInt32(dataRow["device_id"]); deviceInfo.deviceName = dataRow["device_name"].ToString(); deviceInfo.resource = dataRow["resource"].ToString(); deviceInfo.materialType = Convert.ToInt32(dataRow["material_type"]); deviceInfo.deviceId = Convert.ToInt32(dataRow["parent_id"]); } LogHelper.Info(String.Format("{0}设备信息:{1}", resource, JsonChange.ModeToJson(deviceInfo))); if (deviceInfo.materialType == 1) { return false; } else { return false; } }catch(Exception ex) { LogHelper.Error("JudgeResourceIsUplpadData异常", ex); return false; } } /// /// 通过设备资源号获取dc数据组 /// /// public string GerDcGroupByResource() { try { DeviceInfo deviceInfo = new DeviceInfo(); string resource = appConfig.resource; DataTable info = _db.Queryable().Where(x => x.resource == resource).ToDataTable(); if (info.Rows.Count > 0) { DataRow dataRow = info.Rows[0]; deviceInfo.deviceId = Convert.ToInt32(dataRow["device_id"]); deviceInfo.deviceName = dataRow["device_name"].ToString(); deviceInfo.resource = dataRow["resource"].ToString(); deviceInfo.materialType = Convert.ToInt32(dataRow["material_type"]); deviceInfo.parentId = Convert.ToInt32(dataRow["parent_id"]); } LogHelper.Info(String.Format("{0}设备信息:{1}", resource, JsonChange.ModeToJson(deviceInfo))); if (deviceInfo.parentId == 158) { return "1ANEFS1SI00089"; }else if(deviceInfo.parentId == 159) { return "1CAEFS1SI00089"; }else if (deviceInfo.parentId == 132) { return "1ANCAP1SI00089"; }else if (deviceInfo.parentId == 133) { return "1CACAP1SI0089"; } else { return ""; } } catch (Exception ex) { LogHelper.Error("GerDcGroupByResource()异常", ex); return ""; } } /// /// 日志记录 /// /// public void SaveLogRecord(int position,string msg) { try { LogRecord logRecord = new LogRecord() { resource = appConfig.resource, positionId = position, message = msg }; int result = _db.Insertable(logRecord).ExecuteCommand(); } catch (Exception ex) { LogHelper.Error("LogRecord数据上传异常", ex); } } /// /// 获取冷压纵向膜区,根据L2膜卷号获取拆分数量,前三卷为1,其余为2 /// /// /// public int GetLengthwaysArea(string upMaterialSfc) { LogHelper.Info(String.Format("进入GetLengthwaysArea()函数,获取冷压纵向膜区")); int splitCount = 0; // 通过L2在冷压下达的L3膜卷号 DataTable upRecordDataTable = _db.Queryable().Where(x => x.upMaterialId == upMaterialSfc && x.machineId == 11).ToDataTable(); foreach(DataRow item in upRecordDataTable.Rows) { DataTable downRecordDataTable = _db.Queryable().Where(x => x.sfcStr.Contains(item["sfcStr"].ToString()) && x.machineId == 11).ToDataTable(); splitCount = splitCount + downRecordDataTable.Rows.Count; } if(splitCount < 3) { return 1; } else { return 2; } } /// /// 通过MES返回编号获取返回信息 /// /// /// /// public string GetMesMessage(int code,string message) { try { AlarmInfo alarmInfo = new AlarmInfo(); DataTable info = _db.Queryable().Where(x => x.alarmCode == code.ToString()).ToDataTable(); if (info.Rows.Count > 0) { DataRow dataRow = info.Rows[0]; //alarmInfo.objId = Convert.ToInt32(dataRow["objId"]); //alarmInfo.alarmCode = dataRow["alarmCode"].ToString(); //alarmInfo.alarmMessage = dataRow["alarmMessage"].ToString(); //alarmInfo.alarmPropose = dataRow["alarmPropose"].ToString(); //alarmInfo.remark = dataRow["remark"].ToString(); // message = dataRow["alarmMessage"].ToString(); message = dataRow["alarmMessage"].ToString() + "解决建议:" + dataRow["alarmPropose"].ToString(); } } catch (Exception ex) { LogHelper.Error("GetMesMessage()异常", ex); } return message; } public void RefreshDeviceTime(object sender, ElapsedEventArgs e) { try { int result = _db.Updateable() .Where(it => it.resource == appConfig.resource) .SetColumns(it => it.statusTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) .ExecuteCommand(); if (result < 1) { //LogHelper.Info("设备当前时间失败!!!"); } } catch (Exception ex) { LogHelper.Error("RefreshDeviceTime处理异常", ex); } } } }