|
|
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<EquipAlarmDefine>
|
|
|
{
|
|
|
EquipAlarmDefineRepository _elecPriceModelVersionRepository;
|
|
|
public EquipAlarmDefineService(EquipAlarmDefineRepository dal)
|
|
|
{
|
|
|
_elecPriceModelVersionRepository = dal;
|
|
|
BaseDal = dal;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询未处理报警列表
|
|
|
/// </summary>
|
|
|
/// <param name="typeCode">设备类型:-1-查询全部;0-充电机;1-电表;2-水冷机;3-plc<</param>
|
|
|
/// <param name="equipCode">设备编码</param>
|
|
|
/// <returns></returns>
|
|
|
public Result<PageResult<EquipAlarmDefine>> 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<EquipAlarmDefine> 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<EquipAlarmDefine> result = new PageResult<EquipAlarmDefine>()
|
|
|
{
|
|
|
PageNum = req.PageNum,
|
|
|
PageSize = req.PageSize,
|
|
|
ToTal = totalCount,
|
|
|
Rows = list
|
|
|
};
|
|
|
return Result<PageResult<EquipAlarmDefine>>.Success(result);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 导出
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public async Task<IActionResult> ExportEquipAlarmRecord(EquipAlarmDefineReq req)
|
|
|
{
|
|
|
List<EquipAlarmDefine> equipAlarmRecords = await this.QueryEquipAlarmDefineListAsync(req);
|
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
{
|
|
|
cfg.CreateMap<EquipAlarmDefineDto, EquipAlarmDefine>().ReverseMap();
|
|
|
});
|
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
|
List<EquipAlarmDefineDto> logExList = mapper.Map<List<EquipAlarmDefineDto>>(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" };
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 新增
|
|
|
/// </summary>
|
|
|
/// <param name="req"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<Result<string>> Add([FromBody] EquipAlarmDefineReq req)
|
|
|
{
|
|
|
if (req.EquipCode == null)
|
|
|
{
|
|
|
return Result<string>.Fail("设备类型编码不能为空");
|
|
|
}
|
|
|
if (req.ErrorCode == null)
|
|
|
{
|
|
|
return Result<string>.Fail("报警编码不能为空");
|
|
|
}
|
|
|
|
|
|
bool bconvert = int.TryParse(req.ErrorLevel, out int result);
|
|
|
if (!bconvert)
|
|
|
{
|
|
|
return Result<string>.Fail("报警等级不能为空");
|
|
|
}
|
|
|
if (req.ErrorMsg == null)
|
|
|
{
|
|
|
return Result<string>.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<string>.Fail("已存在同名或同编码设备信息");
|
|
|
}
|
|
|
|
|
|
var equipInfo = req.Adapt<EquipAlarmDefine>();
|
|
|
|
|
|
var insertEquipInfoTask = BaseDal.InsertAsync(equipInfo);
|
|
|
|
|
|
await Task.WhenAll(insertEquipInfoTask);
|
|
|
|
|
|
var insertAsync = insertEquipInfoTask.Result;
|
|
|
|
|
|
if (insertAsync.Id > 0)
|
|
|
{
|
|
|
return Result<string>.Success("新增id:" + insertAsync.Id);
|
|
|
}
|
|
|
|
|
|
return Result<string>.Fail("新增失败");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 修改报警数据编码
|
|
|
/// </summary>
|
|
|
/// <param name="req"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<Result<string>> Modify([FromBody] EquipAlarmDefineReq req)
|
|
|
{
|
|
|
// 查询 EquipInfo
|
|
|
var equipInfo = await BaseDal.QueryByClauseAsync(u => u.Id == req.Id);
|
|
|
if (equipInfo == null)
|
|
|
{
|
|
|
return Result<string>.Fail("id不存在");
|
|
|
}
|
|
|
|
|
|
if (req.EquipCode == null)
|
|
|
{
|
|
|
return Result<string>.Fail("设备类型编码不能为空");
|
|
|
}
|
|
|
if (req.ErrorCode == null)
|
|
|
{
|
|
|
return Result<string>.Fail("报警编码不能为空");
|
|
|
}
|
|
|
if (req.ErrorLevel == null)
|
|
|
{
|
|
|
return Result<string>.Fail("报警等级不能为空");
|
|
|
}
|
|
|
if (req.ErrorMsg == null)
|
|
|
{
|
|
|
return Result<string>.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<string>.Success("新增id:" + insertAsync.Id);
|
|
|
}
|
|
|
|
|
|
return Result<string>.Fail("修改失败");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除
|
|
|
/// </summary>
|
|
|
/// <param name="ids">ids id列表</param>
|
|
|
/// <returns></returns>
|
|
|
[HttpPost("DeleteByIds")]
|
|
|
public async Task<Result<string>> DeleteByIds([FromBody] EquipAlarmDefineReq req)
|
|
|
{
|
|
|
// 查询 EquipInfo
|
|
|
var equipInfo = await BaseDal.QueryByClauseAsync(u => u.Id == req.Id);
|
|
|
if (equipInfo == null)
|
|
|
{
|
|
|
return Result<string>.Fail("id不存在");
|
|
|
}
|
|
|
|
|
|
// 查询 EquipNetInfo
|
|
|
var equipNetInfo = await BaseDal.QueryByClauseAsync(u => u.EquipCode == equipInfo.EquipCode);
|
|
|
if (equipNetInfo == null)
|
|
|
{
|
|
|
return Result<string>.Fail("不存在此编码");
|
|
|
}
|
|
|
|
|
|
var deleteEquipInfoTask = BaseDal.DeleteAsync(equipInfo);
|
|
|
|
|
|
await Task.WhenAll(deleteEquipInfoTask);
|
|
|
|
|
|
var deleteResult = deleteEquipInfoTask.Result;
|
|
|
|
|
|
if (deleteResult)
|
|
|
{
|
|
|
return Result<string>.Success("删除成功");
|
|
|
}
|
|
|
|
|
|
return Result<string>.Fail("删除失败");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 文件导入
|
|
|
/// </summary>
|
|
|
/// <param name="filePath">文件路径</param>
|
|
|
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<EquipAlarmDefine> lstEquipAlarmDefine = new();
|
|
|
var lstEquipAlarmDefinesql = BaseDal.QueryListByClause(i=>i.Id>0);
|
|
|
// 初始化一个列表来存储第一行的所有名称
|
|
|
List<string> rowNames = new List<string>();
|
|
|
// 遍历第一行的所有单元格
|
|
|
// 注意: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 "请选择导出的数据";
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询需要导出换电订单
|
|
|
/// </summary>
|
|
|
/// <param name="swapOrder"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<List<EquipAlarmDefine>> QueryEquipAlarmDefineListAsync(EquipAlarmDefineReq req)
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> where = null;
|
|
|
ParameterExpression parameter = Expression.Parameter(typeof(EquipAlarmDefine), "u");
|
|
|
|
|
|
#region 构建动态查询树
|
|
|
|
|
|
where = queryTree(req, where, parameter);
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
// 查询需要导出充电订单 不分页
|
|
|
return await _elecPriceModelVersionRepository.QueryEquipAlarmDefineList(where);
|
|
|
}
|
|
|
|
|
|
private static Expression<Func<EquipAlarmDefine, bool>>? queryTree(EquipAlarmDefineReq req,
|
|
|
Expression<Func<EquipAlarmDefine, bool>>? where, ParameterExpression parameter)
|
|
|
{
|
|
|
if (req.Id!=null)
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.Id == req.Id;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
|
|
|
parameter);
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrEmpty(req.EquipCode))
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.EquipCode == req.EquipCode;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
|
|
|
parameter);
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrEmpty(req.ErrorCode))
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.ErrorCode == req.ErrorCode;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
|
|
|
parameter);
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrEmpty(req.ErrorLevel))
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.ErrorLevel == req.ErrorLevel;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
|
|
|
parameter);
|
|
|
}
|
|
|
|
|
|
if (req.ErrorMsg != null)
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.ErrorMsg == req.ErrorMsg;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
|
|
|
}
|
|
|
|
|
|
if (req.ProcessMethod != null)
|
|
|
{
|
|
|
Expression<Func<EquipAlarmDefine, bool>> condition2Expr = u => u.ProcessMethod == req.ProcessMethod;
|
|
|
where = where == null
|
|
|
? condition2Expr
|
|
|
: Expression.Lambda<Func<EquipAlarmDefine, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
|
|
|
parameter);
|
|
|
}
|
|
|
|
|
|
return where;
|
|
|
}
|
|
|
} |