故障列表信息导出和导入

master
CZ 3 months ago
parent 1d2a5be261
commit aea0abc779

@ -1,3 +1,5 @@
using System.Linq.Expressions;
using Entity.Api.Req;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using SqlSugar;
@ -24,4 +26,25 @@ public class EquipAlarmDefineRepository : BaseRepository<EquipAlarmDefine>
return this.QueryByClause(it => it.EquipTypeCode == equipTypeCode &&
it.EquipCode == equipCode && it.ErrorCode == errorCode);
}
/// <summary>
/// 查询需要导出换电订单
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<List<EquipAlarmDefine>> QueryEquipAlarmDefineList(Expression<Func<EquipAlarmDefine, bool>> predicate)
{
if (predicate == null)
{
return await QueryAsync();
}
List<EquipAlarmDefine> resultList = await DbBaseClient
.Queryable<EquipAlarmDefine>()
.Where(predicate)
.WithNoLockOrNot(false)
.ToListAsync();
return resultList;
}
}

@ -1,3 +1,5 @@
using System.Data;
using System.Linq.Expressions;
using AutoMapper;
using Entity.Api.Req;
using Entity.DbModel.Station;
@ -8,6 +10,7 @@ using HybirdFrameworkCore.Utils;
using Magicodes.ExporterAndImporter.Excel;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;
using Repository.Station;
using SqlSugar;
@ -60,9 +63,9 @@ public class EquipAlarmDefineService: BaseServices<EquipAlarmDefine>
/// 导出
/// </summary>
/// <returns></returns>
public async Task<IActionResult> ExportEquipAlarmRecord()
public async Task<IActionResult> ExportEquipAlarmRecord(EquipAlarmDefineReq req)
{
List<EquipAlarmDefine> equipAlarmRecords = await BaseDal.QueryAsync();
List<EquipAlarmDefine> equipAlarmRecords = await this.QueryEquipAlarmDefineListAsync(req);
var config = new MapperConfiguration(cfg =>
{
@ -96,7 +99,9 @@ public class EquipAlarmDefineService: BaseServices<EquipAlarmDefine>
{
return Result<string>.Fail("报警编码不能为空");
}
if (req.ErrorLevel == null)
bool bconvert = int.TryParse(req.ErrorLevel, out int result);
if (!bconvert)
{
return Result<string>.Fail("报警等级不能为空");
}
@ -217,4 +222,163 @@ public class EquipAlarmDefineService: BaseServices<EquipAlarmDefine>
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;
}
}

@ -39,9 +39,9 @@ public class EquipAlarmDefineController: ControllerBase
/// <returns></returns>
[HttpPost]
[Route("ExportEquipAlarmRecord")]
public async Task<IActionResult> ExportEquipAlarmRecord()
public async Task<IActionResult> ExportEquipAlarmRecord(EquipAlarmDefineReq req)
{
return await equipAlarmDefineRepository.ExportEquipAlarmRecord();
return await equipAlarmDefineRepository.ExportEquipAlarmRecord(req);
}
@ -78,5 +78,13 @@ public class EquipAlarmDefineController: ControllerBase
return await equipAlarmDefineRepository.DeleteByIds(req);
}
/// <summary>
/// 文件导入
/// </summary>
/// <param name="filePath">文件路径</param>
[HttpPost("fileImport")]
public string ImportDataFromExcel(string filePath)
{
return equipAlarmDefineRepository.ImportDataFromExcel(filePath);
}
}

@ -1,135 +0,0 @@
using System.Data;
using Entity.Api.Req;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using log4net;
using Service.Station;
using Magicodes.ExporterAndImporter.Excel;
using OfficeOpenXml;
namespace WebStarter.Controllers.Test;
[ApiController]
[Route("api/[controller]")]
public class EquipAlarmDefineController : ControllerBase
{
private static readonly ILog Log = LogManager.GetLogger(typeof(EquipAlarmDefineController));
private readonly EquipAlarmDefineService _equipAlarmDefineService;
public EquipAlarmDefineController(EquipAlarmDefineService equipAlarmDefineService)
{
this._equipAlarmDefineService = equipAlarmDefineService;
}
/// <summary>
/// 导出
/// </summary>
/// <returns></returns>
[HttpPost("export")]
public async Task<IActionResult> ExportSwapOrder()
{
List<EquipAlarmDefine> list = await _equipAlarmDefineService.QueryAllEquipAlarm();
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "设备报警基础信息.xlsx" };
}
/// <summary>
/// 文件导入
/// </summary>
/// <param name="filePath">文件路径</param>
[HttpPost("fileImport")]
public void 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<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;
}
dt.Rows.Add(dr);
}
//TODO::数据入库
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="ids">ids id列表</param>
/// <returns></returns>
[HttpPost("DeleteByIds")]
public async Task<Result<bool>> DeleteByIds([FromBody] List<long> ids)
{
if (_equipAlarmDefineService.DeleteByIds(ids))
{
return Result<bool>.Success(true, "删除成功");
}
else
{
return Result<bool>.Fail("删除失败");
}
}
/// <summary>
/// 新增
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost("Add")]
public async Task<Result<string>> Add([FromBody] AddEquipAlarmInfoReq req)
{
return await _equipAlarmDefineService.NewAddEquipAlarm(req);
}
/// <summary>
/// 查询所有设备报警基础信息
/// </summary>
/// <param name="pageSize"></param>
/// <returns></returns>
[HttpPost("QueryPage/{pageSize}")]
public async Task<Result<PageResult<EquipAlarmDefine>>> QueryPage(int pageSize)
{
return await _equipAlarmDefineService.VagueQueryResult(pageSize, null);
}
/// <summary>
/// 模糊查询 设备报警基础信息
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost("VagueQueryPage/{pageSize}/{keyValue}")]
public async Task<Result<PageResult<EquipAlarmDefine>>> QueryPage(int pageSize, string keyValue)
{
return await _equipAlarmDefineService.VagueQueryResult(pageSize, keyValue);
}
}
Loading…
Cancel
Save