using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; namespace DllEapDal { public class OrderMstDal { private IDatabase CurrDb = null; public OrderMstDal(IDatabase db) { CurrDb = db; } public OrderMst IUOrderMst(OrderMst mst,string usercode, ref string errorinfo) { try { int result = 0; int id = mst.ID; string sqlstr = ""; if (mst.EntityStatusID==1) { mst.RecCode = usercode; mst.RecTime = DateTime.Now; mst.ModCode = usercode; mst.ModTime = DateTime.Now; //新增时,则需要自动产生编号,如果是新增版次则不需要 if (mst.FCode == "") { sqlstr= "SELECT max(fcode)+1 FROM OrderMst"; object obj = CurrDb.FindObject(sqlstr.ToString()); mst.FCode = obj.ToString().PadLeft(6, '0'); } else { //存在编号,说明是新增版次或者是将指令授予机台 sqlstr = $"SELECT max(FVersion)+1 FROM OrderMst where fcode='{mst.FCode}'"; object obj = CurrDb.FindObject(sqlstr.ToString()); mst.FVersion = obj.ToString() == "" ? 0 : int.Parse(obj.ToString()); } result = CurrDb.InsertFor(mst, usercode); if(result<0) { return null; } object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } id = int.Parse(objid.ToString()); } else { mst.ModCode = usercode; mst.ModTime = DateTime.Now; result = CurrDb.UpdateFor(mst, usercode); if (result < 0) { return null; } } mst = CurrDb.FindEntityFor(id); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } //新增、修改、删除指令数据信息OrderData private int IUOrderData(List datas, string usercode, ref string errorinfo) { try { string sqlstr = ""; int result = 0; //先删除 List temprows = datas.Where(t => t.EntityStatusID == -1).ToList(); foreach (var temprow in temprows) { sqlstr=$"delete from OrderData where id={temprow.ID}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; } //修改,可以批量 temprows = datas.Where(t => t.EntityStatusID == 2).ToList(); foreach(var temprow in temprows) { temprow.ModCode = usercode; temprow.ModTime = DateTime.Now; } result = CurrDb.UpdateFor(temprows, usercode); if (result < 0) { return -1; } temprows = datas.Where(t => t.EntityStatusID == 1).ToList(); foreach (var temprow in temprows) { if (temprow.ID > 0) continue; result = IUOrderDataforpre(temprow, datas,usercode, ref errorinfo); if (result < 0) return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } private OrderData FindPreRow(int id, List oabdt) { List rows = oabdt.Where(t => t.ID == id).ToList();// oabdt.Select("id=" + id.ToString()); if (rows.Count <= 0) return null; return rows[0]; } //新增上级行 private int IUOrderDataforpre(OrderData row, List oabdt,string usercode, ref string errorinfo) { try { //读取行上级ID int parentid = row.ParentID; if (parentid < 0)//说明上级ID小于0,则先处理 { int tempid = IUOrderDataforpre(FindPreRow(parentid, oabdt), oabdt,usercode, ref errorinfo); if (tempid < 0) return -1; //将上级ID全部更新 List ttrows = oabdt.Where(t => t.ParentID == parentid).ToList();// oabdt.Select("上级ID=" + preid.ToString()); foreach (var ttrow in ttrows) { ttrow.ParentID = tempid; } } row.RecCode = usercode; row.ModCode = usercode; row.RecTime = DateTime.Now; row.ModTime = DateTime.Now; int result = CurrDb.InsertFor(row, usercode); if (result < 0) { return -1; } object id = CurrDb.FindObject("select @@IDENTITY"); if (id.ToString() == "") { return -1; } result = int.Parse(id.ToString()); List rows = oabdt.Where(t => t.ParentID == row.ID).ToList();// oabdt.Select("上级ID=" + row["ID"].ToString()); foreach (var temprow in rows) { temprow.ParentID = result; } row.ID = result; return result; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //新增、修改、删除指令信息OrderDetail private int IUOrderDetail(List details, List datas,List secs, int preid,string usercode, ref string errorinfo) { try { string sqlstr =""; int result = 0; //删除OrderData List temprows = datas.Where(t => t.EntityStatusID == -1).ToList(); foreach (var temprow in temprows) { sqlstr=$"delete from OrderData where id={temprow.ID}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; } //删除OrderDetail List tempdetails = details.Where(t => t.EntityStatusID == -1).ToList(); foreach (var temprow in tempdetails) { sqlstr=$"delete from OrderDetail where id={temprow.ID}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; } //修改,可以批量 tempdetails = details.Where(t => t.EntityStatusID == 2).ToList(); foreach(var temprow in tempdetails) { temprow.ModCode = usercode; temprow.ModTime = DateTime.Now; temprow.FCode = $"S{temprow.SVal}F{temprow.FVal}"; } result = CurrDb.UpdateFor(tempdetails, usercode); if (result < 0) { return -1; } //修改,可以批量 tempdetails = details.Where(t => t.EntityStatusID == 1).ToList(); foreach (var temprow in tempdetails) { temprow.PreID = preid; temprow.RecCode = usercode; temprow.ModCode = usercode; temprow.RecTime = DateTime.Now; temprow.ModTime = DateTime.Now; temprow.FCode = $"S{temprow.SVal}F{temprow.FVal}"; result = CurrDb.InsertFor(temprow, usercode); if (result < 0) { return -1; } object id = CurrDb.FindObject("select @@IDENTITY"); if (id.ToString() == "") { return -1; } result = int.Parse(id.ToString()); //更新oab表里的主档ID var ttrows = datas.Where(t => t.PreID == temprow.ID).ToList(); foreach (var ttrow in ttrows) { ttrow.PreID = result; } var secsrows = secs.Where(t => t.PreID == temprow.ID).ToList(); foreach (var secrow in secsrows) secrow.PreID = result; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public OrderMst IUOrderMst(OrderMst mst,List details,List datas,List secs,string usercode, ref string errorinfo) { try { mst = IUOrderMst(mst,usercode, ref errorinfo); if (mst==null) { return null; } //添加指令明细 int result = IUOrderDetail(details, datas,secs, mst.ID,usercode, ref errorinfo); if (result < 0) { return null; } result = IUOrderData(datas,usercode, ref errorinfo); if (result < 0) { return null; } result = IUOrderDetailSec(secs, usercode, ref errorinfo); if (result < 0) { return null; } return mst; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } //新增、修改、删除指令数据信息OrderDetailSec private int IUOrderDetailSec(List secs, string usercode, ref string errorinfo) { try { int result = 0; //先删除 List temprows = secs.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(temprows); if (result < 0) return -1; //修改,可以批量 temprows = secs.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(temprows, usercode); if (result < 0) { return -1; } //新增 temprows = secs.Where(t => t.EntityStatusID == 1).ToList(); result = CurrDb.InsertFor(temprows, usercode); if (result < 0) { return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } private static void ClearAnswer(OrderRelation row) { row.AnswerID = 0; row.AnswerSVal = 0; row.AnswerFVal = 0; row.EntityStatusID = 2; } private OrderRelation AddOac(List dt, int omaid,string usercode) { int? minid = dt.Where(t => t.ID < 0).Min(t => (int?)t.ID); int id =minid==null?-1:minid.Value-1; int? maxfnum = dt.Max(t => (int?)t.FNum); int fnum = maxfnum==null?10:maxfnum.Value+10; OrderRelation entity = new OrderRelation(); dt.Add(entity); entity.ID = id; entity.PreID = omaid; entity.FNum = fnum; entity.SendID = 0; entity.AnswerID = 0; entity.RecCode = usercode; entity.ModCode = usercode; entity.RecTime = DateTime.Now; entity.ModTime = DateTime.Now; //addrow["sendsval"] = 0; //addrow["sendfval"] = 0; //addrow["answersval"] = 0; //addrow["answerfval"] = 0; //addrow["remark"] = ""; return entity; } //自动匹配回复指令 public int AutoProcessOac(int omaid,string usercode, ref string errorinfo) { try { string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderRelation.PreID))}={omaid}"; List oacdt = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (oacdt == null) return -1; condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderDetail.PreID))}={omaid}"; List oaadt = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (oaadt == null) return -1; List rows = null; //遍历oac,删除多余关联 foreach (OrderRelation temprow in oacdt) { //if (temprow["id"].ToString() == "7") //{ // int aa = 100; //} rows = oaadt.Where(t => t.ID == temprow.SendID).ToList();// oaadt.Select("id=" + temprow["sendid"].ToString()); if (rows.Count <= 0) { //说明在指令集合里没有找到发送指令,需要删除 temprow.EntityStatusID = -1; continue; } //判断发送指令F值是否为奇数,如果不是奇数也要删除 if (temprow.SendFVal % 2 == 0) { temprow.EntityStatusID = -1; continue; } //判断指令是否匹配 if (temprow.SendSVal != temprow.AnswerSVal) { ClearAnswer(temprow); continue; } //判断指令是否匹配 if (temprow.SendFVal+ 1 != temprow.AnswerFVal) { ClearAnswer(temprow); continue; } rows = oaadt.Where(t => t.ID == temprow.AnswerID).ToList();// oaadt.Select("id=" + temprow["answerid"].ToString()); if (rows.Count <= 0) { //说明在指令集合里没有找到发送指令,需要将answerid=0 ClearAnswer(temprow); continue; } //判断发送指令F值是否为奇数,如果不是奇数也要删除 if (temprow.AnswerFVal % 2 != 0) { ClearAnswer(temprow); } } List rels = null; OrderRelation addrow = null; //遍历oaa,把奇数指令添加到oac中 foreach (var temprow in oaadt) { rels = oacdt.Where(t => t.SendID == temprow.ID).ToList();// oacdt.Select("sendid=" + temprow["id"].ToString()); if (rels.Count > 0)//说明已经存在,无需添加 { continue; } //将奇数指令添加到oac中 if (temprow.FVal % 2 == 0) continue; addrow = AddOac(oacdt, omaid,usercode); addrow.SendID = temprow.ID; //addrow["sendid"] = temprow["ID"]; //addrow["sendsval"] = temprow["sval"]; //addrow["sendfval"] = temprow["fval"]; // } //遍历oaa,将没有关联回复指令的记录关联回复指令 rels = oacdt.Where(t => t.AnswerID == 0&&t.EntityStatusID>=0).ToList(); foreach (var temprow in rels) { rows = oaadt.Where(t => t.SVal == temprow.SendSVal && t.FVal == (temprow.SendFVal + 1)).ToList(); if (rows.Count <= 0)//说明没有找到回复指令 { continue; } temprow.AnswerID = rows[0].ID; //temprow["answerid"] = rows[0]["ID"]; //temprow["sendsval"] = rows[0]["sval"]; //temprow["sendfval"] = rows[0]["fval"]; temprow.SendSVal = rows[0].SVal; temprow.SendFVal = rows[0].FVal; if (temprow.EntityStatusID != 1) temprow.EntityStatusID = 2; } //oacdt.Columns.Remove("sendsval"); //oacdt.Columns.Remove("sendfval"); //oacdt.Columns.Remove("answersval"); //oacdt.Columns.Remove("answerfval"); int result = 0; string sqlstr = ""; //先删除 rels = oacdt.Where(t => t.EntityStatusID == -1).ToList();// oacdt.Select("", "", DataViewRowState.Deleted); foreach (var temprow in rels) { sqlstr=$"delete from OrderRelation where id={temprow.ID}"; result = CurrDb.ExecuteBySql(sqlstr); if (result < 0) return -1; } //修改,可以批量 rels = oacdt.Where(t => t.EntityStatusID ==2).ToList(); foreach(var item in rels) { item.ModTime = DateTime.Now; item.ModCode = usercode; } result = CurrDb.UpdateFor(rels, usercode); if (result < 0) { return -1; } rels = oacdt.Where(t => t.EntityStatusID == 1).ToList(); result = CurrDb.InsertFor(rels, usercode); if (result < 0) { return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //新增版次 public int AddVersionOrder(OrderMst mst,string usercode, ref string errorinfo) { try { OrderMst result = CopyOrder(mst, 2,usercode, ref errorinfo); if (result ==null) { return -1; } return result.ID; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //复制指令版次 public int CopyOrder(OrderMst mst, string usercode, ref string errorinfo) { try { OrderMst result = CopyOrder(mst, 1, usercode, ref errorinfo); if (result == null) { return -1; } return result.ID; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //复制指令版次到其他机型 public int CopyOrderToModel(OrderMst mst,MacModel model, string usercode, ref string errorinfo) { try { OrderMst result = CopyOrder(mst, 1, usercode, ref errorinfo); if (result == null) { return -1; } //处理指令事件与参数 int tempresult = HandleOrderSec(result, model, usercode, ref errorinfo); if (tempresult <= 0) return -1; return result.ID; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } /// /// 更换指令机型,更换指令对应的事件和参数 /// /// /// /// private int HandleOrderSec(OrderMst mst,MacModel model,string usercode,ref string errorinfo) { try { mst.MModeID = model.ID; int result = CurrDb.UpdateFor(mst, usercode); if (result < 0) return -1; //读取机型所有参数事件 string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(MMSecDetail.PreID))}={model.ID}"; List mmsecdetails= CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (mmsecdetails == null) return -1; //读取指令所有明细的参数 condition = $" and c.PreID={mst.ID}"; List orderdetailsec = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (orderdetailsec == null) return -1; List templist = null; foreach(var item in orderdetailsec) { templist = mmsecdetails.Where(t => t.SecID == item.SecID).ToList(); if(templist.Count<=0) { //说明新机型中没有此参数,要删除此参数 item.DeleteEntity(orderdetailsec); } else { item.MMSecDetailID = templist[0].ID; item.ModifyEntity(); } } List secs = orderdetailsec.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(secs); if (result < 0) return -1; secs = orderdetailsec.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(secs,usercode); if (result < 0) return -1; //读取指令事件 condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderEvent.PreID))}={mst.ID}"; List events = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); foreach(var item in events) { templist = mmsecdetails.Where(t => t.SecID == item.SecID).ToList(); if (templist.Count <= 0) { //说明新机型中没有此参数,要删除此参数 condition = $" and a.PreID={item.ID}"; result = CurrDb.DeleteForCondition(condition); if (result < 0) return -1; item.DeleteEntity(events); } else { item.MMSecDetailID = templist[0].ID; item.ModifyEntity(); //判断事件报工的参数在机型里是否存在,如果不存在则不添加事件 condition = $" and {EntityAttribute.GetPropertyCondition(nameof(EventReportDetail.PreID))}={item.ID}"; List eventreports = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); foreach(var reportmst in eventreports) { //读取报告明细,并判断每个明细参数是否在该机型中,如果不在,则删除事件报工 condition = $" and {EntityAttribute.GetPropertyCondition(nameof(ReportDetail.PreID))}={reportmst.ID}"; List reportdetails= CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); foreach(var reportdetail in reportdetails) { templist = mmsecdetails.Where(t => t.SecID == reportdetail.SecID).ToList(); if(templist.Count<=0) { reportmst.DeleteEntity(eventreports); break; } } } //删除事件报告明细 List tempeventreportdetails = eventreports.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(tempeventreportdetails); if (result < 0) return -1; //判断该事件里是否还包含报告,如果不包含,则事件也要删除 tempeventreportdetails = eventreports.Where(t => t.EntityStatusID >=0).ToList(); if (tempeventreportdetails.Count <= 0) item.DeleteEntity(events); } } List orderevents = events.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(orderevents); if (result < 0) return -1; orderevents = events.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(orderevents, usercode); if (result < 0) return -1; return 1; } catch(Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //复制oac,根据指令版次主档 //public int CopyOac(int orgomaid, int desomaid, ref string errorinfo) //{ // try // { // //删除目的指令版次对应的oac记录 // string sqlstr=$"delete FROM OrderRelation where preid={desomaid}"); // int result = CurrDb.ExecuteBySql(sqlstr.ToString()); // if (result < 0) // return -1; // //读取目的指令的oac记录 // sqlstr.Clear(); // sqlstr.AppendFormat("select a.ID,a.omaid,a.fnum,a.sendid,a.answerid, a.remark from oac a "); // sqlstr.AppendFormat("where a.omaid ={0}", desomaid); // sqlstr = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderRelation.PreID))}={}"; // DataTable desoacdt = db.FindTableFor(sqlstr.ToString(), "oac"); // if (desoacdt == null) // return -1; // //读取原来的oac记录 // sqlstr.Clear(); // sqlstr.AppendFormat("select a.ID,a.omaid,a.fnum,a.sendid,b.oa30 sendfnum,a.answerid,c.oa30 answerfnum, a.remark from oac a "); // sqlstr.AppendFormat(" left outer join oaa b on a.sendid=b.id"); // sqlstr.AppendFormat(" left outer join oaa c on a.answerid=c.id"); // sqlstr.AppendFormat(" where a.omaid ={0}", orgomaid); // DataTable oacdt = db.FindTableFor(sqlstr.ToString(), "oac"); // if (oacdt == null) // return -1; // //读取目的指令版次的oaa // sqlstr.Clear(); // sqlstr.AppendFormat("select a.ID,a.oa00 主档ID,a.oa30 序号 from oaa a "); // sqlstr.AppendFormat("where a.oa00 ={0}", desomaid); // DataTable oaadt = db.FindTableFor(sqlstr.ToString(), "oaa"); // if (oaadt == null) // return -1; // DataRow[] rows = null; // int sendid = 0; // int answerid = 0; // DataRow addrow = null; // foreach (DataRow temprow in oacdt.Rows) // { // if (temprow["sendfnum"].ToString() == "") // temprow["sendfnum"] = 0; // rows = oaadt.Select("序号=" + temprow["sendfnum"].ToString()); // if (rows.Length <= 0) // continue; // sendid = int.Parse(rows[0]["id"].ToString()); // rows = oaadt.Select("序号=" + temprow["answerfnum"].ToString()); // if (rows.Length <= 0) // continue; // //sqlstr.AppendFormat("select a.ID,a.omaid,a.fnum,a.sendid,a.answerid, a.remark from oac a "); // answerid = int.Parse(rows[0]["id"].ToString()); // addrow = desoacdt.NewRow(); // desoacdt.Rows.Add(addrow); // addrow["omaid"] = desomaid; // addrow["fnum"] = temprow["fnum"]; // addrow["sendid"] = sendid; // addrow["answerid"] = answerid; // addrow["remark"] = temprow["remark"]; // } // result = db.BatInsert(desoacdt, ref errorinfo); // if (result < 0) // return -1; // return 1; // } // catch (Exception ex) // { // errorinfo = ex.Message.ToString(); // return -1; // } //} //复制整个指令文件 public OrderMst CopyOrder(OrderMst mst, int mode,string usercode, ref string errorinfo) { string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderDetail.PreID))}={mst.ID}"; List oaadt = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderData.PreID))} in(select id from OrderDetail where preid={mst.ID})"; List oabdt = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderDetailSec.PreID))} in(select id from OrderDetail where preid={mst.ID})"; List orgsecs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); OrderMst tempmst = new OrderMst(); EntityHelper.CopyPropertyValue(mst, tempmst); tempmst.ID = -1; tempmst.EntityStatusID = 1; List desoaadt = new List(); List desoabdt = new List(); List dessec = new List(); List orgoabdt =null; int result = 0; foreach (var temprow in oaadt) { orgoabdt = oabdt.Where(t => t.PreID == temprow.ID).ToList(); result = EapUnity.AddOrderForEntity(temprow, orgoabdt, orgsecs, desoaadt, desoabdt,dessec, -1); if (result < 0) { return null; } } if (mode == 1) { //复制指令,需要将代码清空 tempmst.FCode = ""; tempmst.FVersion = 1; } tempmst = IUOrderMst(tempmst, desoaadt,desoabdt, dessec, usercode, ref errorinfo); if (tempmst == null) { return null; } //复制oac result = AutoProcessOac(tempmst.ID, usercode, ref errorinfo); if (result < 0) return null; //复制指令事件 result = CopyOrderEvent(mst, tempmst,usercode); if (result < 0) return null; //复制指令参数明细 result = CopyOrderSecDetail(mst, tempmst, usercode); if (result < 0) return null; //复制指令状态明细 result = CopyOrderStatus(mst, tempmst, usercode); if (result < 0) return null; return tempmst; } /// /// 把一组指令复制到指定的指令版次中去 /// /// /// /// /// /// public int CopyOrderDetail(List details,OrderMst desmst, string usercode, ref string errorinfo) { string sqlstr = $"select max({nameof(OrderDetail.FNum)}) from OrderDetail where preid={desmst.ID}"; Object obj = CurrDb.FindObject(sqlstr); int maxfnum = obj.ToString() == "" ? 10 : int.Parse(obj.ToString()); string condition = ""; foreach (var item in details) { maxfnum += 10; OrderDetail addrow = new OrderDetail(); EntityHelper.CopyPropertyValue(item, addrow); addrow.ID = 0; addrow.PreID = desmst.ID; addrow.FNum = maxfnum; addrow.EntityStatusID = 1; OrderDetailDal tempdal = new OrderDetailDal(CurrDb); addrow = tempdal.IUOrderDetail(addrow, usercode, ref errorinfo); if (addrow == null) return -1; //读取orderdata condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderData.PreID))}={item.ID}"; List oabdt = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); List desorderdata = new List(); GetOrderDatas(oabdt, addrow.ID, 0, 0, desorderdata); int result=IUOrderData(desorderdata, usercode, ref errorinfo); if (result <= 0) return -1; } return 1; } private void GetOrderDatas(List orgdatas,int preid,int parentid,int newparentid,List desdatas) { List templist = orgdatas.Where(t => t.ParentID == parentid).ToList(); foreach(var item in templist) { int minid = -1; if(desdatas.Count>0) { minid = desdatas.Min(t => t.ID); minid--; } OrderData addrow = new OrderData(); EntityHelper.CopyPropertyValue(item, addrow); addrow.PreID = preid; addrow.ID = minid; addrow.ParentID = newparentid; addrow.EntityStatusID = 1; desdatas.Add(addrow); GetOrderDatas(orgdatas, preid, item.ID, minid, desdatas); } } public int CopyOrderStatus(OrderMst orgmst, OrderMst desmst, string usercode) { string errorinfo = ""; string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderStatus.PreID))}={orgmst.ID}"; List orgevents = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); List desevents = new List(); OrderStatus addrow = null; int id = 0; foreach (var item in orgevents) { id--; addrow = new OrderStatus(); EntityHelper.CopyPropertyValue(item, addrow); addrow.PreID = desmst.ID; addrow.ID = id; addrow.AddEntity(desevents); } int result = CurrDb.InsertFor(desevents, usercode); if (result <= 0) return -1; return 1; } public int CopyOrderSecDetail(OrderMst orgmst, OrderMst desmst, string usercode) { //string errorinfo = ""; //string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderSecDetail.PreID))}={orgmst.ID}"; //List orgevents = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //List desevents = new List(); //OrderSecDetail addrow = null; //int id = 0; //foreach (var item in orgevents) //{ // id--; // addrow = new OrderSecDetail(); // EntityHelper.CopyPropertyValue(item, addrow); // addrow.PreID = desmst.ID; // addrow.ID = id; // addrow.AddEntity(desevents); //} //int result = CurrDb.InsertFor(desevents, usercode); //if (result <= 0) // return -1; return 1; } /// /// 复制指令事件 /// /// /// /// public int CopyOrderEvent(OrderMst orgmst,OrderMst desmst,string usercode) { string errorinfo = ""; string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderEvent.PreID))}={orgmst.ID}"; List orgevents = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); condition = $" and {EntityAttribute.GetPropertyCondition(nameof(EventReportDetail.PreID))} in" + $"(select id from {nameof(OrderEvent)} where PreID={orgmst.ID})"; List< EventReportDetail> orgdetails = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); List desevents = new List(); List desdetails = new List(); OrderEvent addrow = null; EventReportDetail detailaddrow = null; List tempdetails = null; int id = 0; foreach (var item in orgevents) { id--; addrow = new OrderEvent(); EntityHelper.CopyPropertyValue(item, addrow); addrow.PreID = desmst.ID; addrow.ID = id; addrow.AddEntity(desevents); tempdetails = orgdetails.Where(t => t.PreID == item.ID).ToList(); foreach(var ttrow in tempdetails) { detailaddrow = new EventReportDetail(); EntityHelper.CopyPropertyValue(ttrow, detailaddrow); detailaddrow.PreID = id; detailaddrow.ID = 0; detailaddrow.AddEntity(desdetails); } } int result = IUOrderEvent(desevents, desdetails, usercode, ref errorinfo); if (result <= 0) return -1; return 1; } public int DelOrderMst(List msts,ref string errorinfo) { foreach(var item in msts) { int result = DelOrderMst(item.ID,ref errorinfo); if (result <= 0) return -1; } return 1; } private int DelOrderMst(int mstid,ref string errorinfo) { List macorders = CurrDb.FindListForCondition($" and a.preid={mstid}", ref errorinfo).ToList(); if(macorders.Count>0) { errorinfo = "该指令已经关联机台,不能删除。"; return -1; } string sqlstr = $"delete from OrderData where preid in(select id from orderdetail where preid={mstid})"; int result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderDetailSec where preid in(select id from orderdetail where preid={mstid})"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from EventReportDetail where preid in(select id from OrderEvent where preid={mstid})"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderEvent where preid={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderSecDetail where preid={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderStatus where preid={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from orderdetail where preid={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderRelation where preid={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; sqlstr = $"delete from OrderMst where id={mstid}"; result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; return 1; } //检查该机台是否已经存在该指令类别的关联记录,如果存在则不能关联,返回值0未关联,>0已关联且值为关联ID,小于0发生错误 public int JudgeRelation(Machine datarow, ref string errorinfo) { try { string colname = EntityAttribute.GetPropertyCondition(nameof(MacOrder.MacID)); string sqlstr=$" and {colname}={datarow.ID}"; List dt = CurrDb.FindListForCondition(sqlstr, ref errorinfo).ToList();// db.FindTableFor(sqlstr.ToString(), "oma"); if (dt == null) { errorinfo = "函数JudgeRelation发生未知错误"; return -1; } if (dt.Count > 0) return dt[0].ID; return 0; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //指令管理到机台 public int OrderRelationMca(OrderMst orderrow, List datarows, string usercode, ref string errorinfo) { try { int result = 0; foreach (var temprow in datarows) { //检查该机台是否已经存在该指令类别的关联记录,如果存在则不能关联 int judgeid = JudgeRelation(temprow, ref errorinfo); if (judgeid != 0) { return -1; } MacOrder tempentity = new MacOrder(); tempentity.PreID = orderrow.ID; tempentity.MacID = temprow.ID; result = CurrDb.InsertFor(tempentity, usercode); if (result < 0) return -1; //OrderMst tempmst = CopyOrder(orderrow,3,usercode, ref errorinfo); //if (tempmst==null) //{ // return -1; //} ////修改机台ID和机台指令类别ID //tempmst.MacID = temprow.ID; //result = CurrDb.UpdateFor(tempmst, usercode); //if (result < 0) // return -1; } return result; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int DelMacOrder(List msts) { int result = CurrDb.DeleteForEntity(msts); if (result <= 0) return -1; return 1; } public int IUOrderEvent(List orderevents,List details, string usercode, ref string errorinfo) { try { int result = 0; //删除 List temprows = orderevents.Where(t => t.EntityStatusID == -1).ToList(); foreach(OrderEvent item in temprows) { result = DelOrderEvent(item,ref errorinfo); if (result <= 0) return -1; } //新增 temprows = orderevents.Where(t => t.EntityStatusID == 1).ToList(); foreach (OrderEvent item in temprows) { result = AddOrderEvent(item,usercode, ref errorinfo); if (result <= 0) return -1; //更新明细主档ID List ttrows = details.Where(t => t.PreID == item.ID).ToList(); foreach (var ttrow in ttrows) ttrow.PreID = result; } //更新 temprows = orderevents.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(temprows,usercode); if (result < 0) return -1; //处理明细 result = IUEventReportDetail(details, usercode, ref errorinfo); if (result < 0) return -1; return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } private int AddOrderEvent(OrderEvent orderevent,string usercode, ref string errorinfo) { try { int result = CurrDb.InsertFor(orderevent, usercode); if (result < 0) return -1; object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return -1; } result = int.Parse(objid.ToString()); return result; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } private int DelOrderEvent(OrderEvent orderevent, ref string errorinfo) { try { string sqlstr = $"delete from {nameof(EventReportDetail)} where preid={orderevent.ID}"; int result = CurrDb.ExecuteBySql(sqlstr); if (result < 0) return -1; result = CurrDb.DeleteForEntity(orderevent); if (result < 0) return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } // private int IUEventReportDetail(List details, string usercode, ref string errorinfo) { try { int result = 0; //删除 List temprows = details.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(temprows); if (result < 0) return -1; temprows = details.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(temprows,usercode); if (result < 0) return -1; temprows = details.Where(t => t.EntityStatusID == 1).ToList(); result = CurrDb.InsertFor(temprows,usercode); if (result < 0) return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int IUOrderStatus(List orderevents, string usercode, ref string errorinfo) { try { int result = 0; //删除 List temprows = orderevents.Where(t => t.EntityStatusID == -1).ToList(); result = CurrDb.DeleteForEntity(temprows); if (result < 0) return -1; //新增 temprows = orderevents.Where(t => t.EntityStatusID == 1).ToList(); result = CurrDb.InsertFor(temprows,usercode); if (result < 0) return -1; //更新 temprows = orderevents.Where(t => t.EntityStatusID == 2).ToList(); result = CurrDb.UpdateFor(temprows,usercode); if (result < 0) return -1; return 1; } catch (Exception e) { errorinfo = e.Message; return -1; } } //private OrderSecDetail AddOrderSecDetail(List orglist,List standardsec,int preid,int secid) //{ // OrderSecDetail sec = new OrderSecDetail(); // sec.PreID = preid; // sec.SecID = secid; // List temprows = standardsec.Where(t => t.ID == secid).ToList(); // sec.DCode = temprows[0].DCode; // int? minid = orglist.Where(t => t.ID < 0).Min(t => (int?)t.ID); // sec.ID = minid == null ? -1 : minid.Value - 1; // sec.AddEntity(orglist); // return sec; //} //计算指令版次参数 public int AccountOrderSecDetail(OrderMst orderrow, string usercode, ref string errorinfo) { try { //string condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderSecDetail.PreID))}={orderrow.ID}"; //List secdetails = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //List standardsec = CurrDb.FindListForCondition("", ref errorinfo).ToList(); ////读取事件明细 //condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderEvent.PreID))}={orderrow.ID}"; //List events = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //List tempsecs = null; //OrderSecDetail addrow = null; //foreach (var item in events) //{ // tempsecs = secdetails.Where(t => t.SecID == item.SecID).ToList(); // if(tempsecs.Count>0) // { // //说明事件已经存在,无需新增,将状态修改为2 // tempsecs[0].ModifyEntity(); // continue; // } // addrow = AddOrderSecDetail(secdetails, standardsec, orderrow.ID, item.SecID); //} ////读取指令对应的报告参数 //condition = $" and {EntityAttribute.GetPropertyCondition(nameof(ReportDetail.PreID))} in" + // $"(SELECT a.ReportMstID FROM EventReportDetail a where a.PreID in(select id from OrderEvent where preid = {orderrow.ID}))"; //List reports = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //foreach (var item in reports) //{ // tempsecs = secdetails.Where(t => t.SecID == item.SecID).ToList(); // if (tempsecs.Count > 0) // { // //说明事件已经存在,无需新增,将状态修改为2 // tempsecs[0].ModifyEntity(); // continue; // } // addrow = AddOrderSecDetail(secdetails, standardsec, orderrow.ID, item.SecID); //} ////读取指令对应的参数 //condition = $" and {EntityAttribute.GetPropertyCondition(nameof(OrderDetailSec.PreID))} in" + // $"(select id from OrderDetail where preid = {orderrow.ID})"; //List detailsec = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //foreach (var item in detailsec) //{ // tempsecs = secdetails.Where(t => t.SecID == item.SecID).ToList(); // if (tempsecs.Count > 0) // { // //说明事件已经存在,无需新增,将状态修改为2 // tempsecs[0].ModifyEntity(); // continue; // } // addrow = AddOrderSecDetail(secdetails, standardsec, orderrow.ID, item.SecID); //} ////删除多余的参数事件 //tempsecs = secdetails.Where(t => t.EntityStatusID==0).ToList(); //int result = CurrDb.DeleteForEntity(tempsecs); //if (result < 0) // return -1; ////新增事件 //tempsecs = secdetails.Where(t => t.EntityStatusID == 1).ToList(); //result = CurrDb.InsertFor(tempsecs,usercode); //if (result < 0) // return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //public int UOrderSecDetail(List orderevents, string usercode, ref string errorinfo) //{ // try // { // int result = 0; // result = CurrDb.UpdateFor(orderevents,usercode); // if (result < 0) // return -1; // return 1; // } // catch (Exception e) // { // errorinfo = e.Message; // return -1; // } //} } }