EquipmentUtils.java
7.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
package com.skua.modules.equipment.util;
import com.skua.core.context.SpringContextUtils;
import com.skua.modules.equipment.dto.EquipmentDTO;
import com.skua.modules.equipment.entity.EquipmentAsset;
import com.skua.modules.equipment.entity.EquipmentExt;
import com.skua.modules.equipment.entity.EquipmentInfo;
import com.skua.modules.equipment.pojo.Equipment;
import com.skua.modules.equipment.vo.EquipmentAssetVO;
import com.skua.modules.equipment.vo.EquipmentExtVO;
import com.skua.modules.equipment.vo.EquipmentInfoVO;
import com.skua.modules.equipment.vo.EquipmentVO;
import com.skua.redis.component.Key2ValueService;
import com.skua.tool.util.BeanExtUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author sonin
* @date 2021/10/23 11:30
*/
public class EquipmentUtils {
public static Equipment equipDTO2Entity(EquipmentDTO equipmentDTO) throws Exception {
Equipment equipment = new Equipment();
equipment.setEquipmentAsset(BeanExtUtils.bean2Bean(equipmentDTO.getEquipmentAsset(), EquipmentAsset.class));
equipment.setEquipmentInfo(BeanExtUtils.bean2Bean(equipmentDTO.getEquipmentInfo(), EquipmentInfo.class));
//equipment.setEquipmentExt(BeanExtUtils.bean2Bean(equipmentDTO.getEquipmentExt(), EquipmentExt.class));
return equipment;
}
public static EquipmentVO equipEntity2VO(Equipment equipment) throws Exception {
EquipmentVO equipmentVO = new EquipmentVO();
equipmentVO.setEquipmentAsset(BeanExtUtils.bean2Bean(equipment.getEquipmentAsset(), EquipmentAssetVO.class));
equipmentVO.setEquipmentInfo(BeanExtUtils.bean2Bean(equipment.getEquipmentInfo(), EquipmentInfoVO.class));
//equipmentVO.setEquipmentExt(BeanExtUtils.bean2Bean(equipment.getEquipmentExt(), EquipmentExtVO.class));
return equipmentVO;
}
public static List<EquipmentVO> equipEntities2VOs(List<Equipment> equipmentList) throws Exception {
List<EquipmentVO> equipmentVOList = new ArrayList<>();
// assetType: 资产类型value:key的转换
Map<String, String> assetTypeVal2KeyMap = convertFunc("assetType", false);
// suppliesType: 物资类型value:key的转换
Map<String, String> suppliesTypeVal2KeyMap = convertFunc("suppliesType", false);
// equipmentType: 设备类型value:key的转换
Map<String, String> equipmentTypeVal2KeyMap = convertFunc("equipmentType", false);
// equipmentType: 设备类型value:key的转换
Map<String, String> equipmentStatusVal2KeyMap = convertFunc("equipmentStatus", false);
// 安装位置
Map<String, String> installPositionVal2KeyMap = convertFunc("installPosition", false);
// departId
Key2ValueService key2ValueService = SpringContextUtils.getBean(Key2ValueService.class);
Map<String, String> sysDepartVal2KeyMap = key2ValueService.dictKey2Val("sysDepart", false);
// entity => vo list转换
for (Equipment item : equipmentList) {
EquipmentAssetVO equipmentAssetVO = BeanExtUtils.bean2Bean(item.getEquipmentAsset(), EquipmentAssetVO.class, (targetFieldName, srcFieldVal) -> {
if ("assetType_dictText".equals(targetFieldName)) {
return assetTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
} else if ("suppliesType_dictText".equals(targetFieldName)) {
return suppliesTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
}
return "";
});
EquipmentInfoVO equipmentInfoVO = BeanExtUtils.bean2Bean(item.getEquipmentInfo(), EquipmentInfoVO.class, (targetFieldName, srcFieldVal) -> {
if ("departIdName".equals(targetFieldName) || "departId_dictText".equals(targetFieldName)) {
return sysDepartVal2KeyMap.getOrDefault(srcFieldVal, "");
} else if ("equipmentType_dictText".equals(targetFieldName)) {
return equipmentTypeVal2KeyMap.getOrDefault("" + srcFieldVal, "");
} else if ("installPosition_dictText".equals(targetFieldName)) {
return installPositionVal2KeyMap.getOrDefault("" + srcFieldVal, "");
} else if ("equipmentStatus_dictText".equals(targetFieldName)) {
return equipmentStatusVal2KeyMap.getOrDefault("" + srcFieldVal, "");
}
return "";
});
//EquipmentExtVO equipmentExtVO = BeanExtUtils.bean2Bean(item.getEquipmentExt(), EquipmentExtVO.class);
EquipmentVO equipmentVO = new EquipmentVO(equipmentAssetVO, equipmentInfoVO, null);
equipmentVOList.add(equipmentVO);
}
return equipmentVOList;
}
public static Map<String, String> convertFunc(String convertType, boolean isKey2Val) {
Map<String, String> key2ValMap = new LinkedHashMap<>(2);
Map<String, String> val2KeyMap = new LinkedHashMap<>(2);
String sql = "";
JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
if ("assetType".equals(convertType)) {
sql = "select sdi.item_text as itemText, sdi.item_value as itemValue from sys_dict_item as sdi inner join sys_dict as sd on sdi.dict_id = sd.id and sd.dict_code = 'equipment_asset-asset_type'";
} else if ("equipmentType".equals(convertType)) {
sql = "select des as itemText,id as itemValue from equipment_category";
} else if ("depart".equals(convertType)) {
sql = "select depart_name as itemText, id as itemValue from sys_depart";
} else if ("installPosition".equals(convertType)) {
sql = "select struct_name as itemText, id as itemValue from sys_struct_dict";
} else if ("suppliesType".equals(convertType)) {
sql = "select sdi.item_text as itemText, sdi.item_value as itemValue from sys_dict_item as sdi inner join sys_dict as sd on sdi.dict_id = sd.id and sd.dict_code = 'equipment-supplies_type'";
} else if ("equipmentStatus".equals(convertType)) {
sql = "select sdi.item_text as itemText, sdi.item_value as itemValue from sys_dict_item as sdi inner join sys_dict as sd on sdi.dict_id = sd.id and sd.dict_code = 'equipment-equipment_status'";
} else if ("sparepartType".equals(convertType)) {
sql = "select item_text as itemText,id as itemValue from equipment_sparepart_type";
} else if ("sysUser".equals(convertType)) {
sql = "select realname as itemText, id as itemValue from sys_user";
} else if ("faultType".equals(convertType)) {
sql = "select sdi.item_text as itemText, sdi.item_value as itemValue from sys_dict_item as sdi inner join sys_dict as sd on sdi.dict_id = sd.id and sd.dict_code = 'equipment-fault_type'";
} else if ("equipmentBrand".equals(convertType)) {
sql = "select brand_name as itemText, id as itemValue from equipment_brand";
}
List<Map<String, Object>> mapList = masterDB.queryForList(sql);
for (Map<String, Object> map : mapList) {
key2ValMap.put("" + map.get("itemText"), "" + map.get("itemValue"));
val2KeyMap.put("" + map.get("itemValue"), "" + map.get("itemText"));
}
if (isKey2Val) {
return key2ValMap;
} else {
return val2KeyMap;
}
}
}