LayoutMstDal.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DllStatusShowDal
  9. {
  10. public class LayoutMstDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public LayoutMstDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public LayoutMst IULayoutMst(LayoutMst mst, string usercode, ref string errorinfo)
  18. {
  19. try
  20. {
  21. int result = 0;
  22. int id = mst.ID;
  23. if (mst.EntityStatusID == 1)
  24. {
  25. //mst.reccode = usercode;
  26. //mst.RecTime = DateTime.Now;
  27. //mst.ModCode = usercode;
  28. //mst.ModTime = DateTime.Now;
  29. string fcode = GetLayoutMstCode(ref errorinfo);
  30. if (fcode == "")
  31. return null;
  32. mst.FCode = fcode;
  33. result = CurrDb.InsertFor(mst, usercode);
  34. if (result < 0)
  35. {
  36. return null;
  37. }
  38. object objid = CurrDb.FindObject("select @@IDENTITY");
  39. if (objid.ToString() == "")
  40. {
  41. return null;
  42. }
  43. id = int.Parse(objid.ToString());
  44. }
  45. else
  46. {
  47. //mst.ModCode = usercode;
  48. //mst.ModTime = DateTime.Now;
  49. result = CurrDb.UpdateFor(mst, usercode);
  50. if (result < 0)
  51. {
  52. return null;
  53. }
  54. }
  55. mst = CurrDb.FindEntityFor<LayoutMst>(id);
  56. return mst;
  57. }
  58. catch (Exception e)
  59. {
  60. errorinfo = e.Message;
  61. return null;
  62. }
  63. }
  64. private string GetLayoutMstCode(ref string errorinfo)
  65. {
  66. try
  67. {
  68. StringBuilder sqlstr = new StringBuilder(100);
  69. string code = "Y";
  70. sqlstr.AppendFormat("SELECT max(fcode) FROM LayoutMst where fcode like '{0}%'", code);
  71. object obj = CurrDb.FindObject(sqlstr.ToString());
  72. if (obj.ToString() == "")
  73. return code + "00001";
  74. int fnum = int.Parse(obj.ToString().Substring(1, 5));
  75. fnum++;
  76. return code + fnum.ToString().PadLeft(5, '0');
  77. }
  78. catch (Exception ex)
  79. {
  80. errorinfo = ex.Message.ToString();
  81. return "";
  82. }
  83. }
  84. public LayoutMst IULayoutMst(LayoutMst mst,List<LayoutDetail> details, string usercode, ref string errorinfo)
  85. {
  86. try
  87. {
  88. LayoutMst result = IULayoutMst(mst, usercode, ref errorinfo);
  89. if (result == null)
  90. return null;
  91. //处理明细
  92. List<LayoutDetail> templist = details.Where(t => t.EntityStatusID == 1).ToList();
  93. foreach (var item in templist)
  94. item.PreID = result.ID;
  95. int count = CurrDb.InsertFor<LayoutDetail>(templist, usercode);
  96. if (count < 0)
  97. return null;
  98. templist = details.Where(t => t.EntityStatusID == 2).ToList();
  99. count = CurrDb.UpdateFor<LayoutDetail>(templist, usercode);
  100. if (count < 0)
  101. return null;
  102. templist = details.Where(t => t.EntityStatusID == -1).ToList();
  103. count = CurrDb.DeleteForEntity<LayoutDetail>(templist);
  104. if (count < 0)
  105. return null;
  106. return result;
  107. }
  108. catch (Exception e)
  109. {
  110. errorinfo = e.Message;
  111. return null;
  112. }
  113. }
  114. //private string GetTLcdCode(ref string errorinfo)
  115. //{
  116. // try
  117. // {
  118. // StringBuilder sqlstr = new StringBuilder(100);
  119. // string code = "L";
  120. // sqlstr.AppendFormat("SELECT max(fcode) FROM TLcd where fcode like '{0}%'", code);
  121. // object obj = CurrDb.FindObject(sqlstr.ToString());
  122. // if (obj.ToString() == "")
  123. // return code + "00001";
  124. // int fnum = int.Parse(obj.ToString().Substring(1, 5));
  125. // fnum++;
  126. // return code + fnum.ToString().PadLeft(5, '0');
  127. // }
  128. // catch (Exception ex)
  129. // {
  130. // errorinfo = ex.Message.ToString();
  131. // return "";
  132. // }
  133. //}
  134. public LayoutMst DelLayoutMst(List<LayoutMst> mst, string usercode, ref string errorinfo)
  135. {
  136. try
  137. {
  138. foreach(var item in mst)
  139. {
  140. int result = DelLayoutMst(item, usercode, ref errorinfo);
  141. if (result < 0)
  142. return null;
  143. }
  144. return mst[0];
  145. }
  146. catch (Exception e)
  147. {
  148. errorinfo = e.Message;
  149. return null;
  150. }
  151. }
  152. private int DelLayoutMst(LayoutMst mst, string usercode, ref string errorinfo)
  153. {
  154. try
  155. {
  156. string condition = $" and {EntityAttribute.GetPropertyCondition<LayoutDetail>(nameof(LayoutDetail.PreID))}={mst.ID}";
  157. int result = CurrDb.DeleteForCondition<LayoutDetail>(condition);
  158. if (result < 0)
  159. return -1;
  160. result = CurrDb.DeleteFor<LayoutMst>(mst.ID);
  161. if (result < 0)
  162. return -1;
  163. return 1;
  164. }
  165. catch (Exception e)
  166. {
  167. errorinfo = e.Message;
  168. return 1;
  169. }
  170. }
  171. public LayoutMst ReadOrderMst(LayoutMst mst,List<MacOrder> macorders,List<OrderStatus> orderstatus, ref string errorinfo)
  172. {
  173. try
  174. {
  175. 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)";
  176. List<MacOrder> temporders = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  177. macorders.AddRange(temporders);
  178. condition = $@" and a.preid in(select tt.PreID from macorder tt inner join machine ma on tt.MacID = ma.id
  179. where ma.fcode in(
  180. SELECT b.FCode FROM layoutdetail a
  181. inner join tentity b on a.EntityID = b.id
  182. where a.PreID = {mst.ID} and b.FType = 1))";
  183. List<OrderStatus> tempstatus = CurrDb.FindListForCondition<OrderStatus>(condition, ref errorinfo).ToList();
  184. orderstatus.AddRange(tempstatus);
  185. return mst;
  186. }
  187. catch (Exception e)
  188. {
  189. errorinfo = e.Message;
  190. return null;
  191. }
  192. }
  193. }
  194. }