差异标签比对

This commit is contained in:
1378012178@qq.com 2025-08-27 17:28:14 +08:00
parent de2e0f96ed
commit e40e60bad6
7 changed files with 265 additions and 75 deletions

View File

@ -0,0 +1,175 @@
package com.nu.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.aspect.DictAspect;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.common.util.oConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
/**
* 字典翻译辅助工具类
*/
@Component
public class DictUtils {
private static final Logger log = LoggerFactory.getLogger(DictAspect.class);
private CommonAPI commonAPI;
@Autowired
public void setCommonAPI(CommonAPI commonAPI) {
this.commonAPI = commonAPI;
}
/**
* 翻译字典支持@Dict的翻译翻译出的字段放在有@Dict字段的名字 + "_dictText"
* @param o 传入的是单个entity对象
* @return 返回的是json对象
*/
public JSONObject translateDict(Object o){
Field[] fields = ReflectUtil.getFields(o.getClass());
ObjectMapper mapper = new ObjectMapper();
String json = "{}";
try {
json = mapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
log.error("json解析失败" + e.getMessage(), e);
}
JSONObject item = JSONObject.parseObject(json);
for (Field field:fields){
if (field.getAnnotation(Dict.class) != null) {
String key = String.valueOf(item.get(field.getName()));
String textValue = getDictTxt(field,key);
item.put(field.getName() + "_dictText", textValue);
}
}
return item;
}
/**
* 翻译字典支持@Dict的翻译翻译出的字段放在有@Dict字段的名字 + "_dictText"<br/>
* 在clazz与o不是一种对象时使用<br/>
* 如果需要保持一致请使用{@link org.jeecg.modules.tools.DictUtils#translateDictSelf}
* @param o 传入的是单个entity对象
* @param clazz 返回对象的类型
* @return 传入的clazz类型的新对象
* @throws IllegalAccessException
*/
public <T> T translateDict(Object o,Class<T> clazz) throws IllegalAccessException {
Field[] fields = ReflectUtil.getFields(clazz);
T rObject = ReflectUtil.newInstance(clazz);
//将所有内容转入新对象里
BeanUtil.copyProperties(o,rObject);
return translateDictSelf(rObject,clazz);
}
/**
* 翻译字典支持@Dict的翻译翻译出的字段放在有@Dict字段的名字 + "_dictText"<br/>
* 此方法返回自己保证是同一个对象以方便list使用<br/>
* 如果需要保持一致请使用{@link org.jeecg.modules.tools.DictUtils#translateDict}
* @param o 传入的是单个entity对象,也是返回的对象
* @param clazz 返回对象的类型
* @return 返回的是变量o这个对象
* @throws IllegalAccessException
*/
public <T> T translateDictSelf(T o,Class<T> clazz) throws IllegalAccessException {
Field[] fields = ReflectUtil.getFields(clazz);
for (Field field:fields){
if (field.getAnnotation(Dict.class) != null) {
boolean accessible = field.isAccessible();
field.setAccessible(true);
String key = String.valueOf(field.get(o));
field.setAccessible(accessible);
String textValue = getDictTxt(field,key);
Field newField = ReflectUtil.getField(clazz,field.getName() + "_dictText");
//为空则赋值失败
if(newField != null){
boolean accessibleNew = newField.isAccessible();
newField.setAccessible(true);
newField.set(o,textValue);
newField.setAccessible(accessibleNew);
}else{
log.error("字段【{}{}】不存在!值为:{} ",field.getName(),"_dictText",textValue);
}
}
}
return o;
}
/**
* 获取根据属性和真实的值获取字典值
* @param field 反射出来的类属性
* @param key 查询的值
* @return 字典文本
*/
private String getDictTxt(Field field,String key){
boolean accessible = field.isAccessible();
field.setAccessible(true);
String code = field.getAnnotation(Dict.class).dicCode();
String text = field.getAnnotation(Dict.class).dicText();
String table = field.getAnnotation(Dict.class).dictTable();
String textValue = translateDictValue(code, text, table, key);
log.debug(" 字典Val : " + textValue);
log.debug(" __翻译字典字段__ " + field.getName() + "_dictText" + " " + textValue);
field.setAccessible(accessible);
return textValue;
}
/**
* 翻译字典
* @param code 字典code字段
* @param key 字典查询的值
* @return 字典文本
*/
public String translateDictValue(String code, String key) {
return translateDictValue(code,"","",key);
}
/**
* 翻译字典
* @param code 字典code字段
* @param text 字典文字字段
* @param table 字典表名
* @param key 字典查询的值
* @return 字典文本
*/
public String translateDictValue(String code, String text, String table, String key) {
if (oConvertUtils.isEmpty(key)) {
return null;
} else {
StringBuilder textValue = new StringBuilder();
String[] keys = key.split(",");
for(int i = 0; i < keys.length; ++i) {
String k = keys[i];
String tmpValue;
log.debug(" 字典 key : " + k);
if (k.trim().length() != 0) {
if (!StringUtils.isEmpty(table)) {
log.debug("--DictUtils------dicTable=" + table + " ,dicText= " + text + " ,dicCode=" + code);
tmpValue = this.commonAPI.translateDictFromTable(table, text, code, k.trim());
} else {
tmpValue = this.commonAPI.translateDict(code, k.trim());
}
if (tmpValue != null) {
if (!"".equals(textValue.toString())) {
textValue.append(",");
}
textValue.append(tmpValue);
}
}
}
return textValue.toString();
}
}
}

