MacLastStatusDal.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapEntity;
  5. using DllEapEntity.Enums;
  6. using DllEapEntity.OFILM;
  7. using Microsoft.Extensions.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace DllEapDal.OFILM
  14. {
  15. public class MacLastStatusDal
  16. {
  17. private readonly IDatabase CurrDb = null;
  18. public MacLastStatusDal()
  19. {
  20. }
  21. public MacLastStatusDal(IDatabase db)
  22. {
  23. CurrDb = db;
  24. }
  25. /// <summary>
  26. /// 新增
  27. /// </summary>
  28. /// <param name="entity"></param>
  29. /// <param name="db"></param>
  30. /// <returns></returns>
  31. public async Task Insert(MacLastStatus entity, IDatabase db = null)
  32. {
  33. if (db == null)
  34. {
  35. CurrDb.InsertFor(entity, string.Empty);
  36. }
  37. else
  38. {
  39. db.InsertFor(entity, string.Empty);
  40. }
  41. await Task.CompletedTask;
  42. }
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. /// <param name="shift"></param>
  47. /// <returns></returns>
  48. public async Task<IEnumerable<MacLastStatus>> GetLastStatus(DateTime date, EnumShift shift)
  49. {
  50. string errorinfo = string.Empty;
  51. return await CurrDb.FindListForConditionAsync<MacLastStatus>($" and a.fdate='{date.ToString("yyyy-MM-dd 00:00:00")}' " +
  52. $"and a.shift={(int)shift}",
  53. ref errorinfo);
  54. }
  55. /// <summary>
  56. /// 汇总机台最后状态
  57. /// </summary>
  58. /// <returns></returns>
  59. public async Task SyncLastStatus()
  60. {
  61. var logger = AppConfigurtaionServices.MyLog;
  62. try
  63. {
  64. logger.LogError("开始计算机台最后状态");
  65. var dateNow = DateTime.Now;
  66. string errorinfo = string.Empty;
  67. var (shift, date) = GetShift(dateNow);
  68. var dateStart = Convert.ToDateTime(dateNow.ToString("yyyy-MM-dd 08:30:00"));
  69. if (shift == EnumShift.Day)
  70. {
  71. dateStart = Convert.ToDateTime(dateNow.ToString("yyyy-MM-dd 08:30:00"));
  72. }
  73. else if (shift == EnumShift.Night)
  74. {
  75. dateStart = Convert.ToDateTime(dateNow.AddDays(-1).ToString("yyyy-MM-dd 20:30:00"));
  76. }
  77. var runRates = await GetRunRates(dateStart); // 稼动率
  78. var machines = await GetAllMachines();
  79. using (IDatabase db = DbFactory.Base("eap"))
  80. {
  81. db.BeginTrans();
  82. // var
  83. var lastStatus = await db.FindListForConditionAsync<MacStatus01>(string.Empty, ref errorinfo);
  84. foreach (var item in lastStatus)
  85. {
  86. var runTime = runRates.FirstOrDefault(c => c.Name == item.MacCode);
  87. decimal runRate = 0;
  88. if (runTime != null)
  89. {
  90. runRate = (decimal)runTime.Value / (12 * 60 * 60);
  91. }
  92. var tmp = new MacLastStatus
  93. {
  94. MacCode = item.MacCode,
  95. Flen = Convert.ToInt32((dateNow - item.STime).TotalSeconds),
  96. Shift = (int)shift,
  97. StatusId = item.StatusID,
  98. RecTime = Convert.ToDateTime(dateNow.ToString("yyyy-MM-dd HH:mm:ss")),
  99. FDate = Convert.ToDateTime(date),
  100. STime = item.STime,
  101. RunRate = runRate,
  102. Remark = item.Remark?.Replace(" ", "-").Replace("_", "-").Replace("|", "-")
  103. .Replace(".", "-").Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
  104. .FirstOrDefault() ,
  105. FactoryId = machines.FirstOrDefault(c => c.FCode == item.MacCode)?.FactoryId,
  106. RegionId = machines.FirstOrDefault(c => c.FCode == item.MacCode)?.RegionId
  107. };
  108. db.InsertFor(tmp, string.Empty);
  109. }
  110. db.Commit();
  111. }
  112. logger.LogError("计算机台最后状态完成");
  113. }
  114. catch (Exception ex)
  115. {
  116. logger.LogError("计算机台最后状态错误," + ex.ToString());
  117. throw;
  118. }
  119. }
  120. /// <summary>
  121. /// 获取所有机台
  122. /// </summary>
  123. /// <returns></returns>
  124. public async Task<IEnumerable<Machine>> GetAllMachines()
  125. {
  126. using (IDatabase db = DbFactory.Base("eap"))
  127. {
  128. var sql = "select * from machine";
  129. return await db.FindListAsync<Machine>(sql, null);
  130. }
  131. }
  132. /// <summary>
  133. /// 获取机台稼动率
  134. /// </summary>
  135. /// <returns></returns>
  136. public async Task<IEnumerable<RunRateDto>> GetRunRates(DateTime dateStart)
  137. {
  138. var dateEnd = dateStart.AddHours(12);
  139. string timeFormat = "yyyy-MM-dd HH:mm:ss";
  140. using (IDatabase db = DbFactory.Base("eap"))
  141. {
  142. var sql = $@"select sum(tt.flen) as Value,tt.maccode as Name from (
  143. select sum(flen) flen,maccode from macstatus where stime>'{dateStart.ToString(timeFormat)}' and stime<'{dateEnd.ToString(timeFormat)}' and statusid=4
  144. group by maccode
  145. union all
  146. select time_to_sec(timediff('{dateEnd.ToString(timeFormat)}',stime)) as flen,maccode from macstatus where
  147. statusid=4 and etime='0001-01-01 00:00:00' and flen=0 and stime>'{dateStart.ToString(timeFormat)}' and stime<'{dateEnd.ToString(timeFormat)}'
  148. union all
  149. select time_to_sec(timediff(etime, '{dateStart.ToString(timeFormat)}')) as flen,
  150. maccode from
  151. macstatus where statusid=4 and stime<='{dateStart.ToString(timeFormat)}' and etime>'{dateStart.ToString(timeFormat)}'
  152. ) tt
  153. group by tt.maccode";
  154. return await db.FindListAsync<RunRateDto>(sql, null);
  155. }
  156. }
  157. /// <summary>
  158. /// 获取班别
  159. /// </summary>
  160. /// <param name="date"></param>
  161. /// <returns></returns>
  162. private (EnumShift, string) GetShift(DateTime date)
  163. {
  164. if (date.Hour > 9 && date.Hour < 21)
  165. {
  166. return (EnumShift.Day, date.ToString("yyyy-MM-dd"));
  167. }
  168. return (EnumShift.Night, date.AddDays(-1).ToString("yyyy-MM-dd"));
  169. }
  170. }
  171. }