EquipmentManageStatisticsController.java
27.4 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package com.skua.modules.equipment.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.aop.annotation.CustomExceptionAnno;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.util.DateUtils;
import com.skua.modules.common.service.ICommonSqlService;
import com.skua.modules.equipment.entity.*;
import com.skua.modules.equipment.pojo.Equipment;
import com.skua.modules.equipment.pojo.EquipmentInfoJoinRepair;
import com.skua.modules.equipment.service.IEquipmentInfoService;
import com.skua.modules.equipment.util.EquipmentUtils;
import com.skua.modules.equipment.vo.EquipChartsVO;
import com.skua.modules.equipment.vo.EquipStatisticVO;
import com.skua.modules.equipment.vo.EquipTableVO;
import com.skua.modules.equipment.vo.EquipmentSparepartVO;
import com.skua.modules.flow.business.service.FlowBusinessService;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.query.WrapperFactory;
import com.skua.tool.util.BeanExtUtils;
import com.skua.tool.util.CustomPageUtils;
import com.skua.tool.util.JoinSqlUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author sonin
* @date 2021/11/16 10:41
*/
@Slf4j
@Api(tags = "设备管理统计分析")
@RestController
@RequestMapping("/equipment/statistic")
public class EquipmentManageStatisticsController {
@Resource
private ICommonSqlService commonSqlService;
@Resource
private CustomRedisUtil customRedisUtil;
@Resource
private FlowBusinessService flowBusinessService;
@Resource
private IEquipmentInfoService equipmentInfoService;
@CustomExceptionAnno(description = "设备管理统计分析-设备保养")
@AutoLog(value = "设备管理统计分析-设备保养")
@ApiOperation(value = "设备管理统计分析-设备保养", notes = "设备管理统计分析-设备保养")
@GetMapping(value = "/upkeep")
public Result<Object> upkeepCtrl(@RequestParam(name = "departId", defaultValue = "") String departId,
@RequestParam(name = "time", defaultValue = "") String time,
@RequestParam(name = "type", defaultValue = "") String type,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
Result<Object> result = new Result<>();
List<Map<String, Object>> mapList = WrapperFactory.whereWrapper()
.from(EquipmentMaintenancePlan.class, EquipmentMaintenanceTask.class)
.and(EquipmentMaintenancePlan.class.getDeclaredField("id"), EquipmentMaintenanceTask.class.getDeclaredField("planId"))
.where()
.eq(StringUtils.isNotEmpty(departId), "EquipmentMaintenancePlan_departId", departId)
.between(StringUtils.isNotEmpty(time), "EquipmentMaintenanceTask_taskStartTime", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59")
.queryWrapperForList();
EquipStatisticVO equipStatisticVO = new EquipStatisticVO();
// 设备id => name
List<EquipmentInfo> equipmentInfoList = equipmentInfoService.list();
Map<String, String> id2NameMap = equipmentInfoList.stream().collect(Collectors.toMap(EquipmentInfo::getId, EquipmentInfo::getEquipmentName));
String departName = "" + customRedisUtil.hget("sys_depart", departId, "id", "depart_name");
List<EquipStatisticVO.TableData> allList = new ArrayList<>();
List<EquipStatisticVO.TableData> todoList = new ArrayList<>();
List<EquipStatisticVO.TableData> doingList = new ArrayList<>();
List<EquipStatisticVO.TableData> doneList = new ArrayList<>();
for (Map<String, Object> item : mapList) {
EquipStatisticVO.TableData tableData = new EquipStatisticVO.TableData();
tableData.setDepartId(departId);
tableData.setDepartName(departName);
// 设备名称
String infoIds = "" + item.get("EquipmentMaintenancePlan_infoIds");
StringBuilder stringBuilder = new StringBuilder();
for (String infoId : infoIds.split(",")) {
stringBuilder.append(",").append(id2NameMap.get(infoId));
}
tableData.setEquipmentName(stringBuilder.toString().replaceFirst(",", ""));
tableData.setName("" + item.get("EquipmentMaintenancePlan_planName"));
if ("1".equals("" + item.get("EquipmentMaintenanceTask_taskResult"))) {
equipStatisticVO.getLineData().setDoing(equipStatisticVO.getLineData().getDoing() + 1);
tableData.setStatus("保养中");
doingList.add(tableData);
} else if ("2".equals("" + item.get("EquipmentMaintenanceTask_taskResult"))) {
equipStatisticVO.getLineData().setDone(equipStatisticVO.getLineData().getDone() + 1);
tableData.setStatus("已完成");
doneList.add(tableData);
} else {
equipStatisticVO.getLineData().setTodo(equipStatisticVO.getLineData().getTodo() + 1);
tableData.setStatus("待保养");
todoList.add(tableData);
}
allList.add(tableData);
}
if ("todo".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, todoList));
equipStatisticVO.getTableData().setTotal(todoList.size());
} else if ("doing".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, doingList));
equipStatisticVO.getTableData().setTotal(doingList.size());
} else if ("done".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, doneList));
equipStatisticVO.getTableData().setTotal(doneList.size());
} else {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, allList));
equipStatisticVO.getTableData().setTotal(allList.size());
}
equipStatisticVO.getTableData().setTotal(mapList.size());
result.setResult(equipStatisticVO);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-设备维修")
@AutoLog(value = "设备管理统计分析-设备维修")
@ApiOperation(value = "设备管理统计分析-设备维修", notes = "设备管理统计分析-设备维修")
@GetMapping(value = "/maintenance")
public Result<Object> maintenanceCtrl(@RequestParam(name = "departId", defaultValue = "") String departId, @RequestParam(name = "time", defaultValue = "") String time,
@RequestParam(name = "type", defaultValue = "") String type,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
Result<Object> result = new Result<>();
List<Map<String, Object>> mapList = WrapperFactory.whereWrapper()
.from(EquipmentInfo.class, EquipmentRepair.class)
.and(EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
.where()
.eq(StringUtils.isNotEmpty(departId), "EquipmentInfo_departId", departId)
.between(StringUtils.isNotEmpty(time), "EquipmentRepair_repairDate", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59")
.queryWrapperForList();
String departName = "" + customRedisUtil.hget("sys_depart", departId, "id", "depart_name");
EquipStatisticVO equipStatisticVO = new EquipStatisticVO();
Set<String> processInstanceIdSet = new HashSet<>();
for (Map<String, Object> item : mapList) {
if (item.get("EquipmentRepair_processInstanceId") != null && !"".equals(item.get("EquipmentRepair_processInstanceId"))) {
processInstanceIdSet.add("" + item.get("EquipmentRepair_processInstanceId"));
}
}
Map<String, Map<String, String>> processInstanceIdMap;
if (!processInstanceIdSet.isEmpty()) {
processInstanceIdMap = flowBusinessService.getProcessState(processInstanceIdSet);
} else {
processInstanceIdMap = new HashMap<>();
}
List<EquipStatisticVO.TableData> allList = new ArrayList<>();
List<EquipStatisticVO.TableData> todoList = new ArrayList<>();
List<EquipStatisticVO.TableData> doingList = new ArrayList<>();
List<EquipStatisticVO.TableData> doneList = new ArrayList<>();
for (Map<String, Object> item : mapList) {
EquipStatisticVO.TableData tableData = new EquipStatisticVO.TableData();
tableData.setDepartId(departId);
tableData.setDepartName(departName);
tableData.setEquipmentName("" + item.get("EquipmentInfo_equipmentName"));
if (item.get("EquipmentRepair_faultReason") != null) {
tableData.setName("" + item.get("EquipmentRepair_faultReason"));
tableData.setFaultReason("" + item.get("EquipmentRepair_faultReason"));
}
if (item.get("EquipmentRepair_processDefinitionId") == null || item.get("EquipmentRepair_processInstanceId") == null) {
equipStatisticVO.getLineData().setTodo(equipStatisticVO.getLineData().getTodo() + 1);
tableData.setStatus("待维修");
todoList.add(tableData);
} else {
String status = processInstanceIdMap.getOrDefault("" + item.get("EquipmentRepair_processInstanceId"), new HashMap<>()).get("state");
if ("unfinished".equals(status)) {
equipStatisticVO.getLineData().setDoing(equipStatisticVO.getLineData().getDoing() + 1);
tableData.setStatus("维修中");
doingList.add(tableData);
} else if ("finished".equals(status)) {
equipStatisticVO.getLineData().setDone(equipStatisticVO.getLineData().getDone() + 1);
tableData.setStatus("已完成");
doneList.add(tableData);
} else {
equipStatisticVO.getLineData().setTodo(equipStatisticVO.getLineData().getTodo() + 1);
tableData.setStatus("待维修");
todoList.add(tableData);
}
}
allList.add(tableData);
}
if ("todo".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, todoList));
equipStatisticVO.getTableData().setTotal(todoList.size());
} else if ("doing".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, doingList));
equipStatisticVO.getTableData().setTotal(doingList.size());
} else if ("done".equals(type)) {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, doneList));
equipStatisticVO.getTableData().setTotal(doneList.size());
} else {
equipStatisticVO.getTableData().setRecords(CustomPageUtils.curPage(pageNo, pageSize, allList));
equipStatisticVO.getTableData().setTotal(allList.size());
}
result.setResult(equipStatisticVO);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-设备保养维修图表")
@AutoLog(value = "设备管理统计分析-设备保养维修图表")
@ApiOperation(value = "设备管理统计分析-设备保养维修图表", notes = "设备管理统计分析-设备保养维修图表")
@GetMapping(value = "/byWxCharts")
public Result<Object> byWxChartsCtrl(@RequestParam(name = "departId", defaultValue = "") String departId, @RequestParam(name = "time", defaultValue = "") String time) throws Exception {
Result<Object> result = new Result<>();
String yearStr, startTime, endTime;
if (StringUtils.isNotEmpty(time)) {
yearStr = time.split("-")[0];
} else {
Calendar calendar = Calendar.getInstance();
yearStr = String.valueOf(calendar.get(Calendar.YEAR));
}
startTime = yearStr + "-01-01 00:00:00";
endTime = yearStr + "-12-31 23:59:59";
// 维修SQL
String wxSql = JoinSqlUtils.multiJoinSqlQueryWithoutWhere(new EquipmentInfoJoinRepair());
wxSql = "select ifnull(sum(EquipmentRepair_costHour), 0) as hours, count(*) as times, date_format(EquipmentRepair_repairDate, '%Y-%m') as month from (" + wxSql + ") as res";
QueryWrapper<?> wxQueryWrapper = new QueryWrapper<>();
wxQueryWrapper.eq(StringUtils.isNotEmpty(departId), "EquipmentInfo_departId", departId)
.between("EquipmentRepair_repairDate", startTime, endTime)
.groupBy("date_format(EquipmentRepair_repairDate, '%Y-%m')").orderByAsc("date_format(EquipmentRepair_repairDate, '%Y-%m')");
List<Map<String, Object>> wxMapList = commonSqlService.queryWrapperForList(wxSql, wxQueryWrapper);
Map<String, Map<String, Object>> wxMonth2EntityMap = new HashMap<>();
for (Map<String, Object> item : wxMapList) {
wxMonth2EntityMap.put("" + item.get("month"), item);
}
// 保养SQL
String bySql = JoinSqlUtils.singleJoinSqlQueryWithoutWhere(new EquipmentMaintenanceRecordChild());
bySql = "select ifnull(sum(EquipmentMaintenanceRecord_actualCostTime), 0) as hours, count(*) as times, date_format(EquipmentMaintenanceRecord_taskStartTime, '%Y-%m') as month from (" + bySql + ") as res";
QueryWrapper<?> byQueryWrapper = new QueryWrapper<>();
byQueryWrapper.eq(StringUtils.isNotEmpty(departId), "EquipmentMaintenanceRecord_departId", departId)
.between("EquipmentMaintenanceRecord_taskStartTime", startTime, endTime)
.groupBy("date_format(EquipmentMaintenanceRecord_taskStartTime, '%Y-%m')").orderByAsc("date_format(EquipmentMaintenanceRecord_taskStartTime, '%Y-%m')");
List<Map<String, Object>> byMapList = commonSqlService.queryWrapperForList(bySql, byQueryWrapper);
Map<String, Map<String, Object>> byMonth2EntityMap = new HashMap<>();
for (Map<String, Object> item : byMapList) {
byMonth2EntityMap.put("" + item.get("month"), item);
}
List<String> yearMonthList = Arrays.asList(yearStr + "-01", yearStr + "-02", yearStr + "-03", yearStr + "-04", yearStr + "-05", yearStr + "-06", yearStr + "-07", yearStr + "-08", yearStr + "-09", yearStr + "-10", yearStr + "-11", yearStr + "-12");
Map<String, EquipChartsVO> equipChartsVOMap = new HashMap<>();
equipChartsVOMap.put("wx", new EquipChartsVO());
equipChartsVOMap.put("by", new EquipChartsVO());
for (String yearMonth : yearMonthList) {
EquipChartsVO wxVO = equipChartsVOMap.get("wx");
wxVO.getXData().add(yearMonth);
Map<String, Object> wxEntity = wxMonth2EntityMap.get(yearMonth);
if (wxEntity == null) {
wxVO.getHoursYData().add("0");
wxVO.getTimesYData().add("0");
} else {
wxVO.getHoursYData().add("" + wxEntity.getOrDefault("hours", "0"));
wxVO.getTimesYData().add("" + wxEntity.getOrDefault("times", "0"));
}
EquipChartsVO byVO = equipChartsVOMap.get("by");
byVO.getXData().add(yearMonth);
Map<String, Object> byEntity = byMonth2EntityMap.get(yearMonth);
if (byEntity == null) {
byVO.getHoursYData().add("0");
byVO.getTimesYData().add("0");
} else {
byVO.getHoursYData().add("" + byEntity.getOrDefault("hours", "0"));
byVO.getTimesYData().add("" + byEntity.getOrDefault("times", "0"));
}
}
result.setResult(equipChartsVOMap);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-设备保养维修table")
@AutoLog(value = "设备管理统计分析-设备保养维修table")
@ApiOperation(value = "设备管理统计分析-设备保养维修table", notes = "设备管理统计分析-设备保养维修table")
@GetMapping(value = "/byWxTable")
public Result<List<EquipTableVO>> byWxTableCtrl(@RequestParam(name = "departId", defaultValue = "") String departId,
@RequestParam(name = "time", defaultValue = "") String time) throws Exception {
Result<List<EquipTableVO>> result = new Result<>();
String yearStr, startTime, endTime;
if (StringUtils.isNotEmpty(time)) {
yearStr = time.split("-")[0];
} else {
Calendar calendar = Calendar.getInstance();
yearStr = String.valueOf(calendar.get(Calendar.YEAR));
}
startTime = yearStr + "-01-01 00:00:00";
endTime = yearStr + "-12-31 23:59:59";
// 维修SQL
String wxSql = JoinSqlUtils.multiJoinSqlQueryWithoutWhere(new EquipmentInfoJoinRepair());
wxSql = wxSql.replaceFirst("select ", "select ifnull(sum(EquipmentRepair_costHour), 0) as wxHours, count(*) as wxTimes,");
QueryWrapper<?> wxQueryWrapper = new QueryWrapper<>();
wxQueryWrapper.eq(StringUtils.isNotEmpty(departId), "EquipmentInfo_departId", departId)
.between("EquipmentRepair_repairDate", startTime, endTime)
.groupBy("EquipmentRepair_infoId");
List<Map<String, Object>> wxMapList = commonSqlService.queryWrapperForList(wxSql, wxQueryWrapper);
// 保养SQL
String bySql = JoinSqlUtils.singleJoinSqlQueryWithoutWhere(new EquipmentMaintenanceRecordChild());
bySql = bySql.replaceFirst("select ", "select ifnull(sum(EquipmentMaintenanceRecord_actualCostTime), 0) as byHours, count(*) as byTimes,");
QueryWrapper<?> byQueryWrapper = new QueryWrapper<>();
byQueryWrapper.eq(StringUtils.isNotEmpty(departId), "EquipmentMaintenanceRecord_departId", departId)
.between("EquipmentMaintenanceRecord_taskStartTime", startTime, endTime)
.groupBy("EquipmentMaintenanceRecordChild_infoId");
List<Map<String, Object>> byMapList = commonSqlService.queryWrapperForList(bySql, byQueryWrapper);
// 聚合结果
List<EquipTableVO> aggList = new ArrayList<>();
// 聚合维修结果
for (Map<String, Object> item : wxMapList) {
EquipTableVO equipTableVO = new EquipTableVO();
equipTableVO.setEquipmentId("" + item.get("EquipmentInfo_id"));
equipTableVO.setEquipmentName("" + item.get("EquipmentInfo_equipmentName"));
equipTableVO.setWxHours("" + item.get("wxHours"));
equipTableVO.setWxTimes("" + item.get("wxTimes"));
aggList.add(equipTableVO);
}
// 聚合保养结果
for (Map<String, Object> item : byMapList) {
EquipTableVO equipTableVO = new EquipTableVO();
equipTableVO.setEquipmentId("" + item.get("EquipmentInfo_id"));
equipTableVO.setEquipmentName("" + item.get("EquipmentInfo_equipmentName"));
equipTableVO.setByHours("" + item.get("byHours"));
equipTableVO.setByTimes("" + item.get("byTimes"));
aggList.add(equipTableVO);
}
List<EquipTableVO> resList = new ArrayList<>();
// 聚合equipmentId相同的值
aggList.parallelStream()
.collect(Collectors.groupingBy(EquipTableVO::getEquipmentId, Collectors.toList()))
.forEach((id, transfer) -> transfer.stream().reduce((a, b) -> new EquipTableVO(a.getEquipmentId(), a.getEquipmentName(), a.getByTimes() + b.getByTimes(), a.getByHours() + b.getByHours(), a.getWxTimes() + b.getWxTimes(), a.getWxHours() + b.getWxHours())).ifPresent(resList::add));
result.setResult(resList);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-重点关注设备")
@AutoLog(value = "设备管理统计分析-重点关注设备")
@ApiOperation(value = "设备管理统计分析-重点关注设备", notes = "设备管理统计分析-重点关注设备")
@GetMapping(value = "/focusOnEquipment")
public Result<Object> focusOnEquipmentCtrl(@RequestParam(name = "departId", defaultValue = "") String departId,
@RequestParam(name = "time", defaultValue = "") String time) throws Exception {
Result<Object> result = new Result<>();
// 连续3次故障周期缩小
// 累计运行时长超限
// 到达建议更换时间
String sql = JoinSqlUtils.multiJoinSqlQueryWithoutWhere(new Equipment());
QueryWrapper<?> queryWrapper = new QueryWrapper<>();
// queryWrapper.between(StringUtils.isNotEmpty(time), "EquipmentInfo_createTime", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59");
String currentDay = DateUtils.getDate("YYYY-MM-dd");
List<Map<String, Object>> mapList = commonSqlService.queryWrapperForList(sql, queryWrapper);
List<Map<String, Object>> resList = new ArrayList<>();
for (Map<String, Object> map : mapList) {
if (map.get("EquipmentInfo_adviceReplaceDate") == null) {
continue;
}
String adviceReplaceDate = map.get("EquipmentInfo_adviceReplaceDate").toString();
if (currentDay.compareTo(adviceReplaceDate) < 0) {
Map<String, Object> res = new HashMap<>(2);
res.put("status", "到达建议更换时间");
res.put("equipmentName", map.get("EquipmentInfo_equipmentName"));
res.put("happenTime", new Date());
resList.add(res);
}
}
result.setResult(resList);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-备件库存预警")
@AutoLog(value = "设备管理统计分析-备件库存预警")
@ApiOperation(value = "设备管理统计分析-备件库存预警", notes = "设备管理统计分析-备件库存预警")
@GetMapping(value = "/sparepartWarn")
public Result<Object> sparepartWarnCtrl(@RequestParam(name = "departId", defaultValue = "") String departId,
@RequestParam(name = "time", defaultValue = "") String time) throws Exception {
Result<Object> result = new Result<>();
Map<String, List<EquipmentSparepartVO>> zhName2ListMap = new HashMap<>();
String baseSql = JoinSqlUtils.singleJoinSqlQueryWithoutWhere(new EquipmentSparepart());
QueryWrapper<?> lessQueryWrapper = new QueryWrapper<>();
QueryWrapper<?> moreQueryWrapper = new QueryWrapper<>();
// lessQueryWrapper.between(StringUtils.isNotEmpty(time), "EquipmentSparepart_createTime", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59");
lessQueryWrapper.apply("EquipmentSparepart_storageNum < EquipmentSparepart_storageMinNum")
.orderByAsc("EquipmentSparepart_storageNum")
.last("limit 3");
// moreQueryWrapper.between(StringUtils.isNotEmpty(time), "EquipmentSparepart_createTime", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59");
moreQueryWrapper.apply("EquipmentSparepart_storageNum > EquipmentSparepart_storageMaxNum")
.orderByAsc("EquipmentSparepart_storageNum")
.last("limit 3");
List<Map<String, Object>> lessMapList = commonSqlService.queryWrapperForList(baseSql, lessQueryWrapper);
List<Map<String, Object>> moreMapList = commonSqlService.queryWrapperForList(baseSql, moreQueryWrapper);
List<EquipmentSparepart> lessEntityList = JoinSqlUtils.singleMaps2Beans(lessMapList, EquipmentSparepart.class);
List<EquipmentSparepart> moreEntityList = JoinSqlUtils.singleMaps2Beans(moreMapList, EquipmentSparepart.class);
Map<String, String> sparepartTypeVal2KeyMap = EquipmentUtils.convertFunc("sparepartType", false);
List<EquipmentSparepartVO> lessVOList = BeanExtUtils.beans2Beans(lessEntityList, EquipmentSparepartVO.class, (targetFieldName, srcFieldVal) -> {
if ("departIdName".equals(targetFieldName) && srcFieldVal != null) {
return customRedisUtil.hget("sys_depart", srcFieldVal.toString(), "id", "depart_name");
} else if ("sparepartType_dictText".equals(targetFieldName)) {
return sparepartTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "" + srcFieldVal);
}
return "";
});
List<EquipmentSparepartVO> moreVOList = BeanExtUtils.beans2Beans(moreEntityList, EquipmentSparepartVO.class, (targetFieldName, srcFieldVal) -> {
if ("departIdName".equals(targetFieldName) && srcFieldVal != null) {
return customRedisUtil.hget("sys_depart", srcFieldVal.toString(), "id", "depart_name");
} else if ("sparepartType_dictText".equals(targetFieldName)) {
return sparepartTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "" + srcFieldVal);
}
return "";
});
zhName2ListMap.put("库存不足", lessVOList);
zhName2ListMap.put("库存过量", moreVOList);
result.setSuccess(true);
result.setResult(zhName2ListMap);
return result;
}
@CustomExceptionAnno(description = "设备管理统计分析-备件库存消耗")
@AutoLog(value = "设备管理统计分析-备件库存消耗")
@ApiOperation(value = "设备管理统计分析-备件库存消耗", notes = "设备管理统计分析-备件库存消耗")
@GetMapping(value = "/sparepartUse")
public Result<Object> sparepartUseCtrl(@RequestParam(name = "departId", defaultValue = "") String departId,
@RequestParam(name = "time", defaultValue = "") String time) throws Exception {
Result<Object> result = new Result<>();
List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
.select("sum(equipment_out_child.out_num) as EquipmentOutChild_outTotalNum")
.select(EquipmentSparepart.class.getDeclaredField("storageNum"), EquipmentSparepart.class.getDeclaredField("sparepartName"))
.from(EquipmentOutChild.class)
.innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentOutChild.class.getDeclaredField("sparepartId"))
.where()
.between(StringUtils.isNotEmpty(time), "equipment_out_child.create_time", time + "-01 00:00:00", DateUtils.getLastDayOfMonth(time) + " 23:59:59")
.groupBy(true, "equipment_out_child.sparepart_id")
.orderBy(true, false, "EquipmentOutChild_outTotalNum")
.last(true, "limit 3")
.queryWrapperForList();
mapList = WrapperFactory.result()
.maps2MapsWithoutPrefix(mapList);
result.setResult(mapList);
return result;
}
}