1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Rms;
- using DllHsms;
- using DllHsmsWeb;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading;
- namespace DllEapDal
- {
- /// <summary>
- /// 参数比对数据访问层
- /// </summary>
- public class ParamsComMstDal
- {
- private IDatabase CurrDb = null;
- public ParamsComMstDal(IDatabase db)
- {
- CurrDb = db;
- }
- public ParamsComMst IUParamsComMst(ParamsComMst mst,string usercode, ref string errorinfo)
- {
- try
- {
- int result = CurrDb.InsertFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- foreach (var item in mst.CurrDetails)
- {
- item.PreID = id;
- item.EntityStatusID = 1;
- item.ID = 0;
- }
- List<PCSubDetail> pcsubdetails = null;
- foreach(var item in mst.CurrDetails)
- {
- result = CurrDb.InsertFor<ParamsComDetail>(item, usercode);
- if (result < 0)
- return null;
- objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int subid = int.Parse(objid.ToString());
- pcsubdetails = item.pcsubdetails;
- foreach (var subitem in pcsubdetails)
- {
- subitem.PreID = subid;
- subitem.ID = 0;
- subitem.EntityStatusID = 1;
- }
- result = CurrDb.InsertFor<PCSubDetail>(pcsubdetails, usercode);
- if (result < 0)
- return null;
- }
- mst = CurrDb.FindEntityFor<ParamsComMst>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private string GetSecCode(int ftype, ref string errorinfo)
- {
- try
- {
- StringBuilder sqlstr = new StringBuilder(100);
- string code = "";
- switch (ftype)
- {
- case 1:
- code = "S";
- break;
- case 2:
- code = "E";
- break;
- case 3:
- code = "C";
- break;
- }
- sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code);
- object obj = CurrDb.FindObject(sqlstr.ToString());
- if (obj.ToString() == "")
- return code + "00001";
- int fnum = int.Parse(obj.ToString().Substring(1, 5));
- fnum++;
- return code + fnum.ToString().PadLeft(5, '0');
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- public int CompareParamsForOthers(string maccode,string programname, string usercode, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.FCode='{maccode}'";
- Machine mac = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).FirstOrDefault();
- if(mac==null)
- {
- errorinfo = $"未找到机台编号={maccode}的机台信息......";
- return -1;
- }
- if (mac.IsControl == 1)
- {
- condition = $" and a.FName='{programname}' and a.ProcessCode='{mac.PCode}' and a.ModelID={mac.MModeID}";
- List<ProgramMst> programmsts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- if (programmsts.Count <= 0)
- {
- errorinfo = $"未找到程序名称【{programname}】,制程代码【{mac.PCode}】,机型代码【{mac.MModeCode}】对应的信息。";
- return -1;
- }
- ProgramMst programmst = programmsts[0];
- ParamsComMst mst = null;
- //比对程序参数
- mst = CompareParams(mac, programmst, ref errorinfo);
- if (mst == null)
- {
- errorinfo = $"未找到机台【{mac.FCode}】程序【{programmst.FName}】对应的比对主档。{errorinfo}";
- return -1;
- }
- //插入比对记录
- mst = IUParamsComMst(mst, usercode, ref errorinfo);
- if (mst == null)
- {
- errorinfo = $"插入比对结果发生错误:{errorinfo}";
- return -1;
- }
- //如果参数不对,就发送停机指令
- if (mst.IsPass <= 0)
- {
- return -100; //要提交到数据库
- }
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public ParamsComMst CompareParams(Machine mac, ProgramMst programmst, ref string errorinfo)
- {
- try
- {
- //读取机台信息
- //Machine mac = ReadMachine(maccode, ref errorinfo);
- //if (mac == null)
- // return null;
- //菲尼克斯专用函数
- List<ParamsComDetail> deslist = null;
- string client = AppConfigurtaionServices.Configuration["Client"];
- switch(client)
- {
- case "Onsemi":
- deslist = CompareParamsDetailForOnsemi(mac, programmst.ID, programmst.FName, ref errorinfo);
- if (deslist == null)
- return null;
- break;
- default:
- deslist = CompareParamsDetail(mac, programmst.ID, ref errorinfo);
- if (deslist == null)
- return null;
- break;
- }
- ParamsComMst mst = new ParamsComMst();
- mst.CurrDetails = deslist;
- List<ParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
- if (nopass.Count > 0)
- {
- mst.IsPass = -1;
- }
- else
- {
- mst.IsPass = 1;
- }
- mst.MacID = mac.ID;
- mst.ProgramID = programmst.ID;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- //public ParamsComMst CompareParams(string maccode, string programname, ref string errorinfo)
- //{
- // try
- // {
- // string condition = $" and a.fname='{programname}'";
- // List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- // if (msts == null)
- // return null;
- // if(msts.Count<=0)
- // {
- // errorinfo = $"未找到程序名称【{programname}】的记录。";
- // return null;
- // }
- // return CompareParams(maccode,msts[0].ID,ref errorinfo);
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return null;
- // }
- //}
- /// <summary>
- /// 根据机台ID、程序ID,产生参数比较结果
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="programid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public List<ParamsComDetail> CompareParamsDetail(Machine mac,int programid,ref string errorinfo)
- {
- try
- {
- //读取程序参数
- //string condition = $" and a.preid={programid} and a.IsCompare=1";
- //读取程序所有参数,不比对也要抓数据保存,以便今后查找
- string condition = $" and a.preid={programid}";
- List<ProgramParamsDetail> programparams = CurrDb.FindListForCondition< ProgramParamsDetail>(condition, ref errorinfo).ToList();
- if (programparams == null)
- return null;
- //if(programparams.Count<=0)
- //读取程序参数子参数
- condition = $" and a.preid in(select id from ProgramParamsDetail where preid={programid} and IsCompare=1)";
- List<PPSubDetail> ppsubdetails = CurrDb.FindListForCondition<PPSubDetail>(condition, ref errorinfo).ToList();
- if (ppsubdetails == null)
- return null;
- List<ParamsComDetail> deslist = MadeParamsComDetail(programparams,ppsubdetails, ref errorinfo);
- if (deslist == null)
- return null;
- if (deslist.Count <= 0)//没有设置比对参数
- return deslist;
- #region 构造参数结构
- //List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == SecType.SVID).OrderBy(t => t.FNum).ToList();
- //List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
- //if (datas == null)
- // return null;
- //OrderDetail order= ReadMachineOrderDetail(mac.ID,1,3, ref errorinfo);
- //if (order == null)
- // return null;
- //HsmsWeb accessmac = new HsmsWeb();
- //OrderBlock rec= accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- ////将返回值填写到参数列表
- //int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- //if (result <= 0)
- // return null;
- //order = ReadMachineOrderDetail(mac.ID, 2, 13, ref errorinfo);
- //if (order == null)
- // return null;
- ////构造参数结构
- //templist = deslist.Where(t => t.SecFType == SecType.ECID).OrderBy(t => t.FNum).ToList();
- //datas = MadeOrderData(templist, ref errorinfo);
- //if (datas == null)
- // return null;
- //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- ////将返回值填写到参数列表
- //result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- //if (result <= 0)
- // return null;
- #endregion
- int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
- if (result <= 0)
- return null;
- result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
- if (result <= 0)
- return null;
- //遍历参数明细,判断每条的比对结果
- foreach (var item in deslist)
- {
- result = CompareParamsComDetail(item);
- }
- return deslist;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public List<ParamsComDetail> CompareParamsDetailForOnsemi(Machine mac, int programid,string orgpname, ref string errorinfo)
- {
- try
- {
- //读取程序参数
- //string condition = $" and a.preid={programid} and a.IsCompare=1";
- //读取程序所有参数,不比对也要抓数据保存,以便今后查找
- string programname = "0000000000";
- string condition = $" and a.FName='{programname}' and a.ProcessCode='{mac.PCode}' and a.ModelID={mac.MModeID}";
- List<ProgramMst> programmsts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- if (programmsts.Count <= 0)
- {
- errorinfo = $"未找标准程序0000000000,制程代码【{mac.PCode}】,机型代码【{mac.MModeCode}】对应的信息。";
- return null;
- }
- programid = programmsts[0].ID;
- condition = $" and a.preid={programid}";
- List<ProgramParamsDetail> programparams = CurrDb.FindListForCondition<ProgramParamsDetail>(condition, ref errorinfo).OrderBy(t=>t.FNum).ToList();
- if (programparams == null)
- return null;
- //if(programparams.Count<=0)
- //根据机型和程序名称去中间表中读取刀片信息
- condition = $" and a.RecipeName='{orgpname}' and a.MCType='{mac.MModeCode}'";
- List<OnsemiProgram> onprograms = CurrDb.FindListForCondition<OnsemiProgram>(condition, ref errorinfo).ToList();
- if(onprograms==null)
- {
- errorinfo = $"从表OnsemiProgram中抓取刀片信息失败:{errorinfo};程序名称={orgpname},机型={mac.MModeCode}";
- return null;
- }
- if (onprograms.Count<=0)
- {
- errorinfo = $"在表OnsemiProgram中未找到刀片信息;程序名称={orgpname},机型={mac.MModeCode}";
- return null;
- }
- if(mac.MModeCode.ToLower()=="dad3350")
- {
- programparams[0].CL = onprograms[0].SawBlade.Trim();
- }
- else
- {
- string[] tempstrs = onprograms[0].SawBlade.Split('+');
- if(tempstrs.Length<2)
- {
- errorinfo = $"双刀机需要2个刀片信息,而OnsemiProgram表中指记录了一种:{onprograms[0].SawBlade.Trim()}";
- return null;
- }
- programparams[0].CL = tempstrs[0].Trim();
- programparams[1].CL = tempstrs[1].Trim();
- }
- //读取程序参数子参数
- condition = $" and a.preid in(select id from ProgramParamsDetail where preid={programid} and IsCompare=1)";
- List<PPSubDetail> ppsubdetails = CurrDb.FindListForCondition<PPSubDetail>(condition, ref errorinfo).ToList();
- if (ppsubdetails == null)
- return null;
- List<ParamsComDetail> deslist = MadeParamsComDetail(programparams, ppsubdetails, ref errorinfo);
- if (deslist == null)
- return null;
- if (deslist.Count <= 0)//没有设置比对参数
- return deslist;
- int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
- if (result <= 0)
- return null;
- result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
- if (result <= 0)
- return null;
- //遍历参数明细,判断每条的比对结果
- foreach (var item in deslist)
- {
- result = CompareParamsComDetail(item);
- }
- return deslist;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public int CompareParamsDetail(List<ParamsComDetail> deslist,Machine mac, int ftype, ref string errorinfo)
- {
- try
- {
- int sval = 1;
- int fval = 3;
- if(ftype== SecType.ECID)
- {
- sval = 2;
- fval = 13;
- }
- //构造参数结构
- List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == ftype).OrderBy(t => t.FNum).ToList();
- if (templist.Count <= 0)
- return 1;//没有要比较的参数,直接返回
- List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
- if (datas == null)
- return -1;
- OrderDetail order = ReadMachineOrderDetail(mac.ID, sval, fval, ref errorinfo);
- if (order == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- //将返回值填写到参数列表
- int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- if (result <= 0)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //固定值是字符串,以逗号隔开的
- private void CompareConst(ParamsComDetail entity)
- {
- //转换中文逗号
- string fval = entity.CL.Replace(',', ',');
- List<string> templist = fval.Split(',').ToList();
- List<string> flist = templist.Where(t => t == entity.FVal).ToList();
- if (flist.Count > 0)
- entity.IsPass = 1;
- else
- entity.IsPass = -1;
- }
- private int CompareParamsComDetail(ParamsComDetail entity)
- {
- //如果此参数无需比对,则直接通过
- if(entity.IsCompare<=0)
- {
- entity.IsPass = 1;
- return 1;
- }
- if (entity.pcsubdetails.Count<=0)
- {
- if (!string.IsNullOrEmpty(entity.CL))
- {
- //说明是比较固定值
- //if (entity.CL == entity.FVal)
- //{
- // entity.IsPass = 1;
- //}
- //else
- //{
- // entity.IsPass = -1;
- //}
- CompareConst(entity);
- return 1;
- }
- if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- entity.IsPass = 1;
-
- }
- else
- {
- foreach(var item in entity.pcsubdetails)
- {
- if(item.IsCompare<=0)
- {
- item.IsPass = 1;
- continue;
- }
- if(string.IsNullOrEmpty(item.FVal))
- {
- item.IsPass = -1;
- continue;
- }
- if (!string.IsNullOrEmpty(item.CL))
- {
- //说明是比较固定值
- //if (item.CL == item.FVal)
- //{
- // item.IsPass = 1;
- //}
- //else
- //{
- // item.IsPass = -1;
- //}
- CompareConst(entity);
- return 1;
- }
- if (decimal.Parse(item.FVal) < decimal.Parse(item.LCL))
- {
- item.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(item.FVal) > decimal.Parse(item.UCL))
- {
- item.IsPass = -1;
- return 1;
- }
- item.IsPass = 1;
- }
- List<PCSubDetail> tempsub = entity.pcsubdetails.Where(t => t.IsCompare >= 0 && t.IsPass <= 0).ToList();
- if(tempsub.Count>0)
- {
- entity.IsPass = -1;
- }
- else
- {
- entity.IsPass = 1;
- }
- }
- return 1;
- }
- /// <summary>
- /// 根据机台ID读取机台信息
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private Machine ReadMachine(string maccode,ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
- return null;
- }
- return macs[0];
- }
- /// <summary>
- /// 读取机台S1F3指令
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private OrderDetail ReadMachineOrderDetail(int macid,int sval,int fval, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.macid={macid}";
- List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
- return null;
- }
- condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
- List<OrderDetail> details= CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (details == null)
- return null;
- if(details.Count<=0)
- {
- errorinfo = "未找到您要的指令。";
- return null;
- }
- return details[0];
- }
- private OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval,string fname, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.macid={macid}";
- List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
- return null;
- }
- condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
- List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (details == null)
- return null;
- if (details.Count <= 0)
- {
- errorinfo = "未找到您要的指令。";
- return null;
- }
- return details[0];
- }
- /// <summary>
- /// 产生指令数据
- /// </summary>
- /// <param name="programparams"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> MadeOrderData(List<ParamsComDetail> programparams, ref string errorinfo)
- {
- List<OrderData> datas = new List<OrderData>();
- OrderData preid = new OrderData();
- preid.ID = 1;
- preid.ParentID = 0;
- preid.PreID = 0;
- preid.FCode = "L";
- preid.FLen = programparams.Count;
- datas.Add(preid);
- OrderData tempentity = null;
- int id = 2;
- int fnum = 10;
- foreach(var item in programparams)
- {
- tempentity = new OrderData();
- tempentity.ID = id;
- tempentity.FNum = fnum;
- tempentity.FCode = item.DCode;
- tempentity.FContent = item.FVal;
- tempentity.ParentID = preid.ID;
- datas.Add(tempentity);
- id++;
- fnum += 10;
- }
- return datas;
- }
- /// <summary>
- /// 将程序参数列表转换成比对明细列表
- /// </summary>
- /// <param name="programparams"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<ParamsComDetail> MadeParamsComDetail(List<ProgramParamsDetail> programparams,List<PPSubDetail> subdetails, ref string errorinfo)
- {
- List<ParamsComDetail> datas = new List<ParamsComDetail>();
- ParamsComDetail tempentity = null;
- List<PCSubDetail> pcsubdetails = null;
- List<PPSubDetail> ppsubdetails = null;
- PCSubDetail subentity = null;
- foreach (var item in programparams)
- {
- tempentity = new ParamsComDetail();
- tempentity.ID = 0;
- tempentity.FNum = item.FNum.Value;
- tempentity.SecID = item.SecID;
- tempentity.DCode = item.DCode;
- tempentity.FVal = item.FVal;
- tempentity.UCL = item.UCL;
- tempentity.CL = item.CL;
- tempentity.LCL = item.LCL;
- tempentity.SVType = item.SVType.Value;
- tempentity.Module = item.Module.Value;
- tempentity.CTLType = item.CTLType.Value;
- tempentity.SecFType = item.SecFType;
- tempentity.IsCompare = item.IsCompare;
- datas.Add(tempentity);
- pcsubdetails = new List<PCSubDetail>();
- tempentity.pcsubdetails = pcsubdetails;
- ppsubdetails = subdetails.Where(t => t.PreID == item.ID).ToList();
- foreach(var subitem in ppsubdetails)
- {
- subentity = new PCSubDetail();
- subentity.ID = 0;
- subentity.FNum = subitem.FNum;
- subentity.FName = subitem.ParameFName;
- subentity.UCL = subitem.UCL;
- subentity.CL = subitem.CL;
- subentity.LCL = subitem.LCL;
- subentity.IsCompare = subitem.IsCompare;
- pcsubdetails.Add(subentity);
- }
- }
- return datas;
- }
- /// <summary>
- /// 根据返回的参数列表值,更新比对结果值
- /// </summary>
- /// <param name="deslist"></param>
- /// <param name="orglist"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private int UpdateParamsComDetail(List<ParamsComDetail> deslist,List<OrderData> orglist, ref string errorinfo)
- {
- List<OrderData> ldata = orglist.Where(t => t.ParentID == 0).ToList();
- int preid = ldata[0].ID;
- ldata = orglist.Where(t => t.ParentID == preid).OrderBy(t=>t.FNum).ToList();
- if(deslist.Count!=ldata.Count)
- {
- errorinfo = "获取到的参数与实际参数个数不一致。";
- return -1;
- }
- List<PCSubDetail> pcsubdetails = null;
- List<OrderData> subdata = null;
- for(int i=0;i<deslist.Count;i++)
- {
- deslist[i].FVal = ldata[i].FContent;
- //更新子列表值
- pcsubdetails = deslist[i].pcsubdetails;
- subdata = orglist.Where(t => t.ParentID == ldata[i].ID).ToList();
- for(int j=0;j<pcsubdetails.Count;j++)
- {
- pcsubdetails[j].FVal = subdata.Count > j ? subdata[j].FContent : "";
- }
- }
- return 1;
- }
- /// <summary>
- /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="partcode"></param>
- /// <param name="pcode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst FindProgram(string maccode, string partcode, string pcode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return null;
- string condition = $" and b.ModelID={mac.MModeID} and b.ProcessCode='{pcode}'";
- List<ProgramRule> rules = CurrDb.FindListForCondition<ProgramRule>(condition, ref errorinfo).ToList();
- if (rules == null)
- return null;
- if (rules.Count<=0)
- {
- errorinfo = "未找到对应的程序。";
- return null;
- }
- //先精确匹配产品编号
- List<ProgramRule> templist = rules.Where(t => t.PartCode == partcode).ToList();
- if (templist.Count > 0)
- return CurrDb.FindEntityFor<ProgramMst>(templist[0].ProgramMstID);
- //取模糊匹配的规则
- rules = rules.Where(t => t.PartCode.Contains("%")).ToList();
- if(rules.Count<=0)
- {
- errorinfo = "未找到对应的程序。";
- return null;
- }
- //去掉规则里产品编号的“%”符号
- foreach(var item in rules)
- {
- item.PartCode = item.PartCode.Replace("%", "");
- }
- //排序规则列表,按产品编号长度从大到小排列
- rules = rules.OrderByDescending(t => t.PartCode.Length).ToList();
- foreach(var item in rules)
- {
- if (partcode.Contains(item.PartCode))
- {
- return CurrDb.FindEntityFor<ProgramMst>(item.ProgramMstID);
- }
- }
- errorinfo = "未找到对应的程序。";
- return null;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// DP机台装载程序指令数据
- /// </summary>
- /// <param name="programname">要装载的程序名称</param>
- /// <param name="preid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS2F41Data(string programname, int preid, ref string errorinfo)
- {
- string rcmd = "PP-SELECT";
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = rcmd;
- entity.FLen = rcmd.Length;
- ldata.Add(entity);
- entity = new OrderData();//添加L节点
- entity.ID = 3;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "L";
- entity.FLen = 1;
- ldata.Add(entity);
- entity = new OrderData();//添加L节点
- entity.ID = 4;
- entity.ParentID = 3;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "L";
- entity.FLen = 2;
- ldata.Add(entity);
- string cpname = "PPID";
- entity = new OrderData();
- entity.ID = 5;
- entity.ParentID = 4;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = cpname;
- entity.FLen = cpname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 6;
- entity.ParentID = 4;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 构造S7F3数据
- /// </summary>
- /// <param name="programname"></param>
- /// <param name="filestr"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS7F3Data(string programname,int filelen, string filepath,int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "B";
- entity.FContent = filepath;
- entity.OrgDatas = null;
- entity.FLen = filelen;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 构造S7F1数据
- /// </summary>
- /// <param name="programname"></param>
- /// <param name="filestr"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS7F1Data(string programname,int len, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "U4";
- entity.FLen = 1;
- entity.FContent = len.ToString();
- //entity.OrgDatas = filedatas;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="partcode"></param>
- /// <param name="pcode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst DownloadProgram(string maccode, string partcode, string pcode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return null;
- //查找程序文件
- ProgramMst mst = FindProgram(maccode, partcode, pcode, ref errorinfo);
- if(mst==null)
- {
- return null;
- }
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
- if (order == null)
- return null;
- string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
- filedir += "\\" + maccode;
- string filepath = filedir + "\\" + mst.FName;
- byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
- if (filedatas == null)
- {
- return null;
- }
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = GetS7F1Data(mst.FName, filedatas.Length, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return null;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- if(int.Parse(rec.Datalists[0].FContent)!=0)
- {
- errorinfo = "机台不接受程序。";
- return null;
- }
- order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
- datas = GetS7F3Data(mst.FName, filedatas.Length,filepath, order.PreID, ref errorinfo);
- rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- if (int.Parse(rec.Datalists[0].FContent) != 0)
- {
- errorinfo = "机台没有正确接受程序。";
- return null;
- }
- //发送程序装载命令,暂时还不能执行
- order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- datas = GetS2F41Data(mst.FName, order.PreID, ref errorinfo);
- rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- return null;
- }
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 构造获取机台程序名称的数据
- /// </summary>
- /// <param name="mmsec"></param>
- /// <param name="preid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetMacProgram(MMSecDetail mmsec, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 1;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = mmsec.DCode;
- entity.FContent = mmsec.FVal;
- entity.FLen = mmsec.FVal.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 根据机台编号,读取机台当前正在使用的程序名称
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public string GetMacProgram(string maccode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return "";
-
- OrderDetail order = ReadMachineOrderDetail(mac.ID,1, 3, ref errorinfo);
- if (order == null)
- return "";
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs == null)
- return "";
- if(mmsecs.Count<=0)
- {
- errorinfo = "未找到此机台对应的程序参数说明。";
- return "";
- }
- List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
- if (datas == null)
- return "";
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return "";
- return rec.Datalists[1].FContent;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- /// <summary>
- /// 根据机台查找机台使用的程序
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst GetMacProgram(Machine mac, ref string errorinfo)
- {
- try
- {
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
- if (order == null)
- return null;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs == null)
- return null;
- if (mmsecs.Count <= 0)
- {
- errorinfo = "未找到此机台对应的程序参数说明。";
- return null;
- }
- List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
- if (datas == null)
- return null;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- string programname= rec.Datalists[1].FContent;
- condition = $" and a.fname='{programname}'";
- List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- if (msts == null)
- return null;
- if (msts.Count <= 0)
- {
- errorinfo = $"未找到程序名称【{programname}】的记录。";
- return null;
- }
- return msts[0];
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public string GetMacProgramName(Machine mac, ref string errorinfo)
- {
- try
- {
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
- if (order == null)
- return null;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs == null)
- return null;
- if (mmsecs.Count <= 0)
- {
- errorinfo = "未找到此机台对应的程序参数说明。";
- return null;
- }
- List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
- if (datas == null)
- return null;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- string programname = rec.Datalists[1].FContent;
- return programname;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- /// <summary>
- /// 发送停机指令
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
-
- #region Web接口使用
- public IEnumerable<ParamsComMst> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var sql = new ParamsComMst().GetSelectSql();
- var pros = CurrDb.FindListForCondition<ParamsComMst>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
- // 加入参数信息
- if (pros != null && pros.Count() > 0)
- {
- for (var i = 0; i < pros.Count(); i++)
- {
- var detail = CurrDb.FindListForCondition<ParamsComDetail>($" and a.PreID='{pros[i].ID}'", ref errorinfo).ToList();
- // var detail = CurrDb.FindList<ParamsComDetail>(sql).ToList();
- if (detail == null)
- {
- detail = new List<ParamsComDetail>();
- }
- pros[i].CurrDetails = detail;
- }
- }
- return pros;
- }
- public int GetCount(string filter)
- {
- string errorinfo = string.Empty;
- var entities = CurrDb.FindListForCondition<ParamsComMst>(filter, ref errorinfo);
- if (entities != null)
- {
- return entities.Count();
- }
- return 0;
- }
- #endregion
- private List<FileParamsComDetail> MadeFileParamsComDetail(List<ProgramFileParams> programparams, ref string errorinfo)
- {
- List<FileParamsComDetail> datas = new List<FileParamsComDetail>();
- FileParamsComDetail tempentity = null;
- foreach (var item in programparams)
- {
- tempentity = new FileParamsComDetail();
- tempentity.ID = 0;
- tempentity.FName = item.FName;
- tempentity.FNum = item.FNum;
- tempentity.FVal = "";
- tempentity.UCL = item.UCL;
- tempentity.CL = item.CL;
- tempentity.LCL = item.LCL;
- datas.Add(tempentity);
- }
- return datas;
- }
- private int CompareFileParamsComDetail(FileParamsComDetail entity)
- {
- if (!string.IsNullOrEmpty(entity.CL))
- {
- //说明是比较固定值
- if (entity.CL == entity.FVal)
- {
- entity.IsPass = 1;
- }
- else
- {
- entity.IsPass = -1;
- }
- return 1;
- }
- if(string.IsNullOrEmpty(entity.FVal))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- entity.IsPass = 1;
- return 1;
- }
- public List<FileParamsComDetail> CompareFileParamsDetail(Machine mac, ProgramMst program,MacProgram macprogram, ref string errorinfo)
- {
- try
- {
- //读取程序参数
- string condition = $" and a.preid={program.ID} and a.IsCompare=1";
- List<ProgramFileParams> programparams = CurrDb.FindListForCondition<ProgramFileParams>(condition, ref errorinfo).ToList();
- if (programparams == null)
- return null;
- List<FileParamsComDetail> deslist = MadeFileParamsComDetail(programparams, ref errorinfo);
- if (deslist == null)
- return null;
- if (deslist.Count <= 0)//没有设置比对参数
- return deslist;
- //int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
- //if (result <= 0)
- // return null;
- //result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
- //if (result <= 0)
- // return null;
- //读取机台程序文件,并解析出参数值
- string rarpath = AppConfigurtaionServices.Configuration["rarpath"];
- string desdir = AppConfigurtaionServices.Configuration["DesDir"];
- string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
- Dad3350ProgramDal knsprogram = new Dad3350ProgramDal(CurrDb);
- List<FileParams> fileparams = knsprogram.GetProgramParams(rarpath, mac, program, desdir,programdir,macprogram, ref errorinfo);
- if (fileparams == null)
- return null;
- //遍历参数明细,判断每条的比对结果
- foreach (var item in deslist)
- {
- List<FileParams> tempfileparams = fileparams.Where(t => t.FName == item.FName).ToList();
- if (tempfileparams.Count > 0)
- item.FVal = tempfileparams[0].FVal;
- int result = CompareFileParamsComDetail(item);
- }
- return deslist;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 文件参数比对
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="programmst"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public FileParamsComMst CompareFileParams(Machine mac, ProgramMst programmst,MacProgram macprogram, ref string errorinfo)
- {
- try
- {
- //读取机台信息
- //Machine mac = ReadMachine(maccode, ref errorinfo);
- //if (mac == null)
- // return null;
- List<FileParamsComDetail> deslist = CompareFileParamsDetail(mac, programmst,macprogram, ref errorinfo);
- if (deslist == null)
- return null;
- FileParamsComMst mst = new FileParamsComMst();
- mst.CurrDetails = deslist;
- List<FileParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
- if (nopass.Count > 0)
- {
- mst.IsPass = -1;
- }
- else
- {
- mst.IsPass = 1;
- }
- mst.MacID = mac.ID;
- mst.ProgramID = programmst.ID;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public FileParamsComMst IUFileParamsComMst(FileParamsComMst mst, string usercode, ref string errorinfo)
- {
- try
- {
- int result = CurrDb.InsertFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- foreach (var item in mst.CurrDetails)
- {
- item.PreID = id;
- item.EntityStatusID = 1;
- item.ID = 0;
- }
- result = CurrDb.InsertFor<FileParamsComDetail>(mst.CurrDetails, usercode);
- if (result < 0)
- return null;
- mst = CurrDb.FindEntityFor<FileParamsComMst>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- }
- }
|