|
@@ -5,18 +5,15 @@ import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.persagy.dmp.basic.dto.RequestData;
|
|
|
import com.persagy.dmp.basic.model.CodeTableTypeEnum;
|
|
|
-import com.persagy.dmp.basic.utils.JsonNodeUtils;
|
|
|
import com.persagy.dmp.codetable.client.CodeTableDataFacade;
|
|
|
import com.persagy.dmp.codetable.entity.CodeTableData;
|
|
|
import com.persagy.dmp.common.constant.CommonConstant;
|
|
@@ -24,7 +21,6 @@ import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
import com.persagy.dmp.common.constant.ValidEnum;
|
|
|
import com.persagy.dmp.common.context.AppContext;
|
|
|
import com.persagy.dmp.common.exception.BusinessException;
|
|
|
-import com.persagy.dmp.common.helper.SpringHelper;
|
|
|
import com.persagy.dmp.common.model.entity.BaseEntity;
|
|
|
import com.persagy.dmp.common.model.response.CommonResult;
|
|
|
import com.persagy.dmp.common.utils.ResultHelper;
|
|
@@ -46,6 +42,7 @@ import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -119,10 +116,8 @@ public class ObjectDigitalServiceImpl extends ServiceImpl<ObjectDigitalMapper, O
|
|
|
if(CollUtil.isEmpty(voList)) {
|
|
|
return null;
|
|
|
}
|
|
|
- //获取$remove字段
|
|
|
- Map<String, Object> removeMap = getRemoveInfo(voList);
|
|
|
- //获取基础属性
|
|
|
- Set<String> fieldSet = JsonNodeUtils.getFieldMap(ObjectDigital.class).keySet();
|
|
|
+ // 获取$remove字段
|
|
|
+ Map<String, Set<String>> removeMap = convertRemoveInfo(voList);
|
|
|
//查询数据库中存在的数据
|
|
|
List<String> idList = CollUtil.getFieldValues(voList, BaseEntity.PROP_ID, String.class);
|
|
|
List<ObjectDigital> dbList = objectDigitalMapper.selectBatchIds(idList);
|
|
@@ -131,7 +126,7 @@ public class ObjectDigitalServiceImpl extends ServiceImpl<ObjectDigitalMapper, O
|
|
|
//填充classCode
|
|
|
fillClassCodeForUpdate(voList,dbMap);
|
|
|
//信息点校验
|
|
|
- Map<String, Map<String, ObjectInfoDefine>> allInfoMap = validateOnSave(voList, true);
|
|
|
+ validateOnSave(voList, true);
|
|
|
List<ObjectDigital> resultList = new ArrayList<>();
|
|
|
voList.forEach(vo -> {
|
|
|
ObjectDigital dbVO = MapUtil.get(dbMap, vo.getId(), ObjectDigital.class);
|
|
@@ -139,29 +134,80 @@ public class ObjectDigitalServiceImpl extends ServiceImpl<ObjectDigitalMapper, O
|
|
|
if(dbVO == null) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- //数据合并
|
|
|
- ObjectDigital newDbVO = dbVO;
|
|
|
- // 合并通用信息点数据
|
|
|
- if (!vo.getObjType().equals(vo.getClassCode())){
|
|
|
- Map<String, ObjectInfoDefine> commonInfoMap = allInfoMap.getOrDefault(vo.getObjType(), new HashMap<>(16));
|
|
|
- dataMerge(newDbVO,vo,commonInfoMap,fieldSet);
|
|
|
- }
|
|
|
- // 合并对象分类信息点数据
|
|
|
- Map<String, ObjectInfoDefine> infoDefineMap = allInfoMap.getOrDefault(vo.getClassCode(),new HashMap<>(16));
|
|
|
- dataMerge(newDbVO,vo,infoDefineMap,fieldSet);
|
|
|
-
|
|
|
- //移除需要删除的信息点
|
|
|
- Object remove = removeMap.get(newDbVO.getId());
|
|
|
- remove(newDbVO,remove,fieldSet);
|
|
|
-
|
|
|
+ // 更新数据的属性合并和删除
|
|
|
+ ensureUpdateInfos(vo, dbVO, removeMap.get(vo.getId()));
|
|
|
// 修改
|
|
|
- objectDigitalMapper.updateById(newDbVO);
|
|
|
- resultList.add(newDbVO);
|
|
|
+ objectDigitalMapper.updateById(vo);
|
|
|
// 修改后消息
|
|
|
- messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_UPDATE, dbVO, newDbVO, false);
|
|
|
+ messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_UPDATE, dbVO, vo, false);
|
|
|
});
|
|
|
- return resultList;
|
|
|
+ return voList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 补充更新信息
|
|
|
+ * @param vo
|
|
|
+ * @param dbVO
|
|
|
+ * @param removeColumns
|
|
|
+ */
|
|
|
+ private void ensureUpdateInfos(ObjectDigital vo, ObjectDigital dbVO, Set<String> removeColumns) {
|
|
|
+ // 获取对象属性,不需要处理基类属性
|
|
|
+ Field[] fields = ReflectUtil.getFieldsDirectly(ObjectDigital.class, false);
|
|
|
+ // 按属性处理
|
|
|
+ for(Field field:fields) {
|
|
|
+ Object fieldValue = ReflectUtil.getFieldValue(vo, field);
|
|
|
+ Object dbValue = ReflectUtil.getFieldValue(dbVO, field);
|
|
|
+ if(fieldValue == null && dbValue == null) {
|
|
|
+ // 都为空不处理
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果不是扩展属性
|
|
|
+ if(!ObjectDigital.EXTRA_COLUMN.equalsIgnoreCase(field.getName())) {
|
|
|
+ // 如果待删除
|
|
|
+ if(CollUtil.contains(removeColumns, field.getName())) {
|
|
|
+ ReflectUtil.setFieldValue(vo, field, null);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果vo里值为空,则将数据库值赋上
|
|
|
+ if(fieldValue == null) {
|
|
|
+ ReflectUtil.setFieldValue(vo, field, dbValue);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果是扩展属性
|
|
|
+ ObjectNode infos = vo.getInfos();
|
|
|
+ ObjectNode dbInfos = dbVO.getInfos();
|
|
|
+ // 如果数据库有值,则补充到vo的infos中
|
|
|
+ if(dbInfos != null) {
|
|
|
+ if(infos == null) {
|
|
|
+ vo.setInfos(dbInfos);
|
|
|
+ } else {
|
|
|
+ // 如果都不为空,则将dbVO的infos数据拷贝到vo中
|
|
|
+ Iterator<String> infoFields = dbInfos.fieldNames();
|
|
|
+ while(infoFields.hasNext()) {
|
|
|
+ String infoField = infoFields.next();
|
|
|
+ if(infos.has(infoField)) {
|
|
|
+ // 如果已存在,则不需要
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ infos.set(infoField, dbInfos.get(infoField));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 最后,处理vo的infos
|
|
|
+ Iterator<String> infoFields = infos.fieldNames();
|
|
|
+ while(infoFields.hasNext()) {
|
|
|
+ String infoField = infoFields.next();
|
|
|
+ // 如果需要删除,则删除
|
|
|
+ if(CollUtil.contains(removeColumns, infoField)) {
|
|
|
+ infoFields.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果都删没了,则把infos置空
|
|
|
+ if(infos == null || infos.size() <= 0) {
|
|
|
+ vo.setInfos(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -603,44 +649,46 @@ public class ObjectDigitalServiceImpl extends ServiceImpl<ObjectDigitalMapper, O
|
|
|
* @param voList
|
|
|
* @param isUpdate
|
|
|
*/
|
|
|
- private Map<String, Map<String, ObjectInfoDefine>> validateOnSave(List<ObjectDigital> voList, boolean isUpdate) {
|
|
|
- return validateInfoCode(voList);
|
|
|
+ private void validateOnSave(List<ObjectDigital> voList, boolean isUpdate) {
|
|
|
+ validateInfoCode(voList);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 信息点值校验
|
|
|
* @param voList
|
|
|
*/
|
|
|
- private Map<String, Map<String, ObjectInfoDefine>> validateInfoCode(List<ObjectDigital> voList) {
|
|
|
+ private void validateInfoCode(List<ObjectDigital> voList) {
|
|
|
+ if(CollUtil.isEmpty(voList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
//获取该信息点当前classCode
|
|
|
List<String> typeCodeList = CollUtil.getFieldValues(voList, "classCode", String.class);
|
|
|
-
|
|
|
//查询通用类型:equipment,building,floor,space等
|
|
|
Map<String, String> objTypeMap = typeService.queryObjTypeMap();
|
|
|
- queryParentClassCode(typeCodeList,objTypeMap);
|
|
|
-
|
|
|
+ addParentClassCode(typeCodeList,objTypeMap);
|
|
|
// 查询信息点定义
|
|
|
Map<String, Map<String, ObjectInfoDefine>> allInfoMap = infoService.queryByTypes(typeCodeList);
|
|
|
// 信息点校验器
|
|
|
for(ObjectDigital vo:voList) {
|
|
|
- //设置自定义信息点
|
|
|
+ // 获取信息点列表 TODO 先这样处理,后续优化信息点接口后直接删掉此段即可
|
|
|
Map<String, ObjectInfoDefine> infoMap = new HashMap<>(16);
|
|
|
- if(CollUtil.isNotEmpty(allInfoMap)){
|
|
|
- Map<String, ObjectInfoDefine> infoDefineMap = allInfoMap.get(vo.getClassCode());
|
|
|
- if(CollUtil.isNotEmpty(infoDefineMap)){
|
|
|
- infoMap.putAll(infoDefineMap);
|
|
|
- }
|
|
|
- }
|
|
|
- String objType = objTypeMap.getOrDefault(vo.getClassCode(),"");
|
|
|
//设置通用信息点
|
|
|
+ String objType = objTypeMap.getOrDefault(vo.getClassCode(),"");
|
|
|
if(!objType.equals(vo.getClassCode()) && MapUtil.isNotEmpty(allInfoMap)){
|
|
|
Map<String, ObjectInfoDefine> commonInfoDefineMap = allInfoMap.getOrDefault(objType,new HashMap<>(16));
|
|
|
- if(null!=commonInfoDefineMap && commonInfoDefineMap.size()>0){
|
|
|
+ if(MapUtil.isNotEmpty(commonInfoDefineMap)){
|
|
|
infoMap.putAll(commonInfoDefineMap);
|
|
|
}
|
|
|
}
|
|
|
+ //设置自定义信息点
|
|
|
+ if(MapUtil.isNotEmpty(allInfoMap)){
|
|
|
+ Map<String, ObjectInfoDefine> infoDefineMap = allInfoMap.get(vo.getClassCode());
|
|
|
+ if(MapUtil.isNotEmpty(infoDefineMap)){
|
|
|
+ infoMap.putAll(infoDefineMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
if(MapUtil.isEmpty(infoMap)) {
|
|
|
- continue;
|
|
|
+ throw new BusinessException(StrUtil.format("没有对象类型【{}】的信息点定义信息!", vo.getClassCode()));
|
|
|
}
|
|
|
// 信息点值对象
|
|
|
ObjectNode infos = vo.getInfos();
|
|
@@ -653,137 +701,65 @@ public class ObjectDigitalServiceImpl extends ServiceImpl<ObjectDigitalMapper, O
|
|
|
String infoCode = infoKeys.next();
|
|
|
// 如果信息点中不存在,移除
|
|
|
if(!infoMap.containsKey(infoCode)) {
|
|
|
- // infos.remove(infoCode);
|
|
|
infoKeys.remove();
|
|
|
continue;
|
|
|
}
|
|
|
// 校验信息点值
|
|
|
InfoDataFactory.parse(infoMap.get(infoCode), infos.get(infoCode));
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
- return allInfoMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取remove信息点
|
|
|
* @param voList
|
|
|
- * @return
|
|
|
+ * @return Map<id, Set<RemoveColumn>>
|
|
|
*/
|
|
|
- private Map<String, Object> getRemoveInfo(List<ObjectDigital> voList){
|
|
|
- Map<String, Object> removeMap = new HashMap<>(16);
|
|
|
- for (ObjectDigital digital : voList) {
|
|
|
- ObjectNode infos = digital.getInfos();
|
|
|
- if(infos!=null){
|
|
|
- removeMap.put(digital.getId(),infos.get("$remove"));
|
|
|
+ private Map<String, Set<String>> convertRemoveInfo(List<ObjectDigital> voList){
|
|
|
+ final String REMOVE_FIELD = "$remove";
|
|
|
+ Map<String, Set<String>> removeMap = new HashMap<>(16);
|
|
|
+ for (ObjectDigital vo : voList) {
|
|
|
+ ObjectNode infos = vo.getInfos();
|
|
|
+ // 没有此列时不处理
|
|
|
+ if(infos == null || infos.size() <= 0 || infos.get(REMOVE_FIELD) == null
|
|
|
+ || !infos.get(REMOVE_FIELD).isArray()) {
|
|
|
+ continue;
|
|
|
}
|
|
|
+ // 收集删除列
|
|
|
+ ArrayNode removeNode = infos.withArray(REMOVE_FIELD);
|
|
|
+ Set<String> removeSet = new HashSet<>();
|
|
|
+ removeMap.put(vo.getId(), removeSet);
|
|
|
+ for(int i = 0,j = removeNode.size();i < j;i++) {
|
|
|
+ removeSet.add(removeNode.get(i).textValue());
|
|
|
+ }
|
|
|
+ // 处理完后,删除此项
|
|
|
+ infos.remove(REMOVE_FIELD);
|
|
|
}
|
|
|
return removeMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询对应的通用类型
|
|
|
+ * 添加对应的通用类型
|
|
|
* @param typeCodeList
|
|
|
*/
|
|
|
- private void queryParentClassCode(List<String> typeCodeList,Map<String, String> objTypeMap){
|
|
|
+ private void addParentClassCode(List<String> typeCodeList,Map<String, String> objTypeMap){
|
|
|
if(typeCodeList == null || typeCodeList.size()<=0){
|
|
|
return;
|
|
|
}
|
|
|
//查询classCode对应点objType
|
|
|
- List<String> parentCodes = new ArrayList<>();
|
|
|
+ Set<String> parentCodes = new HashSet<>();
|
|
|
for (String classCode : typeCodeList) {
|
|
|
String objType = objTypeMap.get(classCode);
|
|
|
if(StringUtils.isEmpty(objType) || "0".equals(objType) || parentCodes.contains(objType)){
|
|
|
- return ;
|
|
|
+ continue;
|
|
|
}
|
|
|
parentCodes.add(objType);
|
|
|
}
|
|
|
+ parentCodes.addAll(typeCodeList);
|
|
|
+ // 将新Set值返给List
|
|
|
+ typeCodeList.clear();
|
|
|
typeCodeList.addAll(parentCodes);
|
|
|
}
|
|
|
- /**
|
|
|
- * 数据合并
|
|
|
- * @param dbVO 源数据
|
|
|
- * @param vo 目标数据
|
|
|
- * @param vo 待删除待数据
|
|
|
- */
|
|
|
- private void dataMerge(ObjectDigital dbVO,ObjectDigital vo,Map<String, ObjectInfoDefine> infoDefineMap,Set<String> fieldSet){
|
|
|
- //基础属性处理
|
|
|
- handleBasicColumn(dbVO,vo);
|
|
|
- //动态信息点处理
|
|
|
- ObjectNode infos = vo.getInfos();
|
|
|
- for (Map.Entry<String, ObjectInfoDefine> entry : infoDefineMap.entrySet()) {
|
|
|
- String code = entry.getKey();
|
|
|
- if(fieldSet.contains(code)){
|
|
|
- //属于基础信息点
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!infos.has(code)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- JsonNode value = infos.get(code);
|
|
|
- if (dbVO.getInfos() == null) {
|
|
|
- dbVO.setInfos(JsonNodeFactory.instance.objectNode());
|
|
|
- }
|
|
|
- dbVO.getInfos().set(code, value);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 基础属性处理
|
|
|
- * @param dbVO
|
|
|
- * @param vo
|
|
|
- */
|
|
|
- private void handleBasicColumn(ObjectDigital dbVO,ObjectDigital vo){
|
|
|
- if(StringUtils.isNotEmpty(vo.getName())){
|
|
|
- dbVO.setName(vo.getName());
|
|
|
- }
|
|
|
- if(StringUtils.isNotEmpty(vo.getLocalId())){
|
|
|
- dbVO.setLocalId(vo.getLocalId());
|
|
|
- }
|
|
|
- if(StringUtils.isNotEmpty(vo.getLocalName())){
|
|
|
- dbVO.setLocalName(vo.getLocalName());
|
|
|
- }
|
|
|
- if(vo.getVirtualCodes()!=null){
|
|
|
- dbVO.setVirtualCodes(vo.getVirtualCodes());
|
|
|
- }
|
|
|
- if(vo.getValid()!=null){
|
|
|
- dbVO.setValid(vo.getValid());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除需要移除的信息点
|
|
|
- * @param dbVO
|
|
|
- * @param remove
|
|
|
- * @param basicFieldSet
|
|
|
- */
|
|
|
- private void remove(ObjectDigital dbVO,Object remove,Set<String> basicFieldSet){
|
|
|
- if(remove == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- JSONArray array = JSONArray.parseArray(remove.toString());
|
|
|
- array.forEach(item->{
|
|
|
- String removeCode = (String) item;
|
|
|
- if(basicFieldSet.contains(removeCode)){
|
|
|
- //基础信息
|
|
|
- if("name".equals(removeCode)){
|
|
|
- dbVO.setName(null);
|
|
|
- }
|
|
|
- if("localId".equals(removeCode)){
|
|
|
- dbVO.setLocalId(null);
|
|
|
- }
|
|
|
- if("localName".equals(removeCode)){
|
|
|
- dbVO.setLocalName(null);
|
|
|
- }
|
|
|
- if("virtualCodes".equals(removeCode)){
|
|
|
- dbVO.setVirtualCodes(new ArrayList<>());
|
|
|
- }
|
|
|
- return;
|
|
|
- }
|
|
|
- dbVO.getInfos().remove(removeCode);
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 修改时填充classCode
|