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)); } /// /// /// /// /// /// /// /// /// /// public int MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List 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 msts = CurrDb.FindListForCondition(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 msts = CurrDb.FindListForCondition(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($" 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(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 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 msts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (msts.Count > 0 && msts[0].PName != programname) { //说明此机台已经更换了程序,结束上个任务,开始新的任务 msts[0].StatusID = 1; CurrDb.UpdateFor(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 msts = CurrDb.FindListForCondition(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(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(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($" 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(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(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(entity, usercode); return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int AddMacCountDetail(MacCountMst mst, int eventtypeid, List details, string usercode, ref string errorinfo) { try { List templist = new List(); 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(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(mst, usercode); int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo); if (mstid <= 0) return null; mst = CurrDb.FindEntityFor(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(mst, usercode); int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo); if (mstid <= 0) return null; mst = CurrDb.FindEntityFor(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 details = CurrDb.FindList(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(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 details = CurrDb.FindListForCondition(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 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 templist = CurrDb.FindListForCondition($" 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 areadetails, List details, string usercode, ref string errorinfo) { try { List templist = new List(); List 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(templist, usercode); } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int AddMacAreaCountDetail(MacAreaCountMst mst, List 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(entity, usercode); object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); entity = CurrDb.FindEntityFor(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(mst, usercode); object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); mst = CurrDb.FindEntityFor(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(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(); } } } } }