Key2ValueService.java 5.6 KB
package com.skua.redis.component;

import com.skua.tool.javassist.JavassistFactory;
import com.skua.tool.query.WrapperFactory;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sonin
 * @date 2021/12/24 18:14
 */
@Component
public class Key2ValueService {

    private Class sysDictClazz;

    private Class sysDictItemClazz;

    private Class sysUserClazz;

    private Class sysDepartClazz;

    private Class sysStructDictClazz;

    private Class sysUserDepartClazz;

    private Class equipmentSparepartTypeClazz;
    private Class suppliesWarehouseClazz;

    public Key2ValueService() {
        try {
            sysDictClazz = JavassistFactory.create()
                    .className("SysDict")
                    .field("id", String.class)
                    .field("dictCode", String.class)
                    .buildClass();
            sysDictItemClazz = JavassistFactory.create()
                    .className("SysDictItem")
                    .field("dictId", String.class)
                    .field("itemText", String.class)
                    .field("itemValue", String.class)
                    .buildClass();
            sysUserClazz = JavassistFactory.create()
                    .field("id", String.class)
                    .field("realname", String.class)
                    .className("SysUser")
                    .buildClass();
            sysDepartClazz = JavassistFactory.create()
                    .className("SysDepart")
                    .field("id", String.class)
                    .field("departType", String.class)
                    .buildClass();
            sysStructDictClazz = JavassistFactory.create()
                    .className("sysStructDict")
                    .buildClass();
            sysUserDepartClazz = JavassistFactory.create().
                    field("id", String.class)
                    .field("depId", String.class)
                    .field("userId", String.class)
                    .className("SysUserDepart")
                    .buildClass();
            equipmentSparepartTypeClazz = JavassistFactory.create().
                    field("id", String.class)
                    .field("itemText", String.class)
                    .field("itemValue", String.class)
                    .className("EquipmentSparepartType")
                    .buildClass();
            suppliesWarehouseClazz = JavassistFactory.create()
                    .className("SuppliesWarehouse")
                    .field("id", String.class)
                    .field("warehouseName", String.class)
                    .buildClass();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map<String, String> dictKey2Val(String dictCode, boolean isKey2Val) {
        Map<String, String> result = new LinkedHashMap<>();
        List<Map<String, Object>> mapList = null;
        try {
            if ("sysUser".equals(dictCode)) {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("realname as itemText", "id as itemValue")
                        .from(sysUserClazz)
                        .where()
                        .queryWrapperForList();
            } else if ("sysDepart".equals(dictCode)) {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("depart_name as itemText", "id as itemValue")
                        .from(sysDepartClazz)
                        .where()
                        .queryWrapperForList();
            } else if ("sysStructDict".equals(dictCode)) {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("struct_name as itemText", "id as itemValue")
                        .from(sysStructDictClazz)
                        .where()
                        .queryWrapperForList();
            } else if ("equipmentSparepartType".equals(dictCode)) {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("item_text as itemText", "id as itemValue")
                        .from(equipmentSparepartTypeClazz)
                        .where()
                        .queryWrapperForList();
            } else if ("suppliesWarehouse".equals(dictCode)) {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("warehouse_name as itemText", "id as itemValue")
                        .from(suppliesWarehouseClazz)
                        .where()
                        .queryWrapperForList();
            } else {
                mapList = WrapperFactory
                        .joinWrapper()
                        .select("item_text as itemText", "item_value as itemValue")
                        .from(sysDictClazz)
                        .innerJoin(sysDictItemClazz, sysDictItemClazz.getDeclaredField("dictId"), sysDictClazz.getDeclaredField("id"))
                        .where()
                        .eq(true, "dict_code", dictCode)
                        .queryWrapperForList();
            }
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        if (mapList != null) {
            if (isKey2Val) {
                for (Map<String, Object> map : mapList) {
                    result.put("" + map.get("itemText"), "" + map.get("itemValue"));
                }
            } else {
                for (Map<String, Object> map : mapList) {
                    result.put("" + map.get("itemValue"), "" + map.get("itemText"));
                }
            }
        }
        return result;
    }
}