MacAreaCountMstDal.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 Machine = DllEapEntity.Machine;
  9. using DllHsms;
  10. using System.Data;
  11. using Microsoft.Extensions.Logging;
  12. using log4net;
  13. using Cksoft.Data.Repository;
  14. namespace DllEapDal
  15. {
  16. public class MacAreaCountMstDal
  17. {
  18. private IDatabase CurrDb = null;
  19. private ILog loger = null;
  20. public MacAreaCountMstDal(IDatabase db)
  21. {
  22. CurrDb = db;
  23. loger = log4net.LogManager.GetLogger(typeof(MacAreaCountMstDal));
  24. }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="mac"></param>
  29. /// <param name="eventtypeid"></param>
  30. /// <param name="ptime"></param>
  31. /// <param name="details"></param>
  32. /// <param name="usercode"></param>
  33. /// <param name="errorinfo"></param>
  34. /// <returns></returns>
  35. public int MacAreaCountMst(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string programname, string usercode, ref string errorinfo)
  36. {
  37. try
  38. {
  39. int result = 0;
  40. MacCountMst mst = new MacCountMst();
  41. //读取机台正在进行的任务
  42. string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
  43. List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
  44. if (eventtypeid == -1)
  45. {
  46. if (msts.Count > 0)
  47. {
  48. mst = msts[0];
  49. //说明存在正在进行的任务,则要先结束此任务
  50. result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
  51. if (result < 0)
  52. return -1;
  53. mst.StatusID = 1;
  54. //完成片
  55. result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
  56. if (result <= 0)
  57. {
  58. return -1;
  59. }
  60. //统计产量记录
  61. result = TotalMacCountMst(mst, usercode, ref errorinfo);
  62. if (result <= 0)
  63. return -1;
  64. }
  65. //添加主记录
  66. mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
  67. if (mst == null)
  68. return -1;
  69. //添加明细
  70. result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
  71. if (result < 0)
  72. return -1;
  73. }
  74. else
  75. {
  76. if (msts.Count <= 0)
  77. {
  78. //没有找到正在进行的记录,则添加记录,然后将记录设置为异常结束,并将产量统计全部标记为0
  79. //mst = AddMacCountMst(mac, ptime,programname, usercode, ref errorinfo);
  80. //if (mst == null)
  81. // return -1;
  82. //mst.StatusID = 2;
  83. ////添加明细
  84. //result = AddMacCountDetail(mst, 2, details, usercode, ref errorinfo);
  85. //if (result < 0)
  86. // return -1;
  87. return 1;
  88. }
  89. else
  90. {
  91. mst = msts[0];
  92. //判断开始时间与结束时间差,如果结束时间-开始时间小于1分钟,则认为这个结束事件是结束上条的,不做处理
  93. long diff = ptime.Ticks - mst.SDate.Ticks;
  94. if (diff < 10000000 * 90)
  95. return 1;
  96. mst.StatusID = 1;
  97. }
  98. //添加明细
  99. result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
  100. if (result < 0)
  101. return -1;
  102. //完成片
  103. result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
  104. if (result <= 0)
  105. {
  106. return -1;
  107. }
  108. }
  109. //统计产量记录
  110. result = TotalMacCountMst(mst, usercode, ref errorinfo);
  111. if (result <= 0)
  112. return -1;
  113. return 1;
  114. }
  115. catch (Exception ex)
  116. {
  117. errorinfo = ex.Message.ToString();
  118. return -1;
  119. }
  120. }
  121. public int MacAreaCountMst(MacCountMst pmst, MacCountDetail lastDetail, ref string errorinfo)
  122. {
  123. try
  124. {
  125. // 因为抓到产量时不会立即写入数据库,写入数据库时已经延迟了2分钟左右,因此产量异常界限也顺延3分钟左右
  126. int OfflineTime = int.Parse(AppConfigurtaionServices.Configuration["OfflineTime"]) + 3;
  127. string usercode = pmst.RecCode;
  128. int result = 0;
  129. MacCountMst mst = new MacCountMst();
  130. //读取机台正在进行的任务
  131. string condition = $" and a.StatusID=-1 and a.macid={pmst.MacID}";
  132. List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
  133. //处理 说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
  134. if (msts.Count > 1)
  135. {
  136. //说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
  137. msts = msts.OrderBy(t => t.SDate).ToList();
  138. for (int i = 1; i < msts.Count; i++)
  139. {
  140. msts[i].StatusID = 200;
  141. msts[i].Remark = "同时存在多个未结束的记录,则将这些多余的结束.";
  142. CurrDb.UpdateFor(msts[i], usercode);
  143. }
  144. }
  145. int vcount = pmst.FCount;
  146. int sameresult = 0;
  147. DateTime ptime = pmst.SDate;
  148. int statusid = pmst.StatusID;
  149. var newRecipeCount = 0;
  150. if (msts.Count > 0)
  151. {
  152. mst = msts[0];
  153. //var lastMst = CurrDb.FindListForCondition<MacCountMst>($" and a.macid={pmst.ID} order by id desc limit 0,1",
  154. // ref errorinfo).FirstOrDefault();
  155. if (vcount < 0)
  156. {
  157. //说明是断网了,这时候需要判断此条记录的耗时,如果耗时大于设定时间,则设置数量为0
  158. // int timelen = int.Parse(AppConfigurtaionServices.Configuration["OfflineTime"]);
  159. DateTime etime = mst.SDate.AddMinutes(OfflineTime);
  160. if (DateTime.Now > etime)
  161. {
  162. vcount = 0;
  163. }
  164. else
  165. {
  166. return 1;
  167. }
  168. }
  169. // 当开始片的程序和结束片的程序不一致时,将该片的产量置为0
  170. if (!string.IsNullOrEmpty(pmst.PName) && mst.PName != pmst.PName)
  171. {
  172. newRecipeCount = pmst.FCount;
  173. vcount = 0;
  174. }
  175. //判断数量是否相同,如果相同则无需处理
  176. //sameresult = JudgeIsSame(mst, vcount, ptime, ref errorinfo);
  177. //if (sameresult > 0)
  178. // return 1;
  179. //说明存在正在进行的任务,则要先结束此任务
  180. result = AddMacCountDetail(mst, 1, vcount.ToString(), statusid, usercode, ref errorinfo, newRecipeCount);
  181. if (result < 0)
  182. return -1;
  183. //mst.StatusID = 1;
  184. //mst.EDate = ptime;
  185. //CurrDb.UpdateFor<MacCountMst>(mst, usercode);
  186. //完成片
  187. result = FinishMacCountMst(mst, ptime, pmst.FCount, pmst.StatusID, usercode, ref errorinfo, newRecipeCount);
  188. if (result <= 0)
  189. {
  190. return -1;
  191. }
  192. //统计产量记录
  193. result = TotalMacCountMst(mst, usercode, ref errorinfo, newRecipeCount);
  194. if (result <= 0)
  195. return -1;
  196. }
  197. else
  198. {
  199. if (vcount < 0)
  200. {
  201. vcount = 0;
  202. }
  203. }
  204. //添加主记录
  205. mst = AddMacCountMst(pmst.MacID, ptime, pmst.PName, pmst.FCount, usercode, ref errorinfo);
  206. if (mst == null)
  207. return -1;
  208. //添加明细
  209. result = AddMacCountDetail(mst, -1, vcount.ToString(), statusid, usercode, ref errorinfo, newRecipeCount);
  210. if (result < 0)
  211. return -1;
  212. //处理一段时间内的断线数据
  213. return 1;
  214. }
  215. catch (Exception ex)
  216. {
  217. errorinfo = ex.Message.ToString();
  218. return -1;
  219. }
  220. }
  221. private void ProcessError(int macid)
  222. {
  223. try
  224. {
  225. }
  226. catch (Exception ex)
  227. {
  228. loger.Error($"处理机台断线记录发生异常,机台ID={macid},异常信息为:{ex.ToString()}");
  229. }
  230. }
  231. public int MacAreaCountMstFor(Machine mac, int eventtypeid, DateTime ptime, List<McaSecVDetail> details, string programname, string usercode, ref string errorinfo)
  232. {
  233. try
  234. {
  235. int result = 0;
  236. MacCountMst mst = new MacCountMst();
  237. //读取机台正在进行的任务
  238. string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
  239. List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
  240. if (msts.Count > 0 && msts[0].PName != programname)
  241. {
  242. //说明此机台已经更换了程序,结束上个任务,开始新的任务
  243. msts[0].StatusID = 1;
  244. CurrDb.UpdateFor<MacCountMst>(msts[0], usercode);
  245. }
  246. if (eventtypeid == -1)
  247. {
  248. if (msts.Count > 0)
  249. {
  250. mst = msts[0];
  251. //说明存在正在进行的任务,则要先结束此任务
  252. result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
  253. if (result < 0)
  254. return -1;
  255. mst.StatusID = 1;
  256. //完成片
  257. result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
  258. if (result <= 0)
  259. {
  260. return -1;
  261. }
  262. //统计产量记录
  263. result = TotalMacCountMst(mst, usercode, ref errorinfo);
  264. if (result <= 0)
  265. return -1;
  266. }
  267. //添加主记录
  268. mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
  269. if (mst == null)
  270. return -1;
  271. //添加明细
  272. result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
  273. if (result < 0)
  274. return -1;
  275. }
  276. else
  277. {
  278. if (msts.Count <= 0)
  279. {
  280. //没有找到正在进行的记录,则添加记录,然后将记录设置为异常结束,并将产量统计全部标记为0
  281. mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
  282. if (mst == null)
  283. return -1;
  284. mst.StatusID = 2;
  285. //添加明细
  286. result = AddMacCountDetail(mst, -1, details, usercode, ref errorinfo);
  287. if (result < 0)
  288. return -1;
  289. return 1;
  290. }
  291. else
  292. {
  293. mst = msts[0];
  294. //判断开始时间与结束时间差,如果结束时间-开始时间小于1分钟,则认为这个结束事件是结束上条的,不做处理
  295. long diff = ptime.Ticks - mst.SDate.Ticks;
  296. if (diff < 10000000 * 90)
  297. return 1;
  298. mst.StatusID = 1;
  299. }
  300. //添加明细
  301. result = AddMacCountDetail(mst, 1, details, usercode, ref errorinfo);
  302. if (result < 0)
  303. return -1;
  304. //完成片
  305. result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
  306. if (result <= 0)
  307. {
  308. return -1;
  309. }
  310. }
  311. //统计产量记录
  312. result = TotalMacCountMst(mst, usercode, ref errorinfo);
  313. if (result <= 0)
  314. return -1;
  315. return 1;
  316. }
  317. catch (Exception ex)
  318. {
  319. errorinfo = ex.Message.ToString();
  320. return -1;
  321. }
  322. }
  323. public int MacAreaCountMstFor(Machine mac, DateTime ptime, string vcount, string programname, int statusid, string usercode, ref string errorinfo)
  324. {
  325. try
  326. {
  327. if (mac.FCode == "DA00004")
  328. {
  329. int aa = 100;
  330. }
  331. int result = 0;
  332. MacCountMst mst = new MacCountMst();
  333. //读取机台正在进行的任务
  334. string condition = $" and a.StatusID=-1 and a.macid={mac.ID}";
  335. List<MacCountMst> msts = CurrDb.FindListForCondition<MacCountMst>(condition, ref errorinfo).ToList();
  336. int sameresult = 0;
  337. if (msts.Count > 0)
  338. {
  339. if (msts.Count > 1)
  340. {
  341. //说明存在多个未结束的记录,先排序,结束后面的记录,只保留第一个
  342. msts = msts.OrderBy(t => t.SDate).ToList();
  343. for (int i = 1; i < msts.Count; i++)
  344. {
  345. msts[i].StatusID = 2;
  346. CurrDb.UpdateFor(msts[i], usercode);
  347. }
  348. }
  349. mst = msts[0];
  350. if (int.Parse(vcount) < 0)
  351. {
  352. //说明是断网了,这时候需要判断此条记录的耗时,如果耗时大于设定时间,则设置数量为0
  353. int timelen = int.Parse(AppConfigurtaionServices.Configuration["TimeLen"]);
  354. DateTime etime = mst.SDate.AddMinutes(timelen);
  355. if (DateTime.Now > etime)
  356. {
  357. vcount = "0";
  358. }
  359. else
  360. {
  361. return 1;
  362. }
  363. }
  364. //判断数量是否相同,如果相同则无需处理
  365. sameresult = JudgeIsSame(mst, int.Parse(vcount), ptime, ref errorinfo);
  366. if (sameresult > 0)
  367. return 1;
  368. //说明存在正在进行的任务,则要先结束此任务
  369. result = AddMacCountDetail(mst, 1, vcount, statusid, usercode, ref errorinfo);
  370. if (result < 0)
  371. return -1;
  372. //mst.StatusID = 1;
  373. //mst.EDate = ptime;
  374. //CurrDb.UpdateFor<MacCountMst>(mst, usercode);
  375. //完成片
  376. result = FinishMacCountMst(mst, mac, ptime, usercode, ref errorinfo);
  377. if (result <= 0)
  378. {
  379. return -1;
  380. }
  381. //统计产量记录
  382. result = TotalMacCountMst(mst, usercode, ref errorinfo);
  383. if (result <= 0)
  384. return -1;
  385. }
  386. else
  387. {
  388. if (int.Parse(vcount) < 0)
  389. {
  390. vcount = "0";
  391. }
  392. //condition = $@" and a.id=(
  393. // SELECT max(a.id) FROM maccountdetail as a
  394. // inner join maccountmst as b on a.MstID = b.id
  395. // where b.macid = {mac.ID} and a.TypeID != 0)";
  396. //MacCountDetail lastdetail = CurrDb.FindListForCondition<MacCountDetail>(condition, ref errorinfo).FirstOrDefault();
  397. //if (lastdetail != null && lastdetail.FCount == int.Parse(vcount))
  398. // sameresult = -100;
  399. }
  400. //添加主记录
  401. mst = AddMacCountMst(mac, ptime, programname, usercode, ref errorinfo);
  402. if (mst == null)
  403. return -1;
  404. //添加明细
  405. result = AddMacCountDetail(mst, -1, vcount, statusid, usercode, ref errorinfo);
  406. if (result < 0)
  407. return -1;
  408. //
  409. //if (sameresult == -100)
  410. // return 100;//说明产量相同
  411. return 1;
  412. }
  413. catch (Exception ex)
  414. {
  415. errorinfo = ex.Message.ToString();
  416. return -1;
  417. }
  418. }
  419. private int JudgeIsSame(MacCountMst mst, int vcount, DateTime ptime, ref string errorinfo)
  420. {
  421. DateTime etime = DateTime.Now;
  422. MacCountDetail detail = CurrDb.FindListForCondition<MacCountDetail>($" and a.mstid={mst.ID}", ref errorinfo).FirstOrDefault();
  423. //超过3分钟则统计
  424. int timelen = int.Parse(AppConfigurtaionServices.Configuration["TimeLen"]);
  425. etime = mst.SDate.AddMinutes(timelen);
  426. //if (ptime > etime&& detail.FCount == vcount)
  427. // return -100;
  428. if (ptime > etime)
  429. return -1;
  430. //定义班次结束时间
  431. string currdate = mst.SDate.ToString("yyyy-MM-dd");
  432. if (mst.SDate < DateTime.Parse($"{currdate} 08:30:00"))
  433. etime = DateTime.Parse($"{currdate} 08:30:00");
  434. else if (mst.SDate < DateTime.Parse($"{currdate} 20:30:00"))
  435. etime = DateTime.Parse($"{currdate} 20:30:00");
  436. else
  437. etime = DateTime.Parse($"{currdate} 08:30:00").AddDays(1);
  438. if (ptime > etime)
  439. return -1;
  440. if (detail.FCount == vcount)
  441. return 1;
  442. return -1;
  443. }
  444. public int FinishMacCountMst(MacCountMst mst, DateTime ptime, int vcount, int statusid, string usercode,
  445. ref string errorinfo, int newRecipeCount = 0)
  446. {
  447. try
  448. {
  449. mst.EDate = ptime;
  450. // 当机台换了机种时 将新机种信息传入方法
  451. if (newRecipeCount == 0)
  452. {
  453. mst.ECount = vcount;
  454. mst.FCount = mst.ECount - mst.SCount;
  455. }
  456. else
  457. {
  458. mst.ECount = vcount;
  459. mst.FCount = 0;
  460. }
  461. //计算时间
  462. TimeSpan ts1 = new TimeSpan(mst.SDate.Ticks);
  463. TimeSpan ts2 = new TimeSpan(mst.EDate.Ticks);
  464. TimeSpan ts = ts1.Subtract(ts2).Duration();
  465. mst.StatusID = statusid;
  466. mst.TimeLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
  467. CurrDb.UpdateFor<MacCountMst>(mst, usercode);
  468. return 1;
  469. }
  470. catch (Exception ex)
  471. {
  472. errorinfo = ex.Message.ToString();
  473. return -1;
  474. }
  475. }
  476. public int FinishMacCountMst(MacCountMst mst, Machine mac
  477. , DateTime ptime, string usercode, ref string errorinfo)
  478. {
  479. try
  480. {
  481. mst.EDate = ptime;
  482. //从MES里获取MES标识
  483. //string mesid = GetInProductionJobbook(mac.FCode, ref errorinfo);
  484. //mst.MesID = mesid;
  485. //if (!string.IsNullOrEmpty(errorinfo))
  486. //{
  487. // mst.Remark = errorinfo;
  488. // errorinfo = "";
  489. //}
  490. //计算时间
  491. TimeSpan ts1 = new TimeSpan(mst.SDate.Ticks);
  492. TimeSpan ts2 = new TimeSpan(mst.EDate.Ticks);
  493. TimeSpan ts = ts1.Subtract(ts2).Duration();
  494. mst.StatusID = 1;
  495. mst.TimeLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
  496. CurrDb.UpdateFor<MacCountMst>(mst, usercode);
  497. return 1;
  498. }
  499. catch (Exception ex)
  500. {
  501. errorinfo = ex.Message.ToString();
  502. return -1;
  503. }
  504. }
  505. public int AddMacCountDetail(MacCountMst mst, int eventtypeid, string vcount, int statusid, string usercode, ref string errorinfo,
  506. int newRecipeCount = 0)
  507. {
  508. try
  509. {
  510. MacCountDetail entity = new MacCountDetail();
  511. entity.MstID = mst.ID;
  512. entity.ParamCode = StandardCode.SVID_FinishUnit;
  513. if (newRecipeCount == 0)
  514. {
  515. entity.FCount = int.Parse(vcount);
  516. }
  517. else
  518. {
  519. entity.FCount = newRecipeCount;
  520. }
  521. entity.TypeID = eventtypeid;
  522. entity.StatusID = statusid;
  523. CurrDb.InsertFor<MacCountDetail>(entity, usercode);
  524. return 1;
  525. }
  526. catch (Exception ex)
  527. {
  528. errorinfo = ex.Message.ToString();
  529. return -1;
  530. }
  531. }
  532. public int AddMacCountDetail(MacCountMst mst, int eventtypeid, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  533. {
  534. try
  535. {
  536. List<MacCountDetail> templist = new List<MacCountDetail>();
  537. foreach (var item in details)
  538. {
  539. MacCountDetail entity = new MacCountDetail();
  540. entity.MstID = mst.ID;
  541. entity.ParamCode = item.FCode;
  542. entity.FCount = int.Parse(item.FVal);
  543. entity.TypeID = eventtypeid;
  544. templist.Add(entity);
  545. }
  546. CurrDb.InsertFor<MacCountDetail>(templist, usercode);
  547. return 1;
  548. }
  549. catch (Exception ex)
  550. {
  551. errorinfo = ex.Message.ToString();
  552. return -1;
  553. }
  554. }
  555. public MacCountMst AddMacCountMst(int macid, DateTime ptime, string programname, int vcount, string usercode, ref string errorinfo)
  556. {
  557. try
  558. {
  559. if (string.IsNullOrEmpty(programname))
  560. {
  561. programname = GetPreProgramName(macid);
  562. }
  563. int result = 0;
  564. MacCountMst mst = new MacCountMst();
  565. //添加主记录
  566. mst.MacID = macid;
  567. mst.SDate = ptime;
  568. mst.StatusID = -1;
  569. mst.TimeLen = 0;
  570. mst.PName = programname;
  571. mst.SCount = vcount;
  572. mst.ECount = 0;
  573. mst.FCount = 0;
  574. CurrDb.InsertFor<MacCountMst>(mst, usercode);
  575. int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo);
  576. if (mstid <= 0)
  577. return null;
  578. mst = CurrDb.FindEntityFor<MacCountMst>(mstid);
  579. if (mst == null)
  580. return null;
  581. return mst;
  582. }
  583. catch (Exception ex)
  584. {
  585. errorinfo = ex.ToString();
  586. return null;
  587. }
  588. }
  589. public MacCountMst AddMacCountMst(Machine mac, DateTime ptime, string programname, string usercode, ref string errorinfo)
  590. {
  591. try
  592. {
  593. if (string.IsNullOrEmpty(programname))
  594. {
  595. programname = GetPreProgramName(mac);
  596. }
  597. int result = 0;
  598. MacCountMst mst = new MacCountMst();
  599. //添加主记录
  600. mst.MacID = mac.ID;
  601. mst.SDate = ptime;
  602. mst.StatusID = -1;
  603. mst.TimeLen = 0;
  604. mst.PName = programname;
  605. CurrDb.InsertFor<MacCountMst>(mst, usercode);
  606. int mstid = DbHelper.GetMysqlMaxID(CurrDb, ref errorinfo);
  607. if (mstid <= 0)
  608. return null;
  609. mst = CurrDb.FindEntityFor<MacCountMst>(mstid);
  610. if (mst == null)
  611. return null;
  612. return mst;
  613. }
  614. catch (Exception ex)
  615. {
  616. errorinfo = ex.ToString();
  617. return null;
  618. }
  619. }
  620. private string GetPreProgramName(int macid)
  621. {
  622. string sqlstr = $@"SELECT a.PName FROM eap.maccountmst as a
  623. where a.MacID = {macid} and a.PName is not null
  624. order by sdate desc
  625. limit 1";
  626. object pname = CurrDb.FindObject(sqlstr);
  627. return pname == null ? "" : pname.ToString();
  628. }
  629. private string GetPreProgramName(Machine mac)
  630. {
  631. string sqlstr = $@"SELECT a.PName FROM eap.maccountmst as a
  632. where a.MacID = {mac.ID} and a.PName is not null
  633. order by sdate desc
  634. limit 1";
  635. object pname = CurrDb.FindObject(sqlstr);
  636. return pname == null ? "" : pname.ToString();
  637. }
  638. //统计单条产量
  639. public int TotalMacCountMst(MacCountMst mst, string usercode, ref string errorinfo,
  640. int newRecipeCount = 0)
  641. {
  642. try
  643. {
  644. //删除现有统计记录
  645. string sqlstr = $"delete from maccountdetail where mstid={mst.ID} and typeid=0";
  646. CurrDb.ExecuteBySql(sqlstr);
  647. int result = 0;
  648. sqlstr = $@"SELECT a.ParamCode,sum(a.TypeID*a.FCount) fcount FROM maccountdetail a
  649. where a.MstID = {mst.ID} and a.TypeID <> 0
  650. group by a.ParamCode";
  651. List<MacCountDetail> details = CurrDb.FindList<MacCountDetail>(sqlstr).ToList();
  652. foreach (var item in details)
  653. {
  654. item.MstID = mst.ID;
  655. item.TypeID = 0;
  656. item.EntityStatusID = 1;
  657. if (item.FCount < 0)
  658. {
  659. item.FCount = 0;
  660. item.StatusID = MacStatusVal.Error;
  661. }
  662. else
  663. {
  664. result = AccountMacCountDetail(mst, item, ref errorinfo, newRecipeCount);
  665. if (result <= 0)
  666. return -1;
  667. }
  668. }
  669. CurrDb.InsertFor<MacCountDetail>(details, usercode);
  670. return 1;
  671. }
  672. catch (Exception ex)
  673. {
  674. errorinfo = ex.Message.ToString();
  675. return -1;
  676. }
  677. }
  678. //计算产量
  679. private int AccountMacCountDetail(MacCountMst mst, MacCountDetail detail, ref string errorinfo,
  680. int newRecipeCount = 0)
  681. {
  682. try
  683. {
  684. if (newRecipeCount > 0)
  685. {
  686. detail.FCount = 0;
  687. return 1;
  688. }
  689. //读取非0的产量
  690. string condition = $" and a.MstID={mst.ID} and a.TypeID <> 0";
  691. List<MacCountDetail> details = CurrDb.FindListForCondition<MacCountDetail>(condition, ref errorinfo).ToList();
  692. if (details.Where(t => t.StatusID == 1).Count() > 0)
  693. {
  694. //说明存在断网情况,这时则把产量置为0
  695. detail.FCount = 0;
  696. detail.StatusID = MacStatusVal.Disconn;
  697. return 1;
  698. }
  699. //如果是烧机模式,则产量=0
  700. if (details.Where(t => t.StatusID == MacStatusVal.Setup).Count() > 0)
  701. {
  702. //说明存在断网情况,这时则把产量置为0
  703. detail.FCount = 0;
  704. detail.StatusID = MacStatusVal.Setup;
  705. return 1;
  706. }
  707. if (details.Where(t => t.FCount <= 0).Count() > 0)
  708. {
  709. //说明存在小于0的记录,这样的记录标记为不正常状态
  710. detail.StatusID = MacStatusVal.Pause;
  711. }
  712. else
  713. {
  714. //虽然开始和结束都有数据,但数据没变,则认为是闲置状态
  715. if (detail.FCount <= 0)
  716. {
  717. detail.StatusID = MacStatusVal.Idle;
  718. return 1;
  719. }
  720. //说明开始与结束都有数据,将这样的记录标记为运行状态
  721. detail.StatusID = MacStatusVal.Run;
  722. return 1;
  723. }
  724. if (detail.FCount <= 0)
  725. return 1;
  726. //读取最近的10条正常的产量做参考
  727. //string sqlstr = $@"select sum(t.TimeLen) TimeLen,sum(t.FCount) FCount from (
  728. // SELECT b.TimeLen,a.FCount FROM eap.maccountdetail as a
  729. // inner join maccountmst as b on a.MstID=b.id
  730. // where a.TypeID=0 and a.StatusID={MacStatusVal.Run} and b.MacID={mst.MacID} and b.PName='{mst.PName}'
  731. // order by b.SDate desc
  732. // limit 10) as t";
  733. string sqlstr = $@"SELECT b.TimeLen,a.FCount FROM eap.maccountdetail as a
  734. inner join maccountmst as b on a.MstID=b.id
  735. where a.TypeID=0 and a.StatusID={MacStatusVal.Run} and b.MacID={mst.MacID} and b.PName='{mst.PName}'
  736. order by a.FCount desc
  737. limit 10";
  738. DataTable tempdt = CurrDb.FindTable(sqlstr);
  739. if (tempdt.Rows.Count <= 0)
  740. return 1;
  741. if (tempdt.Rows[0]["TimeLen"].ToString() == "" || tempdt.Rows[0]["FCount"].ToString() == "")
  742. {
  743. if (detail.FCount > 200)
  744. {
  745. detail.FCount = 0;
  746. detail.StatusID = MacStatusVal.Error;
  747. }
  748. return 1;
  749. }
  750. int timelen = int.Parse(tempdt.Rows[0]["TimeLen"].ToString());
  751. int FCount = int.Parse(tempdt.Rows[0]["FCount"].ToString());
  752. int factcount = FCount * mst.TimeLen / timelen;
  753. if (detail.FCount > factcount * 2)
  754. {
  755. detail.FCount = 0;
  756. }
  757. return 1;
  758. }
  759. catch (Exception ex)
  760. {
  761. errorinfo = ex.ToString();
  762. return -1;
  763. }
  764. }
  765. public int AccountMacAreaCountMst(int mstid, ref string errorinfo)
  766. {
  767. try
  768. {
  769. string sqlstr = $@"update macareacountdetail
  770. set FCount = (SELECT sum(EventTypeID * FCount) FROM MacCount02 where preid = macareacountdetail.id and ParamCode = macareacountdetail.ParamCode)
  771. where preid = {mstid} and id> 0";
  772. CurrDb.ExecuteBySql(sqlstr);
  773. return 1;
  774. }
  775. catch (Exception ex)
  776. {
  777. errorinfo = ex.Message.ToString();
  778. return -1;
  779. }
  780. }
  781. public MacAreaCountMst AddMacAreaCountMst(int eventtypeid, Machine mac, int statusid, int isrightid, DateTime ptime, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  782. {
  783. try
  784. {
  785. //添加主档
  786. MacAreaCountMst mst = AddMacAreaCountMst(mac.ID, statusid, isrightid, ptime, usercode, ref errorinfo);
  787. if (mst == null)
  788. return null;
  789. //添加明细
  790. int result = AddMacAreaCountDetail(mst, details, usercode, ref errorinfo);
  791. if (result < 0)
  792. return null;
  793. //读取明细
  794. List<MacAreaCountDetail> templist = CurrDb.FindListForCondition<MacAreaCountDetail>($" and a.preid={mst.ID}", ref errorinfo).ToList();
  795. //添加原始数据记录
  796. result = AddMacCount02(mac.ID, eventtypeid, ptime, templist, details, usercode, ref errorinfo);
  797. if (result < 0)
  798. return null;
  799. return mst;
  800. }
  801. catch (Exception ex)
  802. {
  803. errorinfo = ex.Message.ToString();
  804. return null;
  805. }
  806. }
  807. public int AddMacCount02(int macid, int eventtypeid, DateTime ptime, List<MacAreaCountDetail> areadetails, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  808. {
  809. try
  810. {
  811. List<MacCount02> templist = new List<MacCount02>();
  812. List<MacAreaCountDetail> areatemp = null;
  813. MacAreaCountDetail tempentity = null;
  814. foreach (var item in details)
  815. {
  816. areatemp = areadetails.Where(t => t.ParamCode == item.FCode).ToList();
  817. if (areatemp.Count <= 0)
  818. {
  819. tempentity = AddMacAreaCountDetail(areadetails[0].PreID, item.FCode, usercode, ref errorinfo);
  820. if (tempentity == null)
  821. return -1;
  822. }
  823. else
  824. {
  825. tempentity = areatemp[0];
  826. }
  827. MacCount02 entity = new MacCount02();
  828. entity.PreID = tempentity.ID;
  829. entity.ParamCode = item.FCode;
  830. entity.EventTypeID = eventtypeid;
  831. entity.MacID = macid;
  832. entity.FCount = int.Parse(item.FVal);
  833. entity.FDate = ptime;
  834. entity.RecTypeID = 1;
  835. templist.Add(entity);
  836. }
  837. return CurrDb.InsertFor<MacCount02>(templist, usercode);
  838. }
  839. catch (Exception ex)
  840. {
  841. errorinfo = ex.Message.ToString();
  842. return -1;
  843. }
  844. }
  845. public int AddMacAreaCountDetail(MacAreaCountMst mst, List<McaSecVDetail> details, string usercode, ref string errorinfo)
  846. {
  847. try
  848. {
  849. MacAreaCountDetail tempentity = null;
  850. foreach (var item in details)
  851. {
  852. tempentity = AddMacAreaCountDetail(mst.ID, item.FCode, usercode, ref errorinfo);
  853. if (tempentity == null)
  854. return -1;
  855. }
  856. return 1;
  857. }
  858. catch (Exception ex)
  859. {
  860. errorinfo = ex.Message.ToString();
  861. return -1;
  862. }
  863. }
  864. public MacAreaCountDetail AddMacAreaCountDetail(int preid, string paramcode, string usercode, ref string errorinfo)
  865. {
  866. try
  867. {
  868. MacAreaCountDetail entity = new MacAreaCountDetail();
  869. entity.PreID = preid;
  870. entity.ParamCode = paramcode;
  871. entity.FCount = 0;
  872. CurrDb.InsertFor<MacAreaCountDetail>(entity, usercode);
  873. object objid = CurrDb.FindObject("select @@IDENTITY");
  874. if (objid.ToString() == "")
  875. {
  876. return null;
  877. }
  878. int id = int.Parse(objid.ToString());
  879. entity = CurrDb.FindEntityFor<MacAreaCountDetail>(id);
  880. return entity;
  881. }
  882. catch (Exception ex)
  883. {
  884. errorinfo = ex.Message.ToString();
  885. return null;
  886. }
  887. }
  888. public MacAreaCountMst AddMacAreaCountMst(int macid, int statusid, int isrightid, DateTime ptime, string usercode, ref string errorinfo)
  889. {
  890. try
  891. {
  892. MacAreaCountMst mst = new MacAreaCountMst();
  893. mst.MacID = macid;
  894. mst.StartTime = ptime;
  895. mst.StatusID = statusid;
  896. mst.IsRightID = isrightid;
  897. CurrDb.InsertFor<MacAreaCountMst>(mst, usercode);
  898. object objid = CurrDb.FindObject("select @@IDENTITY");
  899. if (objid.ToString() == "")
  900. {
  901. return null;
  902. }
  903. int id = int.Parse(objid.ToString());
  904. mst = CurrDb.FindEntityFor<MacAreaCountMst>(id);
  905. return mst;
  906. }
  907. catch (Exception ex)
  908. {
  909. errorinfo = ex.Message.ToString();
  910. return null;
  911. }
  912. }
  913. public string GetInProductionJobbook(string macCode, ref string errorinfo)
  914. {
  915. IDatabase sqldb = null;
  916. try
  917. {
  918. sqldb = DbFactory.Base("sqlconn");
  919. string sql = $"select Id from JobBooking where MacCode='{macCode}' and statusid=1 and IsRight<>-1";
  920. var entity = sqldb.FindList<string>(sql).FirstOrDefault();
  921. if (string.IsNullOrEmpty(entity))
  922. {
  923. errorinfo = "该机台没有正在进行的报工";
  924. return string.Empty;
  925. }
  926. return entity;
  927. }
  928. catch (Exception ex)
  929. {
  930. errorinfo = ex.Message;
  931. return string.Empty;
  932. }
  933. finally
  934. {
  935. if (sqldb != null)
  936. {
  937. sqldb.Close();
  938. }
  939. }
  940. }
  941. }
  942. }