| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using EasyTemplate.Tool.Entity.App;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection.PortableExecutable;
- using System.Text;
- using System.Threading.Tasks;
- using EasyTemplate.Tool.Entity.App;
- using EasyTemplate.Tool;
- using EasyTemplate.Tool.Entity;
- namespace EasyTemplate.Service
- {
- public class NozzleService
- {
- private readonly SqlSugarRepository<TEngine> _engine;
- private readonly SqlSugarRepository<TBoard> _board;
- private readonly SqlSugarRepository<TNozzle> _nozzle;
- private readonly SqlSugarRepository<TRecord> _record;
- private readonly SqlSugarRepository<TPrewarning> _prewarning;
- public NozzleService(SqlSugarRepository<TEngine> engine, SqlSugarRepository<TBoard> board, SqlSugarRepository<TNozzle> nozzle,
- SqlSugarRepository<TRecord> record, SqlSugarRepository<TPrewarning> prewarning)
- {
- _engine = engine;
- _board = board;
- _nozzle = nozzle;
- _record = record;
- _prewarning = prewarning;
- }
- // Engine操作
- public async Task<List<TEngine>> GetEnginesAsync()
- {
- return await _engine.AsQueryable().ToListAsync();
- }
- public async Task<TEngine> GetEngineByIdAsync(int id)
- {
- return await _engine.AsQueryable()
- .Where(m => m.EngineId == id)
- .FirstAsync();
- }
- public async Task<bool> CreateEngineAsync(TEngine engine)
- {
- return await _engine.InsertAsync(engine);
- }
- public async Task<bool> UpdateEngineAsync(TEngine engine)
- {
- return await _engine.UpdateAsync(engine) == true;
- }
- public async Task<bool> DeleteEngineAsync(int id)
- {
- return await _engine.DeleteByIdAsync(id) == true;
- }
- // Board操作
- public async Task<List<TBoard>> GetBoardsAsync()
- {
- return await _board.AsQueryable().OrderBy(b => b.EngineId).ToListAsync();
- }
- public async Task<List<TBoard>> GetboardsByEngineAsync(int engineId)
- {
- return await _board.AsQueryable()
- .Where(m => m.EngineId == engineId)
- .ToListAsync();
- }
- public async Task<TBoard> GetBoardByIdAsync(int id)
- {
- return await _board.AsQueryable()
- .Where(m => m.BoardId == id)
- .FirstAsync();
- }
- public async Task<bool> CreateBoardAsync(TBoard board)
- {
- return await _board.InsertAsync(board);
- }
- public async Task<bool> UpdateBoardAsync(TBoard board)
- {
- return await _board.UpdateAsync(board) == true;
- }
- public async Task<bool> DeleteBoardAsync(int id)
- {
- return await _board.DeleteByIdAsync(id) == true;
- }
- // Nozzle操作
- public async Task<List<TNozzle>> GetNozzlesAsync()
- {
- return await _nozzle.AsQueryable().ToListAsync();
- }
- public async Task<List<TNozzle>> GetNozzlesByBoardAsync(int boardId)
- {
- return await _nozzle.AsQueryable()
- .Where(n => n.BoardId == boardId)
- .ToListAsync();
- }
- public async Task<TNozzle> GetNozzleByIdAsync(int id)
- {
- return await _nozzle.AsQueryable()
- .Where(n => n.NozzleId == id)
- .FirstAsync();
- }
- public async Task<bool> CreateNozzleAsync(TNozzle nozzle)
- {
- return await _nozzle.InsertAsync(nozzle);
- }
- public async Task<bool> UpdateNozzleAsync(TNozzle nozzle)
- {
- return await _nozzle.UpdateAsync(nozzle) == true;
- }
- public async Task<bool> DeleteNozzleAsync(int id)
- {
- return await _nozzle.DeleteByIdAsync(id) == true;
- }
- // 获取完整数据结构
- public async Task<List<TEngine>> GetEnginesWithDetailsAsync()
- {
- var engines = await _engine.AsQueryable().ToListAsync();
- var boards = await _board.AsQueryable().ToListAsync();
- var nozzles = await _nozzle.AsQueryable().ToListAsync();
-
- foreach (var engine in engines)
- {
- engine.boards = boards
- .Where(m => m.EngineId == engine.EngineId)
- .ToList();
- foreach (var board in engine.boards)
- {
- board.nozzles = nozzles
- .Where(n => n.BoardId == board.BoardId)
- .ToList();
- }
- }
- return engines;
- }
-
- // ===== TRecord 操作 =====
-
- /// <summary>
- /// 插入交易记录,并自动处理预警告数据
- /// </summary>
- public async Task<bool> CreateRecordAsync(TRecord record)
- {
- try
- {
- // 插入记录
- var result = await _record.InsertAsync(record);
-
- if (result)
- {
- // 自动处理预警告数据
- await ProcessPrewarningAsync(record);
- }
-
- return result;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"插入交易记录失败:{ex.Message}");
- return false;
- }
- }
-
- /// <summary>
- /// 根据条件查询交易记录
- /// </summary>
- public async Task<List<TRecord>> GetRecordsByDateAndNozzleAsync(DateTime beginDate, DateTime endDate, int nozzleId)
- {
- return await _record.AsQueryable()
- .Where(r => r.tmBegin >= beginDate && r.tmEnd <= endDate && r.noz == nozzleId)
- .ToListAsync();
- }
-
- /// <summary>
- /// 获取所有交易记录
- /// </summary>
- public async Task<List<TRecord>> GetAllRecordsAsync()
- {
- return await _record.AsQueryable().ToListAsync();
- }
-
- // ===== TPrewarning 操作 =====
-
- /// <summary>
- /// 插入预警告记录
- /// </summary>
- public async Task<bool> CreatePrewarningAsync(TPrewarning prewarning)
- {
- return await _prewarning.InsertAsync(prewarning);
- }
-
- /// <summary>
- /// 更新预警告记录
- /// </summary>
- public async Task<bool> UpdatePrewarningAsync(TPrewarning prewarning)
- {
- return await _prewarning.UpdateAsync(prewarning) == true;
- }
-
- /// <summary>
- /// 根据日期和油枪查询预警告记录
- /// </summary>
- public async Task<TPrewarning> GetPrewarningByDateAndNozzleAsync(DateOnly date, int nozzleId)
- {
- return await _prewarning.AsQueryable()
- .Where(p => p.date == date && p.nozzle == nozzleId)
- .FirstAsync();
- }
-
- /// <summary>
- /// 获取所有预警告记录
- /// </summary>
- public async Task<List<TPrewarning>> GetAllPrewarningsAsync()
- {
- return await _prewarning.AsQueryable().ToListAsync();
- }
-
- // ===== 私有辅助方法 =====
-
- /// <summary>
- /// 处理预警告数据的自动插入或更新逻辑(仿 SQL 触发器逻辑)
- /// </summary>
- private async Task ProcessPrewarningAsync(TRecord record)
- {
- // 检查是否是有效交易(液体体积 >= 1500)
- bool isValidTransaction = record.liquidVl >= 1500;
- if (!isValidTransaction)
- {
- return;
- }
- try
- {
- // 从 tmEnd 提取日期
- var date = DateOnly.FromDateTime(record.tmEnd);
- var nozzleId = record.noz;
-
-
- // 查询是否已存在当天的记录
- var existingPrewarning = await GetPrewarningByDateAndNozzleAsync(date, nozzleId);
-
- if (existingPrewarning == null)
- {
- // 不存在则插入新记录(对应 trigger 的 insert or ignore)
- var newPrewarning = new TPrewarning
- {
- date = date,
- nozzle = nozzleId,
- total = 1,
- overproof = record.overproof,
- overproof_alert = record.overproof_alert,
- overproofrate = (record.overproof > 0) ? 100 : 0,
- overproofrate_alert = (record.overproof_alert > 0) ? 100 : 0,
- overproof_2 = record.overproof_2,
- overproofrate_2 = (record.overproof_2 > 0) ? 100 : 0,
- continueoverproof = record.overproof
- };
-
- await CreatePrewarningAsync(newPrewarning);
- Console.WriteLine($"[预警告] 创建新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}");
- }
- else
- {
- // 存在则更新现有记录(对应 trigger 的 update 逻辑)
-
- existingPrewarning.total += 1;
-
- // 2. overproof: 直接累加
- existingPrewarning.overproof += record.overproof;
-
- // 3. overproofrate
- if (existingPrewarning.total > 0)
- {
- existingPrewarning.overproofrate = (100 * existingPrewarning.overproof) / existingPrewarning.total;
- }
-
- // 4. overproof_alert: 直接累加
- existingPrewarning.overproof_alert += record.overproof_alert;
-
- // 5. overproofrate_alert
- if (existingPrewarning.total > 0)
- {
- existingPrewarning.overproofrate_alert = (100 * existingPrewarning.overproof_alert) / existingPrewarning.total;
- }
-
- // 6. overproof_2: 直接累加
- existingPrewarning.overproof_2 += record.overproof_2;
-
- // 7. overproofrate_2
- if (existingPrewarning.total > 0)
- {
- existingPrewarning.overproofrate_2 = (100 * existingPrewarning.overproof_2) / existingPrewarning.total;
- }
-
- // 8. continueoverproof: 连续超标次数,累加当前超标值
- existingPrewarning.continueoverproof += record.overproof;
-
- await UpdatePrewarningAsync(existingPrewarning);
- Console.WriteLine($"[预警告] 更新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}, 总数:{existingPrewarning.total}");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"处理预警告数据失败:{ex.Message}");
- // 不抛出异常,避免影响主流程
- }
- }
- }
- }
|