tangxu
2022-10-24 805b967b4ee65cd5022cad20451299f6ce56c949
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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 Microsoft.AspNetCore.Authorization;
using IStation.Calculation;
using IStation.Dto;
using System.Collections.Generic;
 
namespace IStation.Application
{
    /// <summary>
    /// ShysPlanAna
    /// </summary>
    [Route("OpenApi/PlanAna/SHYS")]
    [ApiDescriptionSettings("OpenApi", Name = "上海原水计划分析", Order = 1000)]
    public class PlanAna_ShysController : IDynamicApiController
    {
        private const long _corpId = 4;
 
        /// <summary>
        /// 计算
        /// </summary>
        [AllowAnonymous]
        [NonUnify]
        [Route("Calculate")]
        [HttpPost]
        public List<PlanAnaDto> Calculate([Required] List<PlanAnaInput> inputList)
        {
            if (inputList == null || inputList.Count < 1)
            {
                LogHelper.Error("上海原水能耗计划分析计算接口入参为空");
                return default;
            }
            //debug
            LogHelper.Debug(JsonHelper.Object2Json(inputList));
 
            var url_sg = Settings.WebApi.OpenApi.SanGaoPlanUrl;
            var responseText = HttpRequestHelper.Post(url_sg, JsonHelper.Object2Json(inputList));
            //debug
            LogHelper.Debug(responseText);
 
            var planDataList = JsonHelper.Json2Object<List<PlanAnaData>>(responseText);
            return CalculateCore(planDataList);
        }
 
        /// <summary>
        /// 计算-调试
        /// </summary>
        [AllowAnonymous]
        [NonUnify]
        [Route("Calculate@Debug")]
        [HttpGet]
        public List<PlanAnaDto> Calculate_debug()
        {
            var path = @"D:\WorkData\IStation\result_1024lq.json";
            if (!System.IO.File.Exists(path))
                return null;
 
            var responseText = System.IO.File.ReadAllText(path);//(测试用的)   
            var planDataList = JsonHelper.Json2Object<List<PlanAnaData>>(responseText);
            return CalculateCore(planDataList);
        }
 
        /// <summary>
        /// 分析核心
        /// </summary>
        /// <param name="planDataList"></param>
        /// <returns></returns>
        private List<PlanAnaDto> CalculateCore(List<PlanAnaData> planDataList)
        {
            if (planDataList == null || planDataList.Count < 1)
            {
                LogHelper.Error("上海原水能耗计划分析计算三高返回接口出参为空");
                return default;
            }
            if (planDataList.Exists(x => x.datas == null || x.datas.Count < 1))
            {
                LogHelper.Error("上海原水能耗计划分析计算三高返回接口出参格式错误");
                return default;
            }
 
            var sg_factoryIds = planDataList.SelectMany(x => x.datas).Select(x => x.factory).Distinct().ToList();
            var vmList = new List<PlanAnaDto>();
            foreach (var sg_factoryId in sg_factoryIds)
            {
                var vmItem = Ana(planDataList, sg_factoryId);
                if (vmItem != null)
                    vmList.Add(vmItem);
            }
            return vmList;
        }
 
        private PlanAnaDto Ana(List<PlanAnaData> planDataList, int sg_factoryId)
        {
            //找到相关泵站,并进行计算
            var stationInfo = ShysPlanHelper.GetStationInfo(sg_factoryId);
            if (stationInfo == null)
            {
                // LogHelper.Error("未找到id:{factoryId},对应的泵站");
                return null;
            }
 
            var stationId = stationInfo.ID;
            var station = new Service.Station().GetByID(_corpId, stationId);
            if (station == null)
            {
                LogHelper.Error($"上海原水能耗计划分析计算中, 泵站id:{stationId}, 数据库中未找到此泵站");
                return null;
            }
            var calculator = stationInfo.Calculator;
            if (calculator == null)
            {
                LogHelper.Error($"上海原水能耗计划分析计算中, 泵站id:{stationId}, 未构建计算器");
                return null;
            }
            calculator.SetStationID(4, stationInfo.ID);//设置ID
 
            //构造计算入参
            var sumRecordList = new List<MonthSumRecord>();
            foreach (var planData in planDataList)
            {
                var sumRecord = new MonthSumRecord();
                sumRecord.Month = planData.timeflag;
                sumRecord.HourRecords = new List<HourSumRecord>();
                var factoryData = planData.datas.Find(t => t.factory == sg_factoryId);
                for (int i = 0; i <= 23; i++)
                {
                    var hourSumRecord = new HourSumRecord();
                    hourSumRecord.Hour = i;
                    hourSumRecord.Records = new List<Dto.MonitorRecord4SG>();
                    if (factoryData.scada != null && factoryData.scada.Count > 0)
                    {
                        foreach (var scadaItem in factoryData.scada)
                        {
                            var scadaRecord = new Dto.MonitorRecord4SG();
                            scadaRecord.MonitorTag = scadaItem.tagname;
                            var ff = scadaItem.values.Find(t => t.GetHour() == i);
                            if (ff == null)
                            {
                                if (calculator.IsIgnoreAble(scadaItem.tagname))
                                {
                                    scadaRecord.RecordValue = 0;
                                }
                                else if (calculator.Is液位Monitor(scadaItem.tagname))
                                {
                                    scadaRecord.RecordValue = 0;
                                }
                                else
                                {
                                    LogHelper.Error($"上海原水能耗计划分析计算中, 泵站id:{stationId}, {scadaItem.tagname} 时间点{i}, 未找到数据");
                                    return null;
                                }
                            }
                            else
                            {
                                scadaRecord.RecordValue = ff.value;
                            }
 
                            hourSumRecord.Records.Add(scadaRecord);
                        }
                    }
                    sumRecord.HourRecords.Add(hourSumRecord);
                }
                sumRecordList.Add(sumRecord);
            }
 
            string error_info = "";
            var resultList = calculator.Calc(station, sg_factoryId, sumRecordList, out error_info);
            if (resultList == null || resultList.Count() < 1)
            {
                LogHelper.Error($"上海原水能耗计划分析计算中,泵站名称:{station.Name} 泵站id:{stationId},计算错误,原因是:{error_info}");
                return null;
            }
 
            //生成返回结果
            var vmItem = new PlanAnaDto();
            vmItem.factory = sg_factoryId;
            vmItem.name = station.Name;
            vmItem.values = new List<List<double>>();
            for (int i = 1; i <= 12; i++)
            {
                var result = resultList.Find(t => t.Month == i);
                if (result == null)
                {
                    vmItem.values.Add(new List<double>() { 0, 0, 0 });
                }
                else
                {
                    vmItem.values.Add(new List<double>() { result.Qt, result.Dt, result.WP });
                }
            }
 
            return vmItem;
        }
 
 
 
 
 
 
 
 
    }
}