using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllEapDal; using DllEapEntity; using DllEapEntity.Dtos; using DllEapEntity.Rms; using DllHsms; using DllStatusShowDal; using Newtonsoft.Json; using RabbitMQ.Client; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; namespace DllStatusShowBll { public class StatusShowBll : IDisposable { private string CurrDbCode = "sqlconn"; private IDatabase CurrDb = null; public StatusShowBll(string dbcode) { CurrDbCode = dbcode; CurrDb = DbFactory.Base(CurrDbCode); } public StatusShowBll() { } public void Dispose() { if (CurrDb != null) CurrDb.Close(); } public Hashtable IUTLcd(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); TLcd entity = JsonConvert.DeserializeObject((string)imputds[nameof(TLcd)]); string usercode = order.usercode; CurrDb.BeginTrans(); TLcdDal tempdal = new TLcdDal(CurrDb); entity = tempdal.IUTLcd(entity, order.usercode, ref errorinfo); if (entity == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(TLcd), JsonConvert.SerializeObject(entity)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable DelTLcd(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); List entity = JsonConvert.DeserializeObject>((string)imputds[nameof(TLcd)]); string usercode = order.usercode; CurrDb.BeginTrans(); TLcdDal tempdal = new TLcdDal(CurrDb); TLcd result = tempdal.DelTLcd(entity, order.usercode, ref errorinfo); if (result == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(TLcd), JsonConvert.SerializeObject(result)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable IUTEntity(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); TEntity entity = JsonConvert.DeserializeObject((string)imputds[nameof(TEntity)]); string usercode = order.usercode; CurrDb.BeginTrans(); TEntityDal tempdal = new TEntityDal(CurrDb); entity = tempdal.IUTEntity(entity, order.usercode, ref errorinfo); if (entity == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(TEntity), JsonConvert.SerializeObject(entity)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable DelTEntity(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); List entity = JsonConvert.DeserializeObject>((string)imputds[nameof(TEntity)]); string usercode = order.usercode; CurrDb.BeginTrans(); TEntityDal tempdal = new TEntityDal(CurrDb); TEntity result = tempdal.DelTEntity(entity, order.usercode, ref errorinfo); if (result == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(TEntity), JsonConvert.SerializeObject(result)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable IULayoutMst(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); LayoutMst entity = JsonConvert.DeserializeObject((string)imputds[nameof(LayoutMst)]); List details = JsonConvert.DeserializeObject>((string)imputds[nameof(LayoutDetail)]); string usercode = order.usercode; CurrDb.BeginTrans(); LayoutMstDal tempdal = new LayoutMstDal(CurrDb); entity = tempdal.IULayoutMst(entity, details, order.usercode, ref errorinfo); if (entity == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(LayoutMst), JsonConvert.SerializeObject(entity)); CurrDb.Commit(); //广播layout改变 BroadcastLayout(entity, ref errorinfo); errorinfo = ""; return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } private int BroadcastLayout(LayoutMst mst, ref string errorinfo) { IConnection connection = null; try { List mqserver = CurrDb.FindListForCondition("", ref errorinfo).ToList(); if (mqserver == null) return -1; foreach (var item in mqserver) { var factory = new ConnectionFactory(); factory.HostName = item.IpAddress; factory.UserName = item.FUser; factory.Password = item.FPasswd; connection = factory.CreateConnection(); var channel = connection.CreateModel(); byte[] queuedata = EntityHelper.SerializeBytes(mst); if (queuedata == null) return -1; string LayoutExchange = AppConfigurtaionServices.Configuration["rabbitmq:LayoutExchange"]; channel.ExchangeDeclare(exchange: LayoutExchange, type: "fanout", durable: false, autoDelete: true); channel.BasicPublish(LayoutExchange, "", null, queuedata); //开始传递 channel.Close(); connection.Close(); } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public Hashtable DelLayoutMst(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); List entity = JsonConvert.DeserializeObject>((string)imputds[nameof(LayoutMst)]); string usercode = order.usercode; CurrDb.BeginTrans(); LayoutMstDal tempdal = new LayoutMstDal(CurrDb); LayoutMst result = tempdal.DelLayoutMst(entity, order.usercode, ref errorinfo); if (result == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(LayoutMst), JsonConvert.SerializeObject(result)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable IMacStatus(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); MacStatus entity = JsonConvert.DeserializeObject((string)imputds[nameof(MacStatus)]); string usercode = order.usercode; CurrDb.BeginTrans(); MacStatusDal tempdal = new MacStatusDal(CurrDb); entity = tempdal.IMacStatus(entity, order.usercode, ref errorinfo); if (entity == null) { CurrDb.Rollback(); return null; } //将状态广播到消息队列里 string temperrorinfo = ""; BroadcastStatus(entity, ref temperrorinfo); Hashtable reds = new Hashtable(); reds.Add(nameof(MacStatus), JsonConvert.SerializeObject(entity)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } /// /// 广播状态 /// /// /// /// private int BroadcastStatus(MacStatus status, ref string errorinfo) { IConnection connection = null; try { var factory = new ConnectionFactory(); factory.HostName = AppConfigurtaionServices.Configuration["rabbitmq:IPAddress"];// "127.0.0.1";// System.Configuration.ConfigurationManager.AppSettings["mqaddress"].ToString(); factory.UserName = AppConfigurtaionServices.Configuration["rabbitmq:UserName"];// "admin";// System.Configuration.ConfigurationManager.AppSettings["mquser"].ToString(); factory.Password = AppConfigurtaionServices.Configuration["rabbitmq:Password"];// "admin";// System.Configuration.ConfigurationManager.AppSettings["mqpassword"].ToString(); connection = factory.CreateConnection(); var channel = connection.CreateModel(); byte[] queuedata = EntityHelper.SerializeBytes(status); if (queuedata == null) return -1; string StatusExchange = AppConfigurtaionServices.Configuration["rabbitmq:StatusExchange"]; channel.ExchangeDeclare(exchange: StatusExchange, type: "fanout", durable: false, autoDelete: true); channel.BasicPublish(StatusExchange, "", null, queuedata); //开始传递 channel.Close(); return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } finally { if (connection != null) connection.Close(); } } public Hashtable BatUpdatePreStatus(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); List entity = JsonConvert.DeserializeObject>((string)imputds[nameof(LayoutDetail)]); string usercode = order.usercode; CurrDb.BeginTrans(); MacStatusDal tempdal = new MacStatusDal(CurrDb); LayoutDetail result = tempdal.BatUpdatePreStatus(entity, order.usercode, ref errorinfo); if (result == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(LayoutDetail), JsonConvert.SerializeObject(result)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable ReadOrderMst(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); LayoutMst entity = JsonConvert.DeserializeObject((string)imputds[nameof(LayoutMst)]); string usercode = order.usercode; CurrDb.BeginTrans(); List macorders = new List(); List orderstatus = new List(); LayoutMstDal tempdal = new LayoutMstDal(CurrDb); entity = tempdal.ReadOrderMst(entity, macorders, orderstatus, ref errorinfo); if (entity == null) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(MacOrder), JsonConvert.SerializeObject(macorders)); reds.Add(nameof(OrderStatus), JsonConvert.SerializeObject(orderstatus)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable SynchronousMac(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); //TEntity entity = JsonConvert.DeserializeObject((string)imputds[nameof(TEntity)]); string usercode = order.usercode; CurrDb.BeginTrans(); TEntityDal tempdal = new TEntityDal(CurrDb); int result = tempdal.SynchronousMac(order.usercode, ref errorinfo); if (result <= 0) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(ConstOrder), JsonConvert.SerializeObject(order)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } /// /// 读取状态信息 /// /// /// /// /// public Hashtable ReadStatusInfo(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); LayoutDetail entity = JsonConvert.DeserializeObject((string)imputds[nameof(LayoutDetail)]); //MacStatus status = JsonConvert.DeserializeObject((string)imputds[nameof(MacStatus)]); //string usercode = order.usercode; ////CurrDb.BeginTrans(); //List macorders = new List(); //List orderstatus = new List(); //LayoutMstDal tempdal = new LayoutMstDal(CurrDb); ////entity = tempdal.ReadOrderMst(entity, macorders, orderstatus, ref errorinfo); ////if (entity == null) ////{ //// CurrDb.Rollback(); //// return null; ////} //从MES系统里读取数据 StatusEntityInfo result = ReadStatusInfoFromMes(entity.FCode); Hashtable reds = new Hashtable(); reds.Add(nameof(StatusEntityInfo), JsonConvert.SerializeObject(result)); //reds.Add(nameof(OrderStatus), JsonConvert.SerializeObject(orderstatus)); //CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } } private StatusEntityInfo ReadStatusInfoFromMes(string maccode) { StatusEntityInfo entity = new StatusEntityInfo(); IDatabase sqldb = DbFactory.Base("sqlconn"); //查找机台正在进行中的lot string sqlstr = $@"select a.ID,b.PoCode,c.PartCode,d.FName partname from JobBooking a inner join LotMst b on a.LotNo = b.LotNo inner join ProductOrder c on b.PoCode = c.FCode inner join Part d on c.PartCode=d.Fcode where a.MacCode = '{maccode}' and a.StatusID = 1"; DataTable jbdt = sqldb.FindTableFor(sqlstr, "JobBooking"); if (jbdt.Rows.Count <= 0) return entity; entity.PartCode = jbdt.Rows[0]["PartCode"].ToString(); entity.PkgModel = jbdt.Rows[0]["partname"].ToString(); //读取报工记录 sqlstr = $@"select b.FName from JbStaff a inner join Staff b on a.StaffCode = b.FCode where a.JbID = '{jbdt.Rows[0]["ID"].ToString()}' and a.StatusID = 1"; DataTable tempdt = sqldb.FindTableFor(sqlstr, "JbStaff"); if (tempdt.Rows.Count > 0) entity.OperUser = tempdt.Rows[0]["FName"].ToString(); //读取材料信息 sqlstr = $@"select a.PtCode,b.FName from PoPart a inner join Part b on a.PtCode=b.FCode where a.PoCode='{jbdt.Rows[0]["PoCode"].ToString()}'"; tempdt = sqldb.FindTableFor(sqlstr, "PoPart"); if (tempdt.Rows.Count > 0) { int row = tempdt.Rows.Count < 4 ? tempdt.Rows.Count : 4; for (int i = 0; i < row; i++) { EntityHelper.SetPropertyValue(entity, "MaterialModel" + i.ToString(), tempdt.Rows[i]["PtCode"].ToString()); EntityHelper.SetPropertyValue(entity, "MaterialName" + i.ToString(), tempdt.Rows[i]["FName"].ToString()); } } sqldb.Close(); return entity; } /// /// 读取状态统计 /// /// /// /// /// public Hashtable ReadStatusTotal(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); //LayoutDetail entity = JsonConvert.DeserializeObject((string)imputds[nameof(LayoutDetail)]); //MacStatus status = JsonConvert.DeserializeObject((string)imputds[nameof(MacStatus)]); //string usercode = order.usercode; ////CurrDb.BeginTrans(); //List macorders = new List(); //List orderstatus = new List(); //LayoutMstDal tempdal = new LayoutMstDal(CurrDb); ////entity = tempdal.ReadOrderMst(entity, macorders, orderstatus, ref errorinfo); ////if (entity == null) ////{ //// CurrDb.Rollback(); //// return null; ////} //从MES系统里读取数据 StatusTotal result = ReadStatusTotalFromMes(); Hashtable reds = new Hashtable(); reds.Add(nameof(StatusTotal), JsonConvert.SerializeObject(result)); //reds.Add(nameof(OrderStatus), JsonConvert.SerializeObject(orderstatus)); //CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } } public StatusTotal ReadStatusTotalFromMes() { StatusTotal entity = new StatusTotal(); entity.Mtbfs = new List(); IDatabase sqldb = DbFactory.Base("eap"); McaEventStatisticByDayDal totaldal = new McaEventStatisticByDayDal(sqldb); //string condition = $" and StartTime>='2019-03-31 00:00:00'"; DateTime dateStart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); DateTime dateEnd = DateTime.Now; string condition = $" and StartTime>='{DateTime.Now.ToString("yyyy-MM-dd")} 00:00:00'"; List mtbfs = totaldal.GetMTBFs(condition, string.Empty, "machine").ToList(); var pcode = mtbfs.GroupBy(t => t.pcode).Where(p => p.Key != null).ToList(); if (pcode.Count <= 0) { } else { foreach (var item in pcode) { MTBFDto tempentity = new MTBFDto(); tempentity.pcode = item.Key; tempentity.MTBF = item.Sum(t => t.MTBF); entity.Mtbfs.Add(tempentity); } } List effics = totaldal.GetMachineEfficienciesGroupByPcode(condition, string.Empty, dateStart, dateEnd).ToList(); entity.Effics = effics == null ? new List() : effics; // var efficsgroupby = effics.GroupBy(t => t.PCode).Where(p => p.Key != null).ToList(); //if (efficsgroupby.Count <= 0) //{ //} //else //{ // foreach (var item in efficsgroupby) // { // MachineEfficiency tempentity = item; // tempentity.PCode = item.Key; // tempentity.OverallEfficiency = item.Sum(t => t.OverallEfficiency) / item.Count(); // entity.Effics.Add(tempentity); // } //} //entity.TotalMtbf = mtbfs.Sum(t => t.MTBF).Value; //entity.DieMtbf = mtbfs.Where(t => t.pcode == "DB").Sum(t => t.MTBF).Value; //entity.WireMtbf = mtbfs.Where(t => t.pcode == "WB").Sum(t => t.MTBF).Value; //List effics = totaldal.GetMachineEfficienciesGroupByMacModel(condition).ToList(); //if(effics.Count>0) // entity.TotalEfficiency = (double)effics.Sum(t => t.OverallEfficiency)/effics.Count; //effics = effics.Where(t => t.PCode == "DB").ToList(); //if (effics.Count > 0) // entity.DieEfficiency = (double)effics.Sum(t => t.OverallEfficiency)/ effics.Count; //effics = effics.Where(t => t.PCode == "WB").ToList(); //if (effics.Count > 0) // entity.WireEfficiency = (double)effics.Sum(t => t.OverallEfficiency) / effics.Count; sqldb.Close(); return entity; } public Hashtable UStandardStatus(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); List entitys = JsonConvert.DeserializeObject>((string)imputds[nameof(StandardStatus)]); string usercode = order.usercode; CurrDb.BeginTrans(); int result = CurrDb.UpdateFor(entitys, usercode); if (result < 0) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add(nameof(StandardStatus), JsonConvert.SerializeObject(entitys[0])); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } /// /// 上传程序文件 /// /// /// /// /// //public Hashtable UpProgram(Hashtable imputds, ref string errorinfo, ref int error) //{ // try // { // if (imputds == null) // { // errorinfo = "传入数据不能为空,请确认"; // error = 1; // return null; // } // ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); // OrderBlock entitys = JsonConvert.DeserializeObject((string)imputds[nameof(OrderBlock)]); // string usercode = order.usercode; // CurrDb.BeginTrans(); // //上传文件 // int result = UpProgram(CurrDb, entitys, usercode, ref errorinfo); // if(result==-100) // { // CurrDb.Commit(); // return null; // } // else if(result<=0) // { // CurrDb.Rollback(); // return null; // } // Hashtable reds = new Hashtable(); // reds.Add(nameof(McaSecVMst), JsonConvert.SerializeObject(entitys.MainMsg)); // CurrDb.Commit(); // return reds; // } // catch (Exception ex) // { // errorinfo = ex.Message.ToString(); // error = 1; // CurrDb.Rollback(); // return null; // } //} //private int UpProgram(IDatabase db,OrderBlock entity,string usercode,ref string errorinfo) //{ // try // { // string filedir = AppConfigurtaionServices.Configuration["ProgramDir"]; // List datas = entity.Datalists.Where(t => t.ParentID == 0).OrderBy(t => t.FNum).ToList(); // int parentid = datas[0].ID;//获取L的ID值 // datas = entity.Datalists.Where(t => t.ParentID == parentid).OrderBy(t => t.FNum).ToList(); // string programname = datas[0].FContent; // string programdata = datas[1].FContent; // byte[] filedatas = datas[1].OrgDatas; // //查找程序表,是否存在此程序名称的记录 // string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(ProgramMst.FName))}='{programname}'"; // List programmst = db.FindListForCondition(condition, ref errorinfo).ToList(); // ProgramMst programentity = null; // if(programmst.Count<=0) // { // //添加程序记录 // programentity = new ProgramMst(); // errorinfo = $"未找到程序【{programname}】,请先配置程序及其比对的参数。"; // return -1; // } // else // { // programentity = programmst[0]; // } // //比对参数,并保存比对结果 // ParamsComMstDal comparedal = new ParamsComMstDal(db); // ParamsComMst commst = comparedal.CompareParams(entity.MainMsg.McaCode, programentity.ID, ref errorinfo); // if (commst == null) // return -1; // //if(commst.IsPass<=0) // //{ // // errorinfo = "参数比对没有通过。"; // // return -100; // //} // commst = comparedal.IUParamsComMst(commst, usercode, ref errorinfo); // if (commst == null) // return -1; // if (commst.IsPass <= 0) // { // errorinfo = "参数比对没有通过。"; // return -100; // } // programentity.ServerPath = filedir + $"\\{programentity.ID}"; // int result = UnityHelper.WriteFile(programentity.ServerPath, filedatas, ref errorinfo); // if (result <= 0) // return -1; // //更新服务器程序路径 // result=db.UpdateFor(programentity, usercode); // if (result <= 0) // return -1; // return 1; // } // catch(Exception ex) // { // errorinfo = ex.Message.ToString(); // return -1; // } //} /// /// 参数比对 /// /// /// /// /// public Hashtable CompareParams(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); string maccode = (string)imputds["maccode"]; CurrDb.BeginTrans(); //读取机台信息 string condition = $" and a.FCode='{maccode}'"; Machine mac = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; if (mac.IsControl <= 0) { //说明此机台没有开启参数比对功能,无需比对 CurrDb.Commit(); Hashtable tempreds = new Hashtable(); tempreds.Add("result", "1"); return tempreds; } //string programname = (string)imputds["programname"]; ParamsComMstDal comdal = new ParamsComMstDal(CurrDb); //string programname = comdal.GetMacProgram(maccode, ref errorinfo); //if(string.IsNullOrEmpty(programname)) //{ // CurrDb.Rollback(); // return null; //} ProgramMst programmst = comdal.GetMacProgram(mac, ref errorinfo); if (programmst == null) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"查找程序错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } ParamsComMst mst = comdal.CompareParams(mac, programmst, ref errorinfo); if (mst == null) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"比对发生错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } //插入比对记录 mst = comdal.IUParamsComMst(mst, order.usercode, ref errorinfo); if (mst == null) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"添加比对记录发生错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } CurrDb.Commit(); //如果参数不对,就发送停机指令 if (mst.IsPass <= 0) { MacOrderSendDal tempdal = new MacOrderSendDal(CurrDb); int result = tempdal.SendStopMac(maccode, ref errorinfo); if (result <= 0) { return null; } } Hashtable reds = new Hashtable(); reds.Add("result", "1"); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable DownloadProgram(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); string maccode = (string)imputds["maccode"]; string partcode = (string)imputds["partcode"]; string pcode = (string)imputds["pcode"]; //string programname = (string)imputds["programname"]; CurrDb.BeginTrans(); ProgramDal programdal = new ProgramDal(CurrDb); Machine mac = programdal.ReadMachine(maccode, ref errorinfo); if (mac == null) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"读取机台错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } ProgramMst mst = programdal.FindProgram(mac, partcode, pcode, ref errorinfo); if (mst == null) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"查找程序错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } ProgramDal dal = new ProgramDal(CurrDb); int result = dal.DownloadProgram(maccode, mst.FName, ref errorinfo); if (result <= 0) { string logerrorinfo = ""; WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"调程发生错误:{errorinfo}", ref logerrorinfo); CurrDb.Rollback(); return null; } ////读取程序数据 //string programdir = AppConfigurtaionServices.Configuration["ProgramDir"]; //MacProgramDal macprogramdal = new MacProgramDal(CurrDb); //int filelen = 0; //string filepath = ""; //string programid = ""; //int result = macprogramdal.ReadMacProgram(mac.FCode, mst.FName, programdir, ref filelen, ref filepath, ref errorinfo); //if (result <= 0) //{ // CurrDb.Rollback(); // return null; //} //switch (mac.SupplierFCode) //{ // case "ASM": // AsmProgramDal comdal = new AsmProgramDal(CurrDb); // mst = comdal.DownloadProgram(maccode, partcode, pcode, filelen, filepath, ref errorinfo); // if (mst == null) // { // string logerrorinfo = ""; // WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"下载程序错误:{errorinfo}", ref logerrorinfo); // CurrDb.Rollback(); // return null; // } // break; // case "KNS": // KnsProgramDal comdal1 = new KnsProgramDal(CurrDb); // mst = comdal1.DownloadProgram(mac, mst, filelen, filepath, ref errorinfo); // if (mst == null) // { // string logerrorinfo = ""; // WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"下载程序错误:{errorinfo}", ref logerrorinfo); // CurrDb.Rollback(); // return null; // } // //选择程序 // int res = comdal1.SelectProgram(mac, mst.FName, ref errorinfo); // if (res <= 0) // { // string logerrorinfo = ""; // WriteLog.WriteLogStr(maccode, "", DateTime.Now, $"选择程序错误:{errorinfo}", ref logerrorinfo); // CurrDb.Rollback(); // return null; // } // break; //} Hashtable reds = new Hashtable(); reds.Add(nameof(ProgramMst), JsonConvert.SerializeObject(mst)); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if (CurrDb != null) { CurrDb.Close(); } } } public Hashtable GetQuality(Hashtable imputds, ref string errorinfo, ref int error) { try { if (imputds == null) { errorinfo = "传入数据不能为空,请确认"; error = 1; return null; } ConstOrder order = JsonConvert.DeserializeObject((string)imputds[nameof(ConstOrder)]); string maccode = (string)imputds["maccode"]; //string programname = (string)imputds["programname"]; CurrDb.BeginTrans(); ProgramDal programdal = new ProgramDal(CurrDb); Machine mac = programdal.ReadMachine(maccode, ref errorinfo); if (mac == null) { CurrDb.Rollback(); return null; } MacCountDal dal = new MacCountDal(CurrDb); int result = dal.IMacCount(mac, order.usercode, ref errorinfo); if (result < 0) { CurrDb.Rollback(); return null; } Hashtable reds = new Hashtable(); reds.Add("result", result); CurrDb.Commit(); return reds; } catch (Exception ex) { errorinfo = ex.Message.ToString(); error = 1; CurrDb.Rollback(); return null; } finally { if(CurrDb!=null) { CurrDb.Close(); } } } public List ReadLastStatus(ref string errorinfo) { string condition = $@" and {EntityAttribute.GetPropertyCondition(nameof(MacStatus.ETime))} laststatus = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); List result = new List(); MacStatus entity = new MacStatus(); entity.StatusID = MacStatusVal.Run; entity.StatusFName = "运行"; entity.FLen = laststatus.Where(t => t.StatusID == entity.StatusID).Count(); result.Add(entity); entity = new MacStatus(); entity.StatusID = MacStatusVal.Error; entity.StatusFName = "故障"; entity.FLen = laststatus.Where(t => t.StatusID == entity.StatusID).Count(); result.Add(entity); entity = new MacStatus(); entity.StatusID = MacStatusVal.Pause; entity.StatusFName = "暂停"; entity.FLen = laststatus.Where(t => t.StatusID == entity.StatusID).Count(); result.Add(entity); entity = new MacStatus(); entity.StatusID = MacStatusVal.Idle; entity.StatusFName = "空闲"; entity.FLen = laststatus.Where(t => t.StatusID == entity.StatusID).Count(); result.Add(entity); entity = new MacStatus(); entity.StatusID = MacStatusVal.Pending; entity.StatusFName = "待料"; entity.FLen = laststatus.Where(t => t.StatusID == entity.StatusID).Count(); result.Add(entity); return result.Where(c => c.FLen > 0).ToList(); } public List ReadParamsComMst(ref string errorinfo) { string condition = $@" and a.IsPass<=0 and a.rectime between date_format('{DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd")} 00:00:00', '%Y%m%d%H%i%s') and date_format('{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}', '%Y%m%d%H%i%s')"; // string condition = $@" and a.IsPass<=0 "; List mts = CurrDb.FindListForCondition(condition, ref errorinfo).Where(c => !string.IsNullOrEmpty(c.ProgramFName)) .OrderByDescending(c => c.RecTime).Take(5).ToList(); return mts; } } }