using MoviconWebApi.Entities; namespace MoviconWebApi.API.ClearDataQrApi { /// /// 清洗数据二维码查询数据访问层 /// public static class Data { /// /// 根据设备编号和部件二维码查询清洗数据 /// /// 查询请求参数 /// FreeSql实例 /// 清洗数据列表 public static async Task> GetClearDataByQr( ClearDataQrRequest request, IFreeSql freeSql) { try { // 使用FreeSql的Fluent API构建查询 var query = freeSql.Select(); // 添加设备编号条件 if (!string.IsNullOrWhiteSpace(request.DeviceCode)) { query = query.Where(x => x.DeviceCode == request.DeviceCode); } // 添加部件二维码条件 if (!string.IsNullOrWhiteSpace(request.PartQRCode)) { query = query.Where(x => x.part_qrid == request.PartQRCode); } // 按创建时间降序排序 query = query.OrderByDescending(x => x.CreateTime); // 执行查询并映射到响应模型 var result = await query.ToListAsync(x => new ClearDataQrResponse { DeviceCode = x.DeviceCode, DeviceName = x.DeviceName, program_process = x.program_process, vehicle_model = x.vehicle_model, locomotive_number = x.locomotive_number, repair_process = x.repair_process, component_name = x.component_name, part_position = x.part_position, part_num = x.part_num, part_qrid = x.part_qrid, AgentTank_Level = x.AgentTank_Level, Test_CleaningAgentTankAdd = x.Test_CleaningAgentTankAdd, Test_CleaningAgentTankHeat = x.Test_CleaningAgentTankHeat, Test_FrameworkPerModelCleaningAgentUsage = x.Test_FrameworkPerModelCleaningAgentUsage, Test_FrameworkPerModelWaterUsage = x.Test_FrameworkPerModelWaterUsage, WaterTank_Temp = x.WaterTank_Temp, AgentTank_Temp = x.AgentTank_Temp, SoakingTank1_Temp = x.SoakingTank1_Temp, SoakingTank2_Temp = x.SoakingTank2_Temp, Test_ElectricSurveillance = x.Test_ElectricSurveillance, Test_FrameworkPerModelCleaningDuration = x.Test_FrameworkPerModelCleaningDuration, Test_FrameworkProgramProcess = x.Test_FrameworkProgramProcess, Test_FrameworkProgramProcessPercentage = x.Test_FrameworkProgramProcessPercentage, Test_SteamSurveillance = x.Test_SteamSurveillance, Test_WaterTankAdd = x.Test_WaterTankAdd, Test_WaterTankHeat = x.Test_WaterTankHeat, WaterTank_Level = x.WaterTank_Level, CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") }); return result; } catch (Exception ex) { // 记录异常(在生产环境中应使用适当的日志框架) Console.WriteLine($"查询清洗数据时出错: {ex.Message}"); throw; } } /// /// 分页查询清洗数据 /// /// 分页查询请求参数 /// FreeSql实例 /// 分页数据和总数 public static async Task<(List Data, long Total)> GetClearDataPagedByQr( ClearDataQrPagedRequest request, IFreeSql freeSql) { try { // 使用FreeSql的Fluent API构建查询 var query = freeSql.Select(); // 添加设备编号条件 if (!string.IsNullOrWhiteSpace(request.DeviceCode)) { query = query.Where(x => x.DeviceCode == request.DeviceCode); } // 添加部件二维码条件 if (!string.IsNullOrWhiteSpace(request.PartQRCode)) { query = query.Where(x => x.part_qrid == request.PartQRCode); } // 按创建时间降序排序 query = query.OrderByDescending(x => x.CreateTime); // 获取总数 var total = await query.CountAsync(); // 分页查询并映射到响应模型 var data = await query .Page(request.PageIndex, request.PageSize) .ToListAsync(x => new ClearDataQrResponse { DeviceCode = x.DeviceCode, DeviceName = x.DeviceName, program_process = x.program_process, vehicle_model = x.vehicle_model, locomotive_number = x.locomotive_number, repair_process = x.repair_process, component_name = x.component_name, part_position = x.part_position, part_num = x.part_num, part_qrid = x.part_qrid, AgentTank_Level = x.AgentTank_Level, Test_CleaningAgentTankAdd = x.Test_CleaningAgentTankAdd, Test_CleaningAgentTankHeat = x.Test_CleaningAgentTankHeat, Test_FrameworkPerModelCleaningAgentUsage = x.Test_FrameworkPerModelCleaningAgentUsage, Test_FrameworkPerModelWaterUsage = x.Test_FrameworkPerModelWaterUsage, WaterTank_Temp = x.WaterTank_Temp, AgentTank_Temp = x.AgentTank_Temp, SoakingTank1_Temp = x.SoakingTank1_Temp, SoakingTank2_Temp = x.SoakingTank2_Temp, Test_ElectricSurveillance = x.Test_ElectricSurveillance, Test_FrameworkPerModelCleaningDuration = x.Test_FrameworkPerModelCleaningDuration, Test_FrameworkProgramProcess = x.Test_FrameworkProgramProcess, Test_FrameworkProgramProcessPercentage = x.Test_FrameworkProgramProcessPercentage, Test_SteamSurveillance = x.Test_SteamSurveillance, Test_WaterTankAdd = x.Test_WaterTankAdd, Test_WaterTankHeat = x.Test_WaterTankHeat, WaterTank_Level = x.WaterTank_Level, CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") }); return (data, total); } catch (Exception ex) { // 记录异常(在生产环境中应使用适当的日志框架) Console.WriteLine($"分页查询清洗数据时出错: {ex.Message}"); throw; } } /// /// 获取最新的清洗数据记录 /// /// 查询请求(可包含设备编号筛选) /// FreeSql实例 /// 最新的清洗数据记录,如果没有则返回null public static async Task GetLatestClearData( ClearDataQrRequest request, IFreeSql freeSql) { try { // 使用FreeSql的Fluent API构建查询 var query = freeSql.Select(); // 添加设备编号条件(如果提供) if (!string.IsNullOrWhiteSpace(request.DeviceCode)) { query = query.Where(x => x.DeviceCode == request.DeviceCode); } // 添加部件二维码条件(如果提供) if (!string.IsNullOrWhiteSpace(request.PartQRCode)) { query = query.Where(x => x.part_qrid == request.PartQRCode); } // 按创建时间降序排序,取第一条 var result = await query .OrderByDescending(x => x.CreateTime) .Limit(1) .FirstAsync(x => new ClearDataQrResponse { DeviceCode = x.DeviceCode, DeviceName = x.DeviceName, program_process = x.program_process, vehicle_model = x.vehicle_model, locomotive_number = x.locomotive_number, repair_process = x.repair_process, component_name = x.component_name, part_position = x.part_position, part_num = x.part_num, part_qrid = x.part_qrid, AgentTank_Level = x.AgentTank_Level, Test_CleaningAgentTankAdd = x.Test_CleaningAgentTankAdd, Test_CleaningAgentTankHeat = x.Test_CleaningAgentTankHeat, Test_FrameworkPerModelCleaningAgentUsage = x.Test_FrameworkPerModelCleaningAgentUsage, Test_FrameworkPerModelWaterUsage = x.Test_FrameworkPerModelWaterUsage, WaterTank_Temp = x.WaterTank_Temp, AgentTank_Temp = x.AgentTank_Temp, SoakingTank1_Temp = x.SoakingTank1_Temp, SoakingTank2_Temp = x.SoakingTank2_Temp, Test_ElectricSurveillance = x.Test_ElectricSurveillance, Test_FrameworkPerModelCleaningDuration = x.Test_FrameworkPerModelCleaningDuration, Test_FrameworkProgramProcess = x.Test_FrameworkProgramProcess, Test_FrameworkProgramProcessPercentage = x.Test_FrameworkProgramProcessPercentage, Test_SteamSurveillance = x.Test_SteamSurveillance, Test_WaterTankAdd = x.Test_WaterTankAdd, Test_WaterTankHeat = x.Test_WaterTankHeat, WaterTank_Level = x.WaterTank_Level, CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") }); return result; } catch (Exception ex) { // 记录异常(在生产环境中应使用适当的日志框架) Console.WriteLine($"获取最新清洗数据时出错: {ex.Message}"); throw; } } /// /// 根据部件二维码获取所有相关的清洗记录 /// /// 部件二维码 /// FreeSql实例 /// 清洗数据列表 public static async Task> GetClearDataByPartQRCode( string partQRCode, IFreeSql freeSql) { try { // 使用FreeSql的Fluent API构建查询 var result = await freeSql.Select() .Where(x => x.part_qrid == partQRCode) .OrderByDescending(x => x.CreateTime) .ToListAsync(x => new ClearDataQrResponse { DeviceCode = x.DeviceCode, DeviceName = x.DeviceName, program_process = x.program_process, vehicle_model = x.vehicle_model, locomotive_number = x.locomotive_number, repair_process = x.repair_process, component_name = x.component_name, part_position = x.part_position, part_num = x.part_num, part_qrid = x.part_qrid, AgentTank_Level = x.AgentTank_Level, Test_CleaningAgentTankAdd = x.Test_CleaningAgentTankAdd, Test_CleaningAgentTankHeat = x.Test_CleaningAgentTankHeat, Test_FrameworkPerModelCleaningAgentUsage = x.Test_FrameworkPerModelCleaningAgentUsage, Test_FrameworkPerModelWaterUsage = x.Test_FrameworkPerModelWaterUsage, WaterTank_Temp = x.WaterTank_Temp, AgentTank_Temp = x.AgentTank_Temp, SoakingTank1_Temp = x.SoakingTank1_Temp, SoakingTank2_Temp = x.SoakingTank2_Temp, Test_ElectricSurveillance = x.Test_ElectricSurveillance, Test_FrameworkPerModelCleaningDuration = x.Test_FrameworkPerModelCleaningDuration, Test_FrameworkProgramProcess = x.Test_FrameworkProgramProcess, Test_FrameworkProgramProcessPercentage = x.Test_FrameworkProgramProcessPercentage, Test_SteamSurveillance = x.Test_SteamSurveillance, Test_WaterTankAdd = x.Test_WaterTankAdd, Test_WaterTankHeat = x.Test_WaterTankHeat, WaterTank_Level = x.WaterTank_Level, CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") }); return result; } catch (Exception ex) { // 记录异常(在生产环境中应使用适当的日志框架) Console.WriteLine($"根据二维码查询清洗数据时出错: {ex.Message}"); throw; } } } }