ningshuxia
2022-12-01 391e0e408d09e698850eb5daaef485d088612b4f
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Http.Extensions;
using IStation.Untity;
using Furion.DynamicApiController;
using System.ComponentModel.DataAnnotations;
using Mapster;
using IStation.Calculation;
using IStation.Model;
using System.Reflection.Metadata.Ecma335;
 
namespace IStation.Application
{
    /// <summary>
    /// EnergyFlow
    /// </summary>
    [Route("Run/Energy/Flow")]
    [ApiDescriptionSettings("Run", Name = "能流", Order = 299)]
    public class EngineFlow_Controller : IDynamicApiController
    {
 
        /// <summary>
        /// 通过 LogicTreeID 获取某月的能流记录
        /// </summary>
        [Route("GetByLogicTreeIDOfMonth@V1.0")]
        [HttpGet]
        public EnergyFlowLogicalTreeItemDto GetByLogicTreeIDOfMonth
            (
                long CorpID,
                long LogicTreeID,
                int Year,
                int Month
            )
        {
            var currentTime= DateTime.Now;
            var currentYear = currentTime.Year;
            var currentMonth = currentTime.Month;
            if (currentYear == Year && currentMonth == Month)
                return new EnergyFlowLogicalTreeItemDto();
 
            var cacheKey = $"Run_Energy_Flow_GetByLogicTreeIDOfMonth_{CorpID}_{LogicTreeID}_{Year}_{Month}";
            var vm_list = MemoryCacheHelper.GetSet(cacheKey, () =>
            {
                var logicTreeList = new Service.LogicTree().GetExChildAndSelfByID(CorpID, LogicTreeID);
                if (logicTreeList == null || logicTreeList.Count < 1)
                    return default;
 
                #region 统计
 
                var bllMonitor = new Service.MonitorPoint();
                var bllMonitorMappings = new Service.MonitorPointMapping();
                var bllMonitorMonthRecord = new Service.MonitorMonthRecord();
                var vmList = new List<EnergyFlowLogicalTreeItemDto>();
                foreach (var logicTree in logicTreeList)
                { 
                    var vm = new EnergyFlowLogicalTreeItemDto();
                    vm.ID = $"{IStation.ObjectType.LogicTree}_{logicTree.ID}";
                    vm.ParentID = $"{IStation.ObjectType.LogicTree}_{TreeParentIdsHelper.GetLastParentID(logicTree.ParentIds)}";
                    vm.LogicalName = logicTree.LogicName;
                    vm.LogicalType = logicTree.LogicType;
                    vm.LogicalID = logicTree.LogicID;
                    vm.StatisticValue = null;
                    vm.Children = new List<EnergyFlowLogicalTreeItemDto>();
                    vmList.Add(vm);
 
                    if (vm.LogicalType == IStation.ObjectType.Station)
                    {
                        var enginePumpPipeList = new Service.PipeLine().GetEnginePumpListByBelongTypeAndBelongID(CorpID, logicTree.LogicType, logicTree.LogicID);
                        if (enginePumpPipeList != null && enginePumpPipeList.Count > 0)
                        {
                            foreach (var enginePumpPipe in enginePumpPipeList)
                            {
                                var vmEnginePump = new EnergyFlowLogicalTreeItemDto();
                                vmEnginePump.ID = $"{IStation.ObjectType.PipeLine}_{enginePumpPipe.ID}";
                                vmEnginePump.ParentID = vm.ID;
                                vmEnginePump.LogicalName = enginePumpPipe.Name;
                                vmEnginePump.LogicalType = IStation.ObjectType.PipeLine;
                                vmEnginePump.LogicalID = enginePumpPipe.ID;
                                vmEnginePump.Children = new List<EnergyFlowLogicalTreeItemDto>();
 
                                var enginePumpPipeMonitorMappings = bllMonitorMappings.GetByObjectTypeAndObjectID(CorpID, vmEnginePump.LogicalType, vmEnginePump.LogicalID);
                                if (enginePumpPipeMonitorMappings != null && enginePumpPipeMonitorMappings.Count > 0)
                                {
                                    var mappingIds = enginePumpPipeMonitorMappings.Select(x => x.MonitorPointID).ToList();
                                    var enginePumpPipeMonitorList = bllMonitor.GetExSignalWithSignalTypeByIds(CorpID, mappingIds);
                                    enginePumpPipeMonitorList = enginePumpPipeMonitorList?.Where(x => x.MonitorType == eMonitorType.General && x.SourceType == Model.Monitor.eSourceType.Analyse && x.CronType == Model.Monitor.eCronType.EachMonth).ToList();
                                    enginePumpPipeMonitorList = enginePumpPipeMonitorList?.Where(x => x.SignalList != null && x.SignalList.Count > 0).ToList();
                                    if (enginePumpPipeMonitorList != null && enginePumpPipeMonitorList.Count > 0)
                                    {
                                        var enginePumpPipeElMonitorList = enginePumpPipeMonitorList.Where(x => x.SignalList.Exists(x => x.SignalType.Identifier == IStation.SignalType.有功电度)).ToList();
                                        if (enginePumpPipeElMonitorList != null && enginePumpPipeElMonitorList.Count > 0)
                                        {
                                            var enginePumpPipeElMonitor = enginePumpPipeElMonitorList.Find(x => x.Flags != null && x.Flags.Contains(IStation.LogicFlags.默认));
                                            if (enginePumpPipeElMonitor == null)
                                                enginePumpPipeElMonitor = enginePumpPipeElMonitorList.First();
                                            var enginePumpPipeElValues = bllMonitorMonthRecord.GetByMonitorPointIDOfMonth(CorpID, enginePumpPipeElMonitor.ID, Year, Month);
                                            vmEnginePump.StatisticValue = enginePumpPipeElValues?.Sum(x =>
                                            {
                                                if (double.TryParse(x.DataValue, out double enginePumpPipeElValue))
                                                {
                                                    return enginePumpPipeElValue;
                                                }
                                                return 0;
                                            });
                                        }
                                    }
                                }
                                vm.Children.Add(vmEnginePump);
                            }
                        }
 
                        var vmMonitorList = bllMonitor.GetExSignalWithSignalTypeByBelongTypeAndBelongID(CorpID, vm.LogicalType, vm.LogicalID);
                        vmMonitorList = vmMonitorList?.Where(x => x.MonitorType == eMonitorType.General && x.SourceType == Model.Monitor.eSourceType.Analyse && x.CronType == Model.Monitor.eCronType.EachMonth).ToList();
                        vmMonitorList = vmMonitorList?.Where(x => x.SignalList != null && x.SignalList.Count > 0).ToList();
                        if (vmMonitorList != null && vmMonitorList.Count > 0)
                        {
                            var vmElMonitorList = vmMonitorList.Where(x => x.SignalList.Exists(x => x.SignalType.Identifier == IStation.SignalType.有功电度)).ToList();
                            if (vmElMonitorList != null && vmElMonitorList.Count > 0)
                            {
                                var vmElMonitor = vmElMonitorList.Find(x => x.Flags != null && x.Flags.Contains(IStation.LogicFlags.总站));
                                if (vmElMonitor != null)
                                {
                                    var vmElValues = bllMonitorMonthRecord.GetByMonitorPointIDOfMonth(CorpID, vmElMonitor.ID, Year, Month);
                                    vm.StatisticValue = vmElValues?.Sum(x =>
                                    {
                                        if (double.TryParse(x.DataValue, out double vmElValue))
                                        {
                                            return vmElValue;
                                        }
                                        return 0;
                                    });
                                }
                            }
                        }
 
                    }
 
                    var vmParent = vmList.Find(x => x.ID == vm.ParentID);
                    if (vmParent != null)
                    {
                        vmParent.Children.Add(vm);
                    }
                }
 
                logicTreeList.Reverse();
                foreach (var logicTree in logicTreeList)
                {
                    var vm = vmList.Find(x => x.ID == $"{IStation.ObjectType.LogicTree}_{logicTree.ID}");
                    if (vm.Children.Count > 0)
                    {
                        var validChildren = vm.Children.Where(x => x.StatisticValue.HasValue).ToList();
                        if (validChildren.Count > 0)
                        {
                            var validChildrenStatisticValue = validChildren.Sum(x => x.StatisticValue.Value);
                            /*if (vm.LogicalType == IStation.ObjectType.Station)
                            {
                                if (vm.StatisticValue > validChildrenStatisticValue)
                                    continue;
                            }*/
                            vm.StatisticValue = validChildrenStatisticValue;
                        }
                    }
                }
 
                #endregion
 
                return vmList.Find(x => x.ID == $"{IStation.ObjectType.LogicTree}_{LogicTreeID}");
 
 
            }, CacheHelper.CacheLevel2);
 
            return vm_list;
        }
 
 
 
 
 
 
 
 
 
 
 
    }
}