using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapDal { public class ReportMstDal { private IDatabase CurrDb = null; public ReportMstDal(IDatabase db) { CurrDb = db; } public ReportMst IUReportMst(ReportMst mst,string usercode, ref string errorinfo) { try { int result = 0; int id = mst.ID; if(mst.EntityStatusID==1) { string code = GetReportMstCode(ref errorinfo); if (code == "") { return null; } mst.FCode = code; mst.RecCode = usercode; mst.RecTime = DateTime.Now; mst.ModCode = usercode; mst.ModTime = DateTime.Now; 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; } } private string GetReportMstCode(ref string errorinfo) { try { StringBuilder sqlstr = new StringBuilder(100); string code = "R"; sqlstr.AppendFormat("SELECT max(fcode) FROM ReportMst 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 int DelReportMst(ReportMst datarow, ref string errorinfo) { try { StringBuilder sqlstr = new StringBuilder(100); //删除oab记录 //sqlstr.AppendFormat("delete from oab where ab00 in (select b.uid from (select id uid from oaa where oa00={0}) as b)", datarow["id"].ToString()); sqlstr.AppendFormat("delete from ReportDetail where preid={0}", datarow.ID); int result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) { return -1; } sqlstr.Clear(); sqlstr.AppendFormat("delete from ReportMst where id={0}", datarow.ID); result = CurrDb .ExecuteBySql(sqlstr.ToString()); if (result < 0) { return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //删除报告 public int DelReportMst(List dt, ref string errorinfo) { try { int result = 0; foreach (var temprow in dt) { result = DelReportMst(temprow, ref errorinfo); if (result < 0) { return -1; } } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public ReportMst IUReportMst(ReportMst mst,List details,string usercode, ref string errorinfo) { try { ReportMst remst = IUReportMst(mst,usercode, ref errorinfo); if (remst==null) { return null; } //添加指令明细 int result = IUReportDetail(details, remst.ID,usercode, ref errorinfo); if (result < 0) { return null; } return remst; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } //新增、修改、删除指令信息oaa private int IUReportDetail(List detaildt, int preid,string usercode, ref string errorinfo) { try { StringBuilder sqlstr = new StringBuilder(100); int result = 0; //删除 List temprows = detaildt.Where(t => t.EntityStatusID < 0).ToList(); foreach (var temprow in temprows) { sqlstr.Clear(); sqlstr.AppendFormat("delete from ReportDetail where id={0}", temprow.ID); result = CurrDb.ExecuteBySql(sqlstr.ToString()); if (result < 0) return -1; } //修改,可以批量 temprows = detaildt.Where(t => t.EntityStatusID ==2).ToList(); foreach(var item in temprows) { item.ModCode = usercode; item.ModTime = DateTime.Now; } result = CurrDb.UpdateFor(temprows, usercode); if (result < 0) { return -1; } temprows = detaildt.Where(t => t.EntityStatusID ==1).ToList(); foreach (var item in temprows) { item.PreID = preid; item.RecCode = usercode; item.RecTime = DateTime.Now; item.ModCode = usercode; item.ModTime = DateTime.Now; } result = CurrDb.InsertFor(temprows, usercode); if (result < 0) { return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //复制整个指令文件 public ReportMst CopyReportMst( ReportMst datarow,string usercode, ref string errorinfo) { string sqlstr = ""; List details = CurrDb.FindListForCondition(sqlstr, ref errorinfo).ToList(); foreach(var item in details) { item.ID = 0; item.PreID = 0; item.EntityStatusID = 1; } ReportMst mst = new ReportMst(); EntityHelper.CopyPropertyValue(datarow, mst); mst.EntityStatusID = 1; mst.ID = 0; ReportMst reentity= IUReportMst(mst,details,usercode, ref errorinfo); if (reentity==null) { return null; } return reentity; } } }