TrackHelperDal.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace DllEapDal
  9. {
  10. public class TrackHelperDal
  11. {
  12. private IDatabase CurrDb = null;
  13. public TrackHelperDal(IDatabase db)
  14. {
  15. CurrDb = db;
  16. }
  17. public DateTime? GetTrackIn(Machine mac, ref string errorinfo)
  18. {
  19. try
  20. {
  21. DateTime currtime = DateTime.Now;
  22. //读入小于当前时间且不超过60秒的机台记录
  23. string condition = $@" and a.fdate>STR_TO_DATE('{currtime.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  24. and a.fdate<STR_TO_DATE('{currtime.ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  25. and a.macid={mac.ID} and a.ParamCode='{DllHsms.StandardCode.SVID_FinishUnit}'";
  26. //if (mac.FCode.ToUpper() == "WKB-055")
  27. //{
  28. // condition = $@" and a.fdate>STR_TO_DATE('{currtime.AddMinutes(-2).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  29. // and a.fdate<STR_TO_DATE('{currtime.ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  30. // and a.macid={mac.ID}";
  31. //}
  32. List<MacCount02> maccount = CurrDb.FindListForCondition<MacCount02>(condition, ref errorinfo).OrderByDescending(t => t.FDate).ToList();
  33. if (maccount.Count <= 0)//前面没有操作记录,则以当前时间为开始
  34. return DateTime.Now;
  35. if (maccount.Count > 2)
  36. {
  37. //判断最后2条是否重复,如果重复则要删除1条
  38. if (maccount[0].EventTypeID == maccount[1].EventTypeID && maccount[0].FCount == maccount[1].FCount)
  39. maccount.Remove(maccount[0]);
  40. }
  41. //如果最后条记录是开始事件,则以该条开始时间为trackin时间
  42. if (maccount[0].EventTypeID == -1)
  43. return maccount[0].FDate;
  44. if (maccount[0].EventTypeID == 1 && maccount.Count == 1)
  45. return DateTime.Now;
  46. //如果最后记录是结束事件,且结束事件前面是开始事件,则以此开始事件为trackin时间
  47. if (maccount[0].EventTypeID == 1 && maccount.Count > 1)
  48. return maccount[1].FDate;
  49. return DateTime.Now;
  50. }
  51. catch (Exception ex)
  52. {
  53. errorinfo = ex.Message.ToString();
  54. return null;
  55. }
  56. }
  57. public DateTime? GetTrackOut(Machine mac, ref string errorinfo)
  58. {
  59. try
  60. {
  61. //读入小于当前时间且不超过60秒的机台记录
  62. DateTime currtime = DateTime.Now;
  63. string condition = $@" and a.fdate>STR_TO_DATE('{currtime.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  64. and a.fdate<STR_TO_DATE('{currtime.ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s')
  65. and a.macid={mac.ID} and a.ParamCode='{DllHsms.StandardCode.SVID_FinishUnit}'";
  66. List<MacCount02> maccount = CurrDb.FindListForCondition<MacCount02>(condition, ref errorinfo).OrderByDescending(t => t.FDate).ToList();
  67. if (maccount.Count <= 0)//前面没有操作记录,则以当前时间为开始
  68. return DateTime.Now;
  69. if (maccount.Count > 2)
  70. {
  71. //判断最后2条是否重复,如果重复则要删除1条
  72. if (maccount[0].EventTypeID == maccount[1].EventTypeID && maccount[0].FCount == maccount[1].FCount)
  73. maccount.Remove(maccount[0]);
  74. }
  75. //如果最后条记录是开始事件,则以该条开始时间为trackout时间
  76. if (maccount[0].EventTypeID == -1)
  77. return maccount[0].FDate;
  78. if (maccount[0].EventTypeID == 1 && maccount.Count == 1)
  79. return maccount[0].FDate.AddSeconds(1);
  80. //如果最后记录是结束事件,且结束事件前面是开始事件,则以此开始事件为trackin时间
  81. if (maccount[0].EventTypeID == 1 && maccount.Count > 1)
  82. {
  83. var endTypes = maccount.Where(c => c.EventTypeID == -1).OrderByDescending(c => c.FDate);
  84. if (endTypes == null || endTypes.Count() == 0)
  85. {
  86. return maccount[0].FDate.AddSeconds(1);
  87. }
  88. return endTypes.ElementAt(0).FDate;
  89. }
  90. return DateTime.Now;
  91. }
  92. catch (Exception ex)
  93. {
  94. errorinfo = ex.Message.ToString();
  95. return null;
  96. }
  97. }
  98. }
  99. }