123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using DllHsmsWeb;
- using DllHsms;
- namespace DllEapDal
- {
- public class KnsMacAreaCountMstDal
- {
- private IDatabase CurrDb = null;
- public KnsMacAreaCountMstDal(IDatabase db)
- {
- CurrDb = db;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="eventtypeid"></param>
- /// <param name="ptime"></param>
- /// <param name="details"></param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int MacAreaCountMst01(Machine mac, int eventtypeid,DateTime ptime, List<McaSecVDetail> details,string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- //查找该机台是否存在进行中的任务
- string condition = $" and a.MacID={mac.ID} and a.StatusID=1";
- List<MacAreaCountMst> msts = CurrDb.FindListForCondition<MacAreaCountMst>(condition, ref errorinfo).ToList();
- MacAreaCountMst mst = null;
- if (eventtypeid==-1&&msts.Count>0)
- {
- mst = msts[0];
- //当前是开始事件,且当前机台存在进行中的任务,这种情况属于异常,直接将进行中的任务结束
- //将开始作为当前进行中得结束数据
- List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
- result = AddMacCount02(mac.ID, 1, ptime, templist, details, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- //汇总明细数量
- result = AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
- if (result <= 0)
- return -1;
- //更新主记录状态
- mst.StatusID = -1;
- mst.CompleteTime = ptime;
- CurrDb.UpdateFor<MacAreaCountMst>(mst, usercode);
- //根据列表添加新的任务
- mst = AddMacAreaCountMst(eventtypeid, mac,1,1, ptime, details, usercode, ref errorinfo);
- if (mst==null)
- return -1;
- }
- if (eventtypeid == -1 && msts.Count <= 0)
- {
- //当前是开始事件,且当前机台不存在进行中的任务,这种情况属于正常,直接添加记录
- //根据列表添加新的任务
- mst = AddMacAreaCountMst(eventtypeid, mac, 1, 1, ptime, details, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- }
- if (eventtypeid == 1 && msts.Count > 0)
- {
- mst = msts[0];
- //结束且存在进行中的,属于正常情况
- //读取明细
- List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
- //添加原始数据记录
- result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
- if (result<=0)
- return -1;
- //汇总明细数量
- result=AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
- if (result <= 0)
- return -1;
- //更新主记录状态
- mst.StatusID = -1;
- mst.CompleteTime = ptime;
- CurrDb.UpdateFor<MacAreaCountMst>(mst, usercode);
- }
- if (eventtypeid == 1 && msts.Count <= 0)
- {
- //结束且不存在进行中的,属于不正常情况
- //读取明细
- //mst = AddMacAreaCountMst(eventtypeid, mac, -1, -1, ptime, details, usercode, ref errorinfo);
- //if (mst == null)
- // return -1;
- //忽略,直接返回
- return 1;
- }
- return 1;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- if(eventtypeid==-1)
- {
- //每条开始时间,则添加每条记录
- }
- List<MacCount02> templist = new List<MacCount02>();
- foreach (var item in details)
- {
- MacCount02 entity = new MacCount02();
- entity.PreID = 0;
- entity.ParamCode = item.FCode;
- entity.EventTypeID = eventtypeid;
- entity.MacID = mac.ID;
- entity.FCount = int.Parse(item.FVal);
- entity.FDate = ptime;
- entity.RecTypeID = 1;
- templist.Add(entity);
- }
- return CurrDb.InsertFor<MacCount02>(templist, usercode);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int AccountMacAreaCountMst(int mstid, ref string errorinfo)
- {
- try
- {
- string sqlstr = $@"update macareacountdetail
- set FCount = (SELECT sum(EventTypeID * FCount) FROM MacCount02 where preid = macareacountdetail.id and ParamCode = macareacountdetail.ParamCode)
- where preid = {mstid} and id> 0";
- CurrDb.ExecuteBySql(sqlstr);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public MacAreaCountMst AddMacAreaCountMst(int eventtypeid,Machine mac,int statusid,int isrightid,DateTime ptime, List<McaSecVDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- //添加主档
- MacAreaCountMst mst = AddMacAreaCountMst(mac.ID,statusid,isrightid, ptime, usercode, ref errorinfo);
- if (mst == null)
- return null;
- //添加明细
- int result = AddMacAreaCountDetail(mst, details, usercode, ref errorinfo);
- if (result < 0)
- return null;
- //读取明细
- List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={mst.ID}", ref errorinfo).ToList();
- //添加原始数据记录
- result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
- if (result < 0)
- return null;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public int AddMacCount02(int macid,int eventtypeid,DateTime ptime,List<MacAreaCountDetail> areadetails, List<McaSecVDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- List<MacCount02> templist = new List<MacCount02>();
- List<MacAreaCountDetail> areatemp = null;
- MacAreaCountDetail tempentity = null;
- foreach (var item in details)
- {
- areatemp = areadetails.Where(t => t.ParamCode == item.FCode).ToList();
- if(areatemp.Count<=0)
- {
- tempentity = AddMacAreaCountDetail(areadetails[0].PreID, item.FCode, usercode, ref errorinfo);
- if (tempentity == null)
- return -1;
- }
- else
- {
- tempentity = areatemp[0];
- }
- MacCount02 entity = new MacCount02();
- entity.PreID = tempentity.ID;
- entity.ParamCode = item.FCode;
- entity.EventTypeID = eventtypeid;
- entity.MacID = macid;
- entity.FCount = int.Parse(item.FVal);
- entity.FDate = ptime;
- entity.RecTypeID = 1;
- templist.Add(entity);
- }
- return CurrDb.InsertFor<MacCount02>(templist, usercode);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int AddMacAreaCountDetail(MacAreaCountMst mst, List<McaSecVDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- MacAreaCountDetail tempentity = null;
- foreach (var item in details)
- {
- tempentity = AddMacAreaCountDetail(mst.ID, item.FCode, usercode, ref errorinfo);
- if (tempentity == null)
- return -1;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public MacAreaCountDetail AddMacAreaCountDetail(int preid,string paramcode, string usercode, ref string errorinfo)
- {
- try
- {
- MacAreaCountDetail entity = new MacAreaCountDetail();
- entity.PreID = preid;
- entity.ParamCode = paramcode;
- entity.FCount = 0;
- CurrDb.InsertFor<MacAreaCountDetail>(entity, usercode);
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- entity = CurrDb.FindEntityFor<MacAreaCountDetail>(id);
- return entity;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public MacAreaCountMst AddMacAreaCountMst(int macid,int statusid,int isrightid, DateTime ptime, string usercode, ref string errorinfo)
- {
- try
- {
- MacAreaCountMst mst = new MacAreaCountMst();
- mst.MacID = macid;
- mst.StartTime = ptime;
- mst.StatusID = statusid;
- mst.IsRightID = isrightid;
- CurrDb.InsertFor<MacAreaCountMst>(mst, usercode);
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- mst = CurrDb.FindEntityFor<MacAreaCountMst>(id);
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public KnsMacCount AreaProcessStart(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- //查找该机台是否存在进行中的任务
- string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
- List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
- KnsMacCount entity = new KnsMacCount();
- entity.StartTime = ptime;
- entity.ProcessStatusID = -1;
- entity.YieldStatusID = -1;
- entity.MacID = mac.ID;
- entity.RecTypeID = 1;
- CurrDb.InsertFor<KnsMacCount>(entity, usercode);
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- KnsMacCount resultentity = CurrDb.FindEntityFor<KnsMacCount>(id);
- if (msts.Count > 0)
- {
- result= AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
- if (result <= 0)
- return null;
- }
- return resultentity;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 每条完成事件函数
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="ptime"></param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int AreaProcessComplete(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- //查找该机台是否存在进行中的任务
- string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
- List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
- if(msts.Count>0)
- {
- return AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private int AreaProcessComplete(KnsMacCount knsarea, DateTime ptime, string usercode, ref string errorinfo)
- {
- try
- {
- knsarea.EndTime = ptime;
- knsarea.ProcessStatusID = 1;
- CurrDb.UpdateFor<KnsMacCount>(knsarea, usercode);
- //发送读取产量指令
- int result = SendS2F15DataSetTime(knsarea, ref errorinfo);
- if (result < 0)
- return -1;
- result= SendS2F41(knsarea, ref errorinfo);
- if (result !=0&&result!=4)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
-
- private int SendS2F15DataSetTime(KnsMacCount maccount, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=15";
- List<OrderDetail> orders = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (orders.Count <= 0)
- {
- errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F15指令。";
- return -1;
- }
- List<OrderData> datas = GetS2F15DataSetTime(maccount.StartTime,maccount.EndTime, orders[0].ID, ref errorinfo);
- if (datas == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
- if (rec == null)
- return -1;
- return int.Parse(rec.Datalists[0].FContent);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private List<OrderData> GetS2F41Data(int preid, ref string errorinfo)
- {
- string rcmd = "CALC_PM_DATA";
- 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);
- return ldata;
- }
- private int SendS2F41(KnsMacCount maccount, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=41";
- List<OrderDetail> orders = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (orders.Count <= 0)
- {
- errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F41指令。";
- return -1;
- }
- List<OrderData> datas = GetS2F41Data(orders[0].ID, ref errorinfo);
- if (datas == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
- if (rec == null)
- return -1;
- return int.Parse(rec.Datalists[1].FContent);
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //
- private static List<OrderData> GetS2F15DataSetTime(DateTime start,DateTime end, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = GetOrderData(1, preid, 0, "L", 3, "",10, ref errorinfo);
- ldata.Add(lentity);
- OrderData l1 = GetOrderData(2, preid, 1, "L", 2, "",20, ref errorinfo);
- ldata.Add(l1);
- OrderData entity = GetOrderData(3, preid, 2, "U2", 1, "1500",30, ref errorinfo);
- ldata.Add(entity);
- entity = GetOrderData(4, preid, 2, "U1", 1, "0",40, ref errorinfo);
- ldata.Add(entity);
- OrderData l2 = GetOrderData(5, preid, 1, "L", 2, "",50, ref errorinfo);
- ldata.Add(l2);
- entity = GetOrderData(6, preid, 5, "U2", 1, "1501",60, ref errorinfo);
- ldata.Add(entity);
- entity = GetOrderData(7, preid, 5, "A", 14, start.ToString("yyyyMMddHHmmss"),70, ref errorinfo);
- ldata.Add(entity);
- OrderData l3 = GetOrderData(8, preid, 1, "L", 2, "",80, ref errorinfo);
- ldata.Add(l3);
- entity = GetOrderData(9, preid, 8, "U2", 1, "1502",90, ref errorinfo);
- ldata.Add(entity);
- entity = GetOrderData(10, preid, 8, "A", 14, end.ToString("yyyyMMddHHmmss"),100, ref errorinfo);
- ldata.Add(entity);
- return ldata;
- }
- private static OrderData GetOrderData(int id, int preid,int parentid,string fcode,int flen,string fcontent,int fnum, ref string errorinfo)
- {
- OrderData lentity = new OrderData();
- lentity.ID = id;
- lentity.ParentID = parentid;
- lentity.PreID = preid;
- lentity.FCode = fcode;
- lentity.FLen = flen;
- lentity.FContent = fcontent;
- lentity.FNum = fnum;
- return lentity;
- }
- public int KnsYield(Machine mac,List<OrderData> lists, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- //查找该机台是否存在进行中的任务
- string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=1 and a.YieldStatusID=-1";
- List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
- if (msts.Count <= 0)
- {
- return 100;
- }
- List<ReportDetail> CurrReportDetail = CurrDb.FindListForCondition<ReportDetail>("", ref errorinfo).ToList();
- if (CurrReportDetail == null)
- {
- errorinfo = $"读取数据失败,错误信息:{errorinfo}";
- return -1;
- }
- List<McaSecVDetail> tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo);
- if (tempdt == null)
- {
- errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo;
- return -1;
- }
- foreach(var item in tempdt)
- {
- KnsMacCountDetail detail = new KnsMacCountDetail();
- detail.MstID = msts[0].ID;
- detail.ParamCode = item.FCode;
- detail.FCount = int.Parse(item.FVal);
- CurrDb.InsertFor<KnsMacCountDetail>(detail, usercode);
- }
- msts[0].YieldStatusID = 1;
- CurrDb.UpdateFor<KnsMacCount>(msts[0], usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SendStopOrder(string maccode,ref string errorinfo)
- {
- try
- {
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if(macs.Count<=0)
- {
- errorinfo = $"未找到机台【{maccode}】信息。";
- return -1;
- }
- condition = $" and a.MacID={macs[0].ID}";
- List<MacOrder> macorders = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if(macorders.Count<=0)
- {
- errorinfo = $"未找到机台【{maccode}】对应的指令信息。";
- return -1;
- }
- int orderid = macorders[0].PreID;
- condition = $" and a.PreID={orderid} and a.FName='S2F41stop'";
- List<OrderDetail> orderdetails = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- return 1;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
-
- }
- }
|