tangxu
2024-12-19 9acdf3c826311bd67180821ce19c625d0e384ca8
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using IStation.CalcModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation
{
    public class CalcPrjHelper
    {
        //bool isLimitLevel = false;
 
        private List<Model.ElecPriceMonthSetting> _elePriceMonthList = null;
        private List<IStation.CalcModel.PumpInfo> _allCalcPumpInfos;
 
        public CalcPrjHelper()
        { }
 
        public string Calc(IStation.CalcModel.AnaRequest anaRequest, out AnaPrj optPrj)
        {
            if (!InitalPumpInfo())
            {
                optPrj = null;
                return "数据初始化失败";
            }
            InitialElePrice();
 
            var calcHelper = GetCalcHelper(anaRequest);
 
            string error_info;
            optPrj = calcHelper.CalcOptPrj(out error_info);
 
            if (!string.IsNullOrEmpty(error_info) || optPrj == null)
            {
                optPrj = null;
                return error_info;
            }
            //anaPrjs.Sort(new AnaPrj.Comparer(anaRequest.CalcOptType));
            //optPrj = anaPrjs.First();
 
            //
            double maxHeight = 0;
            DateTime maxHeightTime = optPrj.StartTime;
            if (optPrj.BlockTimes != null)
            { 
                foreach (var bt in optPrj.BlockTimes)
                {
                    if (bt.OpenPumpIndexs == null)
                        continue;
 
                    if (bt.PointTimes != null)
                    { 
                        foreach (var bt2 in bt.PointTimes)
                        {
                            if (maxHeight < bt2.WaterLevelH)
                            {
                                maxHeight = bt2.WaterLevelH;
                                //maxHeight = Math.Max(maxHeight, bt2.WaterLevelH);
                                maxHeightTime = bt2.Time;
                            }
                        }
                    }
                }
            }
            optPrj.MaxWaterLevelH = maxHeight;
            optPrj.MaxWaterLevelTime = maxHeightTime;
 
            return null;
        }
 
        /// <summary>
        /// 构建计算辅助类
        /// </summary>
        /// <param name="isIgnoreOpenLimitAble"></param>
        /// <returns></returns>
        private IStation.Calc.ErQuCalcBaseHelper GetCalcHelper(IStation.CalcModel.AnaRequest anaRequest)
        {         
            IStation.Calc.ErQuCalcBaseHelper calcHelper = IStation.Calc.ErQuCalcBaseHelper.Build(anaRequest);
            calcHelper.MinOpenPumpMinute = IStation.AnaGlobalParas.Setting.MinOpenTimeMinute;
            calcHelper.MinSwitchPumpMinute = IStation.AnaGlobalParas.Setting.MinSwitchTimeMinute;
            calcHelper.SetOptimalPumpIndexs(
                IStation.AnaGlobalParas.Setting.OptimalPumpIndexSequence,
                IStation.AnaGlobalParas.Setting.UnablePumpIndexArray);
 
            calcHelper.CalcSpaceMinute = IStation.AnaGlobalParas.Setting.CalcSpaceMinute;
            calcHelper.MaxPumpSwitchCount = IStation.AnaGlobalParas.Setting.MaxPumpSwitchCount;
 
            DateTime rangeStartTime = anaRequest.StartTime;
            DateTime rangeEndTime = anaRequest.EndTime;
            calcHelper.SetPumpInfo(_allCalcPumpInfos);
            calcHelper.SetCalcTimeRange(rangeStartTime, rangeEndTime, IStation.AnaGlobalParas.Setting.CalcSpaceMinute);
 
            calcHelper.ElecPrice = _elePriceMonthList;
 
            calcHelper.IsDispDebug = false;
            calcHelper.OnShowDebugInfo += (info) =>
            {
                //if (tabPageDebug.PageVisible)
                //    this.listBoxDebug.Items.Add(info);
            };
 
            //不许切泵时间
            if (IStation.AnaGlobalParas.Setting.SwitchPumpIgnoreTimes != null && IStation.AnaGlobalParas.Setting.SwitchPumpIgnoreTimes.Count > 0)
            {
                List<IStation.CalcModel.TimeRange> limit_times = new List<IStation.CalcModel.TimeRange>();
                foreach (var t in IStation.AnaGlobalParas.Setting.SwitchPumpIgnoreTimes)
                {
                    if (t.IsUse == false)
                        continue;
 
                    int ed_h = t.EndHour;
                    int st_h = t.StartHour;
 
                    IStation.CalcModel.TimeRange v = new IStation.CalcModel.TimeRange();
 
                    if (ed_h <= rangeStartTime.Hour)
                    {
                        var nextDay = rangeStartTime.AddDays(1);
                        v.Start = new DateTime(nextDay.Year, nextDay.Month,
    nextDay.Day, st_h, t.StartMinute, 0);
                    }
                    else
                    {
                        v.Start = new DateTime(rangeStartTime.Year, rangeStartTime.Month,
    rangeStartTime.Day, st_h, t.StartMinute, 0);
                    }
 
                    bool isNext = ed_h > 23 ? true : false;
                    if (isNext)
                    {
                        var nextDay = v.Start.AddDays(1);
                        v.End = new DateTime(nextDay.Year, nextDay.Month,
                        nextDay.Day, ed_h - 24, t.EndMinute, 0);
                    }
                    else
                    {
                        v.End = new DateTime(v.Start.Year, v.Start.Month,
                        v.Start.Day, ed_h, t.EndMinute, 0);
                    }
                    if (v.End > v.Start)
                    {
                        limit_times.Add(v);
                    }
                }
                calcHelper.LimitSwitchPumpTimes = limit_times;
            }
 
           
            CalcLimitOpenPumpTime(ref calcHelper, anaRequest);
 
           
            calcHelper.SetAnaRequest(anaRequest);
 
            return calcHelper;
        }
 
        //不许开泵时间
        private void CalcLimitOpenPumpTime(ref IStation.Calc.ErQuCalcBaseHelper calcHelper, IStation.CalcModel.AnaRequest anaRequest)
        {
            calcHelper.LimitOpenPumpTimes = null;
 
            if (IStation.AnaGlobalParas.Setting.OpenPumpTimes == null)
                return;
 
            if (IStation.AnaGlobalParas.Setting.OpenPumpTimes.Count == 0)
                return;
            var listUse = (from x in IStation.AnaGlobalParas.Setting.OpenPumpTimes where x.IsUse select x).ToList();
            if (listUse.Count == 0)
                return;
            //LogHelper.Info("Json转换" + JsonHelper.Object2Json(listUse));
 
            DateTime rangeStartTime = anaRequest.StartTime;
            DateTime rangeEndTime = anaRequest.EndTime;
 
            List<IStation.CalcModel.TimeRange> limit_times = new List<IStation.CalcModel.TimeRange>();
            IStation.CalcModel.TimeRange last_range = null;
            for (DateTime dt = rangeStartTime; dt <= rangeEndTime; dt = dt.AddMinutes(5))
            {
                var ttt = dt.Hour * 60 + dt.Minute;
                bool isOkOpen = false;
                foreach (var openTime in listUse)
                {
                    if (ttt >= openTime.StartHour * 60 + openTime.StartMinute && ttt <= openTime.EndHour * 60 + openTime.EndMinute)
                    {
                        isOkOpen = true;
                        break;
                    }
                }
                if (!isOkOpen)
                {//限制开泵
                    if (last_range == null)
                    {
                        last_range = new TimeRange();
                        last_range.Start = dt;
                    }
                }
                else
                {
                    if (last_range != null)
                    {
                        last_range.End = dt;
                        limit_times.Add(new IStation.CalcModel.TimeRange(last_range));
                        last_range = null;
                    }
                }
            }
 
            calcHelper.LimitOpenPumpTimes = limit_times;
 
            if (limit_times.Count > 0)
            {
                //LogHelper.Info("limit_times[0].Start时间:" + limit_times[0].Start.ToString());
                //LogHelper.Info("rangeStartTime时间:" + rangeStartTime.ToString());
                if (limit_times[0].Start == rangeStartTime)
                {
                    if (anaRequest.StartOpenPumpIndexArray != null)
                        anaRequest.StartOpenPumpIndexArray.Clear();
                    else
                        anaRequest.StartOpenPumpIndexArray = new List<int>();
                    anaRequest.IsKnownStartOpenPump = true;//不考虑 ???
                }
            }
        }
 
        #region 初始化
 
        /// <summary>
        ///
        /// </summary>
        private bool InitalPumpInfo()
        {
            var allCurveList = IStation.Common.PumpCurve.GetAll();
            if (allCurveList == null)
                return false;
            var allPumpMapList = IStation.Common.PumpCurveMapping.GetAll();
            var allPumpList = IStation.Common.Product.GetAllPump();
            if (allPumpList == null)
                return false;
            _allCalcPumpInfos = new List<IStation.CalcModel.PumpInfo>();
            foreach (var pump in allPumpList)
            {
                var curve_wrk_map = (from x in allPumpMapList where x.PumpID == pump.ID && x.IsWorking select x).FirstOrDefault();
                if (curve_wrk_map == null)
                {
                    IStation.LogHelper.Info(pump.Name + "未设置工作曲线");
                    return false;
                }
                var curve = allCurveList.Find(x => x.ID == curve_wrk_map.CurveID);
 
                IStation.CalcModel.PumpInfo calcPumpInfo = new IStation.CalcModel.PumpInfo();
                calcPumpInfo.Product = pump;
                calcPumpInfo.ID = pump.ID;
                calcPumpInfo.Name = pump.Name;
                calcPumpInfo.CurveInfo = curve.CurveInfo;
                calcPumpInfo.RatedParas = pump.RatedParas;
 
                _allCalcPumpInfos.Add(calcPumpInfo);
            }
            return true;
        }
 
        /// <summary>
        ///
        /// </summary>
        private void InitialElePrice()
        {
            var allPrices = IStation.Common.ElecPrice.GetAll();
            _elePriceMonthList = allPrices.First().Settings.MonthList;
        }
 
        #endregion 初始化
    }
}