schangxiang@126.com
2024-06-19 193c31efe3b7c9f61aeb06fc06c1765669a9efcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
        }
    }
}