MachineChangeDal.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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
  9. {
  10. public class MachineChangeDal
  11. {
  12. private readonly IDatabase Db = null;
  13. public MachineChangeDal(IDatabase db)
  14. {
  15. Db = db;
  16. }
  17. public IEnumerable<MachineChange> Get(string factory, string plant, string floor, string pcode, string maccode, string macnum, DateTime? start, DateTime? end,
  18. out int total,
  19. int pageIndex = 1, int pageSize = 20)
  20. {
  21. var sql = "from machinechangemst as t left JOIN machine as a on t.MacID=a.id left JOIN factoryregion as b on a.RegionId=b.id inner JOIN factoryregion as c on b.ParentId=c.id inner JOIN factoryregion as d on c.ParentId=d.id inner JOIN factoryregion as e on d.ParentId=e.id inner JOIN mactprocess as f on a.id=f.MacID left JOIN machinenumber as g on a.id=g.macid left JOIN machinechangedetail as h on t.id=h.MstId and h.Type=1 LEFT JOIN machinechangedetail as j on t.id= j.MstId and j.Type=-1 left join staff s on t.RecCode=s.fcode where 1=1 ";
  22. if (!string.IsNullOrEmpty(factory))
  23. {
  24. sql += $" and e.id={factory}";
  25. }
  26. if (!string.IsNullOrEmpty(plant))
  27. {
  28. sql += $" and d.id={plant}";
  29. }
  30. if (!string.IsNullOrEmpty(floor))
  31. {
  32. sql += $" and c.id={floor}";
  33. }
  34. if (!string.IsNullOrEmpty(pcode))
  35. {
  36. sql += $" and f.PCode ='{pcode}'";
  37. }
  38. if (!string.IsNullOrEmpty(maccode))
  39. {
  40. sql += $" and a.FCode like '%{maccode}%'";
  41. }
  42. if (!string.IsNullOrEmpty(macnum))
  43. {
  44. sql += $" and g.MacNumber like'%{macnum}%'";
  45. }
  46. if (start.HasValue)
  47. {
  48. sql += $" and t.EndDate > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  49. }
  50. if (end.HasValue)
  51. {
  52. sql += $" and t.EndDate < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  53. }
  54. sql += $" AND t.EndDate!='0001-01-01 00:00:00'ORDER BY t.RecTime DESC";
  55. var ssql = "select e.FName as Factory,d.FName as Plant , c.FName as Floor ,f.pcode as PCode,a.Fcode as MacCode,g.MacNumber as MacNum,t.RecTime as STime,h.Recipe as PreRecipe,j.Recipe as CurentRecipe,t.EndDate as ETime,t.RecCode,s.fname Name " + sql;
  56. var datas = Db.FindList<MachineChange>(ssql+ $" limit {(pageIndex - 1) * pageSize},{pageSize}");
  57. var sql1 = "select count(1)" + sql;
  58. total = Convert.ToInt32(Db.FindObject(sql1));
  59. foreach (var item in datas)
  60. {
  61. item.Time = Math.Round((item.ETime - item.STime).TotalHours, 2);
  62. item.StandardTime = 12;
  63. item.ExtraTime = (item.Time - item.StandardTime) > 0 ? Math.Round((item.Time - item.StandardTime),2) : 0;
  64. item.STime = Convert.ToDateTime(item.STime.ToString("yyyy-MM-dd HH:mm:ss"));
  65. }
  66. return datas;
  67. }
  68. public int delete(string code)
  69. {
  70. //return Db.DeleteFor<MachineNumber>(code);
  71. string sql = $@"delete from machinenumber where macid=(select id from machine where fcode='{code}')";
  72. return Db.ExecuteBySql(sql);
  73. }
  74. public ChartDto CreateCategory(string factory, string plant, string floor, string maccode, string macnum, string pcode, DateTime? start, DateTime? end)
  75. {
  76. string sql = $@"SELECT a.FCode MacCode, round(time_to_sec(timediff(t.EndDate,t.Date))/3600.00,2) Time
  77. FROM
  78. machinechangemst AS t
  79. LEFT JOIN machine AS a ON t.MacID = a.id
  80. LEFT JOIN factoryregion AS b ON a.RegionId = b.id
  81. INNER JOIN factoryregion AS c ON b.ParentId = c.id
  82. INNER JOIN factoryregion AS d ON c.ParentId = d.id
  83. INNER JOIN factoryregion AS e ON d.ParentId = e.id
  84. INNER JOIN mactprocess AS f ON a.id = f.MacID
  85. LEFT JOIN machinenumber AS g ON a.id = g.macid
  86. WHERE 1 = 1 ";
  87. if (!string.IsNullOrEmpty(factory))
  88. {
  89. sql += $" and e.id={factory}";
  90. }
  91. if (!string.IsNullOrEmpty(plant))
  92. {
  93. sql += $" and d.id={plant}";
  94. }
  95. if (!string.IsNullOrEmpty(floor))
  96. {
  97. sql += $" and c.id={floor}";
  98. }
  99. if (!string.IsNullOrEmpty(pcode))
  100. {
  101. sql += $" and f.PCode = '{pcode}'";
  102. }
  103. if (!string.IsNullOrEmpty(maccode))
  104. {
  105. sql += $" and a.FCode like '%{maccode}%'";
  106. }
  107. if (!string.IsNullOrEmpty(macnum))
  108. {
  109. sql += $" and g.MacNumber like'%{macnum}%'";
  110. }
  111. if (start.HasValue)
  112. {
  113. sql += $" and t.EndDate > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  114. }
  115. if (end.HasValue)
  116. {
  117. sql += $" and t.EndDate < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  118. }
  119. sql += $"AND t.EndDate != '0001-01-01 00:00:00' ORDER BY Time desc limit 10";
  120. var data = Db.FindList<MachineChange>(sql);
  121. var chartDto = new ChartDto
  122. {
  123. text = "机台改机统计",
  124. legend = new string[] { "单次改机用时" },
  125. xdata = data.Select(c => c.MacCode).ToArray(),
  126. ydata = data.Select(c => (float)c.Time).ToArray()
  127. };
  128. return chartDto;
  129. }
  130. public void ChangeStart(MacStatus macStatus, string recipe)
  131. {
  132. MachineChangeMst mcm = new MachineChangeMst();
  133. string str = $@"Select ID from machine where FCode='{macStatus.MacCode}'";
  134. var data = Db.FindObject(str);
  135. mcm.MacId = (int)data;
  136. mcm.Date = macStatus.STime;
  137. mcm.RecCode = macStatus.RecCode;
  138. mcm.RecTime = macStatus.STime;
  139. int i = Db.InsertFor<MachineChangeMst>(mcm, mcm.RecCode);
  140. if (i > 0)
  141. {
  142. MachineChangeDetail mcd = new MachineChangeDetail();
  143. var msId = Db.FindObject($@"Select @@identity");
  144. mcd.MstId = Convert.ToInt32(msId);
  145. mcd.Type = 1;
  146. mcd.Recipe = recipe;
  147. mcd.Date = macStatus.STime;
  148. Db.InsertFor<MachineChangeDetail>(mcd, mcm.RecCode);
  149. }
  150. }
  151. public void ChangeEnd(MacStatus macStatus, string recipe)
  152. {
  153. string str = $@"SELECT StatusID from macstatus01 where MacCode='{ macStatus.MacCode}'";
  154. var data = Db.FindObject(str);
  155. if ((int)data == 204905)
  156. {
  157. string sql = $@"SELECT * from machinechangemst where Macid=(select id from machine where Fcode='{macStatus.MacCode}') and EndDate='0001-01-01 00:00:00'";
  158. List<MachineChangeMst> list = Db.FindList<MachineChangeMst>(sql).ToList();
  159. if (list == null || list.Count == 0)
  160. return;
  161. MachineChangeMst mcm = new MachineChangeMst();
  162. mcm = list[0];
  163. mcm.ModCode = macStatus.RecCode;
  164. mcm.ModTime = macStatus.STime;
  165. mcm.EndDate = macStatus.STime;
  166. MachineChangeDetail mcd = new MachineChangeDetail();
  167. mcd.MstId = mcm.Id;
  168. mcd.Type = -1;
  169. mcd.Recipe = recipe;
  170. mcd.Date = macStatus.STime;
  171. Db.InsertFor<MachineChangeDetail>(mcd, macStatus.RecCode);
  172. Db.UpdateFor<MachineChangeMst>(mcm, macStatus.RecCode);
  173. }
  174. }
  175. }
  176. }