using System.Data; using System.Linq.Expressions; using AutoMapper; using Entity.Api.Req; using Entity.DbModel.Station; using Entity.Dto; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Utils; using Magicodes.ExporterAndImporter.Excel; using Mapster; using Microsoft.AspNetCore.Mvc; using OfficeOpenXml; using Repository.Station; using SqlSugar; namespace Service.Station; [Scope("SingleInstance")] public class EquipAlarmDefineService: BaseServices { EquipAlarmDefineRepository _elecPriceModelVersionRepository; public EquipAlarmDefineService(EquipAlarmDefineRepository dal) { _elecPriceModelVersionRepository = dal; BaseDal = dal; } /// /// 查询未处理报警列表 /// /// 设备类型:-1-查询全部;0-充电机;1-电表;2-水冷机;3-plc< /// 设备编码 /// public Result> QueryEquipAlarmPage(EquipAlarmDefineReq req) { int totalCount = 0; int EquipTypeCode = req.EquipTypeCode; string EquipCode = req.EquipCode; string ErrorCode = req.ErrorCode; string ErrorLevel = req.ErrorLevel; string ErrorMsg = req.ErrorMsg; string ProcessMethod = req.ProcessMethod; List list = BaseDal.Queryable() .WhereIF(EquipTypeCode != -1, it => it.EquipTypeCode == EquipTypeCode) .WhereIF(ObjUtils.IsNotNullOrWhiteSpace(EquipCode), it => it.EquipCode == EquipCode) .WhereIF(ObjUtils.IsNotNullOrWhiteSpace(ErrorLevel), it => it.ErrorLevel == ErrorLevel) .WhereIF(ObjUtils.IsNotNullOrWhiteSpace(ErrorMsg), it => it.ErrorMsg.Contains(ErrorMsg)) .WhereIF(ObjUtils.IsNotNullOrWhiteSpace(ProcessMethod), it => it.ProcessMethod.Contains(ProcessMethod)) .ToList(); // 注意:这里添加了 ToList() 来确保执行查询 PageResult result = new PageResult() { PageNum = req.PageNum, PageSize = req.PageSize, ToTal = totalCount, Rows = list }; return Result>.Success(result); } /// /// 导出 /// /// public async Task ExportEquipAlarmRecord(EquipAlarmDefineReq req) { List equipAlarmRecords = await this.QueryEquipAlarmDefineListAsync(req); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); List logExList = mapper.Map>(equipAlarmRecords); IExcelExporter excelExporter = new ExcelExporter(); var res = await excelExporter.ExportAsByteArray(logExList); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "设备报警基础信息.xlsx" }; } /// /// 新增 /// /// /// public async Task> Add([FromBody] EquipAlarmDefineReq req) { if (req.EquipCode == null) { return Result.Fail("设备类型编码不能为空"); } if (req.ErrorCode == null) { return Result.Fail("报警编码不能为空"); } bool bconvert = int.TryParse(req.ErrorLevel, out int result); if (!bconvert) { return Result.Fail("报警等级不能为空"); } if (req.ErrorMsg == null) { return Result.Fail("报警描述不能为空"); } var equipAlarmDefineTask = BaseDal.QueryByClauseAsync(u => u.EquipTypeCode == req.EquipTypeCode && u.EquipCode == req.EquipCode && u.ErrorCode == req.ErrorCode && u.ErrorLevel == req.ErrorLevel); await Task.WhenAll(equipAlarmDefineTask); var isExist = equipAlarmDefineTask.Result; if (isExist != null) { return Result.Fail("已存在同名或同编码设备信息"); } var equipInfo = req.Adapt(); var insertEquipInfoTask = BaseDal.InsertAsync(equipInfo); await Task.WhenAll(insertEquipInfoTask); var insertAsync = insertEquipInfoTask.Result; if (insertAsync.Id > 0) { return Result.Success("新增id:" + insertAsync.Id); } return Result.Fail("新增失败"); } /// /// 修改报警数据编码 /// /// /// public async Task> Modify([FromBody] EquipAlarmDefineReq req) { // 查询 EquipInfo var equipInfo = await BaseDal.QueryByClauseAsync(u => u.Id == req.Id); if (equipInfo == null) { return Result.Fail("id不存在"); } if (req.EquipCode == null) { return Result.Fail("设备类型编码不能为空"); } if (req.ErrorCode == null) { return Result.Fail("报警编码不能为空"); } if (req.ErrorLevel == null) { return Result.Fail("报警等级不能为空"); } if (req.ErrorMsg == null) { return Result.Fail("报警描述不能为空"); } //判断是否重复 var equipAlarmDefineTask = BaseDal.QueryByClauseAsync(u => u.EquipTypeCode == req.EquipTypeCode && u.EquipCode == req.EquipCode && u.ErrorCode == req.ErrorCode && u.ErrorLevel == req.ErrorLevel); await Task.WhenAll(equipAlarmDefineTask); var insertAsync = equipAlarmDefineTask.Result; if (insertAsync.Id > 0) { return Result.Success("新增id:" + insertAsync.Id); } return Result.Fail("修改失败"); } /// /// 删除 /// /// ids id列表 /// [HttpPost("DeleteByIds")] public async Task> DeleteByIds([FromBody] EquipAlarmDefineReq req) { // 查询 EquipInfo var equipInfo = await BaseDal.QueryByClauseAsync(u => u.Id == req.Id); if (equipInfo == null) { return Result.Fail("id不存在"); } // 查询 EquipNetInfo var equipNetInfo = await BaseDal.QueryByClauseAsync(u => u.EquipCode == equipInfo.EquipCode); if (equipNetInfo == null) { return Result.Fail("不存在此编码"); } var deleteEquipInfoTask = BaseDal.DeleteAsync(equipInfo); await Task.WhenAll(deleteEquipInfoTask); var deleteResult = deleteEquipInfoTask.Result; if (deleteResult) { return Result.Success("删除成功"); } return Result.Fail("删除失败"); } /// /// 文件导入 /// /// 文件路径 public string ImportDataFromExcel(string filePath) { var fileInfo = new FileInfo(filePath); using (var package = new ExcelPackage(fileInfo)) { // 操作第一个工作表 ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; int rowCount = worksheet.Dimension.End.Row; int colCount = worksheet.Dimension.End.Column; DataTable dt = new DataTable(); List lstEquipAlarmDefine = new(); var lstEquipAlarmDefinesql = BaseDal.QueryListByClause(i=>i.Id>0); // 初始化一个列表来存储第一行的所有名称 List rowNames = new List(); // 遍历第一行的所有单元格 // 注意:Excel的索引通常是从1开始的,所以第一行是1,第一列也是1 for (int col = 1; col <= worksheet.Dimension.End.Column; col++) { string cellValue = worksheet.Cells[1, col].Value?.ToString() ?? string.Empty; rowNames.Add(cellValue); } for (int col = 1; col <= colCount; col++) { dt.Columns.Add(worksheet.Cells[1, col].Value.ToString()); } // 跳过标题行,从第二行开始读取数据 for (int row = 2; row <= rowCount; row++) { DataRow dr = dt.NewRow(); for (int col = 1; col <= colCount; col++) { dr[col - 1] = worksheet.Cells[row, col].Value ?? DBNull.Value; } try { dt.Rows.Add(dr); EquipAlarmDefine define = new(); int rows = row - 2; define.EquipTypeCode = Convert.ToInt16(dt.Rows[rows]["设备类型编码"]); define.EquipTypeCode = Convert.ToInt16(dt.Rows[rows]["设备类型编码"]); define.EquipCode = dt.Rows[rows]["设备编码"] == null ? "" : dt.Rows[rows]["设备编码"].ToString(); define.ErrorCode= dt.Rows[rows]["报警编码"] == null ? "" : dt.Rows[rows]["报警编码"].ToString(); define.ErrorLevel= dt.Rows[rows]["报警编码"] == null ? "" : dt.Rows[rows]["报警编码"].ToString(); define.ErrorMsg= dt.Rows[rows]["报警等级"] == null ? "" : dt.Rows[rows]["报警等级"].ToString(); define.ProcessMethod= dt.Rows[rows]["报警描述"] == null ? "" : dt.Rows[rows]["报警描述"].ToString(); lstEquipAlarmDefine.Add(define); // 找出两个列表中的重复项 var duplicates = lstEquipAlarmDefine.Join(lstEquipAlarmDefinesql, l1 => new { l1.EquipTypeCode, l1.EquipCode, l1.ErrorCode }, l2 => new { l2.EquipTypeCode, l2.EquipCode, l2.ErrorCode }, (l1, l2) => l1) .Distinct() // 确保没有重复添加(理论上如果Join条件正确,这里不需要Distinct) .ToList(); if (duplicates.Count <= 0) { BaseDal.Insert(lstEquipAlarmDefine); } else { return "内容中有重复项"; } } catch (Exception e) { return "内容中有重复项"; } } } return "请选择导出的数据"; } /// /// 查询需要导出换电订单 /// /// /// public async Task> QueryEquipAlarmDefineListAsync(EquipAlarmDefineReq req) { Expression> where = null; ParameterExpression parameter = Expression.Parameter(typeof(EquipAlarmDefine), "u"); #region 构建动态查询树 where = queryTree(req, where, parameter); #endregion // 查询需要导出充电订单 不分页 return await _elecPriceModelVersionRepository.QueryEquipAlarmDefineList(where); } private static Expression>? queryTree(EquipAlarmDefineReq req, Expression>? where, ParameterExpression parameter) { if (req.Id!=null) { Expression> condition2Expr = u => u.Id == req.Id; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(req.EquipCode)) { Expression> condition2Expr = u => u.EquipCode == req.EquipCode; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(req.ErrorCode)) { Expression> condition2Expr = u => u.ErrorCode == req.ErrorCode; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(req.ErrorLevel)) { Expression> condition2Expr = u => u.ErrorLevel == req.ErrorLevel; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (req.ErrorMsg != null) { Expression> condition2Expr = u => u.ErrorMsg == req.ErrorMsg; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (req.ProcessMethod != null) { Expression> condition2Expr = u => u.ProcessMethod == req.ProcessMethod; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } return where; } }