OfTrackMstDal.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using DllEapEntity.Dtos;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DllEapDal.OFILM.MES
  9. {
  10. public class OfTrackMstDal
  11. {
  12. private IDatabase CurrDb = null;
  13. string[] pcodes = new string[4] { "DA", "WB", "LHA", "AA" };
  14. public OfTrackMstDal(IDatabase db)
  15. {
  16. CurrDb = db;
  17. }
  18. public int Insert(OfTrackMst trackMst, string userCode)
  19. {
  20. return CurrDb.InsertFor(trackMst, userCode);
  21. }
  22. /// <summary>
  23. /// 验证记录是否可以进出站
  24. /// </summary>
  25. /// <param name="trackDetail"></param>
  26. /// <param name="errorinfo"></param>
  27. /// <returns></returns>
  28. public bool Check(OfTrackDetail trackDetail, ref string errorinfo)
  29. {
  30. return true;
  31. var mac = CurrDb.FindEntityFor<Machine>(trackDetail.MacId);
  32. var currentPCode = mac.PCode;
  33. var exists = CurrDb.FindListForCondition<OfTrackMst>($" and a.stripId='{trackDetail.StripId}'",
  34. ref errorinfo);
  35. if (exists == null || exists.Count() <= 0)
  36. {
  37. if (currentPCode != pcodes.First())
  38. {
  39. errorinfo = $"当前STRIP还没有生产[{pcodes.First()}]站";
  40. return false;
  41. }
  42. }
  43. else
  44. {
  45. var last = exists.OrderBy(c => c.Id).Last();
  46. var lastPcode = last.ProcessCode;
  47. if (lastPcode == currentPCode)
  48. {
  49. if (trackDetail.Type == 1)
  50. {
  51. if (last.TrackInTime != null)
  52. {
  53. errorinfo = $"当前STRIP已开始[{lastPcode}]站生产,请勿重复操作";
  54. return false;
  55. }
  56. }
  57. else
  58. {
  59. if (last.TrackInTime == null)
  60. {
  61. errorinfo = $"当前STRIP在[{lastPcode}]还未进站";
  62. return false;
  63. }
  64. }
  65. }
  66. else
  67. {
  68. var currentIndex = pcodes.ToList().IndexOf(currentPCode);
  69. var lastIndex = pcodes.ToList().IndexOf(lastPcode);
  70. if (lastIndex != currentIndex - 1)
  71. {
  72. errorinfo = $"当前STRIP还没有生产完前道工序";
  73. return false;
  74. }
  75. else
  76. {
  77. if (last.TrackOutTime == null)
  78. {
  79. errorinfo = $"当前STRIP在[{last.ProcessCode}]站还出站," +
  80. $"不能开始下一站";
  81. return false;
  82. }
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// TrackIn操作
  90. /// </summary>
  91. /// <param name="trackDetail"></param>
  92. /// <param name="errorinfo"></param>
  93. /// <returns></returns>
  94. public int TrackIn(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
  95. {
  96. if (!Check(trackDetail, ref errorinfo))
  97. return -1;
  98. var mst = new OfTrackMst
  99. {
  100. MacId = trackDetail.MacId,
  101. ModCode = userCode,
  102. ModTime = DateTime.Now,
  103. ProcessId = trackDetail.ProcessId,
  104. RecCode = userCode,
  105. RecTime = DateTime.Now,
  106. StripId = trackDetail.StripId,
  107. };
  108. if (CurrDb.InsertFor(mst, userCode) < 0)
  109. return -1;
  110. var sql = "select @@identity;";
  111. var mstId = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  112. trackDetail.MstId = mstId;
  113. trackDetail.ModCode = userCode;
  114. trackDetail.RecCode = userCode;
  115. trackDetail.FDate = DateTime.Now;
  116. trackDetail.IsSuccess = 1;
  117. trackDetail.Type = 1;
  118. if (CurrDb.InsertFor(trackDetail, userCode) < 0)
  119. return -1;
  120. mst = CurrDb.FindEntityFor<OfTrackMst>(mstId);
  121. mst.TrackInTime = DateTime.Now;
  122. if (CurrDb.UpdateFor(mst, userCode) < 0)
  123. return -1;
  124. return 1;
  125. }
  126. /// <summary>
  127. /// TrackOut操作
  128. /// </summary>
  129. /// <param name="trackDetail"></param>
  130. /// <param name="userCode"></param>
  131. /// <param name="errorinfo"></param>
  132. /// <returns></returns>
  133. public int TrackOut(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
  134. {
  135. if (!Check(trackDetail, ref errorinfo))
  136. return -1;
  137. var mst = CurrDb.FindListForCondition<OfTrackMst>($" and a.processId={trackDetail.ProcessId}" +
  138. $" and a.StripId='{trackDetail.StripId}'", ref errorinfo).FirstOrDefault();
  139. if (mst == null)
  140. {
  141. errorinfo = "未找到该Strip的进站记录";
  142. return -1;
  143. }
  144. var mstId = mst.Id;
  145. trackDetail.MstId = mstId;
  146. trackDetail.ModCode = userCode;
  147. trackDetail.RecCode = userCode;
  148. trackDetail.FDate = DateTime.Now;
  149. trackDetail.IsSuccess = 1;
  150. trackDetail.Type = -1;
  151. if (CurrDb.InsertFor(trackDetail, userCode) < 0)
  152. return -1;
  153. mst.TrackOutTime = DateTime.Now;
  154. if (CurrDb.UpdateFor(mst, userCode) < 0)
  155. return -1;
  156. return 1;
  157. }
  158. public IEnumerable<OfTrackMst> GetTrackMst(PagedQuery query, out int total)
  159. {
  160. string errorinfo = string.Empty;
  161. var entities = CurrDb.FindListForCondition<OfTrackMst>($" {query.Filter} " +
  162. $"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
  163. ref errorinfo);
  164. var countSql = new OfTrackMst().GetCountSql() + $" where 1=1 {query.Filter}";
  165. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "-1");
  166. return entities;
  167. }
  168. public IEnumerable<OfTrackDetail> GetTrackDetail(PagedQuery query, out int total)
  169. {
  170. string errorinfo = string.Empty;
  171. var entities = CurrDb.FindListForCondition<OfTrackDetail>($" {query.Filter} " +
  172. $"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
  173. ref errorinfo);
  174. var countSql = new OfTrackDetail().GetCountSql() + $" where 1=1 {query.Filter}";
  175. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "-1");
  176. return entities;
  177. }
  178. /// <summary>
  179. /// 根据STRIPID获取Strip的状态
  180. /// </summary>
  181. /// <param name="stripId"></param>
  182. /// <returns></returns>
  183. public OfTrackMst Get(string stripId, int processId)
  184. {
  185. string errorinfo = string.Empty;
  186. var entity = CurrDb.FindListForCondition<OfTrackMst>($" and a.StripID='{stripId}' " +
  187. $"and a.processId={processId}",
  188. ref errorinfo).FirstOrDefault();
  189. if (entity != null)
  190. {
  191. if (entity.TrackInTime == null)
  192. {
  193. entity.CurrentState = "未进站";
  194. }
  195. else if (entity.TrackOutTime == null)
  196. {
  197. entity.CurrentState = "已进站";
  198. }
  199. else
  200. {
  201. entity.CurrentState = "已出站";
  202. }
  203. return entity;
  204. }
  205. return new OfTrackMst
  206. {
  207. CurrentState = "未进站"
  208. };
  209. }
  210. /// <summary>
  211. /// 进出站
  212. /// </summary>
  213. /// <param name="entitys"></param>
  214. /// <param name="errorinfo"></param>
  215. /// <returns></returns>
  216. public int TrackInOut(string macCode, string stripId, string userCode, int? type, ref string errorinfo)
  217. {
  218. try
  219. {
  220. OfTrackMstDal dal = new OfTrackMstDal(CurrDb);
  221. var machine = CurrDb.FindListForCondition<Machine>($" and a.fcode='{macCode}'",
  222. ref errorinfo).FirstOrDefault();
  223. var mst = CurrDb.FindListForCondition<OfTrackMst>($" and a.macid='{machine.ID}' " +
  224. $"and a.processid='{machine.ProcessId}' and stripId='{stripId}'", ref errorinfo).FirstOrDefault();
  225. var detail = new OfTrackDetail
  226. {
  227. MacId = machine.ID,
  228. FDate = DateTime.Now,
  229. StripId = stripId,
  230. IsSuccess = 1,
  231. ModTime = DateTime.Now,
  232. ProcessId = machine.ProcessId,
  233. OperatorType = 1,
  234. RecTime = DateTime.Now
  235. };
  236. if (type == null || type == 1)
  237. {
  238. detail.Type = 1;
  239. if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
  240. {
  241. return -1;
  242. }
  243. }
  244. else
  245. {
  246. detail.Type = -1;
  247. if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
  248. {
  249. return -1;
  250. }
  251. }
  252. //if (mst == null || mst.TrackInTime == null)
  253. //{
  254. // detail.Type = 1;
  255. // if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
  256. // {
  257. // return -1;
  258. // }
  259. //}
  260. //else
  261. //{
  262. // detail.Type = -1;
  263. // if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
  264. // {
  265. // return -1;
  266. // }
  267. //}
  268. return 1;
  269. }
  270. catch (Exception ex)
  271. {
  272. errorinfo = ex.Message.ToString();
  273. return -1;
  274. }
  275. }
  276. }
  277. }