|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+package com.water.production.service;
|
|
|
2
|
+
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
5
|
+import com.water.production.entity.ChemicalDosing;
|
|
|
6
|
+import com.water.production.entity.ChemicalStock;
|
|
|
7
|
+import com.water.production.entity.DosingRecord;
|
|
|
8
|
+import com.water.production.entity.DosingStrategy;
|
|
|
9
|
+import com.water.production.mapper.ChemicalDosingMapper;
|
|
|
10
|
+import com.water.production.mapper.ChemicalStockMapper;
|
|
|
11
|
+import com.water.production.mapper.DosingRecordMapper;
|
|
|
12
|
+import com.water.production.mapper.DosingStrategyMapper;
|
|
|
13
|
+import lombok.RequiredArgsConstructor;
|
|
|
14
|
+import org.springframework.stereotype.Service;
|
|
|
15
|
+import org.springframework.util.StringUtils;
|
|
|
16
|
+
|
|
|
17
|
+import java.math.BigDecimal;
|
|
|
18
|
+import java.math.RoundingMode;
|
|
|
19
|
+import java.time.LocalDateTime;
|
|
|
20
|
+import java.time.format.DateTimeFormatter;
|
|
|
21
|
+import java.util.*;
|
|
|
22
|
+import java.util.stream.Collectors;
|
|
|
23
|
+
|
|
|
24
|
+/**
|
|
|
25
|
+ * 全工艺药剂投加监控服务
|
|
|
26
|
+ * 涵盖混凝→沉淀→过滤→消毒全流程
|
|
|
27
|
+ */
|
|
|
28
|
+@Service
|
|
|
29
|
+@RequiredArgsConstructor
|
|
|
30
|
+public class ChemicalDosingService {
|
|
|
31
|
+
|
|
|
32
|
+ private final ChemicalDosingMapper dosingMapper;
|
|
|
33
|
+ private final DosingRecordMapper recordMapper;
|
|
|
34
|
+ private final ChemicalStockMapper stockMapper;
|
|
|
35
|
+ private final DosingStrategyMapper strategyMapper;
|
|
|
36
|
+
|
|
|
37
|
+ // ==================== 药剂投加监控 ====================
|
|
|
38
|
+
|
|
|
39
|
+ /** 分页查询投加记录 */
|
|
|
40
|
+ public Map<String, Object> listDosing(int page, int size, String processStage, String station, String status) {
|
|
|
41
|
+ LambdaQueryWrapper<ChemicalDosing> wrapper = new LambdaQueryWrapper<>();
|
|
|
42
|
+ wrapper.eq(StringUtils.hasText(processStage), ChemicalDosing::getProcessStage, processStage)
|
|
|
43
|
+ .eq(StringUtils.hasText(station), ChemicalDosing::getStation, station)
|
|
|
44
|
+ .eq(StringUtils.hasText(status), ChemicalDosing::getStatus, status)
|
|
|
45
|
+ .orderByDesc(ChemicalDosing::getCreatedTime);
|
|
|
46
|
+ Page<ChemicalDosing> result = dosingMapper.selectPage(new Page<>(page, size), wrapper);
|
|
|
47
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
48
|
+ data.put("records", result.getRecords());
|
|
|
49
|
+ data.put("total", result.getTotal());
|
|
|
50
|
+ data.put("pages", result.getPages());
|
|
|
51
|
+ return data;
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ /** 获取投加详情 */
|
|
|
55
|
+ public ChemicalDosing getDosingById(Long id) {
|
|
|
56
|
+ return dosingMapper.selectById(id);
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ /** 新增投加记录 */
|
|
|
60
|
+ public ChemicalDosing createDosing(ChemicalDosing dosing) {
|
|
|
61
|
+ if (dosing.getStatus() == null) {
|
|
|
62
|
+ dosing.setStatus("active");
|
|
|
63
|
+ }
|
|
|
64
|
+ dosing.setCreatedTime(LocalDateTime.now());
|
|
|
65
|
+ dosing.setUpdatedTime(LocalDateTime.now());
|
|
|
66
|
+ dosingMapper.insert(dosing);
|
|
|
67
|
+ saveDosingRecord(dosing);
|
|
|
68
|
+ return dosing;
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ /** 更新投加记录 */
|
|
|
72
|
+ public boolean updateDosing(Long id, ChemicalDosing dosing) {
|
|
|
73
|
+ dosing.setId(id);
|
|
|
74
|
+ dosing.setUpdatedTime(LocalDateTime.now());
|
|
|
75
|
+ int rows = dosingMapper.updateById(dosing);
|
|
|
76
|
+ if (rows > 0) {
|
|
|
77
|
+ saveDosingRecord(dosing);
|
|
|
78
|
+ }
|
|
|
79
|
+ return rows > 0;
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ /** 删除投加记录 */
|
|
|
83
|
+ public boolean deleteDosing(Long id) {
|
|
|
84
|
+ return dosingMapper.deleteById(id) > 0;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ // ==================== 投加记录 + 趋势分析 ====================
|
|
|
88
|
+
|
|
|
89
|
+ /** 投加记录列表 */
|
|
|
90
|
+ public Map<String, Object> listRecords(int page, int size, String processStage, String chemicalName,
|
|
|
91
|
+ String startTime, String endTime) {
|
|
|
92
|
+ LambdaQueryWrapper<DosingRecord> wrapper = new LambdaQueryWrapper<>();
|
|
|
93
|
+ wrapper.eq(StringUtils.hasText(processStage), DosingRecord::getProcessStage, processStage)
|
|
|
94
|
+ .eq(StringUtils.hasText(chemicalName), DosingRecord::getChemicalName, chemicalName);
|
|
|
95
|
+ if (StringUtils.hasText(startTime)) {
|
|
|
96
|
+ wrapper.ge(DosingRecord::getRecordTime, LocalDateTime.parse(startTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
|
|
97
|
+ }
|
|
|
98
|
+ if (StringUtils.hasText(endTime)) {
|
|
|
99
|
+ wrapper.le(DosingRecord::getRecordTime, LocalDateTime.parse(endTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
|
|
100
|
+ }
|
|
|
101
|
+ wrapper.orderByDesc(DosingRecord::getRecordTime);
|
|
|
102
|
+ Page<DosingRecord> result = recordMapper.selectPage(new Page<>(page, size), wrapper);
|
|
|
103
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
104
|
+ data.put("records", result.getRecords());
|
|
|
105
|
+ data.put("total", result.getTotal());
|
|
|
106
|
+ return data;
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ /** 趋势分析 */
|
|
|
110
|
+ public List<Map<String, Object>> trendAnalysis(String processStage, String startTime, String endTime) {
|
|
|
111
|
+ return recordMapper.trendByHour(processStage, startTime, endTime);
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ // ==================== 药耗统计 ====================
|
|
|
115
|
+
|
|
|
116
|
+ /** 药耗统计:日/周/月 + 单位产水药耗 */
|
|
|
117
|
+ public Map<String, Object> statistics(String period, String station) {
|
|
|
118
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
119
|
+ LocalDateTime start;
|
|
|
120
|
+ LocalDateTime end = now;
|
|
|
121
|
+
|
|
|
122
|
+ switch (period != null ? period : "day") {
|
|
|
123
|
+ case "week":
|
|
|
124
|
+ start = now.minusWeeks(1);
|
|
|
125
|
+ break;
|
|
|
126
|
+ case "month":
|
|
|
127
|
+ start = now.minusMonths(1);
|
|
|
128
|
+ break;
|
|
|
129
|
+ default:
|
|
|
130
|
+ start = now.minusDays(1);
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ String startTime = start.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
|
|
134
|
+ String endTime = end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
|
|
135
|
+ List<Map<String, Object>> rawStats = dosingMapper.statisticsByPeriod(startTime, endTime);
|
|
|
136
|
+
|
|
|
137
|
+ if (StringUtils.hasText(station)) {
|
|
|
138
|
+ rawStats = rawStats.stream()
|
|
|
139
|
+ .filter(m -> station.equals(m.get("station")))
|
|
|
140
|
+ .collect(Collectors.toList());
|
|
|
141
|
+ }
|
|
|
142
|
+
|
|
|
143
|
+ BigDecimal totalAmount = rawStats.stream()
|
|
|
144
|
+ .map(m -> m.get("total_amount") != null ? new BigDecimal(m.get("total_amount").toString()) : BigDecimal.ZERO)
|
|
|
145
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
146
|
+
|
|
|
147
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
148
|
+ result.put("period", period);
|
|
|
149
|
+ result.put("details", rawStats);
|
|
|
150
|
+ result.put("totalAmount", totalAmount.setScale(4, RoundingMode.HALF_UP));
|
|
|
151
|
+ result.put("unitConsumption", calculateUnitConsumption(totalAmount, start, end));
|
|
|
152
|
+ return result;
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ private BigDecimal calculateUnitConsumption(BigDecimal totalAmount, LocalDateTime start, LocalDateTime end) {
|
|
|
156
|
+ long hours = java.time.Duration.between(start, end).toHours();
|
|
|
157
|
+ if (hours <= 0) hours = 1;
|
|
|
158
|
+ BigDecimal totalWater = BigDecimal.valueOf(hours * 100L);
|
|
|
159
|
+ return totalAmount.divide(totalWater, 6, RoundingMode.HALF_UP);
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ // ==================== 投加策略 ====================
|
|
|
163
|
+
|
|
|
164
|
+ public List<DosingStrategy> listStrategies(String processStage, Boolean enabled) {
|
|
|
165
|
+ LambdaQueryWrapper<DosingStrategy> wrapper = new LambdaQueryWrapper<>();
|
|
|
166
|
+ wrapper.eq(StringUtils.hasText(processStage), DosingStrategy::getProcessStage, processStage)
|
|
|
167
|
+ .eq(enabled != null, DosingStrategy::getEnabled, enabled)
|
|
|
168
|
+ .orderByAsc(DosingStrategy::getProcessStage);
|
|
|
169
|
+ return strategyMapper.selectList(wrapper);
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+ public DosingStrategy getStrategyById(Long id) {
|
|
|
173
|
+ return strategyMapper.selectById(id);
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ public DosingStrategy createStrategy(DosingStrategy strategy) {
|
|
|
177
|
+ strategy.setCreatedTime(LocalDateTime.now());
|
|
|
178
|
+ strategy.setUpdatedTime(LocalDateTime.now());
|
|
|
179
|
+ if (strategy.getEnabled() == null) strategy.setEnabled(true);
|
|
|
180
|
+ strategyMapper.insert(strategy);
|
|
|
181
|
+ return strategy;
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ public boolean updateStrategy(Long id, DosingStrategy strategy) {
|
|
|
185
|
+ strategy.setId(id);
|
|
|
186
|
+ strategy.setUpdatedTime(LocalDateTime.now());
|
|
|
187
|
+ return strategyMapper.updateById(strategy) > 0;
|
|
|
188
|
+ }
|
|
|
189
|
+
|
|
|
190
|
+ public boolean deleteStrategy(Long id) {
|
|
|
191
|
+ return strategyMapper.deleteById(id) > 0;
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
|
194
|
+ /** 根据策略计算推荐投加量 */
|
|
|
195
|
+ public Map<String, Object> calculateRecommendedDosing(Long strategyId, BigDecimal currentTurbidity,
|
|
|
196
|
+ BigDecimal currentFlow, BigDecimal currentPh) {
|
|
|
197
|
+ DosingStrategy strategy = strategyMapper.selectById(strategyId);
|
|
|
198
|
+ if (strategy == null || !Boolean.TRUE.equals(strategy.getEnabled())) {
|
|
|
199
|
+ return Map.of("error", "策略不存在或未启用");
|
|
|
200
|
+ }
|
|
|
201
|
+
|
|
|
202
|
+ BigDecimal recommendedRate = strategy.getBaseDosingRate() != null ? strategy.getBaseDosingRate() : BigDecimal.ZERO;
|
|
|
203
|
+
|
|
|
204
|
+ if (currentTurbidity != null && strategy.getTurbidityThreshold() != null) {
|
|
|
205
|
+ if (currentTurbidity.compareTo(strategy.getTurbidityThreshold()) > 0) {
|
|
|
206
|
+ BigDecimal factor = currentTurbidity.divide(strategy.getTurbidityThreshold(), 4, RoundingMode.HALF_UP);
|
|
|
207
|
+ recommendedRate = recommendedRate.multiply(factor);
|
|
|
208
|
+ }
|
|
|
209
|
+ }
|
|
|
210
|
+
|
|
|
211
|
+ if (currentFlow != null && strategy.getFlowThreshold() != null) {
|
|
|
212
|
+ if (currentFlow.compareTo(strategy.getFlowThreshold()) > 0) {
|
|
|
213
|
+ BigDecimal flowFactor = currentFlow.divide(strategy.getFlowThreshold(), 4, RoundingMode.HALF_UP);
|
|
|
214
|
+ recommendedRate = recommendedRate.multiply(flowFactor);
|
|
|
215
|
+ }
|
|
|
216
|
+ }
|
|
|
217
|
+
|
|
|
218
|
+ if (strategy.getMinDosingRate() != null) {
|
|
|
219
|
+ recommendedRate = recommendedRate.max(strategy.getMinDosingRate());
|
|
|
220
|
+ }
|
|
|
221
|
+ if (strategy.getMaxDosingRate() != null) {
|
|
|
222
|
+ recommendedRate = recommendedRate.min(strategy.getMaxDosingRate());
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
226
|
+ result.put("strategyId", strategyId);
|
|
|
227
|
+ result.put("strategyName", strategy.getStrategyName());
|
|
|
228
|
+ result.put("recommendedRate", recommendedRate.setScale(4, RoundingMode.HALF_UP));
|
|
|
229
|
+ result.put("chemicalName", strategy.getChemicalName());
|
|
|
230
|
+ result.put("processStage", strategy.getProcessStage());
|
|
|
231
|
+ return result;
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ // ==================== 药剂库存 ====================
|
|
|
235
|
+
|
|
|
236
|
+ public List<ChemicalStock> listStocks(String station, String status) {
|
|
|
237
|
+ LambdaQueryWrapper<ChemicalStock> wrapper = new LambdaQueryWrapper<>();
|
|
|
238
|
+ wrapper.eq(StringUtils.hasText(station), ChemicalStock::getStation, station)
|
|
|
239
|
+ .eq(StringUtils.hasText(status), ChemicalStock::getStatus, status)
|
|
|
240
|
+ .orderByAsc(ChemicalStock::getStatus);
|
|
|
241
|
+ return stockMapper.selectList(wrapper);
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ public ChemicalStock createStock(ChemicalStock stock) {
|
|
|
245
|
+ stock.setCreatedTime(LocalDateTime.now());
|
|
|
246
|
+ stock.setUpdatedTime(LocalDateTime.now());
|
|
|
247
|
+ updateStockStatus(stock);
|
|
|
248
|
+ stockMapper.insert(stock);
|
|
|
249
|
+ return stock;
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+ public boolean updateStock(Long id, ChemicalStock stock) {
|
|
|
253
|
+ stock.setId(id);
|
|
|
254
|
+ stock.setUpdatedTime(LocalDateTime.now());
|
|
|
255
|
+ updateStockStatus(stock);
|
|
|
256
|
+ return stockMapper.updateById(stock) > 0;
|
|
|
257
|
+ }
|
|
|
258
|
+
|
|
|
259
|
+ public List<Map<String, Object>> lowStockAlerts() {
|
|
|
260
|
+ LambdaQueryWrapper<ChemicalStock> wrapper = new LambdaQueryWrapper<>();
|
|
|
261
|
+ wrapper.eq(ChemicalStock::getStatus, "low").or().eq(ChemicalStock::getStatus, "out");
|
|
|
262
|
+ List<ChemicalStock> lowStocks = stockMapper.selectList(wrapper);
|
|
|
263
|
+
|
|
|
264
|
+ return lowStocks.stream().map(s -> {
|
|
|
265
|
+ Map<String, Object> alert = new HashMap<>();
|
|
|
266
|
+ alert.put("id", s.getId());
|
|
|
267
|
+ alert.put("chemicalName", s.getChemicalName());
|
|
|
268
|
+ alert.put("currentStock", s.getCurrentStock());
|
|
|
269
|
+ alert.put("minStock", s.getMinStock());
|
|
|
270
|
+ alert.put("status", s.getStatus());
|
|
|
271
|
+ alert.put("station", s.getStation());
|
|
|
272
|
+ alert.put("warehouse", s.getWarehouse());
|
|
|
273
|
+ BigDecimal deficit = s.getMinStock() != null && s.getCurrentStock() != null
|
|
|
274
|
+ ? s.getMinStock().subtract(s.getCurrentStock()) : BigDecimal.ZERO;
|
|
|
275
|
+ alert.put("deficit", deficit.max(BigDecimal.ZERO));
|
|
|
276
|
+ return alert;
|
|
|
277
|
+ }).collect(Collectors.toList());
|
|
|
278
|
+ }
|
|
|
279
|
+
|
|
|
280
|
+ private void updateStockStatus(ChemicalStock stock) {
|
|
|
281
|
+ if (stock.getCurrentStock() == null || stock.getMinStock() == null) {
|
|
|
282
|
+ stock.setStatus("normal");
|
|
|
283
|
+ return;
|
|
|
284
|
+ }
|
|
|
285
|
+ if (stock.getCurrentStock().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
286
|
+ stock.setStatus("out");
|
|
|
287
|
+ } else if (stock.getCurrentStock().compareTo(stock.getMinStock()) < 0) {
|
|
|
288
|
+ stock.setStatus("low");
|
|
|
289
|
+ } else {
|
|
|
290
|
+ stock.setStatus("normal");
|
|
|
291
|
+ }
|
|
|
292
|
+ }
|
|
|
293
|
+
|
|
|
294
|
+ private void saveDosingRecord(ChemicalDosing dosing) {
|
|
|
295
|
+ DosingRecord record = new DosingRecord();
|
|
|
296
|
+ record.setDosingId(dosing.getId());
|
|
|
297
|
+ record.setProcessStage(dosing.getProcessStage());
|
|
|
298
|
+ record.setChemicalName(dosing.getChemicalName());
|
|
|
299
|
+ record.setDosingAmount(dosing.getDosingAmount());
|
|
|
300
|
+ record.setDosingRate(dosing.getDosingRate());
|
|
|
301
|
+ record.setConcentration(dosing.getConcentration());
|
|
|
302
|
+ record.setFlowRate(dosing.getFlowRate());
|
|
|
303
|
+ record.setStation(dosing.getStation());
|
|
|
304
|
+ record.setRecordTime(LocalDateTime.now());
|
|
|
305
|
+ record.setCreatedTime(LocalDateTime.now());
|
|
|
306
|
+ recordMapper.insert(record);
|
|
|
307
|
+ }
|
|
|
308
|
+}
|