EquipmentCostAnalysisController.java
13.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
package com.skua.modules.equipment.controller;
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.EquipmentInfo;
import com.skua.modules.equipment.entity.EquipmentRepair;
import com.skua.modules.equipment.entity.EquipmentRepairChild;
import com.skua.modules.equipment.entity.EquipmentSparepart;
import com.skua.modules.equipment.util.EquipmentUtils;
import com.skua.modules.equipment.vo.EquipmentCostAnalysisVO;
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.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.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author sonin
* @date 2021/12/2 10:07
*/
@Slf4j
@Api(tags = "设备成本核算")
@RestController
@RequestMapping("/equipment/cost/analysis")
public class EquipmentCostAnalysisController {
@Resource
private CustomRedisUtil customRedisUtil;
@AutoLog(value = "累计维修成本")
@ApiOperation(value = "累计维修成本", notes = "累计维修成本")
@CustomExceptionAnno(description = "累计维修成本")
@GetMapping(value = "/totalRepairCost")
public Result<EquipmentCostAnalysisVO> totalRepairCostCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
Result<EquipmentCostAnalysisVO> result = new Result<>();
EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
// 实际维修成本
Map<String, Object> equipmentRepairMap = WrapperFactory.joinWrapper()
.select("ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price + equipment_repair.commission_cost), 0) as actualRepairCost")
.from(EquipmentRepairChild.class)
.innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
.innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.queryWrapperForMap();
equipmentCostAnalysisVO.setActualRepairCost("" + equipmentRepairMap.get("actualRepairCost"));
// 备件消耗情况
List<Map<String, Object>> sparepartMapList = WrapperFactory.joinWrapper()
.select("ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) as cost", "equipment_sparepart.sparepart_type as sparepartType", "count(*) as num")
.from(EquipmentRepairChild.class)
.innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
.innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.groupBy(true, "equipment_sparepart.sparepart_type")
.queryWrapperForList();
Map<String, String> sparepartTypeVal2KeyMap = EquipmentUtils.convertFunc("sparepartType", false);
List<EquipmentCostAnalysisVO.Charts> sparepartCharts = WrapperFactory.result()
.addCallback("sparepartType", "sparepartType_dictText")
.maps2Beans(sparepartMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
if ("sparepartType_dictText".equals(targetFieldName)) {
return sparepartTypeVal2KeyMap.get("" + srcFieldVal);
}
return srcFieldVal;
});
for (Map.Entry<String, String> item : sparepartTypeVal2KeyMap.entrySet()) {
if (!sparepartCharts.stream().map(EquipmentCostAnalysisVO.Charts::getSparepartType_dictText).collect(Collectors.toList()).contains(item.getKey())) {
EquipmentCostAnalysisVO.Charts charts = new EquipmentCostAnalysisVO.Charts();
charts.setSparepartType_dictText(item.getValue());
sparepartCharts.add(charts);
}
}
equipmentCostAnalysisVO.setFirstCharts(sparepartCharts);
result.setResult(equipmentCostAnalysisVO);
return result;
}
@AutoLog(value = "故障次数")
@ApiOperation(value = "故障次数", notes = "故障次数")
@CustomExceptionAnno(description = "故障次数")
@GetMapping(value = "/fault")
public Result<EquipmentCostAnalysisVO> faultCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
Result<EquipmentCostAnalysisVO> result = new Result<>();
EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
// 故障次数
List<Map<String, Object>> faultTimesMapList = WrapperFactory.joinWrapper()
.select("count(*) as num", "equipment_info.depart_id as departId")
.from(EquipmentRepair.class)
.innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.groupBy(true, "equipment_info.depart_id")
.orderBy(true, false, "num")
.queryWrapperForList();
List<EquipmentCostAnalysisVO.Charts> faultTimesCharts = WrapperFactory.result()
.addCallback("departId", "departId_dictText")
.maps2Beans(faultTimesMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
if ("departId_dictText".equals(targetFieldName)) {
return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
}
return srcFieldVal;
});
equipmentCostAnalysisVO.setFirstCharts(faultTimesCharts);
// 故障类型
List<Map<String, Object>> faultTypeMapList = WrapperFactory.joinWrapper()
.select("count(*) as num", "equipment_repair.fault_type as faultType")
.from(EquipmentRepair.class)
.innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.groupBy(true, "equipment_repair.fault_type")
.queryWrapperForList();
Map<String, String> faultTypeVal2KeyMap = EquipmentUtils.convertFunc("faultType", false);
List<EquipmentCostAnalysisVO.Charts> faultTypeCharts = WrapperFactory.result()
.addCallback("faultType", "faultType_dictText")
.maps2Beans(faultTypeMapList, EquipmentCostAnalysisVO.Charts.class, (targetFieldName, srcFieldVal) -> {
if ("faultType_dictText".equals(targetFieldName)) {
return faultTypeVal2KeyMap.get("" + srcFieldVal);
}
return srcFieldVal;
});
for (Map.Entry<String, String> item : faultTypeVal2KeyMap.entrySet()) {
if (!faultTypeCharts.stream().map(EquipmentCostAnalysisVO.Charts::getFaultType_dictText).collect(Collectors.toList()).contains(item.getValue())) {
EquipmentCostAnalysisVO.Charts charts = new EquipmentCostAnalysisVO.Charts();
charts.setFaultType_dictText(item.getValue());
faultTypeCharts.add(charts);
}
}
equipmentCostAnalysisVO.setSecondCharts(faultTypeCharts);
result.setResult(equipmentCostAnalysisVO);
return result;
}
@AutoLog(value = "各厂站维修成本")
@ApiOperation(value = "各厂站维修成本", notes = "各厂站维修成本")
@CustomExceptionAnno(description = "各厂站维修成本")
@GetMapping(value = "/departRepairCost")
public Result<EquipmentCostAnalysisVO> departRepairCostCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
Result<EquipmentCostAnalysisVO> result = new Result<>();
EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
.select("equipment_info.depart_id as departId", "ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) as sparepartCost", "ifnull(sum(equipment_repair.commission_cost), 0) as commissionCost", "ifnull(sum(equipment_repair_child.repair_num * equipment_sparepart.unit_price), 0) + ifnull(sum(equipment_repair.commission_cost), 0) as totalCost")
.from(EquipmentRepairChild.class)
.innerJoin(EquipmentRepair.class, EquipmentRepair.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("repairId"))
.innerJoin(EquipmentSparepart.class, EquipmentSparepart.class.getDeclaredField("id"), EquipmentRepairChild.class.getDeclaredField("sparepartId"))
.innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.groupBy(true, "equipment_info.depart_id")
.queryWrapperForList();
List<EquipmentCostAnalysisVO.DepartCost> departCostList = WrapperFactory.result()
.addCallback("departId", "departId_dictText")
.maps2Beans(mapList, EquipmentCostAnalysisVO.DepartCost.class, (targetFieldName, srcFieldVal) -> {
if ("departId_dictText".equals(targetFieldName)) {
return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
}
return srcFieldVal;
});
equipmentCostAnalysisVO.setDepartCostList(departCostList);
result.setResult(equipmentCostAnalysisVO);
return result;
}
@AutoLog(value = "设备故障Top10")
@ApiOperation(value = "设备故障Top10", notes = "设备故障Top10")
@CustomExceptionAnno(description = "设备故障Top10")
@GetMapping(value = "/equipmentFault")
public Result<EquipmentCostAnalysisVO> equipmentFaultCtrl(@RequestParam(value = "startTime") String startTime) throws Exception {
Result<EquipmentCostAnalysisVO> result = new Result<>();
EquipmentCostAnalysisVO equipmentCostAnalysisVO = new EquipmentCostAnalysisVO();
List<Map<String, Object>> mapList = WrapperFactory.joinWrapper()
.select("depart_id as departId", "equipment_name as equipmentName", "count(*) as equipmentFaultTimes", "install_position as installPosition", "depart_id as departId", "manufacturer")
.from(EquipmentRepair.class)
.innerJoin(EquipmentInfo.class, EquipmentInfo.class.getDeclaredField("id"), EquipmentRepair.class.getDeclaredField("infoId"))
.where()
.apply(!startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y') = '" + startTime + "'")
.apply(startTime.contains("-"), "date_format(equipment_repair.repair_date,'%Y-%m') = '" + startTime + "'")
.groupBy(true, "equipment_info.id")
.orderBy(true, false, "equipmentFaultTimes")
.last(true, "limit 10")
.queryWrapperForList();
List<EquipmentCostAnalysisVO.EquipmentFault> equipmentFaultList = WrapperFactory.result()
.addCallback("departId", "departId_dictText")
.maps2Beans(mapList, EquipmentCostAnalysisVO.EquipmentFault.class, (targetFieldName, srcFieldVal) -> {
if ("departId_dictText".equals(targetFieldName)) {
return customRedisUtil.hget("sys_depart", "" + srcFieldVal, "id", "depart_name");
}
return srcFieldVal;
});
equipmentCostAnalysisVO.setEquipmentFaultList(equipmentFaultList);
result.setResult(equipmentCostAnalysisVO);
return result;
}
}