using Furion.DatabaseAccessor;
|
using Furion.DatabaseAccessor.Extensions;
|
using Furion.DependencyInjection;
|
using Furion.DynamicApiController;
|
using Furion.FriendlyException;
|
using iWare.Wms.Core;
|
using Mapster;
|
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.EntityFrameworkCore;
|
using System.Linq.Dynamic.Core;
|
|
namespace iWare.Wms.Application
|
{
|
/// <summary>
|
/// 设备状态分析服务
|
/// </summary>
|
[ApiDescriptionSettings("数据查询服务", Name = "EquipmentStatusAnalyse", Order = 100)]
|
[Route("api/[Controller]")]
|
public class EquipmentStatusAnalyseService : IEquipmentStatusAnalyseService, IDynamicApiController, ITransient
|
{
|
private readonly IRepository<EquipmentStatusAnalyse,MasterDbContextLocator> _equipmentStatusAnalyseRep;
|
private readonly IRepository<SysDictType, MasterDbContextLocator> _sysDictTypeRep;
|
private readonly IRepository<SysDictData, MasterDbContextLocator> _sysDictDataRep;
|
private readonly IRepository<EquipmentWorkingLog, MasterDbContextLocator> _equipmentWorkingLog;
|
|
/// <summary>
|
/// 设备状态分析构造
|
/// </summary>
|
/// <param name="equipmentStatusAnalyseRep"></param>
|
/// <param name="sysDictTypeRep"></param>
|
/// <param name="sysDictDataRep"></param>
|
public EquipmentStatusAnalyseService(
|
IRepository<EquipmentStatusAnalyse,MasterDbContextLocator> equipmentStatusAnalyseRep,
|
IRepository<SysDictType, MasterDbContextLocator> sysDictTypeRep,
|
IRepository<SysDictData, MasterDbContextLocator> sysDictDataRep,
|
IRepository<EquipmentWorkingLog, MasterDbContextLocator> equipmentWorkingLog
|
)
|
{
|
_equipmentStatusAnalyseRep = equipmentStatusAnalyseRep;
|
_sysDictTypeRep = sysDictTypeRep;
|
_sysDictDataRep = sysDictDataRep;
|
_equipmentWorkingLog = equipmentWorkingLog;
|
}
|
|
/// <summary>
|
/// 获取设备报警分析
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpGet("GetEquipmentAlertAnalyse")]
|
public async Task<List<GetEquipmentAlertAnalyseOutput>> GetEquipmentAlertAnalyse([FromQuery] EquipmentStatusAnalyseSearch input)
|
{
|
DateTime currertTime = input.StartTime;
|
List<GetEquipmentAlertAnalyseOutput> outPutLst = new List<GetEquipmentAlertAnalyseOutput>();
|
|
while (currertTime < input.EndTime)
|
{
|
GetEquipmentAlertAnalyseOutput output = new GetEquipmentAlertAnalyseOutput();
|
output.CurrentYearMonth = currertTime.ToString("yyyy-MM");
|
|
// 数据来源于设备运行历史表信息 统计每月的次数 然后故障结束时间-故障开始时间 查询条件为:故障开始时间
|
List<EquipmentWorkingLog> workingLogsLst = await _equipmentWorkingLog.DetachedEntities
|
.Where(x => x.EquipmentID == input.EquipmentId)
|
.Where(x => x.FailureStartTime >= currertTime.AddDays(1-currertTime.Day))
|
.Where(x => x.FailureStartTime < currertTime.AddDays(1 - currertTime.Day).AddMonths(1))
|
.ToListAsync();
|
// 获取设备报警次数
|
output.AlertNumber = workingLogsLst.Count;
|
|
// 获取设备报警持续时间
|
output.AlertDuration =Convert.ToDecimal(workingLogsLst.Sum(x => x.FailureEndTime?.Second - x.FailureStartTime?.Second)/3600);
|
|
// 月份加一
|
currertTime = currertTime.AddMonths(1);
|
|
outPutLst.Add(output);
|
}
|
|
return outPutLst;
|
}
|
|
|
/// <summary>
|
/// 获取设备状态占比分析
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpGet("GetEquipmentStatuRatioAnalyse")]
|
public async Task<List<GetEquipmentStatuRatioAnalyseOutput>> GetEquipmentStatuRatioAnalyse([FromQuery] EquipmentStatusAnalyseSearch input)
|
{
|
DateTime currertTime = input.StartTime;
|
List<GetEquipmentStatuRatioAnalyseOutput> outPutLst = new List<GetEquipmentStatuRatioAnalyseOutput>();
|
while (currertTime < input.EndTime)
|
{
|
GetEquipmentStatuRatioAnalyseOutput outPut = new GetEquipmentStatuRatioAnalyseOutput();
|
outPut.CurrentYearMonth = currertTime.ToString("yyyy-MM");
|
|
List<GetEquipmentStatuRatioAnalyseDetailOutput> detailOutputsLst = new List<GetEquipmentStatuRatioAnalyseDetailOutput>();
|
|
// 获取设备状态
|
var equipmentStatuInfoLst = await _sysDictDataRep.DetachedEntities
|
.Join(_sysDictTypeRep.DetachedEntities, u => u.TypeId, e => e.Id, (u, e) => new { u, e })
|
.Where(x => (x.e.Code.Equals("equipment_state")))
|
.Select(s => new EquipmentStatuOutput
|
{
|
EquipmentState = s.u.Code,
|
//EquipmentStateName = s.u.Value
|
})
|
.ProjectToType<EquipmentStatuOutput>()
|
.ToListAsync();
|
|
// 循环设备状态列表
|
foreach (var item in equipmentStatuInfoLst)
|
{
|
// 整理数据,追加返回集合列表中
|
GetEquipmentStatuRatioAnalyseDetailOutput detailOutput = new GetEquipmentStatuRatioAnalyseDetailOutput();
|
detailOutput.EquipmentStatu = item.EquipmentState;
|
detailOutput.EquipmentStatuName = item.EquipmentStateName;
|
|
// 获取当前月份设备状态占比的值
|
var equipmentStatusAnalyses = await _equipmentStatusAnalyseRep.DetachedEntities
|
.Where(!string.IsNullOrEmpty(input.EquipmentId), u => u.EquipmentId == input.EquipmentId)
|
.OrderBy(PageInputOrder.OrderBuilder<EquipmentStatusAnalyseSearch>(input))
|
.ProjectToType<EquipmentStatusAnalyseOutput>()
|
.ToADPagedListAsync(input.PageNo, input.PageSize);
|
|
// 设备占比分析值
|
detailOutput.RatioAnalyseValue = 1;
|
|
detailOutputsLst.Add(detailOutput);
|
}
|
|
// 设备占比分析明细集合列表追加
|
outPut.getEquipmentStatuRatioLst = detailOutputsLst;
|
// 月份加一
|
currertTime = currertTime.AddMonths(1);
|
|
outPutLst.Add(outPut);
|
}
|
|
|
return outPutLst;
|
}
|
}
|
}
|