119 lines
4.5 KiB
C#
119 lines
4.5 KiB
C#
using langguanApi.Common.Proxy;
|
|
using langguanApi.Extensions.AutoDI;
|
|
using langguanApi.Model;
|
|
using langguanApi.Model.Dto;
|
|
using langguanApi.Model.Entity;
|
|
using Mapster;
|
|
using Npoi.Mapper;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace langguanApi.Service
|
|
{
|
|
[ServiceInjection(InjectionType.Transient)]
|
|
public class AlertService : BaseService<Alert>
|
|
{
|
|
private readonly HttpProxy _httpProxy;
|
|
private readonly IConfiguration _configuration;
|
|
public AlertService(IConfiguration config, HttpProxy httpProxy) : base(config, nameof(Alert))
|
|
{
|
|
_httpProxy = httpProxy;
|
|
_configuration = config;
|
|
}
|
|
/// <summary>
|
|
/// 新加
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> Add(AddAlertDto input)
|
|
{
|
|
if (input != null)
|
|
{
|
|
var entity = input.Adapt<Alert>();
|
|
await base.CreateAsync(entity);
|
|
return new ApiResult { code = 0, msg = "" };
|
|
}
|
|
return new ApiResult { code = -1, msg = "" }; ;
|
|
}
|
|
/// <summary>
|
|
/// 首页数据,最近7天的
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<alarm>> IndexData(int num = 50)
|
|
{
|
|
Expression<Func<Alert, bool>> exp = filter => filter.IsDelete == false;
|
|
return (await base.GetListWithExp(exp))
|
|
.OrderByDescending(s => s.CreateDateTime)
|
|
.Take(num)
|
|
.Select(s => new alarm
|
|
{
|
|
AlarmName = s.deviceName,
|
|
DeviceName = s.deviceName,
|
|
Time = s.CreateDateTime.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
}).ToList();
|
|
}
|
|
/// <summary>
|
|
///导出数据
|
|
/// </summary>
|
|
/// <param name="start"></param>
|
|
/// <param name="end"></param>
|
|
/// <returns></returns>
|
|
public async Task<byte[]> ExportData(DateTime? start, DateTime? end)
|
|
{
|
|
Expression<Func<Alert, bool>> exp = filter => filter.CreateDateTime >= start
|
|
&& filter.CreateDateTime <= end && filter.IsDelete == false;
|
|
List<Alert> result = new List<Alert>();
|
|
//获取接口报警数据
|
|
Dictionary<string, string> dic = new Dictionary<string, string>() { };
|
|
dic.Add("BeginTime", start.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
dic.Add("EndTime", end.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
var AlermResp = await _httpProxy.Get<RespModel<alarmList>>(dic, _configuration.GetValue<string>("Apis:AlertUrl"));
|
|
if (AlermResp.code == 0)
|
|
{
|
|
var romte = AlermResp.data.List.Select(s => new Alert
|
|
{
|
|
AlertContent = s.AlarmName,
|
|
deviceName = s.DeviceName,
|
|
CreateDateTime = DateTime.Parse(s.Time)
|
|
}).ToList();
|
|
if (romte.Any())
|
|
{
|
|
result.AddRange(romte);
|
|
}
|
|
}
|
|
var list = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).ToList();
|
|
if (list.Any())
|
|
{
|
|
result.AddRange(list);
|
|
}
|
|
if (result.Any())
|
|
{
|
|
result = result.OrderByDescending(s => s.CreateDateTime).ToList();
|
|
var mapper = new Mapper();
|
|
mapper.Map<Alert>("报警开始时间", s => s.CreateDateTime)
|
|
.Map<Alert>("设备名称", s => s.deviceName)
|
|
// .Map<Alert>("工序", s => s.)
|
|
.Map<Alert>("内容", s => s.AlertContent)
|
|
.Format<Alert>("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime);
|
|
MemoryStream stream = new MemoryStream();
|
|
mapper.Save(stream, result, sheetName: "sheet1", leaveOpen: true);
|
|
return stream.ToArray();
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
/// 分页取数据
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public async Task<object> GetPage(reqpage input)
|
|
{
|
|
Expression<Func<Alert, bool>> exp = filter => filter.IsDelete == false;
|
|
return await base.GetPager(new ReqPaing()
|
|
{
|
|
pageSize = input.pageSize,
|
|
current = input.current
|
|
}, exp);
|
|
}
|
|
}
|
|
}
|