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 { /// /// 设备状态分析服务 /// [ApiDescriptionSettings("数据查询服务", Name = "EquipmentStatusAnalyse", Order = 100)] [Route("api/[Controller]")] public class EquipmentStatusAnalyseService : IEquipmentStatusAnalyseService, IDynamicApiController, ITransient { private readonly IRepository _equipmentStatusAnalyseRep; private readonly IRepository _sysDictTypeRep; private readonly IRepository _sysDictDataRep; private readonly IRepository _equipmentWorkingLog; /// /// 设备状态分析构造 /// /// /// /// public EquipmentStatusAnalyseService( IRepository equipmentStatusAnalyseRep, IRepository sysDictTypeRep, IRepository sysDictDataRep, IRepository equipmentWorkingLog ) { _equipmentStatusAnalyseRep = equipmentStatusAnalyseRep; _sysDictTypeRep = sysDictTypeRep; _sysDictDataRep = sysDictDataRep; _equipmentWorkingLog = equipmentWorkingLog; } /// /// 获取设备报警分析 /// /// /// [HttpGet("GetEquipmentAlertAnalyse")] public async Task> GetEquipmentAlertAnalyse([FromQuery] EquipmentStatusAnalyseSearch input) { DateTime currertTime = input.StartTime; List outPutLst = new List(); while (currertTime < input.EndTime) { GetEquipmentAlertAnalyseOutput output = new GetEquipmentAlertAnalyseOutput(); output.CurrentYearMonth = currertTime.ToString("yyyy-MM"); // 数据来源于设备运行历史表信息 统计每月的次数 然后故障结束时间-故障开始时间 查询条件为:故障开始时间 List 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; } /// /// 获取设备状态占比分析 /// /// /// [HttpGet("GetEquipmentStatuRatioAnalyse")] public async Task> GetEquipmentStatuRatioAnalyse([FromQuery] EquipmentStatusAnalyseSearch input) { DateTime currertTime = input.StartTime; List outPutLst = new List(); while (currertTime < input.EndTime) { GetEquipmentStatuRatioAnalyseOutput outPut = new GetEquipmentStatuRatioAnalyseOutput(); outPut.CurrentYearMonth = currertTime.ToString("yyyy-MM"); List detailOutputsLst = new List(); // 获取设备状态 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() .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(input)) .ProjectToType() .ToADPagedListAsync(input.PageNo, input.PageSize); // 设备占比分析值 detailOutput.RatioAnalyseValue = 1; detailOutputsLst.Add(detailOutput); } // 设备占比分析明细集合列表追加 outPut.getEquipmentStatuRatioLst = detailOutputsLst; // 月份加一 currertTime = currertTime.AddMonths(1); outPutLst.Add(outPut); } return outPutLst; } } }