AssetAnalysisController.java
10.3 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
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.modules.equipment.entity.EquipmentAsset;
import com.skua.modules.equipment.entity.EquipmentInfo;
import com.skua.modules.equipment.entity.EquipmentOutChild;
import com.skua.modules.equipment.entity.EquipmentSparepart;
import com.skua.modules.equipment.service.IEquipmentSparepartService;
import com.skua.modules.equipment.util.EquipmentUtils;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.query.WrapperFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author sonin
* @date 2021/12/8 11:19
*/
@Slf4j
@Api(tags = "资产分析")
@RestController
@RequestMapping("/equipment/assetAnalysis")
public class AssetAnalysisController {
@Autowired
private CustomRedisUtil customRedisUtil;
@Autowired
private IEquipmentSparepartService equipmentSparepartService;
@AutoLog(value = "资产占比")
@ApiOperation(value = "资产占比", notes = "资产占比")
@GetMapping(value = "/assetPercent")
@CustomExceptionAnno(description = "资产占比")
public Result<Object> assetPercentCtrl(@RequestParam String startTime) throws Exception {
Result<Object> result = new Result<>();
// assetType: 资产类型value:key的转换
Map<String, String> assetTypeVal2KeyMap = EquipmentUtils.convertFunc("assetType", false);
List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
.select(EquipmentAsset.class.getDeclaredField("assetType"), EquipmentInfo.class.getDeclaredField("departId"))
.select("count(*) as total")
.from(EquipmentInfo.class)
.innerJoin(EquipmentAsset.class, EquipmentAsset.class.getDeclaredField("id"), EquipmentInfo.class.getDeclaredField("assetId"))
.where()
.eq(true, "date_format(equipment_info.create_time, '%Y')", startTime)
.groupBy(true, "equipment_asset.asset_type", "equipment_info.depart_id")
.queryWrapperForList();
List<Map<String, Object>> convertMapList = WrapperFactory.result()
.addCallback(EquipmentInfo.class.getDeclaredField("departId"), "departId_dictText")
.addCallback(EquipmentAsset.class.getDeclaredField("assetType"), "assetType_dictText")
.maps2MapsWithoutPrefix(mapList, (targetFieldName, srcFieldVal) -> {
if ("departId_dictText".equals(targetFieldName)) {
return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
} else if ("assetType_dictText".equals(targetFieldName)) {
return assetTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
}
return srcFieldVal;
});
Map<String, List<Map<String, Object>>> assetType2EntityMap = new HashMap<>();
for (Map<String, Object> item : convertMapList) {
assetType2EntityMap.computeIfAbsent("" + item.get("assetType_dictText"), k -> new ArrayList<>());
assetType2EntityMap.get("" + item.get("assetType_dictText")).add(item);
}
result.setResult(assetType2EntityMap);
return result;
}
@AutoLog(value = "污水厂资产")
@ApiOperation(value = "污水厂资产", notes = "污水厂资产")
@GetMapping(value = "/assetOfFactory")
@CustomExceptionAnno(description = "污水厂资产")
public Result<Object> assetOfFactoryCtrl(@RequestParam String startTime) throws Exception {
Result<Object> result = new Result<>();
// assetType: 资产类型value:key的转换
Map<String, String> assetTypeVal2KeyMap = EquipmentUtils.convertFunc("assetType", false);
List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
.select(EquipmentAsset.class.getDeclaredField("assetType"), EquipmentInfo.class.getDeclaredField("departId"))
.select("count(*) as total")
.from(EquipmentInfo.class)
.innerJoin(EquipmentAsset.class, EquipmentAsset.class.getDeclaredField("id"), EquipmentInfo.class.getDeclaredField("assetId"))
.where()
.eq(true, "date_format(equipment_info.create_time, '%Y')", startTime)
.groupBy(true, "equipment_info.depart_id", "equipment_asset.asset_type")
.queryWrapperForList();
List<Map<String, Object>> convertMapList = WrapperFactory.result()
.addCallback(EquipmentInfo.class.getDeclaredField("departId"), "departId_dictText")
.addCallback(EquipmentAsset.class.getDeclaredField("assetType"), "assetType_dictText")
.maps2MapsWithoutPrefix(mapList, (targetFieldName, srcFieldVal) -> {
if ("departId_dictText".equals(targetFieldName)) {
return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
} else if ("assetType_dictText".equals(targetFieldName)) {
return assetTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
}
return srcFieldVal;
});
Map<String, List<Map<String, Object>>> departId2EntityMap = new HashMap<>();
for (Map<String, Object> item : convertMapList) {
departId2EntityMap.computeIfAbsent("" + item.get("departId_dictText"), k -> new ArrayList<>());
departId2EntityMap.get("" + item.get("departId_dictText")).add(item);
}
for (List<Map<String, Object>> item : departId2EntityMap.values()) {
double sum = item.stream().mapToDouble(it -> Double.parseDouble("" + it.get("total"))).sum();
for (Map<String, Object> it : item) {
DecimalFormat decimalFormat = new DecimalFormat("0.00");
it.put("percentage", decimalFormat.format(Integer.parseInt("" + it.get("total")) / sum * 100) + "%");
}
}
result.setResult(departId2EntityMap);
return result;
}
@AutoLog(value = "备件情况")
@ApiOperation(value = "备件情况", notes = "备件情况")
@GetMapping(value = "/equipmentSparepart")
@CustomExceptionAnno(description = "备件情况")
public Result<Object> equipmentSparepartCtrl(@RequestParam String startTime) throws Exception {
Result<Object> result = new Result<>();
Map<String, Object> resMap = new HashMap<>();
// 备件过多
QueryWrapper<EquipmentSparepart> moreQueryWrapper = new QueryWrapper<>();
moreQueryWrapper
.eq(true, "date_format(create_time, '%Y')", startTime)
.apply("storage_num > storage_max_num");
List<EquipmentSparepart> moreEquipmentSparepartList = equipmentSparepartService.list(moreQueryWrapper);
resMap.put("more", moreEquipmentSparepartList);
// 备件过少
QueryWrapper<EquipmentSparepart> lessQueryWrapper = new QueryWrapper<>();
lessQueryWrapper
.eq(true, "date_format(create_time, '%Y')", startTime)
.apply("storage_num < storage_min_num");
List<EquipmentSparepart> lessEquipmentSparepartList = equipmentSparepartService.list(lessQueryWrapper);
resMap.put("less", lessEquipmentSparepartList);
// 出库数量最多的备件top5
List<Map<String, Object>> top5MapList = WrapperFactory.joinWrapper()
.select("sum(equipment_out_child.out_num) as sumOutNum", "sparepart_name as sparepartName", "storage_num as storageNum", "storage_max_num as storageMaxNum", "storage_min_num as storageMinNum")
.from(EquipmentOutChild.class)
.innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentOutChild.class.getDeclaredField("sparepartId"))
.where()
.eq(true, "date_format(equipment_out_child.create_time, '%Y')", startTime)
.groupBy(true, "equipment_sparepart.id")
.orderBy(true, false, "sum(equipment_out_child.out_num)")
.last(true, "limit 5")
.queryWrapperForList();
resMap.put("top", top5MapList);
result.setResult(resMap);
return result;
}
@AutoLog(value = "物资分析")
@ApiOperation(value = "物资分析", notes = "物资分析")
@GetMapping(value = "/sparepartType")
@CustomExceptionAnno(description = "物资分析")
public Result<Object> sparepartTypeCtrl(@RequestParam String startTime) throws Exception {
Result<Object> result = new Result<>();
QueryWrapper<EquipmentSparepart> queryWrapper = new QueryWrapper<>();
queryWrapper.select("count(*) as total", "sparepart_type as sparepartType")
.eq("date_format(create_time, '%Y')", startTime)
.groupBy("sparepart_type");
List<Map<String, Object>> mapList = equipmentSparepartService.listMaps(queryWrapper);
Map<String, String> sparepartTypeVal2KeyMap = EquipmentUtils.convertFunc("sparepartType", false);
List<Map<String, Object>> resList = new ArrayList<>();
for (Map.Entry<String, String> item : sparepartTypeVal2KeyMap.entrySet()) {
Map<String, Object> resMap = new HashMap<>();
resMap.put("sparepartType", item.getKey());
resMap.put("sparepartType_dictText", sparepartTypeVal2KeyMap.get(item.getKey()));
resMap.put("total", 0);
for (Map<String, Object> it : mapList) {
if (item.getKey().equals(it.get("sparepartType"))) {
resMap.put("total", it.get("total"));
}
}
resList.add(resMap);
}
result.setResult(resList);
return result;
}
}