using System.Linq.Expressions;
using AutoMapper;
using Common.Const;
using Entity.Api.Req;
using Entity.DbModel.Station;
using Entity.Dto;
using Furion.LinqBuilder;
using HybirdFrameworkCore.Entity;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Repository.Station;
using Service.Station;
using SqlSugar;
namespace WebStarter.Controllers;
///
/// 换电设备报警
///
[ApiController]
[Route("api/[controller]")]
public class EquipAlarmRecordController : ControllerBase
{
private readonly EquipAlarmRecordService equipAlarmRecordService;
public readonly EquipAlarmProcessRecordRepository ProcessRecordRepository;
private readonly IStringLocalizer _localizer;
public EquipAlarmRecordController(IStringLocalizer localizer,EquipAlarmProcessRecordRepository equipAlarmProcessRecordRepository,EquipAlarmRecordService equipAlarmRecordService)
{
ProcessRecordRepository = equipAlarmProcessRecordRepository;
_localizer = localizer;
this.equipAlarmRecordService = equipAlarmRecordService;
}
///
/// 查询未处理报警列表
///
/// 设备类型:-1-查询全部;0-充电机;1-电表;2-水冷机;3-plc<
/// 设备编码
///
[HttpPost("QueryEquipAlarmPage")]
public Result> QueryEquipAlarmPage([FromBody] QueryAlarmReq req)
{
Result> result = equipAlarmRecordService.QueryEquipAlarmPage(req);
if (result.Data.Rows.Count > 0)
{
foreach (var recordAlarm in result.Data.Rows)
{
recordAlarm.ErrorMsg = _localizer[recordAlarm.ErrorMsg];
}
}
return result;
}
///
/// 导出未处理报警
///
///
[HttpPost]
[Route("ExportEquipAlarmRecord")]
public async Task ExportEquipAlarmRecord([FromBody] QueryAlarmReq req)
{
var language = Request.Headers["Accept-Language"].ToString().ToLower();
// 创建查询条件的表达式
Expression> predicate = it => true;
// 动态添加查询条件
if (req.EquipTypeCode != -1)
{
predicate = predicate.And(it => it.EquipTypeCode == req.EquipTypeCode);
}
if (!string.IsNullOrWhiteSpace(req.EquipCode))
{
predicate = predicate.And(it => it.EquipCode == req.EquipCode);
}
if (!string.IsNullOrWhiteSpace(req.ErrorLevel))
{
predicate = predicate.And(it => it.ErrorLevel == req.ErrorLevel);
}
if (req.StartTime.HasValue && req.ProcessTime.HasValue)
{
predicate = predicate.And(it => it.StartTime >= req.StartTime && it.StartTime <= req.ProcessTime);
}
// 执行查询
List equipAlarmRecords = await equipAlarmRecordService.QueryListByClauseAsync(
predicate,
it => it.CreatedTime,
OrderByType.Desc
);
if (equipAlarmRecords.Count > 0)
{
foreach (var recordAlarm in equipAlarmRecords)
{
recordAlarm.ErrorMsg = _localizer[recordAlarm.ErrorMsg];
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
});
IMapper mapper = config.CreateMapper();
if (req.EquipTypeCode==(int)EquipmentType.Plc)
{
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Unhandled_Alarm_Logs.xlsx" };
}
else
{
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" };
}
}
if (req.EquipTypeCode==(int)EquipmentType.Charger)
{
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Unhandled_Alarm_Logs.xlsx" };
}
else
{
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" };
}
}
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Unhandled_Alarm_Logs.xlsx" };
}
else
{
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" };
}
}
///
/// 查询已处理报警列表
///
/// 设备类型:-1-查询全部;0-充电机;1-电表;2-水冷机;3-plc
/// 设备编码
///
[HttpPost("QueryEquipAlarmProcessPage")]
public Result> QueryEquipAlarmProcessPage([FromBody] QueryAlarmReq req)
{
Result> result = equipAlarmRecordService.QueryEquipAlarmProcessPage(req);
if (result.Data.Rows.Count > 0)
{
foreach (var recordAlarm in result.Data.Rows)
{
recordAlarm.ErrorMsg = _localizer[recordAlarm.ErrorMsg];
}
}
return result;
}
///
/// 导出已处理报警
///
///
[HttpPost]
[Route("ExportEquipAlarmProcessRecord")]
public async Task ExportEquipAlarmProcessRecord([FromBody] QueryAlarmReq req)
{
var language = Request.Headers["Accept-Language"].ToString().ToLower();
// 创建查询条件的表达式
Expression> predicate = it => true;
// 动态添加查询条件
if (req.EquipTypeCode != -1)
{
predicate = predicate.And(it => it.EquipTypeCode == req.EquipTypeCode);
}
if (!string.IsNullOrWhiteSpace(req.EquipCode))
{
predicate = predicate.And(it => it.EquipCode == req.EquipCode);
}
if (!string.IsNullOrWhiteSpace(req.ErrorLevel))
{
predicate = predicate.And(it => it.ErrorLevel == req.ErrorLevel);
}
if (req.StartTime.HasValue && req.ProcessTime.HasValue)
{
predicate = predicate.And(it => it.StartTime >= req.StartTime && it.ProcessTime <= req.ProcessTime);
}
// 执行查询
List equipAlarmProcessRecords = await ProcessRecordRepository.QueryListByClauseAsync(
predicate,
it => it.CreatedTime,
OrderByType.Desc
);
if (equipAlarmProcessRecords.Count > 0)
{
foreach (var recordAlarm in equipAlarmProcessRecords)
{
recordAlarm.ErrorMsg = _localizer[recordAlarm.ErrorMsg];
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
});
IMapper mapper = config.CreateMapper();
if (req.EquipTypeCode==(int)EquipmentType.Plc)
{
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmProcessRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Processed_Alarm_Logs.xlsx" };
}
else
{
List logExList = mapper.Map>(equipAlarmProcessRecords);
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" };
}
}
if (req.EquipTypeCode==(int)EquipmentType.Charger)
{
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmProcessRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Processed_Alarm_Logs.xlsx" };
}
else
{
List logExList = mapper.Map>(equipAlarmProcessRecords);
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" };
}
}
if (language == "en")
{
List logExList2 = mapper.Map>(equipAlarmProcessRecords);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(logExList2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_Processed_Alarm_Logs.xlsx" };
}
else
{
List logExList = mapper.Map>(equipAlarmProcessRecords);
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" };
}
}
}