java字典翻译工具类

package com.nuzar.fcms.bill.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.nuzar.cloud.common.utils.ApplicationContextHelper;
import com.nuzar.fcms.bill.dto.RefCodes;
import com.nuzar.fcms.bill.feign.StdFeignService;
import com.nuzar.fcms.bill.feign.model.STDCommonQuery;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * desc
 *
 * @author Alex
 * @since 2023/9/1 001 11:21
 */
@Slf4j
public class RefCodeUtil {

    /**
     * 填充ref对应的meaning给指定的字段
     *
     * @param list     待填充的集合
     * @param domain   domain
     * @param function 需要替换的code
     * @param consumer 具体替换逻辑,主要指set到那个字段上
     * @param <T>      t
     */
    public static <T> void describeRefCode(List<T> list, String domain, Function<T, String> function, BiConsumer<T, RefCodes> consumer) {
        List<RefCodes> refCodeList = null;
        try {
            refCodeList = ApplicationContextHelper.getBean(StdFeignService.class).listRefCodes(domain);
        } catch (Exception e) {
            log.error("从STD模块获取数据字典数据[{}]失败", domain, e);
        }
        if (ObjectUtil.isEmpty(refCodeList) || ObjectUtil.isEmpty(list)) {
            return;
        }
        for (T t : list) {
            for (RefCodes ref : refCodeList) {
                if (ref.getLowValue().equals(function.apply(t))) {
                    consumer.accept(t, ref);
                    break;
                }
            }
        }
    }


    /**
     * 将字段数据直接转换为MAP类型
     *
     * @param domain
     * @return map<code, name>
     */
    public static Map<String, String> mapRefCode(String domain) {
        Map<String, String> codeNameMap = new HashMap<>();
        List<RefCodes> refCodeList = null;
        try {
            refCodeList = ApplicationContextHelper.getBean(StdFeignService.class).listRefCodes(domain);
        } catch (Exception e) {
            log.error("从STD模块获取数据字典数据[{}]失败", domain, e);
        }
        if (ObjectUtil.isEmpty(refCodeList)) {
            return codeNameMap;
        }
        return refCodeList.stream().collect(Collectors.toMap(RefCodes::getLowValue, RefCodes::getMeaning));
    }

    /**
     * 使用模块code获取对应的基础数据
     *
     * @param module 参照 @link{com.nuzar.fcms.common.core.enums.STDServiceEnum}
     * @param query
     * @return
     */
    public static List<JSONObject> listStandardData(String module, STDCommonQuery query) {
        List<JSONObject> jsonList = null;
        try {
            jsonList = ApplicationContextHelper.getBean(StdFeignService.class).listByModule(module, query);
        } catch (Exception e) {
            log.error("从STD获取基础数据[{}]失败",module,e);
        }
        if (ObjectUtil.isEmpty(jsonList)) {
            return Collections.emptyList();
        }
        return jsonList;
    }

    /**
     * 将标准数据转化炉为map,k为code,v为对应名字
     *
     * @param jsonList
     * @param codeField
     * @param nameField
     * @return
     */
    public static Map<String, String> standardData2Map(List<JSONObject> jsonList, String codeField, String nameField) {
        Map<String, String> typeNameMap = new HashMap<>();
        if (ObjectUtil.isEmpty(jsonList) || ObjectUtil.isEmpty(codeField) || ObjectUtil.isEmpty(nameField)) {
            return typeNameMap;
        }
        for (JSONObject json : jsonList) {
            String code = json.getStr(codeField);
            String name = json.getStr(nameField);
            if (ObjectUtil.isNotEmpty(code)) {
                typeNameMap.put(code, name);
            }
        }
        return typeNameMap;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值