MacCheckMstDal.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using DllEapEntity.OFILM;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DllEapDal
  9. {
  10. public class MacCheckMstDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public MacCheckMstDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public MacCheckMst IU(MacCheckMst 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. result = CurrDb.InsertFor(mst, usercode);
  26. if (result < 0)
  27. {
  28. return null;
  29. }
  30. object objid = CurrDb.FindObject("select @@IDENTITY");
  31. if (objid.ToString() == "")
  32. {
  33. return null;
  34. }
  35. id = int.Parse(objid.ToString());
  36. }
  37. else
  38. {
  39. result = CurrDb.UpdateFor(mst, usercode);
  40. if (result < 0)
  41. {
  42. return null;
  43. }
  44. }
  45. mst = CurrDb.FindEntityFor<MacCheckMst>(id);
  46. return mst;
  47. }
  48. catch (Exception e)
  49. {
  50. errorinfo = e.Message;
  51. return null;
  52. }
  53. }
  54. public int AddCheck(string maccode, List<MacCheckDetail> details, string usercode, ref string errorinfo, int checkMode = 1)
  55. {
  56. try
  57. {
  58. //var machine = CurrDb.FindListForCondition<Step>($" and a.FCode='{maccode}'", ref errorinfo).FirstOrDefault();
  59. //var smDal = new StaffMachineDal(CurrDb);
  60. //if (!smDal.IsPermitted(usercode, machine.ID, ref errorinfo))
  61. //{
  62. // return -1;
  63. //}
  64. MacCheckMst mst = new MacCheckMst();
  65. mst.MacCode = maccode;
  66. mst.CheckMode = checkMode;
  67. mst = IU(mst, usercode, ref errorinfo);
  68. if (mst == null)
  69. return -1;
  70. foreach (var item in details)
  71. item.MacCheckMstID = mst.ID;
  72. int result = CurrDb.InsertFor<MacCheckDetail>(details, usercode);
  73. if (result <= 0)
  74. return -1;
  75. return 1;
  76. }
  77. catch (Exception e)
  78. {
  79. errorinfo = e.Message;
  80. return -1;
  81. }
  82. }
  83. /// <summary>
  84. /// 获取点检记录列表
  85. /// </summary>
  86. /// <param name="start"></param>
  87. /// <param name="length"></param>
  88. /// <param name="order"></param>
  89. /// <param name="sort"></param>
  90. /// <param name="filter"></param>
  91. /// <param name="errorinfo"></param>
  92. /// <returns></returns>
  93. public IEnumerable<MacCheckMstDto> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  94. {
  95. var sql = $@"select a.id,a.macCode,a.CheckMode,a.Rectime from maccheckmst a where 1=1 {filter}
  96. order by {sort} {order} limit {start - 1},{length} ";
  97. var msts = CurrDb.FindList<MacCheckMstDto>(sql);
  98. if (msts != null && msts.Count() > 0)
  99. {
  100. foreach (var item in msts)
  101. {
  102. sql = $@"select a.paramCode,a.paramVal,b.fname paramName from maccheckdetail a
  103. left join sec b on a.ParamCode = b.FCode
  104. where a.MacCheckMstID={item.Id}";
  105. var details = CurrDb.FindList<MacCheckDetailDto>(sql);
  106. item.MacCheckDetails = details;
  107. }
  108. }
  109. return msts;
  110. }
  111. public IEnumerable<MacCheckMstDetailDto> GetMstDetail(int start, int length, string order, string sort, string filter, string errorinfo)
  112. {
  113. var sql = $@"SELECT
  114. a.id ID,
  115. i.FName FactoryName,
  116. k.FName FloorName,
  117. a.macCode MacCode,
  118. b.paramCode ParamCode,
  119. b.paramVal ParamVal,
  120. c.fname ParamName,
  121. a.CheckMode,
  122. a.Rectime
  123. FROM
  124. maccheckmst a
  125. LEFT JOIN maccheckdetail b ON a.id = b.MacCheckMstID
  126. LEFT JOIN sec c ON b.ParamCode = c.FCode
  127. LEFT JOIN Machine d on d.FCode=a.macCode
  128. left outer join factoryregion i on d.factoryid =i.id
  129. left outer join factoryregion j on d.regionid =j.id
  130. left outer join factoryregion k on j.parentid =k.id where 1=1 {filter}
  131. order by {sort} {order} limit {start - 1},{length} ";
  132. var msts = CurrDb.FindList<MacCheckMstDetailDto>(sql);
  133. var list = new List<MacCheckMstDetailDto>();
  134. var groupfactory = msts.GroupBy(c => c.FactoryName);
  135. var no = 0;
  136. if (groupfactory != null && groupfactory.Count() > 0)
  137. {
  138. foreach (var item in groupfactory)
  139. {
  140. var renderFactory = true;
  141. no++;
  142. var dateGroups = item.Where(c => c.FactoryName == item.Key).GroupBy(c => c.FloorName);
  143. if (dateGroups != null && dateGroups.Count() > 0)
  144. {
  145. foreach (var dateItem in dateGroups)
  146. {
  147. var renderFloor = true;
  148. var dateIt = dateItem.Where(c => c.FloorName == dateItem.Key).GroupBy(c => c.MacCode);
  149. foreach (var reportItem in dateIt)
  150. {
  151. var renderMac = true;
  152. foreach (var it in reportItem)
  153. {
  154. var temp = new MacCheckMstDetailDto
  155. {
  156. FactoryName = item.Key,
  157. FloorName = dateItem.Key,
  158. MacCode = reportItem.Key,
  159. ParamCode = it.ParamCode,
  160. ParamVal = it.ParamVal,
  161. ParamName = it.ParamName,
  162. CheckMode = it.CheckMode,
  163. RecTime = it.RecTime,
  164. FactoryRowSpan = item.Count(),
  165. FloorRowSpan = dateItem.Count(),
  166. MacRowSpan = reportItem.Count(),
  167. RenderFactor = renderFactory,
  168. RenderFloor = renderFloor,
  169. RenderMac = renderMac,
  170. };
  171. list.Add(temp);
  172. renderFactory = false;
  173. renderFloor = false;
  174. renderMac = false;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. return list;
  182. }
  183. public IEnumerable<MacCheckMstDetailDto> GetMstDetailExport(int start, int length, string order, string sort, string filter, string errorinfo)
  184. {
  185. var sql = $@"SELECT
  186. a.id ID,
  187. i.FName FactoryName,
  188. k.FName FloorName,
  189. a.macCode MacCode,
  190. b.paramCode ParamCode,
  191. b.paramVal ParamVal,
  192. c.fname ParamName,
  193. a.CheckMode,
  194. a.Rectime
  195. FROM
  196. maccheckmst a
  197. LEFT JOIN maccheckdetail b ON a.id = b.MacCheckMstID
  198. LEFT JOIN sec c ON b.ParamCode = c.FCode
  199. LEFT JOIN Machine d on d.FCode=a.macCode
  200. left outer join factoryregion i on d.factoryid =i.id
  201. left outer join factoryregion j on d.regionid =j.id
  202. left outer join factoryregion k on j.parentid =k.id where 1=1 {filter}
  203. order by {sort} {order} limit 10000 ";
  204. var msts = CurrDb.FindList<MacCheckMstDetailDto>(sql);
  205. return msts;
  206. }
  207. /// <summary>
  208. /// 获取点检记录的数量
  209. /// </summary>
  210. /// <param name="filter"></param>
  211. /// <returns></returns>
  212. public int GetCount(string filter)
  213. {
  214. var sql = new MacCheckMst().GetSelectSql();
  215. sql = $"select count(1) from ({sql} where 1=1 {filter}) tt";
  216. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  217. }
  218. public IEnumerable<MacCheckQueryDto> GetCheckDtos(int start, int length, string order, string sort, string filter, string errorinfo, out int total)
  219. {
  220. var datas = CurrDb.FindListForCondition<MacCheckQueryDto>($"{filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  221. var sql = $"select count(1) from maccheckmst a where 1=1 {filter}";
  222. total = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  223. return datas;
  224. }
  225. }
  226. }