ningshuxia
2024-07-08 04e811eb41fe6adce30ec78926973b0e02ed6bbe
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
namespace IStation.Application
{
    /// <summary>
    /// 调度
    /// </summary>
    [Route("ChenHang/Dispatch")]
    [ApiDescriptionSettings("Web", Name = "优化调度", Order = 3)]
    public class OptimalSchedule_Controller : IDynamicApiController
    {
        private readonly Service.ScheduleRequest _service_schedule_request = new();
        private readonly Service.ScheduleConclusion _service_schedule_conclusion = new();
        private readonly Service.SchedulePump _service_schedule_pump = new();
 
        /// <summary>
        ///获取泵站列表
        /// </summary>
        [Route("GetStationList@V1.0")]
        [HttpGet]
        public List<StationTreeItemOutput> GetStationList()
        {
            var station1 = new StationTreeItemOutput()
            {
                ID = 1,
                Name = "陈行1输",
                SortCode = 1
            };
 
            var station2 = new StationTreeItemOutput()
            {
                ID = 2,
                Name = "陈行2输",
                SortCode = 2
            };
            return new List<StationTreeItemOutput>() { station1, station2 };
        }
 
        /// <summary>
        /// 调度
        /// </summary>
        [AllowAnonymous]
        [Route("Calculate@V1.0")]
        [HttpPost]
        public OptScheduleOutput Calculate([Required] OptScheduleStationInput input)
        {
            var stationId = input.StationID;
            var e_station = stationId == 1 ? IStation.eDockingStation.Ch1s : IStation.eDockingStation.Ch2s;
            var receipt_time = DateTime.Now;
            var request_id = Yw.YitIdHelper.NextId();
            var log_title = string.Empty;
 
            var station = new Service.Station().Get();
 
 
            var water_level = input.WaterLevel;
            var target_flow = input.TargetFlow;
            var target_pressure = input.TargetPressure;
            var target_head = Curve.PumpCalculateHelper.Mpa2M(target_pressure) - water_level;
 
            log_title = "需水请求";
            Log.Info(request_id, log_title, $"water_level:{water_level},target_flow:{target_flow},target_pressure:{target_pressure},target_head:{target_head}");
            Log.Debug(request_id, log_title, JsonHelper.Object2Json(input));
 
            List<Model.Pump> pumps = null;
            List<int> flags_part1 = null, flags_part2 = null, current_open_pump_flags = null;
 
            if (e_station == IStation.eDockingStation.Ch1s)
            {
                pumps = station.S1;
                flags_part1 = station.S1FlagsPart1;
                flags_part2 = station.S1FlagsPart2;
            }
            else
            {
                pumps = station.S2;
                flags_part1 = station.S2FlagsPart1;
                flags_part2 = station.S2FlagsPart2;
            }
 
            var helper = new IStation.Algorithm.ScheduleHelper();
            var optimal_combine = helper.Calc(pumps, flags_part1, flags_part2, target_flow, target_head);
 
            var schedule_request = new Model.ScheduleRequest();
            schedule_request.ID = request_id;
            if (stationId == 1)
            {
                schedule_request.WaterLevel1 = water_level;
                schedule_request.TargetFlow1 = target_flow;
                schedule_request.TargetPressure1 = target_pressure;
                schedule_request.ScheduleStatus1 = optimal_combine != null;
            }
            else
            {
                schedule_request.WaterLevel2 = water_level;
                schedule_request.TargetFlow2 = target_flow;
                schedule_request.TargetPressure2 = target_pressure;
                schedule_request.ScheduleStatus2 = optimal_combine != null;
            }
 
            schedule_request.TotalTimeSpent = (DateTime.Now - receipt_time).TotalSeconds;
            schedule_request.ReceptionTime = receipt_time;
 
            Model.ScheduleConclusion schedule_solution = null;
            List<Model.SchedulePump> schedule_pump_list = null;
            OptScheduleOutput output = null;
 
            var msg = "计算成功";
            log_title = "调度返回";
            if (optimal_combine == null)
            {
                Log.Info(request_id, log_title, $"{stationId}:调度计算失败,无法满足目标流量:{target_flow},目标压力:{target_pressure}!");
                Yw.Dto.YOops.Oh(Yw.Dto.eResultCode.Error, Yw.Dto.InternalErrorCodes.A001, $"{stationId}:调度计算失败,无法满足目标流量:{target_flow},目标压力:{target_pressure}!");
            }
            else
            {
 
                schedule_solution = new Model.ScheduleConclusion();
                schedule_solution.ID = Yw.YitIdHelper.NextId();
                schedule_solution.RequestID = request_id;
                schedule_solution.Station = e_station;
                schedule_solution.TotalFlow = optimal_combine.Flow;
                schedule_solution.TotalHead = optimal_combine.Head;
                schedule_solution.TotalPower = optimal_combine.Power;
                schedule_solution.TotalEfficiency = optimal_combine.Efficiency;
                schedule_solution.WP = optimal_combine.WP;
                schedule_solution.UWP = optimal_combine.UWP;
                schedule_solution.Flags = IntListHelper.ToString(optimal_combine.Flags);
                schedule_solution.MeritRatio = optimal_combine.MeritRatio;
                schedule_pump_list = new List<Model.SchedulePump>();
 
                output = new OptScheduleOutput();
                output.Station = new OptScheduleStationOutput();
                output.Station.TotalFlow = optimal_combine.Flow;
                output.Station.TotalPressure = Math.Round(Curve.PumpCalculateHelper.M2Mpa(optimal_combine.Head), 4);
                output.Station.TotalPower = optimal_combine.Power;
                output.Station.TotalEfficiency = optimal_combine.Efficiency;
                output.Station.WP = optimal_combine.WP;
                output.Station.UWP = optimal_combine.UWP;
                output.Pumps = new List<OptSchedulePumpOutput>();
 
 
                foreach (var combine in optimal_combine.Combines)
                {
                    foreach (var fre_pump in combine.FrePumps)
                    {
                        var flag = fre_pump.Flag;
                        var chPump = new OptSchedulePumpOutput();
                        chPump.ID = flag;
                        chPump.Name = flag + "#";
                        chPump.Flow = fre_pump.Flow;
                        chPump.Head = fre_pump.Head;
                        chPump.Power = fre_pump.Power;
                        chPump.Efficiency = fre_pump.Efficiency;
                        chPump.Frequency = fre_pump.Frequency;
                        chPump.Speed = fre_pump.Speed;
                        chPump.Status = 1;
                        output.Pumps.Add(chPump);
 
                        var schedule_pump = new Model.SchedulePump();
                        schedule_pump.RequestID = request_id;
                        schedule_pump.Station = e_station;
                        schedule_pump.Flag = flag;
                        schedule_pump.Flow = fre_pump.Flow;
                        schedule_pump.Head = fre_pump.Head;
                        schedule_pump.Power = fre_pump.Power;
                        schedule_pump.Efficiency = fre_pump.Efficiency;
                        schedule_pump.Frequency = fre_pump.Frequency;
                        schedule_pump.Speed = fre_pump.Speed;
                        schedule_pump_list.Add(schedule_pump);
                    }
                }
            }
 
 
            Log.Info(request_id, log_title, msg);
            try
            {
                log_title = "存储方案";
                var bol = false;
                bol = _service_schedule_request.Insert(schedule_request) > 0;
                if (!bol)
                {
                    Log.Info(request_id, log_title, "schedule_request 插入异常");
                    Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_request));
                }
 
                if (schedule_solution != null)
                {
                    bol = _service_schedule_conclusion.Insert(schedule_solution) > 0;
                    if (!bol)
                    {
                        Log.Info(request_id, log_title, "schedule_solution 插入异常");
                        Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_solution));
                    }
                }
 
 
                if (schedule_pump_list != null)
                {
                    bol = _service_schedule_pump.Inserts(schedule_pump_list);
                    if (!bol)
                    {
                        Log.Info(request_id, log_title, "schedule_pump_list 插入异常");
                        Log.Debug(request_id, log_title, JsonHelper.Object2Json(schedule_pump_list));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(request_id, log_title, "数据库异常!", ex);
            }
            return output;
        }
 
 
 
    }
}