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;
}
}
10-07
8795
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交