123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DllStatusShowDal
- {
- public class LayoutMstDal
- {
- private IDatabase CurrDb = null;
- public LayoutMstDal(IDatabase db)
- {
- CurrDb = db;
- }
- public LayoutMst IULayoutMst(LayoutMst mst, string usercode, ref string errorinfo)
- {
- try
- {
- int result = 0;
- int id = mst.ID;
- if (mst.EntityStatusID == 1)
- {
- //mst.reccode = usercode;
- //mst.RecTime = DateTime.Now;
- //mst.ModCode = usercode;
- //mst.ModTime = DateTime.Now;
- string fcode = GetLayoutMstCode(ref errorinfo);
- if (fcode == "")
- return null;
- mst.FCode = fcode;
- 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<LayoutMst>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private string GetLayoutMstCode(ref string errorinfo)
- {
- try
- {
- StringBuilder sqlstr = new StringBuilder(100);
- string code = "Y";
- sqlstr.AppendFormat("SELECT max(fcode) FROM LayoutMst where fcode like '{0}%'", code);
- object obj = CurrDb.FindObject(sqlstr.ToString());
- if (obj.ToString() == "")
- return code + "00001";
- int fnum = int.Parse(obj.ToString().Substring(1, 5));
- fnum++;
- return code + fnum.ToString().PadLeft(5, '0');
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- public LayoutMst IULayoutMst(LayoutMst mst,List<LayoutDetail> details, string usercode, ref string errorinfo)
- {
- try
- {
- LayoutMst result = IULayoutMst(mst, usercode, ref errorinfo);
- if (result == null)
- return null;
- //处理明细
- List<LayoutDetail> templist = details.Where(t => t.EntityStatusID == 1).ToList();
- foreach (var item in templist)
- item.PreID = result.ID;
- int count = CurrDb.InsertFor<LayoutDetail>(templist, usercode);
- if (count < 0)
- return null;
- templist = details.Where(t => t.EntityStatusID == 2).ToList();
- count = CurrDb.UpdateFor<LayoutDetail>(templist, usercode);
- if (count < 0)
- return null;
- templist = details.Where(t => t.EntityStatusID == -1).ToList();
- count = CurrDb.DeleteForEntity<LayoutDetail>(templist);
- if (count < 0)
- return null;
- return result;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- //private string GetTLcdCode(ref string errorinfo)
- //{
- // try
- // {
- // StringBuilder sqlstr = new StringBuilder(100);
- // string code = "L";
- // sqlstr.AppendFormat("SELECT max(fcode) FROM TLcd where fcode like '{0}%'", code);
- // object obj = CurrDb.FindObject(sqlstr.ToString());
- // if (obj.ToString() == "")
- // return code + "00001";
- // int fnum = int.Parse(obj.ToString().Substring(1, 5));
- // fnum++;
- // return code + fnum.ToString().PadLeft(5, '0');
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return "";
- // }
- //}
- public LayoutMst DelLayoutMst(List<LayoutMst> mst, string usercode, ref string errorinfo)
- {
- try
- {
- foreach(var item in mst)
- {
- int result = DelLayoutMst(item, usercode, ref errorinfo);
- if (result < 0)
- return null;
- }
- return mst[0];
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private int DelLayoutMst(LayoutMst mst, string usercode, ref string errorinfo)
- {
- try
- {
- string condition = $" and {EntityAttribute.GetPropertyCondition<LayoutDetail>(nameof(LayoutDetail.PreID))}={mst.ID}";
- int result = CurrDb.DeleteForCondition<LayoutDetail>(condition);
- if (result < 0)
- return -1;
- result = CurrDb.DeleteFor<LayoutMst>(mst.ID);
- if (result < 0)
- return -1;
- return 1;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return 1;
- }
- }
- public LayoutMst ReadOrderMst(LayoutMst mst,List<MacOrder> macorders,List<OrderStatus> orderstatus, ref string errorinfo)
- {
- try
- {
- string condition = $" and b.fcode in(SELECT b.FCode FROM layoutdetail a inner join tentity b on a.EntityID = b.id where a.PreID = {mst.ID} and b.FType = 1)";
- List<MacOrder> temporders = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- macorders.AddRange(temporders);
- condition = $@" and a.preid in(select tt.PreID from macorder tt inner join machine ma on tt.MacID = ma.id
- where ma.fcode in(
- SELECT b.FCode FROM layoutdetail a
- inner join tentity b on a.EntityID = b.id
- where a.PreID = {mst.ID} and b.FType = 1))";
- List<OrderStatus> tempstatus = CurrDb.FindListForCondition<OrderStatus>(condition, ref errorinfo).ToList();
- orderstatus.AddRange(tempstatus);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- }
- }
|