KnsMacAreaCountMstDal.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Linq;
  8. using DllHsmsWeb;
  9. using DllHsms;
  10. namespace DllEapDal
  11. {
  12. public class KnsMacAreaCountMstDal
  13. {
  14. private IDatabase CurrDb = null;
  15. public KnsMacAreaCountMstDal(IDatabase db)
  16. {
  17. CurrDb = db;
  18. }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. /// <param name="mac"></param>
  23. /// <param name="eventtypeid"></param>
  24. /// <param name="ptime"></param>
  25. /// <param name="details"></param>
  26. /// <param name="usercode"></param>
  27. /// <param name="errorinfo"></param>
  28. /// <returns></returns>
  29. public int MacAreaCountMst01(Machine mac, int eventtypeid,DateTime ptime, List<McaSecVDetail> details,string usercode, ref string errorinfo)
  30. {
  31. try
  32. {
  33. int result = 0;
  34. //查找该机台是否存在进行中的任务
  35. string condition = $" and a.MacID={mac.ID} and a.StatusID=1";
  36. List<MacAreaCountMst> msts = CurrDb.FindListForCondition<MacAreaCountMst>(condition, ref errorinfo).ToList();
  37. MacAreaCountMst mst = null;
  38. if (eventtypeid==-1&&msts.Count>0)
  39. {
  40. mst = msts[0];
  41. //当前是开始事件,且当前机台存在进行中的任务,这种情况属于异常,直接将进行中的任务结束
  42. //将开始作为当前进行中得结束数据
  43. List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
  44. result = AddMacCount02(mac.ID, 1, ptime, templist, details, usercode, ref errorinfo);
  45. if (result <= 0)
  46. return -1;
  47. //汇总明细数量
  48. result = AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
  49. if (result <= 0)
  50. return -1;
  51. //更新主记录状态
  52. mst.StatusID = -1;
  53. mst.CompleteTime = ptime;
  54. CurrDb.UpdateFor<MacAreaCountMst>(mst, usercode);
  55. //根据列表添加新的任务
  56. mst = AddMacAreaCountMst(eventtypeid, mac,1,1, ptime, details, usercode, ref errorinfo);
  57. if (mst==null)
  58. return -1;
  59. }
  60. if (eventtypeid == -1 && msts.Count <= 0)
  61. {
  62. //当前是开始事件,且当前机台不存在进行中的任务,这种情况属于正常,直接添加记录
  63. //根据列表添加新的任务
  64. mst = AddMacAreaCountMst(eventtypeid, mac, 1, 1, ptime, details, usercode, ref errorinfo);
  65. if (mst == null)
  66. return -1;
  67. }
  68. if (eventtypeid == 1 && msts.Count > 0)
  69. {
  70. mst = msts[0];
  71. //结束且存在进行中的,属于正常情况
  72. //读取明细
  73. List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={msts[0].ID}", ref errorinfo).ToList();
  74. //添加原始数据记录
  75. result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
  76. if (result<=0)
  77. return -1;
  78. //汇总明细数量
  79. result=AccountMacAreaCountMst(msts[0].ID, ref errorinfo);
  80. if (result <= 0)
  81. return -1;
  82. //更新主记录状态
  83. mst.StatusID = -1;
  84. mst.CompleteTime = ptime;
  85. CurrDb.UpdateFor<MacAreaCountMst>(mst, usercode);
  86. }
  87. if (eventtypeid == 1 && msts.Count <= 0)
  88. {
  89. //结束且不存在进行中的,属于不正常情况
  90. //读取明细
  91. //mst = AddMacAreaCountMst(eventtypeid, mac, -1, -1, ptime, details, usercode, ref errorinfo);
  92. //if (mst == null)
  93. // return -1;
  94. //忽略,直接返回
  95. return 1;
  96. }
  97. return 1;
  98. }
  99. catch(Exception ex)
  100. {
  101. errorinfo = ex.Message.ToString();
  102. return -1;
  103. }
  104. }
  105. public int MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  106. {
  107. try
  108. {
  109. if(eventtypeid==-1)
  110. {
  111. //每条开始时间,则添加每条记录
  112. }
  113. List<MacCount02> templist = new List<MacCount02>();
  114. foreach (var item in details)
  115. {
  116. MacCount02 entity = new MacCount02();
  117. entity.PreID = 0;
  118. entity.ParamCode = item.FCode;
  119. entity.EventTypeID = eventtypeid;
  120. entity.MacID = mac.ID;
  121. entity.FCount = int.Parse(item.FVal);
  122. entity.FDate = ptime;
  123. entity.RecTypeID = 1;
  124. templist.Add(entity);
  125. }
  126. return CurrDb.InsertFor<MacCount02>(templist, usercode);
  127. }
  128. catch (Exception ex)
  129. {
  130. errorinfo = ex.Message.ToString();
  131. return -1;
  132. }
  133. }
  134. public int AccountMacAreaCountMst(int mstid, ref string errorinfo)
  135. {
  136. try
  137. {
  138. string sqlstr = $@"update macareacountdetail
  139. set FCount = (SELECT sum(EventTypeID * FCount) FROM MacCount02 where preid = macareacountdetail.id and ParamCode = macareacountdetail.ParamCode)
  140. where preid = {mstid} and id> 0";
  141. CurrDb.ExecuteBySql(sqlstr);
  142. return 1;
  143. }
  144. catch (Exception ex)
  145. {
  146. errorinfo = ex.Message.ToString();
  147. return -1;
  148. }
  149. }
  150. public MacAreaCountMst AddMacAreaCountMst(int eventtypeid,Machine mac,int statusid,int isrightid,DateTime ptime, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  151. {
  152. try
  153. {
  154. //添加主档
  155. MacAreaCountMst mst = AddMacAreaCountMst(mac.ID,statusid,isrightid, ptime, usercode, ref errorinfo);
  156. if (mst == null)
  157. return null;
  158. //添加明细
  159. int result = AddMacAreaCountDetail(mst, details, usercode, ref errorinfo);
  160. if (result < 0)
  161. return null;
  162. //读取明细
  163. List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={mst.ID}", ref errorinfo).ToList();
  164. //添加原始数据记录
  165. result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
  166. if (result < 0)
  167. return null;
  168. return mst;
  169. }
  170. catch (Exception ex)
  171. {
  172. errorinfo = ex.Message.ToString();
  173. return null;
  174. }
  175. }
  176. public int AddMacCount02(int macid,int eventtypeid,DateTime ptime,List<MacAreaCountDetail> areadetails, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  177. {
  178. try
  179. {
  180. List<MacCount02> templist = new List<MacCount02>();
  181. List<MacAreaCountDetail> areatemp = null;
  182. MacAreaCountDetail tempentity = null;
  183. foreach (var item in details)
  184. {
  185. areatemp = areadetails.Where(t => t.ParamCode == item.FCode).ToList();
  186. if(areatemp.Count<=0)
  187. {
  188. tempentity = AddMacAreaCountDetail(areadetails[0].PreID, item.FCode, usercode, ref errorinfo);
  189. if (tempentity == null)
  190. return -1;
  191. }
  192. else
  193. {
  194. tempentity = areatemp[0];
  195. }
  196. MacCount02 entity = new MacCount02();
  197. entity.PreID = tempentity.ID;
  198. entity.ParamCode = item.FCode;
  199. entity.EventTypeID = eventtypeid;
  200. entity.MacID = macid;
  201. entity.FCount = int.Parse(item.FVal);
  202. entity.FDate = ptime;
  203. entity.RecTypeID = 1;
  204. templist.Add(entity);
  205. }
  206. return CurrDb.InsertFor<MacCount02>(templist, usercode);
  207. }
  208. catch (Exception ex)
  209. {
  210. errorinfo = ex.Message.ToString();
  211. return -1;
  212. }
  213. }
  214. public int AddMacAreaCountDetail(MacAreaCountMst mst, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  215. {
  216. try
  217. {
  218. MacAreaCountDetail tempentity = null;
  219. foreach (var item in details)
  220. {
  221. tempentity = AddMacAreaCountDetail(mst.ID, item.FCode, usercode, ref errorinfo);
  222. if (tempentity == null)
  223. return -1;
  224. }
  225. return 1;
  226. }
  227. catch (Exception ex)
  228. {
  229. errorinfo = ex.Message.ToString();
  230. return -1;
  231. }
  232. }
  233. public MacAreaCountDetail AddMacAreaCountDetail(int preid,string paramcode, string usercode, ref string errorinfo)
  234. {
  235. try
  236. {
  237. MacAreaCountDetail entity = new MacAreaCountDetail();
  238. entity.PreID = preid;
  239. entity.ParamCode = paramcode;
  240. entity.FCount = 0;
  241. CurrDb.InsertFor<MacAreaCountDetail>(entity, usercode);
  242. object objid = CurrDb.FindObject("select @@IDENTITY");
  243. if (objid.ToString() == "")
  244. {
  245. return null;
  246. }
  247. int id = int.Parse(objid.ToString());
  248. entity = CurrDb.FindEntityFor<MacAreaCountDetail>(id);
  249. return entity;
  250. }
  251. catch (Exception ex)
  252. {
  253. errorinfo = ex.Message.ToString();
  254. return null;
  255. }
  256. }
  257. public MacAreaCountMst AddMacAreaCountMst(int macid,int statusid,int isrightid, DateTime ptime, string usercode, ref string errorinfo)
  258. {
  259. try
  260. {
  261. MacAreaCountMst mst = new MacAreaCountMst();
  262. mst.MacID = macid;
  263. mst.StartTime = ptime;
  264. mst.StatusID = statusid;
  265. mst.IsRightID = isrightid;
  266. CurrDb.InsertFor<MacAreaCountMst>(mst, usercode);
  267. object objid = CurrDb.FindObject("select @@IDENTITY");
  268. if (objid.ToString() == "")
  269. {
  270. return null;
  271. }
  272. int id = int.Parse(objid.ToString());
  273. mst = CurrDb.FindEntityFor<MacAreaCountMst>(id);
  274. return mst;
  275. }
  276. catch (Exception ex)
  277. {
  278. errorinfo = ex.Message.ToString();
  279. return null;
  280. }
  281. }
  282. public KnsMacCount AreaProcessStart(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
  283. {
  284. try
  285. {
  286. int result = 0;
  287. //查找该机台是否存在进行中的任务
  288. string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
  289. List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
  290. KnsMacCount entity = new KnsMacCount();
  291. entity.StartTime = ptime;
  292. entity.ProcessStatusID = -1;
  293. entity.YieldStatusID = -1;
  294. entity.MacID = mac.ID;
  295. entity.RecTypeID = 1;
  296. CurrDb.InsertFor<KnsMacCount>(entity, usercode);
  297. object objid = CurrDb.FindObject("select @@IDENTITY");
  298. if (objid.ToString() == "")
  299. {
  300. return null;
  301. }
  302. int id = int.Parse(objid.ToString());
  303. KnsMacCount resultentity = CurrDb.FindEntityFor<KnsMacCount>(id);
  304. if (msts.Count > 0)
  305. {
  306. result= AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
  307. if (result <= 0)
  308. return null;
  309. }
  310. return resultentity;
  311. }
  312. catch (Exception ex)
  313. {
  314. errorinfo = ex.Message.ToString();
  315. return null;
  316. }
  317. }
  318. /// <summary>
  319. /// 每条完成事件函数
  320. /// </summary>
  321. /// <param name="mac"></param>
  322. /// <param name="ptime"></param>
  323. /// <param name="usercode"></param>
  324. /// <param name="errorinfo"></param>
  325. /// <returns></returns>
  326. public int AreaProcessComplete(Machine mac, DateTime ptime, string usercode, ref string errorinfo)
  327. {
  328. try
  329. {
  330. int result = 0;
  331. //查找该机台是否存在进行中的任务
  332. string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=-1";
  333. List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
  334. if(msts.Count>0)
  335. {
  336. return AreaProcessComplete(msts[0], ptime, usercode, ref errorinfo);
  337. }
  338. return 1;
  339. }
  340. catch (Exception ex)
  341. {
  342. errorinfo = ex.Message.ToString();
  343. return -1;
  344. }
  345. }
  346. private int AreaProcessComplete(KnsMacCount knsarea, DateTime ptime, string usercode, ref string errorinfo)
  347. {
  348. try
  349. {
  350. knsarea.EndTime = ptime;
  351. knsarea.ProcessStatusID = 1;
  352. CurrDb.UpdateFor<KnsMacCount>(knsarea, usercode);
  353. //发送读取产量指令
  354. int result = SendS2F15DataSetTime(knsarea, ref errorinfo);
  355. if (result < 0)
  356. return -1;
  357. result= SendS2F41(knsarea, ref errorinfo);
  358. if (result !=0&&result!=4)
  359. return -1;
  360. return 1;
  361. }
  362. catch (Exception ex)
  363. {
  364. errorinfo = ex.Message.ToString();
  365. return -1;
  366. }
  367. }
  368. private int SendS2F15DataSetTime(KnsMacCount maccount, ref string errorinfo)
  369. {
  370. try
  371. {
  372. string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=15";
  373. List<OrderDetail> orders = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  374. if (orders.Count <= 0)
  375. {
  376. errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F15指令。";
  377. return -1;
  378. }
  379. List<OrderData> datas = GetS2F15DataSetTime(maccount.StartTime,maccount.EndTime, orders[0].ID, ref errorinfo);
  380. if (datas == null)
  381. return -1;
  382. HsmsWeb accessmac = new HsmsWeb();
  383. OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
  384. if (rec == null)
  385. return -1;
  386. return int.Parse(rec.Datalists[0].FContent);
  387. }
  388. catch (Exception ex)
  389. {
  390. errorinfo = ex.Message.ToString();
  391. return -1;
  392. }
  393. }
  394. private List<OrderData> GetS2F41Data(int preid, ref string errorinfo)
  395. {
  396. string rcmd = "CALC_PM_DATA";
  397. List<OrderData> ldata = new List<OrderData>();
  398. OrderData lentity = new OrderData();
  399. lentity.ID = 1;
  400. lentity.ParentID = 0;
  401. lentity.PreID = preid;
  402. lentity.FCode = "L";
  403. lentity.FLen = 2;
  404. ldata.Add(lentity);
  405. OrderData entity = new OrderData();
  406. entity.ID = 2;
  407. entity.ParentID = 1;
  408. entity.FNum = 10;
  409. entity.PreID = preid;
  410. entity.FCode = "A";
  411. entity.FContent = rcmd;
  412. entity.FLen = rcmd.Length;
  413. ldata.Add(entity);
  414. entity = new OrderData();//添加L节点
  415. entity.ID = 3;
  416. entity.ParentID = 1;
  417. entity.FNum = 20;
  418. entity.PreID = preid;
  419. entity.FCode = "L";
  420. entity.FLen = 1;
  421. ldata.Add(entity);
  422. return ldata;
  423. }
  424. private int SendS2F41(KnsMacCount maccount, ref string errorinfo)
  425. {
  426. try
  427. {
  428. string condition = $" and a.preid=(SELECT preid FROM macorder where macid={maccount.MacID}) and a.sval=2 and a.fval=41";
  429. List<OrderDetail> orders = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  430. if (orders.Count <= 0)
  431. {
  432. errorinfo = $"未找到机台{maccount.MacFCode}对应的S2F41指令。";
  433. return -1;
  434. }
  435. List<OrderData> datas = GetS2F41Data(orders[0].ID, ref errorinfo);
  436. if (datas == null)
  437. return -1;
  438. HsmsWeb accessmac = new HsmsWeb();
  439. OrderBlock rec = accessmac.SendOrderFor(maccount.MacFCode, orders[0], datas, ref errorinfo);
  440. if (rec == null)
  441. return -1;
  442. return int.Parse(rec.Datalists[1].FContent);
  443. }
  444. catch (Exception ex)
  445. {
  446. errorinfo = ex.Message.ToString();
  447. return -1;
  448. }
  449. }
  450. //
  451. private static List<OrderData> GetS2F15DataSetTime(DateTime start,DateTime end, int preid, ref string errorinfo)
  452. {
  453. List<OrderData> ldata = new List<OrderData>();
  454. OrderData lentity = GetOrderData(1, preid, 0, "L", 3, "",10, ref errorinfo);
  455. ldata.Add(lentity);
  456. OrderData l1 = GetOrderData(2, preid, 1, "L", 2, "",20, ref errorinfo);
  457. ldata.Add(l1);
  458. OrderData entity = GetOrderData(3, preid, 2, "U2", 1, "1500",30, ref errorinfo);
  459. ldata.Add(entity);
  460. entity = GetOrderData(4, preid, 2, "U1", 1, "0",40, ref errorinfo);
  461. ldata.Add(entity);
  462. OrderData l2 = GetOrderData(5, preid, 1, "L", 2, "",50, ref errorinfo);
  463. ldata.Add(l2);
  464. entity = GetOrderData(6, preid, 5, "U2", 1, "1501",60, ref errorinfo);
  465. ldata.Add(entity);
  466. entity = GetOrderData(7, preid, 5, "A", 14, start.ToString("yyyyMMddHHmmss"),70, ref errorinfo);
  467. ldata.Add(entity);
  468. OrderData l3 = GetOrderData(8, preid, 1, "L", 2, "",80, ref errorinfo);
  469. ldata.Add(l3);
  470. entity = GetOrderData(9, preid, 8, "U2", 1, "1502",90, ref errorinfo);
  471. ldata.Add(entity);
  472. entity = GetOrderData(10, preid, 8, "A", 14, end.ToString("yyyyMMddHHmmss"),100, ref errorinfo);
  473. ldata.Add(entity);
  474. return ldata;
  475. }
  476. private static OrderData GetOrderData(int id, int preid,int parentid,string fcode,int flen,string fcontent,int fnum, ref string errorinfo)
  477. {
  478. OrderData lentity = new OrderData();
  479. lentity.ID = id;
  480. lentity.ParentID = parentid;
  481. lentity.PreID = preid;
  482. lentity.FCode = fcode;
  483. lentity.FLen = flen;
  484. lentity.FContent = fcontent;
  485. lentity.FNum = fnum;
  486. return lentity;
  487. }
  488. public int KnsYield(Machine mac,List<OrderData> lists, string usercode, ref string errorinfo)
  489. {
  490. try
  491. {
  492. int result = 0;
  493. //查找该机台是否存在进行中的任务
  494. string condition = $" and a.MacID={mac.ID} and a.ProcessStatusID=1 and a.YieldStatusID=-1";
  495. List<KnsMacCount> msts = CurrDb.FindListForCondition<KnsMacCount>(condition, ref errorinfo).ToList();
  496. if (msts.Count <= 0)
  497. {
  498. return 100;
  499. }
  500. List<ReportDetail> CurrReportDetail = CurrDb.FindListForCondition<ReportDetail>("", ref errorinfo).ToList();
  501. if (CurrReportDetail == null)
  502. {
  503. errorinfo = $"读取数据失败,错误信息:{errorinfo}";
  504. return -1;
  505. }
  506. List<McaSecVDetail> tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo);
  507. if (tempdt == null)
  508. {
  509. errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo;
  510. return -1;
  511. }
  512. foreach(var item in tempdt)
  513. {
  514. KnsMacCountDetail detail = new KnsMacCountDetail();
  515. detail.MstID = msts[0].ID;
  516. detail.ParamCode = item.FCode;
  517. detail.FCount = int.Parse(item.FVal);
  518. CurrDb.InsertFor<KnsMacCountDetail>(detail, usercode);
  519. }
  520. msts[0].YieldStatusID = 1;
  521. CurrDb.UpdateFor<KnsMacCount>(msts[0], usercode);
  522. return 1;
  523. }
  524. catch (Exception ex)
  525. {
  526. errorinfo = ex.Message.ToString();
  527. return -1;
  528. }
  529. }
  530. public int SendStopOrder(string maccode,ref string errorinfo)
  531. {
  532. try
  533. {
  534. string condition = $" and a.fcode='{maccode}'";
  535. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  536. if(macs.Count<=0)
  537. {
  538. errorinfo = $"未找到机台【{maccode}】信息。";
  539. return -1;
  540. }
  541. condition = $" and a.MacID={macs[0].ID}";
  542. List<MacOrder> macorders = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  543. if(macorders.Count<=0)
  544. {
  545. errorinfo = $"未找到机台【{maccode}】对应的指令信息。";
  546. return -1;
  547. }
  548. int orderid = macorders[0].PreID;
  549. condition = $" and a.PreID={orderid} and a.FName='S2F41stop'";
  550. List<OrderDetail> orderdetails = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  551. return 1;
  552. }
  553. catch(Exception ex)
  554. {
  555. errorinfo = ex.Message.ToString();
  556. return -1;
  557. }
  558. }
  559. }
  560. }