NozzleService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using EasyTemplate.Tool.Entity.App;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection.PortableExecutable;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using EasyTemplate.Tool.Entity.App;
  10. using EasyTemplate.Tool;
  11. using EasyTemplate.Tool.Entity;
  12. namespace EasyTemplate.Service
  13. {
  14. public class NozzleService
  15. {
  16. private readonly SqlSugarRepository<TEngine> _engine;
  17. private readonly SqlSugarRepository<TBoard> _board;
  18. private readonly SqlSugarRepository<TNozzle> _nozzle;
  19. private readonly SqlSugarRepository<TRecord> _record;
  20. private readonly SqlSugarRepository<TPrewarning> _prewarning;
  21. public NozzleService(SqlSugarRepository<TEngine> engine, SqlSugarRepository<TBoard> board, SqlSugarRepository<TNozzle> nozzle,
  22. SqlSugarRepository<TRecord> record, SqlSugarRepository<TPrewarning> prewarning)
  23. {
  24. _engine = engine;
  25. _board = board;
  26. _nozzle = nozzle;
  27. _record = record;
  28. _prewarning = prewarning;
  29. }
  30. // Engine操作
  31. public async Task<List<TEngine>> GetEnginesAsync()
  32. {
  33. return await _engine.AsQueryable().ToListAsync();
  34. }
  35. public async Task<TEngine> GetEngineByIdAsync(int id)
  36. {
  37. return await _engine.AsQueryable()
  38. .Where(m => m.EngineId == id)
  39. .FirstAsync();
  40. }
  41. public async Task<bool> CreateEngineAsync(TEngine engine)
  42. {
  43. return await _engine.InsertAsync(engine);
  44. }
  45. public async Task<bool> UpdateEngineAsync(TEngine engine)
  46. {
  47. return await _engine.UpdateAsync(engine) == true;
  48. }
  49. public async Task<bool> DeleteEngineAsync(int id)
  50. {
  51. return await _engine.DeleteByIdAsync(id) == true;
  52. }
  53. // Board操作
  54. public async Task<List<TBoard>> GetBoardsAsync()
  55. {
  56. return await _board.AsQueryable().OrderBy(b => b.EngineId).ToListAsync();
  57. }
  58. public async Task<List<TBoard>> GetboardsByEngineAsync(int engineId)
  59. {
  60. return await _board.AsQueryable()
  61. .Where(m => m.EngineId == engineId)
  62. .ToListAsync();
  63. }
  64. public async Task<TBoard> GetBoardByIdAsync(int id)
  65. {
  66. return await _board.AsQueryable()
  67. .Where(m => m.BoardId == id)
  68. .FirstAsync();
  69. }
  70. public async Task<bool> CreateBoardAsync(TBoard board)
  71. {
  72. return await _board.InsertAsync(board);
  73. }
  74. public async Task<bool> UpdateBoardAsync(TBoard board)
  75. {
  76. return await _board.UpdateAsync(board) == true;
  77. }
  78. public async Task<bool> DeleteBoardAsync(int id)
  79. {
  80. return await _board.DeleteByIdAsync(id) == true;
  81. }
  82. // Nozzle操作
  83. public async Task<List<TNozzle>> GetNozzlesAsync()
  84. {
  85. return await _nozzle.AsQueryable().ToListAsync();
  86. }
  87. public async Task<List<TNozzle>> GetNozzlesByBoardAsync(int boardId)
  88. {
  89. return await _nozzle.AsQueryable()
  90. .Where(n => n.BoardId == boardId)
  91. .ToListAsync();
  92. }
  93. public async Task<TNozzle> GetNozzleByIdAsync(int id)
  94. {
  95. return await _nozzle.AsQueryable()
  96. .Where(n => n.NozzleId == id)
  97. .FirstAsync();
  98. }
  99. public async Task<bool> CreateNozzleAsync(TNozzle nozzle)
  100. {
  101. return await _nozzle.InsertAsync(nozzle);
  102. }
  103. public async Task<bool> UpdateNozzleAsync(TNozzle nozzle)
  104. {
  105. return await _nozzle.UpdateAsync(nozzle) == true;
  106. }
  107. public async Task<bool> DeleteNozzleAsync(int id)
  108. {
  109. return await _nozzle.DeleteByIdAsync(id) == true;
  110. }
  111. // 获取完整数据结构
  112. public async Task<List<TEngine>> GetEnginesWithDetailsAsync()
  113. {
  114. var engines = await _engine.AsQueryable().ToListAsync();
  115. var boards = await _board.AsQueryable().ToListAsync();
  116. var nozzles = await _nozzle.AsQueryable().ToListAsync();
  117. foreach (var engine in engines)
  118. {
  119. engine.boards = boards
  120. .Where(m => m.EngineId == engine.EngineId)
  121. .ToList();
  122. foreach (var board in engine.boards)
  123. {
  124. board.nozzles = nozzles
  125. .Where(n => n.BoardId == board.BoardId)
  126. .ToList();
  127. }
  128. }
  129. return engines;
  130. }
  131. // ===== TRecord 操作 =====
  132. /// <summary>
  133. /// 插入交易记录,并自动处理预警告数据
  134. /// </summary>
  135. public async Task<bool> CreateRecordAsync(TRecord record)
  136. {
  137. try
  138. {
  139. // 插入记录
  140. var result = await _record.InsertAsync(record);
  141. if (result)
  142. {
  143. // 自动处理预警告数据
  144. await ProcessPrewarningAsync(record);
  145. }
  146. return result;
  147. }
  148. catch (Exception ex)
  149. {
  150. Console.WriteLine($"插入交易记录失败:{ex.Message}");
  151. return false;
  152. }
  153. }
  154. /// <summary>
  155. /// 根据条件查询交易记录
  156. /// </summary>
  157. public async Task<List<TRecord>> GetRecordsByDateAndNozzleAsync(DateTime beginDate, DateTime endDate, int nozzleId)
  158. {
  159. return await _record.AsQueryable()
  160. .Where(r => r.tmBegin >= beginDate && r.tmEnd <= endDate && r.noz == nozzleId)
  161. .ToListAsync();
  162. }
  163. /// <summary>
  164. /// 获取所有交易记录
  165. /// </summary>
  166. public async Task<List<TRecord>> GetAllRecordsAsync()
  167. {
  168. return await _record.AsQueryable().ToListAsync();
  169. }
  170. // ===== TPrewarning 操作 =====
  171. /// <summary>
  172. /// 插入预警告记录
  173. /// </summary>
  174. public async Task<bool> CreatePrewarningAsync(TPrewarning prewarning)
  175. {
  176. return await _prewarning.InsertAsync(prewarning);
  177. }
  178. /// <summary>
  179. /// 更新预警告记录
  180. /// </summary>
  181. public async Task<bool> UpdatePrewarningAsync(TPrewarning prewarning)
  182. {
  183. return await _prewarning.UpdateAsync(prewarning) == true;
  184. }
  185. /// <summary>
  186. /// 根据日期和油枪查询预警告记录
  187. /// </summary>
  188. public async Task<TPrewarning> GetPrewarningByDateAndNozzleAsync(DateOnly date, int nozzleId)
  189. {
  190. return await _prewarning.AsQueryable()
  191. .Where(p => p.date == date && p.nozzle == nozzleId)
  192. .FirstAsync();
  193. }
  194. /// <summary>
  195. /// 获取所有预警告记录
  196. /// </summary>
  197. public async Task<List<TPrewarning>> GetAllPrewarningsAsync()
  198. {
  199. return await _prewarning.AsQueryable().ToListAsync();
  200. }
  201. // ===== 私有辅助方法 =====
  202. /// <summary>
  203. /// 处理预警告数据的自动插入或更新逻辑(仿 SQL 触发器逻辑)
  204. /// </summary>
  205. private async Task ProcessPrewarningAsync(TRecord record)
  206. {
  207. // 检查是否是有效交易(液体体积 >= 1500)
  208. bool isValidTransaction = record.liquidVl >= 1500;
  209. if (!isValidTransaction)
  210. {
  211. return;
  212. }
  213. try
  214. {
  215. // 从 tmEnd 提取日期
  216. var date = DateOnly.FromDateTime(record.tmEnd);
  217. var nozzleId = record.noz;
  218. // 查询是否已存在当天的记录
  219. var existingPrewarning = await GetPrewarningByDateAndNozzleAsync(date, nozzleId);
  220. if (existingPrewarning == null)
  221. {
  222. // 不存在则插入新记录(对应 trigger 的 insert or ignore)
  223. var newPrewarning = new TPrewarning
  224. {
  225. date = date,
  226. nozzle = nozzleId,
  227. total = 1,
  228. overproof = record.overproof,
  229. overproof_alert = record.overproof_alert,
  230. overproofrate = (record.overproof > 0) ? 100 : 0,
  231. overproofrate_alert = (record.overproof_alert > 0) ? 100 : 0,
  232. overproof_2 = record.overproof_2,
  233. overproofrate_2 = (record.overproof_2 > 0) ? 100 : 0,
  234. continueoverproof = record.overproof
  235. };
  236. await CreatePrewarningAsync(newPrewarning);
  237. Console.WriteLine($"[预警告] 创建新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}");
  238. }
  239. else
  240. {
  241. // 存在则更新现有记录(对应 trigger 的 update 逻辑)
  242. existingPrewarning.total += 1;
  243. // 2. overproof: 直接累加
  244. existingPrewarning.overproof += record.overproof;
  245. // 3. overproofrate
  246. if (existingPrewarning.total > 0)
  247. {
  248. existingPrewarning.overproofrate = (100 * existingPrewarning.overproof) / existingPrewarning.total;
  249. }
  250. // 4. overproof_alert: 直接累加
  251. existingPrewarning.overproof_alert += record.overproof_alert;
  252. // 5. overproofrate_alert
  253. if (existingPrewarning.total > 0)
  254. {
  255. existingPrewarning.overproofrate_alert = (100 * existingPrewarning.overproof_alert) / existingPrewarning.total;
  256. }
  257. // 6. overproof_2: 直接累加
  258. existingPrewarning.overproof_2 += record.overproof_2;
  259. // 7. overproofrate_2
  260. if (existingPrewarning.total > 0)
  261. {
  262. existingPrewarning.overproofrate_2 = (100 * existingPrewarning.overproof_2) / existingPrewarning.total;
  263. }
  264. // 8. continueoverproof: 连续超标次数,累加当前超标值
  265. existingPrewarning.continueoverproof += record.overproof;
  266. await UpdatePrewarningAsync(existingPrewarning);
  267. Console.WriteLine($"[预警告] 更新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}, 总数:{existingPrewarning.total}");
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. Console.WriteLine($"处理预警告数据失败:{ex.Message}");
  273. // 不抛出异常,避免影响主流程
  274. }
  275. }
  276. }
  277. }