EquipmentUtils.java 7.3 KB
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;
        }
    }

}