using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Json; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using QMAPP.BLL; using QMAPP.Entity; using QMAPP.FJC.BLL.Basic; using QMAPP.FJC.BLL.FIS; using QMAPP.FJC.BLL.Operation; using QMAPP.FJC.DAL.Basic; using QMAPP.FJC.DAL.EM; using QMAPP.FJC.DAL.Equipment; using QMAPP.FJC.DAL.FIS; using QMAPP.FJC.DAL.Injection; using QMAPP.FJC.DAL.Operation; using QMAPP.FJC.Entity; using QMAPP.FJC.Entity.Basic; using QMAPP.FJC.Entity.Equipment; using QMAPP.FJC.Entity.FIS; using QMAPP.FJC.Entity.Injection; using QMAPP.FJC.Entity.Operation; using QMAPP.FJC.Entity.ProcessParameter; using QMAPP.FJC.Entity.QT; using QMAPP.FJC.Entity.SA; using QMAPP.MD.Entity; using QMFrameWork.Data; using QMFrameWork.Log; using Resource = QMAPP.FJC.Entity.Resource; namespace QMAPP.FJC.BLL.Equipment { public class TempProductBLL : BaseBLL { private List JsonToList(string jsonString) { // 实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List)); //把Json传入内存流中保存 //jsonString = "[" + jsonString + "]"; MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); // 使用ReadObject方法反序列化成对象 object ob = serializer.ReadObject(stream); List ls = (List)ob; return ls; } public static string HttpGetWebApi(string url) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { //获取json字符串 var json= reader.ReadToEnd(); //解析字符串 JObject jo = (JObject)JsonConvert.DeserializeObject(json); string code = jo["code"].ToString(); //string msg = jo["msg"].ToString(); if (code=="1") { return jo["data"].ToString(); } return null; } } public string returnJson(string jsons) { //解析字符串 JObject jo = (JObject)JsonConvert.DeserializeObject(jsons); string code = jo["code"].ToString(); //string msg = jo["msg"].ToString(); if (code == "1") { return jo["data"].ToString(); } return null; } //执行task程序 public void TempProductInsertProduct() { string monitorCodeJson = ""; string monitorDataJson = ""; try { monitorCodeJson = HttpGetWebApi("http://10.123.4.18:8210/KQService/getMonitorCodeData"); monitorDataJson = HttpGetWebApi("http://10.123.4.18:8210/KQService/getMachineMonitorData"); //var monitorCodeJson = "[\r\n {\r\n \"id\": \"99599\",\r\n \"monitorid\": \"124444\",\r\n \"molddataname\": \"VW331-MYNB-HEI-SOP\",\r\n \"currentmode\": \"3\",\r\n \"ipaddr\": \"10.123.4.175\",\r\n \"partnum\": \"19G.857.303 82V\",\r\n \"barcode\": \"CFAA-QD1805221459302F0499\",\r\n \"recorddate\": \"5/22/2018 11:40:09 AM\"\r\n }, \r\n {\r\n \"id\": \"101924\",\r\n \"monitorid\": \"126369\",\r\n \"molddataname\": \"VW331-HMZWD-HEI-SOP#0#0\",\r\n \"currentmode\": \"2\",\r\n \"ipaddr\": \"10.123.4.164\",\r\n \"partnum\": \"19G.867.212.030-82V\",\r\n \"barcode\": \"CFAA-QD18052311400920002\",\r\n \"recorddate\": \"5/23/2018 11:40:09 AM\"\r\n } \r\n]"; //var monitorDataJson = "[{\"id\":124444,\"IpAddr\":\"2\",\"CycleTime\":\"3\",\"MoldOpenTime\":\"2\",\"MoldCloseTime\":\"2\",\"InjHoldTime\":\"19G.868.007 RYQ\",\"ChargeTime\":\"123\",\"InjSwitchTime\":\"123\",\"MoldProTime\":\"123\",\"InjectStartPos\":\"123\",\"HoldEndPos\":\"123\",\"ChargeEndPos\":\"123\",\"MoldOpenEndPos\":\"123\",\"ChargeMaxRPM\":\"123\",\"InjectMaxSP\":\"123\",\"InjectMaxPressure\":\"123\",\"RecordDate\":\"\\/Date(1017101216023+0800)\\/\",\"InjectEPos\":\"123\",\"HoldPresSPos\":\"123\",\"CutOffPressure\":\"123\"},{\"id\":3,\"IpAddr\":\"2\",\"CycleTime\":\"3\",\"MoldOpenTime\":\"2\",\"MoldCloseTime\":\"2\",\"InjHoldTime\":\"19G.868.007 RYQ\",\"ChargeTime\":\"123\",\"InjSwitchTime\":\"123\",\"MoldProTime\":\"123\",\"InjectStartPos\":\"123\",\"HoldEndPos\":\"123\",\"ChargeEndPos\":\"123\",\"MoldOpenEndPos\":\"123\",\"ChargeMaxRPM\":\"123\",\"InjectMaxSP\":\"123\",\"InjectMaxPressure\":\"123\",\"RecordDate\":\"\\/Date(1017101216023+0800)\\/\",\"InjectEPos\":\"123\",\"HoldPresSPos\":\"123\",\"CutOffPressure\":\"123\"} ]"; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { Tag = "从ims接口中获取数据异常!", ErrorInfo = ex }); return; } #region Json数据转换 Dictionary inDict = new Dictionary(); List monitorCodes = null; if (monitorCodeJson != null) { try { monitorCodes = JsonToList(monitorCodeJson); } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { Tag = "转换monitorCodeJson异常", ErrorInfo = ex }); LogManager.LogHelper.Error(new LogInfo() { Tag = "monitorDataJson数据", Info = monitorDataJson }); return; } } List monitorDatas = null; if (monitorDataJson != null) { try { monitorDatas = JsonToList(monitorDataJson); } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { Tag = "转换monitorDataJson异常", ErrorInfo = ex }); LogManager.LogHelper.Error(new LogInfo() { Tag = "monitorDataJson数据", Info = monitorDataJson }); return; } } #endregion #region 先插入Monitor表 List monitorCodeList = new List(); MonitorDataEntity monitorData = null; if (monitorCodes != null && monitorCodes.Count > 0) { foreach (var item in monitorCodes) { try { //新增判断T_TM_MonitorCode表里有的数据不执行 20180518 zxd if (ExistsMonitorCode(item.barcode)) continue; #region 时间获取 try { //如果是美国时间 if (item.recorddate.Contains("AM") || item.recorddate.Contains("PM")) { System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.CultureInfo("en-US", false).DateTimeFormat; dtfi.ShortTimePattern = "t"; item.CREATEDATE = DateTime.Parse(item.recorddate, dtfi); } else { item.CREATEDATE = DateTime.Parse(item.recorddate); } } catch (Exception ex) { item.CREATEDATE = System.DateTime.Now; LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Info = item.barcode + "时间转换出错:" + item.recorddate }); } #endregion monitorCodeList.Add(item); //先插入临时表,防止丢失数据。20180613 zxd //插入T_TM_MonitorData表 using (IDataSession sessionData = AppDataFactory.CreateMainSession()) { #region //插入数据库 try { if (monitorDatas != null && monitorDatas.Count > 0) { monitorData = monitorDatas.FirstOrDefault(x => x.id == item.monitorid); if (monitorData != null) { //更新T_TM_MonitorData表 monitorData.RecordDate = item.CREATEDATE; monitorData.ReadFlag = 1; //插入 sessionData.Insert(monitorData); } } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "插入T_TM_MonitorData表失败" }); LogManager.LogHelper.Info(new LogInfo() { Tag = "插入T_TM_MonitorData表", Info = "插入T_TM_MonitorData表失败,Code表条码:" + item.barcode + "失败原因:" + ex }); throw ex; } #endregion } //插入T_TM_MonitorCode表 using (IDataSession sessionCode = AppDataFactory.CreateMainSession()) { #region //插入数据库 try { //更新T_TM_MonitorCode表 item.ReadFlag = 1; //全部插入 sessionCode.Insert(item); #region 更新注塑件生成记录 //更新注塑件生成记录 string updateStr = string.Format("update T_AW_PRODUCTCOUNT set COUNTVALUE=COUNTVALUE+1 where MATERIAL_CODE='{0}' and PROTYPE=0", item.partnum); int i = sessionCode.ExecuteSql(updateStr); if (i == 0) { //插入注塑生成数量记录数量为1 string insertSql = string.Format("insert into T_AW_PRODUCTCOUNT values(newid(),'{0}',1,0,'{1}')", item.partnum, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); i = sessionCode.ExecuteSql(insertSql); //插入注塑件消耗数量记录,数量为0 string insertConsumeSql = string.Format("insert into T_AW_PRODUCTCOUNT values(newid(),'{0}',0,1,'{1}')", item.partnum, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); i = sessionCode.ExecuteSql(insertConsumeSql); } #endregion } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "插入T_TM_MonitorCode表失败" }); LogManager.LogHelper.Info(new LogInfo() { Tag = "插入T_TM_MonitorCode表", Info = "插入T_TM_MonitorCode表失败,条码:" + item.barcode + "失败原因:" + ex }); throw ex; } #endregion } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "插入T_TM_MonitorCode表失败" }); LogManager.LogHelper.Info(new LogInfo() { Tag = "插入T_TM_MonitorCode表", Info = "插入T_TM_MonitorCode表失败,条码:" + item.barcode + "失败原因:" + ex }); continue; } } } #endregion #region 再插入其他的业务逻辑表 if (monitorCodeList != null && monitorCodeList.Count > 0) { var shift = new ProduceShiftBLL().GetWorkingShift(""); foreach (var item in monitorCodeList) { try { //新增判断T_AW_PRODUCT表里有的数据不执行 20180330 zxd ProductDAL productDal = new ProductDAL(); if (productDal.ExistsProduct(new Product { PRODUCTCODE = item.barcode })) continue; //根据MATERIAL_CODE查出订单 状态1,2 var orderPlan = new OrderPlanDAL().GetOne(new OrderPlan { MATERIAL_CODE = item.partnum }); if (orderPlan == null || !string.Equals(orderPlan.MATERIAL_CODE, item.partnum)) { orderPlan = null; } //根据IPADDRESS查出设备id var machineId = ""; var machineCode = ""; var machineName = ""; var worklogCode = ""; var machines = new MachineInfoBLL().GetAllList(new MachineInfo { IPADDRESS = item.ipaddr }); if (machines != null && machines.Count > 0) { machineId = machines[0].PID; machineCode = machines[0].MACHINECODDE; machineName = machines[0].MACHINENAME; worklogCode = machines[0].WORKLOC_CODE; } //MainOperation表的模块号,防止mould = null. var MoldNumber = ""; UsageLogEntity usageLog = null; Mould mould = null; //根据模具资料找到模具 var mouldData = new MouldDAL().GetMouldCode(new MouldData { MOULDDATA_NAME = item.molddataname }); if (mouldData != null) { var moulds = new MouldBLL().GetAllList(new Mould { MOULD_CODE = mouldData.MOULDDATA_CODE }); if (moulds != null && moulds.Count > 0) { mould = moulds[0]; //模具使用次数加一 mould.USAGECOUNT = mould.USAGECOUNT + 1; MoldNumber = mould.MOULD_CODE; //模具使用记录增加一条 usageLog = new UsageLogEntity { PID = Guid.NewGuid().ToString(), MoldDataName = item.molddataname, MATERIAL_CODE = item.partnum, MOULD_PID = mould.PID, MACHINE_PID = machineId, OPERATION_DATE = item.CREATEDATE }; } } //根据零件号找到设备类别 var materials = new MD.DAL.MaterialDAL().GetList(new Material { MATERIAL_CODE = item.partnum }); var materialType = ""; var materialName = ""; var outSource = ""; if (materials != null && materials.Count > 0) { materialType = materials[0].MATERIAL_TYPE_CODE; materialName = materials[0].MATERIAL_NAME; outSource = materials[0].OUTSOURCE; } //投料 #region var product = new Product { //基本信息 PID = Guid.NewGuid().ToString(), MATERIAL_TYPE = materialType, MATERIAL_NAME = materialName, PRODUCTCODE = item.barcode, MATERIAL_CODE = item.partnum, MACHINECODDE = machineCode, MACHINENAME = machineName, PRODUCTSOURCE = outSource, STATUS = "0", //零件状态///0:合格 PRODUCESHIFTTCODE = shift != null ? shift.PRODUCESHIFTTCODE : "", OUTFLAG = "0", //出库标记///0:在库 USINGSTATE = "0", //使用状态///0:未使用 CAPACITY = 1, //实际容量 USINGCOUNT = 0, //使用数量 ENDOFLINE = "1", //加工结束标记 CREATEUSER = LoginUser.UserID, CREATEDATE = DateTime.Now, UPDATEUSER = LoginUser.UserID, UPDATEDATE = DateTime.Now }; #endregion DataResult> result = new DataResult>(); List proList = new List(); List mainOperations = new List(); List updateInjectList = new List(); List listCodes = new List(); //零件投料关联表 List addInjectItemList = new List(); var workcellCode = ""; var workcenterCode = ""; #region 初始化零件号 //获取产品零件号 //string productType = product.PRODUCTATTRIBUTE.VAL2 + product.PRODUCTATTRIBUTE.VAL3 + product.PRODUCTATTRIBUTE.VAL4; List initList = new MaterialCodeInitDAL().GetList(new MaterialCodeInit() { MATERIAL_CODE = product.MATERIAL_CODE }); if (initList.Count > 0) { MaterialCodeInit init = initList[0]; workcellCode = init.WORKCELL_CODE_INIT; workcenterCode = init.WORKCENTER_CODE_INIT; product.WORKCELL_CODE = init.WORKCELL_CODE; product.WORKCENTER_CODE = init.WORKCENTER_CODE; product.WORKLOC_CODE = init.WORKLOC_CODE; } ProcessRecord recorder = new ProcessRecord { PID = Guid.NewGuid().ToString(), PRODUCT_PID = product.PID, WORKLOC_CODE = worklogCode, WORKCELL_CODE = workcellCode, IN_WORKCELL_TIME = DateTime.Now, WORK_START_TIME = DateTime.Now, WORK_END_TIME = DateTime.Now, PROCESS_STATE = "1", MANUAL_PASS = "0", MODULE_CODE = "1" }; #endregion proList.Add(product); var operationPid = ""; foreach (Product pro in proList) { #region MainOperation MainOperation operation = new MainOperation { PID = Guid.NewGuid().ToString(), PDID = pro.PID, PRODUCTCODE = pro.PRODUCTCODE, MACHINENAME = product.MACHINENAME, MACHINECODDE = product.MACHINECODDE, STATUS = EnumGeter.STATUS.QUALIFIED.GetHashCode().ToString(), OPERATESTATE = EnumGeter.OPERATESTATE.COMPLETED.GetHashCode().ToString(), OPERATEDDATE = item.CREATEDATE, CURRENTPROCESS = EnumGeter.ProcessType.tangsu.GetHashCode().ToString(), PRODUCESHIFTNAME = product.PRODUCESHIFTNAME, PRODUCESHIFTTCODE = product.PRODUCESHIFTTCODE, PRODUCELINE = "", MODELSTATION = "", MOLDNUMBER = MoldNumber, CREATEUSER = product.CREATEUSER, CREATEDATE = System.DateTime.Now, UPDATEUSER = product.UPDATEUSER, UPDATEDATE = System.DateTime.Now, MATERIAL_CODE = product.MATERIAL_CODE, MATERIAL_NAME = product.MATERIAL_NAME, MATERIAL_TYPE_CODE = product.MATERIAL_TYPE, WORKCENTER_CODE = "INJECT", WORKCELL_CODE = workcellCode }; #endregion operationPid = operation.PID; mainOperations.Add(operation); listCodes.Add(operation.PRODUCTCODE); } if (product.AddM) //如果需要添加物料扣减需要调用接口 { MachineInfo mInfo = new MachineInfoBLL().GetAllList(new MachineInfo { MACHINECODDE = product.MACHINECODDE })[0]; Process.SlushProcess slush = new Process.SlushProcess(); addInjectItemList = slush.GetProductInjectionList(proList, mInfo, ref updateInjectList); } else { InjectionRecorderDAL injectDal = new InjectionRecorderDAL(); List listInject = injectDal.GetLastRecorder(new InjectionRecorder() { MACHINECODDE = product.MACHINECODDE, }); #region 获取投料信息 foreach (Product proitem in proList) { if (listInject.Count > 0) { InjectionRecorder inRecorder = listInject[0]; #region 零件投料关联记录 ProductInjection proInject = new ProductInjection(); proInject.PID = Guid.NewGuid().ToString(); proInject.IPID = proitem.PID; proInject.MPPID = inRecorder.PID; proInject.MATERIALNAME = inRecorder.MATERIALNAME; proInject.MATERIALCODE = inRecorder.MATERIALCODE; proInject.MATERIALTYPE = inRecorder.MATERIALTYPE; proInject.MATERIALBATCH = inRecorder.MATERIALBATCH; proInject.INJECTIONINDEX = inRecorder.INJECTIONINDEX; proInject.INJECTIONTERMINAL = inRecorder.INJECTIONTERMINAL; proInject.USEDWEIGHT = 0; proInject.CREATEDATE = System.DateTime.Now; proInject.CREATEUSER = inRecorder.CREATEUSER; proInject.UPDATEUSER = inRecorder.UPDATEUSER; proInject.PROCESSTYPE = product.CURRENTPROCESS; addInjectItemList.Add(proInject); #endregion } } #endregion } //插入T_AW_INJECTIONPARAMETER表 #region InJectionParameter InJectionParameter inJectionParameter = new InJectionParameter { PID = Guid.NewGuid().ToString(), PDID = product.PID, PRODUCTCODE = product.PRODUCTCODE, MOID = operationPid, MACHINENAME = machineName, MACHINECODDE = machineCode, RecordDate = DateTime.Now, CREATEDATE = DateTime.Now, OPERATEDATE = DateTime.Now }; #endregion if (monitorData != null) { #region inJectionParameter.IpAddr = monitorData.ipaddr; inJectionParameter.CycleTime = monitorData.cycletime; inJectionParameter.MoldOpenTime = monitorData.moldopentime; inJectionParameter.MoldCloseTime = monitorData.moldclosetime; inJectionParameter.InjHoldTime = monitorData.injholdtime; inJectionParameter.ChargeTime = monitorData.chargetime; inJectionParameter.InjSwitchTime = monitorData.injswitchtime; inJectionParameter.MoldProTime = monitorData.moldprotime; inJectionParameter.InjectStartPos = monitorData.injectstartpos; inJectionParameter.HoldEndPos = monitorData.holdendpos; inJectionParameter.ChargeEndPos = monitorData.chargeendpos; inJectionParameter.MoldOpenEndPos = monitorData.moldopentime; inJectionParameter.ChargeMaxRPM = monitorData.chargemaxrpm; inJectionParameter.InjectMaxSP = monitorData.injectmaxsp; inJectionParameter.InjectMaxPressure = monitorData.injectmaxpressure; inJectionParameter.InjectEPos = monitorData.injectepos; inJectionParameter.HoldPresSPos = monitorData.holdpresspos; inJectionParameter.CutOffPressure = monitorData.cutoffpressure; inJectionParameter.RecordDate = monitorData.RecordDate; #endregion } using (IDataSession session = AppDataFactory.CreateMainSession()) { #region 数据库操作 // 开始事务 session.OpenTs(); MainOperationDAL mainOpeDal = new MainOperationDAL(); ProductInjectionDAL proInDal = new ProductInjectionDAL(); InjectionRecorderDAL recoderDal = new InjectionRecorderDAL(); productDal.BaseSession = session; mainOpeDal.BaseSession = session; proInDal.BaseSession = session; recoderDal.BaseSession = session; try { //处理状态2,3 if (item.currentmode == "3" || item.currentmode == "2") { #region if (orderPlan != null) { if (orderPlan.PLAN_STATE == "1") { orderPlan.PLAN_STATE = "2"; } ++orderPlan.COMPLETE_QTY; //更新 session.Update(orderPlan); //插入 var pwp = new ProductWithPlan { PID = Guid.NewGuid().ToString(), PRODUCT_PID = product.PID, PLANID = orderPlan.PID }; session.Insert(pwp); } //mouldDal.Update(mould); if (mould != null) { if (mould.CREATEDATE == DateTime.MinValue) { mould.CREATEDATE = DateTime.Now; } if (mould.UPDATEDATE == DateTime.MinValue) { mould.UPDATEDATE = DateTime.Now; } if (mould.SYNC_DATE == DateTime.MinValue) { mould.SYNC_DATE = DateTime.Now; } session.Update(mould); } if (usageLog != null) { session.Insert(usageLog); } if (inJectionParameter != null) { session.Insert(inJectionParameter); } if (recorder != null) { //2018-7-12闫永刚 IMS系统的条码暂时不适用,插入T_AW_PROCESSRECORD造成冗余,先注释掉 //session.Insert(recorder); } #endregion #region 插入产品信息 foreach (Product proitem in proList) { productDal.InsertMainAndAttribute(proitem); //插入T_SA_WORKCELLQTYCOUNT表 #region WorkcellQtyCount WorkcellQtyCount workCellQtyCount = new WorkcellQtyCount { PID = Guid.NewGuid().ToString(), WORKCENTER_CODE = "INJECT", EQUIPMENT_CODE = proitem.MACHINECODDE, WORKCELL_CODE = proitem.WORKCELL_CODE, MATRIAL_CODE = proitem.MATERIAL_CODE, SHIFT_CODE = proitem.SHIFT_CODE, STATIS_DATE = DateTime.Now.Date, CREATEDATE = DateTime.Now, QTY = 1, OK_QTY=1 }; session.Insert(workCellQtyCount); #endregion } #endregion #region 插入操作记录 foreach (MainOperation mainitem in mainOperations) { mainOpeDal.Insert(mainitem); } #endregion #region 投料关联表 foreach (ProductInjection Injectitem in addInjectItemList) { proInDal.Insert(Injectitem); } foreach (InjectionRecorder Injectitem in updateInjectList) { recoderDal.Update(Injectitem); } #endregion } // 事务提交 session.CommitTs(); result.Result = listCodes; result.IsSuccess = true; result.Msg = Resource.MsgSuccess; } catch (Exception ex) { LogManager.LogHelper.Info(new LogInfo() { Tag = "读取模具ims信息", Info = "读取模具ims信息失败,条码:" + item.barcode + "失败原因:" + ex }); session.RollbackTs(); result.IsSuccess = false; result.Msg = Resource.MsgFaile; continue; } #endregion } } catch (Exception ex) { LogManager.LogHelper.Info(new LogInfo() { Tag = "读取模具ims信息", Info = "读取模具ims信息失败,条码:" + item.barcode + "失败原因:" + ex }); continue; } } } #endregion } /// /// 判断是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsMonitorCode(string PRODUCTCODE) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { sqlBuilder.Append("SELECT COUNT(1) FROM T_TM_MonitorCode"); if (string.IsNullOrEmpty(PRODUCTCODE) == false) { whereBuilder.Append(" AND PRODUCTCODE =@PRODUCTCODE "); parameters.Add(new DataParameter { ParameterName = "PRODUCTCODE", DataType = DbType.String, Value = PRODUCTCODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray())); } return count > 0; } catch (Exception ex) { throw ex; } } //执行task程序-报警数据插入 public void TempAlarmInsertTable() { var alarmDataJson = HttpGetWebApi("http://10.123.4.18:8210/KQService/getMachineAlarmData"); //var alarmDataJson = "[ { \"id\":\"1103883\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"测定模厚 (3)\",\"alarmgentime\":\"7/13/2018 6:06:48 PM\",\"alarmclrtime\":\"7/13/2018 11:09:15 AM\"}, { \"id\":\"1103884\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"锁模单元安全门 1 打开 (3)\",\"alarmgentime\":\"7/13/2018 6:08:31 PM\",\"alarmclrtime\":\"7/13/2018 11:17:44 AM\"}, { \"id\":\"1103885\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 6:08:33 PM\",\"alarmclrtime\":\"7/13/2018 11:17:44 AM\"}, { \"id\":\"1103886\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 6:08:34 PM\",\"alarmclrtime\":\"7/13/2018 11:17:44 AM\"}, { \"id\":\"1103887\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"按启动键 (3)\",\"alarmgentime\":\"7/13/2018 11:10:24 AM\",\"alarmclrtime\":\"7/13/2018 11:21:22 AM\"}, { \"id\":\"1103888\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 11:10:26 AM\",\"alarmclrtime\":\"7/13/2018 11:21:22 AM\"}, { \"id\":\"1103889\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 11:10:27 AM\",\"alarmclrtime\":\"7/13/2018 11:21:22 AM\"}, { \"id\":\"1103890\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"模具未关上 (3)\",\"alarmgentime\":\"7/13/2018 6:17:13 PM\",\"alarmclrtime\":\"7/13/2018 11:18:30 AM\"}, { \"id\":\"1103891\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"模具未关上 (1)\",\"alarmgentime\":\"7/13/2018 6:17:22 PM\",\"alarmclrtime\":\"7/13/2018 11:19:17 AM\"}, { \"id\":\"1103892\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"自动塑化 已激活 (3)\",\"alarmgentime\":\"7/13/2018 6:17:31 PM\",\"alarmclrtime\":\"7/13/2018 11:19:17 AM\"}, { \"id\":\"1103893\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"锁模单元安全门 1 打开 (3)\",\"alarmgentime\":\"7/13/2018 6:17:37 PM\",\"alarmclrtime\":\"7/13/2018 11:19:17 AM\"}, { \"id\":\"1103894\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 6:17:38 PM\",\"alarmclrtime\":\"7/13/2018 11:19:17 AM\"}, { \"id\":\"1103895\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 6:17:39 PM\",\"alarmclrtime\":\"7/13/2018 11:19:17 AM\"}, { \"id\":\"1103896\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"按启动键 (3)\",\"alarmgentime\":\"7/13/2018 6:18:10 PM\",\"alarmclrtime\":\"7/13/2018 11:20:03 AM\"}, { \"id\":\"1103897\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"按启动键 (3)\",\"alarmgentime\":\"7/13/2018 6:18:51 PM\",\"alarmclrtime\":\"7/13/2018 11:20:03 AM\"}, { \"id\":\"1103898\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 6:18:53 PM\",\"alarmclrtime\":\"7/13/2018 11:20:50 AM\"}, { \"id\":\"1103899\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"机械手马达关闭 (3)\",\"alarmgentime\":\"7/13/2018 6:19:34 PM\",\"alarmclrtime\":\"7/13/2018 11:20:50 AM\"}, { \"id\":\"1103900\",\"ipaddr\":\"10.123.4.172\",\"alarmdescription\":\"按启动键 (3)\",\"alarmgentime\":\"7/13/2018 6:20:07 PM\",\"alarmclrtime\":\"\"}, { \"id\":\"1103901\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"锁模单元安全门 1 打开 (3)\",\"alarmgentime\":\"7/13/2018 11:21:48 AM\",\"alarmclrtime\":\"7/13/2018 11:22:12 AM\"}, { \"id\":\"1103902\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 11:21:49 AM\",\"alarmclrtime\":\"7/13/2018 11:22:12 AM\"}, { \"id\":\"1103903\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 11:21:50 AM\",\"alarmclrtime\":\"7/13/2018 11:22:12 AM\"}, { \"id\":\"1103904\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"锁模单元安全门 1 打开 (3)\",\"alarmgentime\":\"7/13/2018 11:22:14 AM\",\"alarmclrtime\":\"7/13/2018 11:30:20 AM\"}, { \"id\":\"1103905\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 11:22:16 AM\",\"alarmclrtime\":\"7/13/2018 11:30:20 AM\"}, { \"id\":\"1103906\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 11:22:17 AM\",\"alarmclrtime\":\"7/13/2018 11:30:20 AM\"}, { \"id\":\"1103907\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"按启动键 (3)\",\"alarmgentime\":\"7/13/2018 11:30:06 AM\",\"alarmclrtime\":\"\"}, { \"id\":\"1103908\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围激活 (3)\",\"alarmgentime\":\"7/13/2018 11:30:08 AM\",\"alarmclrtime\":\"\"}, { \"id\":\"1103909\",\"ipaddr\":\"10.123.4.179\",\"alarmdescription\":\"安全踏板 - 范围1激活 (3)\",\"alarmgentime\":\"7/13/2018 11:30:09 AM\",\"alarmclrtime\":\"\"} ]"; LogManager.LogHelper.Info(new LogInfo { Info = alarmDataJson }); List alarmDatas = null; if (alarmDataJson != null) { alarmDatas = JsonToList(alarmDataJson); } if (alarmDatas != null && alarmDatas.Count > 0) { foreach (var item in alarmDatas) { try { //新增判断T_TM_MonitorCode表里有的数据不执行 20180518 zxd //if (ExistsTable(item.barcode)) continue; //插入T_TM_ALARM表 using (IDataSession sessionData = AppDataFactory.CreateMainSession()) { //插入数据库 try { //根据ip获得设备 var machine = new MachineInfoBLL().GetAllList(new MachineInfo { IPADDRESS = item.ipaddr }).FirstOrDefault(); if (machine != null) { item.MACHINECODE = machine.MACHINECODDE; } if (string.IsNullOrEmpty(item.alarmgentime) == false) { item.ALARMGENTIME = DateTime.Parse(item.alarmgentime); } if (string.IsNullOrEmpty(item.alarmclrtime) == false) { item.ALARMCLRTIME = DateTime.Parse(item.alarmclrtime); } item.CREATEDATE = DateTime.Now; //全部插入 sessionData.Insert(item); } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Tag = ex.StackTrace, Info = "插入T_TM_ALARM表失败" }); LogManager.LogHelper.Error(new LogInfo() { Tag = "插入T_TM_ALARM表", Info = "插入T_TM_ALARM表失败,id:" + item.id + "失败原因:" + ex.Message, ErrorInfo = ex }); } } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { Tag = "插入T_TM_ALARM表", Info = "插入T_TM_ALARM表失败,id:" + item.id + "失败原因:" + ex.Message, ErrorInfo = ex }); continue; } } } } } }