123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using Machine = DllEapEntity.Machine;
- using DllHsms;
- using System.Data;
- using Microsoft.Extensions.Logging;
- using log4net;
- using Cksoft.Data.Repository;
- namespace DllEapDal
- {
- public class MacAreaCountMstDal
- {
- private IDatabase CurrDb = null;
- private ILog loger = null;
- public MacAreaCountMstDal(IDatabase db)
- {
- CurrDb = db;
- loger = log4net.LogManager.GetLogger(typeof(MacAreaCountMstDal));
- }
- /// <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 MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string programname, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //读取机台正在进行的任务
- string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
- List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
- if (eventtypeid == -1)
- {
- if (msts.Count > 0)
- {
- mst = msts[0];
- //说明存在正在进行的任务,则要先结束此任务
- result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- mst.StatusID = 1;
- //完成片
- result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- }
- //添加主记录
- mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- //添加明细
- result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- }
- else
- {
- if (msts.Count <= 0)
- {
- //没有找到正在进行的记录,则添加记录,然后将记录设置为异常结束,并将产量统计全部标记为0
- //mst = AddMacCountMst(mac, ptime,programname, usercode, ref errorinfo);
- //if (mst == null)
- // return -1;
- //mst.StatusID = 2;
- ////添加明细
- //result = AddMacCountDetail(mst, 2, details, usercode, ref errorinfo);
- //if (result < 0)
- // return -1;
- return 1;
- }
- else
- {
- mst = msts[0];
- //判断开始时间与结束时间差,如果结束时间-开始时间小于1分钟,则认为这个结束事件是结束上条的,不做处理
- long diff = ptime.Ticks - mst.SDate.Ticks;
- if (diff < 10000000 * 90)
- return 1;
- mst.StatusID = 1;
- }
- //添加明细
- result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- //完成片
- result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int MacAreaCountMst(MacCountMst pmst, MacCountDetail lastDetail, ref string errorinfo)
- {
- try
- {
- // 因为抓到产量时不会立即写入数据库,写入数据库时已经延迟了2分钟左右,因此产量异常界限也顺延3分钟左右
- int OfflineTime = int.Parse(AppConfigurtaionServices.Configuration["OfflineTime"]) + 3;
- string usercode = pmst.RecCode;
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //读取机台正在进行的任务
- string condition = $" and a.StatusID=-1 and a.macid={pmst.MacID}";
- List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
- //处理 说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
- if (msts.Count > 1)
- {
- //说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
- msts = msts.OrderBy(t => t.SDate).ToList();
- for (int i = 1; i < msts.Count; i++)
- {
- msts[i].StatusID = 200;
- msts[i].Remark = "同时存在多个未结束的记录,则将这些多余的结束.";
- CurrDb.UpdateFor(msts[i], usercode);
- }
- }
- int vcount = pmst.FCount;
- int sameresult = 0;
- DateTime ptime = pmst.SDate;
- int statusid = pmst.StatusID;
- var newRecipeCount = 0;
- if (msts.Count > 0)
- {
- mst = msts[0];
- //var lastMst = CurrDb.FindListForCondition<MacCountMst>($" and a.macid={pmst.ID} order by id desc limit 0,1",
- // ref errorinfo).FirstOrDefault();
- if (vcount < 0)
- {
- //说明是断网了,这时候需要判断此条记录的耗时,如果耗时大于设定时间,则设置数量为0
- // int timelen = int.Parse(AppConfigurtaionServices.Configuration["OfflineTime"]);
- DateTime etime = mst.SDate.AddMinutes(OfflineTime);
- if (DateTime.Now > etime)
- {
- vcount = 0;
- }
- else
- {
- return 1;
- }
- }
- // 当开始片的程序和结束片的程序不一致时,将该片的产量置为0
- if (!string.IsNullOrEmpty(pmst.PName) && mst.PName != pmst.PName)
- {
- newRecipeCount = pmst.FCount;
- vcount = 0;
- }
- //判断数量是否相同,如果相同则无需处理
- //sameresult = JudgeIsSame(mst, vcount, ptime, ref errorinfo);
- //if (sameresult > 0)
- // return 1;
- //说明存在正在进行的任务,则要先结束此任务
- result = AddMacCountDetail(mst, 1, vcount.ToString(), statusid, usercode, ref errorinfo, newRecipeCount);
- if (result < 0)
- return -1;
- //mst.StatusID = 1;
- //mst.EDate = ptime;
- //CurrDb.UpdateFor<MacCountMst>(mst, usercode);
- //完成片
- result = FinishMacCountMst(mst, ptime, pmst.FCount, pmst.StatusID, usercode, ref errorinfo, newRecipeCount);
- if (result <= 0)
- {
- return -1;
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo, newRecipeCount);
- if (result <= 0)
- return -1;
- }
- else
- {
- if (vcount < 0)
- {
- vcount = 0;
- }
- }
- //添加主记录
- mst = AddMacCountMst(pmst.MacID, ptime, pmst.PName, pmst.FCount, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- //添加明细
- result = AddMacCountDetail(mst, -1, vcount.ToString(), statusid, usercode, ref errorinfo, newRecipeCount);
- if (result < 0)
- return -1;
- //处理一段时间内的断线数据
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private void ProcessError(int macid)
- {
- try
- {
- }
- catch (Exception ex)
- {
- loger.Error($"处理机台断线记录发生异常,机台ID={macid},异常信息为:{ex.ToString()}");
- }
- }
- public int MacAreaCountMstFor(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string programname, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //读取机台正在进行的任务
- string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
- List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
- if (msts.Count > 0 && msts[0].PName != programname)
- {
- //说明此机台已经更换了程序,结束上个任务,开始新的任务
- msts[0].StatusID = 1;
- CurrDb.UpdateFor<MacCountMst>(msts[0], usercode);
- }
- if (eventtypeid == -1)
- {
- if (msts.Count > 0)
- {
- mst = msts[0];
- //说明存在正在进行的任务,则要先结束此任务
- result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- mst.StatusID = 1;
- //完成片
- result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- }
- //添加主记录
- mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- //添加明细
- result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- }
- else
- {
- if (msts.Count <= 0)
- {
- //没有找到正在进行的记录,则添加记录,然后将记录设置为异常结束,并将产量统计全部标记为0
- mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- mst.StatusID = 2;
- //添加明细
- result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- return 1;
- }
- else
- {
- mst = msts[0];
- //判断开始时间与结束时间差,如果结束时间-开始时间小于1分钟,则认为这个结束事件是结束上条的,不做处理
- long diff = ptime.Ticks - mst.SDate.Ticks;
- if (diff < 10000000 * 90)
- return 1;
- mst.StatusID = 1;
- }
- //添加明细
- result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- //完成片
- result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int MacAreaCountMstFor(Machine mac, DateTime ptime, string vcount, string programname, int statusid, string usercode, ref string errorinfo)
- {
- try
- {
- if (mac.FCode == "DA00004")
- {
- int aa = 100;
- }
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //读取机台正在进行的任务
- string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
- List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
- int sameresult = 0;
- if (msts.Count > 0)
- {
- if (msts.Count > 1)
- {
- //说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
- msts = msts.OrderBy(t => t.SDate).ToList();
- for (int i = 1; i < msts.Count; i++)
- {
- msts[i].StatusID = 2;
- CurrDb.UpdateFor(msts[i], usercode);
- }
- }
- mst = msts[0];
- if (int.Parse(vcount) < 0)
- {
- //说明是断网了,这时候需要判断此条记录的耗时,如果耗时大于设定时间,则设置数量为0
- int timelen = int.Parse(AppConfigurtaionServices.Configuration["TimeLen"]);
- DateTime etime = mst.SDate.AddMinutes(timelen);
- if (DateTime.Now > etime)
- {
- vcount = "0";
- }
- else
- {
- return 1;
- }
- }
- //判断数量是否相同,如果相同则无需处理
- sameresult = JudgeIsSame(mst, int.Parse(vcount), ptime, ref errorinfo);
- if (sameresult > 0)
- return 1;
- //说明存在正在进行的任务,则要先结束此任务
- result = AddMacCountDetail(mst, 1, vcount, statusid, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- //mst.StatusID = 1;
- //mst.EDate = ptime;
- //CurrDb.UpdateFor<MacCountMst>(mst, usercode);
- //完成片
- result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- //统计产量记录
- result = TotalMacCountMst(mst, usercode, ref errorinfo);
- if (result <= 0)
- return -1;
- }
- else
- {
- if (int.Parse(vcount) < 0)
- {
- vcount = "0";
- }
- //condition = $@" and a.id=(
- // SELECT max(a.id) FROM maccountdetail as a
- // inner join maccountmst as b on a.MstID = b.id
- // where b.macid = {mac.ID} and a.TypeID != 0)";
- //MacCountDetail lastdetail = CurrDb.FindListForCondition<MacCountDetail>(condition, ref errorinfo).FirstOrDefault();
- //if (lastdetail != null && lastdetail.FCount == int.Parse(vcount))
- // sameresult = -100;
- }
- //添加主记录
- mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
- if (mst == null)
- return -1;
- //添加明细
- result = AddMacCountDetail(mst, -1, vcount, statusid, usercode, ref errorinfo);
- if (result < 0)
- return -1;
- //
- //if (sameresult == -100)
- // return 100;//说明产量相同
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private int JudgeIsSame(MacCountMst mst, int vcount, DateTime ptime, ref string errorinfo)
- {
- DateTime etime = DateTime.Now;
- MacCountDetail detail = CurrDb.FindListForCondition<MacCountDetail>($" and a.mstid={mst.ID}", ref errorinfo).FirstOrDefault();
- //超过3分钟则统计
- int timelen = int.Parse(AppConfigurtaionServices.Configuration["TimeLen"]);
- etime = mst.SDate.AddMinutes(timelen);
- //if (ptime > etime&& detail.FCount == vcount)
- // return -100;
- if (ptime > etime)
- return -1;
- //定义班次结束时间
- string currdate = mst.SDate.ToString("yyyy-MM-dd");
- if (mst.SDate < DateTime.Parse($"{currdate} 08:30:00"))
- etime = DateTime.Parse($"{currdate} 08:30:00");
- else if (mst.SDate < DateTime.Parse($"{currdate} 20:30:00"))
- etime = DateTime.Parse($"{currdate} 20:30:00");
- else
- etime = DateTime.Parse($"{currdate} 08:30:00").AddDays(1);
- if (ptime > etime)
- return -1;
- if (detail.FCount == vcount)
- return 1;
- return -1;
- }
- public int FinishMacCountMst(MacCountMst mst, DateTime ptime, int vcount, int statusid, string usercode,
- ref string errorinfo, int newRecipeCount = 0)
- {
- try
- {
- mst.EDate = ptime;
- // 当机台换了机种时 将新机种信息传入方法
- if (newRecipeCount == 0)
- {
- mst.ECount = vcount;
- mst.FCount = mst.ECount - mst.SCount;
- }
- else
- {
- mst.ECount = vcount;
- mst.FCount = 0;
- }
- //计算时间
- TimeSpan ts1 = new TimeSpan(mst.SDate.Ticks);
- TimeSpan ts2 = new TimeSpan(mst.EDate.Ticks);
- TimeSpan ts = ts1.Subtract(ts2).Duration();
- mst.StatusID = statusid;
- mst.TimeLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
- CurrDb.UpdateFor<MacCountMst>(mst, usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int FinishMacCountMst(MacCountMst mst, Machine mac
- , DateTime ptime, string usercode, ref string errorinfo)
- {
- try
- {
- mst.EDate = ptime;
- //从MES里获取MES标识
- //string mesid = GetInProductionJobbook(mac.FCode, ref errorinfo);
- //mst.MesID = mesid;
- //if (!string.IsNullOrEmpty(errorinfo))
- //{
- // mst.Remark = errorinfo;
- // errorinfo = "";
- //}
- //计算时间
- TimeSpan ts1 = new TimeSpan(mst.SDate.Ticks);
- TimeSpan ts2 = new TimeSpan(mst.EDate.Ticks);
- TimeSpan ts = ts1.Subtract(ts2).Duration();
- mst.StatusID = 1;
- mst.TimeLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
- CurrDb.UpdateFor<MacCountMst>(mst, usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int AddMacCountDetail(MacCountMst mst, int eventtypeid, string vcount, int statusid, string usercode, ref string errorinfo,
- int newRecipeCount = 0)
- {
- try
- {
- MacCountDetail entity = new MacCountDetail();
- entity.MstID = mst.ID;
- entity.ParamCode = StandardCode.SVID_FinishUnit;
- if (newRecipeCount == 0)
- {
- entity.FCount = int.Parse(vcount);
- }
- else
- {
- entity.FCount = newRecipeCount;
- }
- entity.TypeID = eventtypeid;
- entity.StatusID = statusid;
- CurrDb.InsertFor<MacCountDetail>(entity, usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int AddMacCountDetail(MacCountMst mst, int eventtypeid, List<McaSecVDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- List<MacCountDetail> templist = new List<MacCountDetail>();
- foreach (var item in details)
- {
- MacCountDetail entity = new MacCountDetail();
- entity.MstID = mst.ID;
- entity.ParamCode = item.FCode;
- entity.FCount = int.Parse(item.FVal);
- entity.TypeID = eventtypeid;
- templist.Add(entity);
- }
- CurrDb.InsertFor<MacCountDetail>(templist, usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public MacCountMst AddMacCountMst(int macid, DateTime ptime, string programname, int vcount, string usercode, ref string errorinfo)
- {
- try
- {
- if (string.IsNullOrEmpty(programname))
- {
- programname = GetPreProgramName(macid);
- }
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //添加主记录
- mst.MacID = macid;
- mst.SDate = ptime;
- mst.StatusID = -1;
- mst.TimeLen = 0;
- mst.PName = programname;
- mst.SCount = vcount;
- mst.ECount = 0;
- mst.FCount = 0;
- CurrDb.InsertFor<MacCountMst>(mst, usercode);
- int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo);
- if (mstid <= 0)
- return null;
- mst = CurrDb.FindEntityFor<MacCountMst>(mstid);
- if (mst == null)
- return null;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.ToString();
- return null;
- }
- }
- public MacCountMst AddMacCountMst(Machine mac, DateTime ptime, string programname, string usercode, ref string errorinfo)
- {
- try
- {
- if (string.IsNullOrEmpty(programname))
- {
- programname = GetPreProgramName(mac);
- }
- int result = 0;
- MacCountMst mst = new MacCountMst();
- //添加主记录
- mst.MacID = mac.ID;
- mst.SDate = ptime;
- mst.StatusID = -1;
- mst.TimeLen = 0;
- mst.PName = programname;
- CurrDb.InsertFor<MacCountMst>(mst, usercode);
- int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo);
- if (mstid <= 0)
- return null;
- mst = CurrDb.FindEntityFor<MacCountMst>(mstid);
- if (mst == null)
- return null;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.ToString();
- return null;
- }
- }
- private string GetPreProgramName(int macid)
- {
- string sqlstr = $@"SELECT a.PName FROM eap.maccountmst as a
- where a.MacID = {macid} and a.PName is not null
- order by sdate desc
- limit 1";
- object pname = CurrDb.FindObject(sqlstr);
- return pname == null ? "" : pname.ToString();
- }
- private string GetPreProgramName(Machine mac)
- {
- string sqlstr = $@"SELECT a.PName FROM eap.maccountmst as a
- where a.MacID = {mac.ID} and a.PName is not null
- order by sdate desc
- limit 1";
- object pname = CurrDb.FindObject(sqlstr);
- return pname == null ? "" : pname.ToString();
- }
- //统计单条产量
- public int TotalMacCountMst(MacCountMst mst, string usercode, ref string errorinfo,
- int newRecipeCount = 0)
- {
- try
- {
- //删除现有统计记录
- string sqlstr = $"delete from maccountdetail where mstid={mst.ID} and typeid=0";
- CurrDb.ExecuteBySql(sqlstr);
- int result = 0;
- sqlstr = $@"SELECT a.ParamCode,sum(a.TypeID*a.FCount) fcount FROM maccountdetail a
- where a.MstID = {mst.ID} and a.TypeID <> 0
- group by a.ParamCode";
- List<MacCountDetail> details = CurrDb.FindList<MacCountDetail>(sqlstr).ToList();
- foreach (var item in details)
- {
- item.MstID = mst.ID;
- item.TypeID = 0;
- item.EntityStatusID = 1;
- if (item.FCount < 0)
- {
- item.FCount = 0;
- item.StatusID = MacStatusVal.Error;
- }
- else
- {
- result = AccountMacCountDetail(mst, item, ref errorinfo, newRecipeCount);
- if (result <= 0)
- return -1;
- }
- }
- CurrDb.InsertFor<MacCountDetail>(details, usercode);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //计算产量
- private int AccountMacCountDetail(MacCountMst mst, MacCountDetail detail, ref string errorinfo,
- int newRecipeCount = 0)
- {
- try
- {
- if (newRecipeCount > 0)
- {
- detail.FCount = 0;
- return 1;
- }
- //读取非0的产量
- string condition = $" and a.MstID={mst.ID} and a.TypeID <> 0";
- List<MacCountDetail> details = CurrDb.FindListForCondition<MacCountDetail>(condition, ref errorinfo).ToList();
- if (details.Where(t => t.StatusID == 1).Count() > 0)
- {
- //说明存在断网情况,这时则把产量置为0
- detail.FCount = 0;
- detail.StatusID = MacStatusVal.Disconn;
- return 1;
- }
- //如果是烧机模式,则产量=0
- if (details.Where(t => t.StatusID == MacStatusVal.Setup).Count() > 0)
- {
- //说明存在断网情况,这时则把产量置为0
- detail.FCount = 0;
- detail.StatusID = MacStatusVal.Setup;
- return 1;
- }
- if (details.Where(t => t.FCount <= 0).Count() > 0)
- {
- //说明存在小于0的记录,这样的记录标记为不正常状态
- detail.StatusID = MacStatusVal.Pause;
- }
- else
- {
- //虽然开始和结束都有数据,但数据没变,则认为是闲置状态
- if (detail.FCount <= 0)
- {
- detail.StatusID = MacStatusVal.Idle;
- return 1;
- }
- //说明开始与结束都有数据,将这样的记录标记为运行状态
- detail.StatusID = MacStatusVal.Run;
- return 1;
- }
- if (detail.FCount <= 0)
- return 1;
- //读取最近的10条正常的产量做参考
- //string sqlstr = $@"select sum(t.TimeLen) TimeLen,sum(t.FCount) FCount from (
- // SELECT b.TimeLen,a.FCount FROM eap.maccountdetail as a
- // inner join maccountmst as b on a.MstID=b.id
- // where a.TypeID=0 and a.StatusID={MacStatusVal.Run} and b.MacID={mst.MacID} and b.PName='{mst.PName}'
- // order by b.SDate desc
- // limit 10) as t";
- string sqlstr = $@"SELECT b.TimeLen,a.FCount FROM eap.maccountdetail as a
- inner join maccountmst as b on a.MstID=b.id
- where a.TypeID=0 and a.StatusID={MacStatusVal.Run} and b.MacID={mst.MacID} and b.PName='{mst.PName}'
- order by a.FCount desc
- limit 10";
- DataTable tempdt = CurrDb.FindTable(sqlstr);
- if (tempdt.Rows.Count <= 0)
- return 1;
- if (tempdt.Rows[0]["TimeLen"].ToString() == "" || tempdt.Rows[0]["FCount"].ToString() == "")
- {
- if (detail.FCount > 200)
- {
- detail.FCount = 0;
- detail.StatusID = MacStatusVal.Error;
- }
- return 1;
- }
- int timelen = int.Parse(tempdt.Rows[0]["TimeLen"].ToString());
- int FCount = int.Parse(tempdt.Rows[0]["FCount"].ToString());
- int factcount = FCount * mst.TimeLen / timelen;
- if (detail.FCount > factcount * 2)
- {
- detail.FCount = 0;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.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 string GetInProductionJobbook(string macCode, ref string errorinfo)
- {
- IDatabase sqldb = null;
- try
- {
- sqldb = DbFactory.Base("sqlconn");
- string sql = $"select Id from JobBooking where MacCode='{macCode}' and statusid=1 and IsRight<>-1";
- var entity = sqldb.FindList<string>(sql).FirstOrDefault();
- if (string.IsNullOrEmpty(entity))
- {
- errorinfo = "该机台没有正在进行的报工";
- return string.Empty;
- }
- return entity;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message;
- return string.Empty;
- }
- finally
- {
- if (sqldb != null)
- {
- sqldb.Close();
- }
- }
- }
- }
- }
|