View File

@ -100,4 +100,13 @@ public class ElderTag implements Serializable {
/**需要同步的id */
@TableField(exist = false)
private String SyncIds;
/**对比的机构编码 */
@TableField(exist = false)
private String compareOrgCode;
//本平台是否存在该指令
@TableField(exist = false)
private boolean ownExist;
//目标平台是否存在该指令
@TableField(exist = false)
private boolean targetExist;
}

View File

@ -13,5 +13,5 @@ import java.util.List;
*/
public interface ElderTagMapper extends BaseMapper<ElderTag> {
List<ElderTag> allData();
List<ElderTag> allDataIds();
}

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nu.modules.eldertag.mapper.ElderTagMapper">
<select id="allData" resultType="com.nu.modules.eldertag.entity.ElderTag">
<select id="allDataIds" resultType="com.nu.modules.eldertag.entity.ElderTag">
select id from nu_elder_tag
</select>
</mapper>

View File

@ -19,7 +19,7 @@ public interface IElderTagService extends IService<ElderTag> {
ElderTagMQDto syncElderTag(String sourceOrgCode, String syncIds);
List<ElderTag> allData();
List<ElderTag> allDataIds();
void insertAllDirectives(List<ElderTag> needAddETList);

View File

@ -15,6 +15,7 @@ import com.nu.modules.eldertag.mapper.ElderTagMapper;
import com.nu.modules.eldertag.service.IElderTagService;
import com.nu.modules.mq.eldertag.listener.ElderTagMQListener;
import com.nu.modules.sysconfig.ISysConfigApi;
import com.nu.utils.DictUtils;
import com.nu.utils.HttpRequestUtil;
import com.nu.utils.NuFileUtils;
import com.nu.utils.RabbitMQUtil;
@ -52,6 +53,8 @@ public class ElderTagServiceImpl extends ServiceImpl<ElderTagMapper, ElderTag> i
private RabbitMQUtil rabbitMQUtil;
@Autowired
private ICanAddElderTagService canAddElderTagService;
@Autowired
private DictUtils dictUtils;
/**
* 将资源放到静态目录中
@ -109,8 +112,8 @@ public class ElderTagServiceImpl extends ServiceImpl<ElderTagMapper, ElderTag> i
}
@Override
public List<ElderTag> allData() {
return baseMapper.allData();
public List<ElderTag> allDataIds() {
return baseMapper.allDataIds();
}
@Override
@ -125,75 +128,78 @@ public class ElderTagServiceImpl extends ServiceImpl<ElderTagMapper, ElderTag> i
List<ElderTag> result = Lists.newArrayList();
//查询本平台的已有指令
// List<ElderTag> ownList = baseMapper.queryAndTranslate(null);
// String existIds = "";
// if (ownList != null && ownList.size() > 0) {
// existIds = ownList.stream()
// .map(l -> l.getId())
// .map(String::valueOf)
// .collect(Collectors.joining(","));
// }
//
// // 查询目标平台已有指令
// //各平台api地址都存在管理系统中 管理系统api地址在系统参数配置中 ope_open_url
// JSONObject opeOpenUrl = sysConfigApi.getByKeyByDS("master", "ope_open_url");
// String opeApiAddress = opeOpenUrl.getString("configValue");
// if (opeApiAddress.endsWith("/")) {
// opeApiAddress = opeApiAddress.substring(0, opeApiAddress.length() - 1);
// }
// String apiAddress = opeApiAddress + "/api/services/directive/queryCompareDirectives?orgCode=" + configServiceDirective.getCompareOrgCode();
// List<ConfigServiceDirective> targetList = Lists.newArrayList();
// try {
// String res = HttpRequestUtil.doGet(apiAddress, HttpRequestUtil.createDefaultHeaders());
// JSONObject jsonResponse = JSON.parseObject(res);
// JSONObject r_ = jsonResponse.getJSONObject("result");
// targetList = r_.getJSONArray("list")
// .toJavaList(ConfigServiceDirective.class);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// Map<String, ConfigServiceDirective> merged = new LinkedHashMap<>();
//
// // 先把本平台的放进去标记 ownExist = true, targetExist = false
// if (ownList != null) {
// for (ConfigServiceDirective o : ownList) {
// if (o == null || o.getId() == null) continue;
// String id = String.valueOf(o.getId());
// // 直接在原对象上设置标志如果不想修改原对象请复制对象
// o.setOwnExist(true);
// o.setTargetExist(false);
// merged.put(id, o);
// }
// }
//
// // 再处理目标平台的数据若已存在则把 targetExist true否则加入并标记为仅 target
// if (targetList != null) {
// for (ConfigServiceDirective t : targetList) {
// if (t == null || t.getId() == null) continue;
// String id = String.valueOf(t.getId());
// if (merged.containsKey(id)) {
// // 本平台已有标记目标平台也存在
// merged.get(id).setTargetExist(true);
// } else {
// // 仅目标平台有
// t.setOwnExist(false);
// t.setTargetExist(true);
// merged.put(id, t);
// }
// }
// }
//
// // 把合并后的值放到列表并按需求排序both -> own only -> target only
// result = new ArrayList<>(merged.values());
// result.sort(Comparator.comparingInt(d -> {
// boolean own = Boolean.TRUE.equals(d.isOwnExist());
// boolean target = Boolean.TRUE.equals(d.isTargetExist());
// if (own && target) return 0; // 两个平台都有 -> 最前
// if (own) return 1; // 仅本平台有 -> 中间
// return 2; // 仅目标平台有 -> 最后
// }));
List<ElderTag> ownList = baseMapper.selectList(null);
String existIds = "";
if (ownList != null && ownList.size() > 0) {
existIds = ownList.stream()
.map(l -> l.getId())
.map(String::valueOf)
.collect(Collectors.joining(","));
}
// 查询目标平台已有指令
//各平台api地址都存在管理系统中 管理系统api地址在系统参数配置中 ope_open_url
JSONObject opeOpenUrl = sysConfigApi.getByKeyByDS("master", "ope_open_url");
String opeApiAddress = opeOpenUrl.getString("configValue");
if (opeApiAddress.endsWith("/")) {
opeApiAddress = opeApiAddress.substring(0, opeApiAddress.length() - 1);
}
String apiAddress = opeApiAddress + "/api/services/elderTag/queryCompareElderTag?orgCode=" + elderTag.getCompareOrgCode();
List<ElderTag> targetList = Lists.newArrayList();
try {
String res = HttpRequestUtil.doGet(apiAddress, HttpRequestUtil.createDefaultHeaders());
JSONObject jsonResponse = JSON.parseObject(res);
JSONObject r_ = jsonResponse.getJSONObject("result");
targetList = r_.getJSONArray("list")
.toJavaList(ElderTag.class);
} catch (Exception e) {
e.printStackTrace();
}
Map<String, ElderTag> merged = new LinkedHashMap<>();
// 先把本平台的放进去标记 ownExist = true, targetExist = false
if (ownList != null) {
for (ElderTag o : ownList) {
if (o == null || o.getId() == null) continue;
String id = String.valueOf(o.getId());
// 直接在原对象上设置标志如果不想修改原对象请复制对象
o.setOwnExist(true);
o.setTargetExist(false);
merged.put(id, o);
}
}
// 再处理目标平台的数据若已存在则把 targetExist true否则加入并标记为仅 target
if (targetList != null) {
for (ElderTag t : targetList) {
if (t == null || t.getId() == null) continue;
String id = String.valueOf(t.getId());
if (merged.containsKey(id)) {
// 本平台已有标记目标平台也存在
merged.get(id).setTargetExist(true);
} else {
// 仅目标平台有
t.setOwnExist(false);
t.setTargetExist(true);
merged.put(id, t);
}
}
}
// 把合并后的值放到列表并按需求排序both -> own only -> target only
result = new ArrayList<>(merged.values());
result.sort(Comparator.comparingInt(d -> {
boolean own = Boolean.TRUE.equals(d.isOwnExist());
boolean target = Boolean.TRUE.equals(d.isTargetExist());
if (own && target) return 0; // 两个平台都有 -> 最前
if (own) return 1; // 仅本平台有 -> 中间
return 2; // 仅目标平台有 -> 最后
}));
result.stream().forEach(et -> {
et.setType(dictUtils.translateDictValue("elder_tag_type",et.getType()));
});
return result;
}

View File

@ -168,7 +168,7 @@ public class ElderTagMQListener {
private void handleIncremental(ElderTagMQDto dto) {
//增量传过来的是已勾选的全部数据需将重复部分去除
//先查出所有标签id 然后进行去重
List<ElderTag> tempList = elderTagService.allData();
List<ElderTag> tempList = elderTagService.allDataIds();
Set<String> existingIds = tempList.stream().map(ElderTag::getId).map(String::valueOf).collect(Collectors.toSet());
String idStr = dto.getIdStr();
@ -274,7 +274,7 @@ public class ElderTagMQListener {
public void handleIncremental2(ElderTagMQDto dto) {
//增量传过来的是已勾选的全部数据需将重复部分去除
//先查出所有标签id 然后进行去重
List<ElderTag> tempList = elderTagService.allData();
List<ElderTag> tempList = elderTagService.allDataIds();
Set<String> existingIds = tempList.stream().map(ElderTag::getId).map(String::valueOf).collect(Collectors.toSet());
dto.setIzInc(true);