Browse Source

优化 关系保存逻辑及效率

yucheng 3 years ago
parent
commit
36782c4430

+ 15 - 67
dmp-business/dmp-rwd/src/main/java/com/persagy/dmp/rwd/digital/controller/ObjectRelationController.java

@@ -1,25 +1,20 @@
 package com.persagy.dmp.rwd.digital.controller;
 
 import cn.hutool.core.collection.CollUtil;
-import cn.hutool.extra.spring.SpringUtil;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.persagy.dmp.basic.model.QueryCriteria;
+import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
 import com.persagy.dmp.common.constant.CommonConstant;
 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.model.response.CommonResult;
 import com.persagy.dmp.common.utils.ParamCheckUtil;
 import com.persagy.dmp.common.utils.ResultHelper;
 import com.persagy.dmp.digital.entity.ObjectRelation;
 import com.persagy.dmp.mybatis.utils.ConditionUtil;
-import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
 import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
-import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -46,11 +41,8 @@ public class ObjectRelationController {
         if(criteria == null) {
             throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
         }
-        //基础参数校验
-        // ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
         // 2021年9月8日18:23:25 放开单项目限制
         ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
-
         QueryWrapper<ObjectRelation> wrapper = new QueryWrapper<>();
         // 添加所属项目条件
         ConditionUtil.ensureProjectCriteriaDirectly(wrapper);
@@ -58,68 +50,48 @@ public class ObjectRelationController {
         QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelation.class);
         Page page = QueryCriteriaHelper.toPage(criteria);
         if(criteria.isOnlyCount()) {
-            Integer total = service.queryCount(wrapper);
+            Integer total = service.count(wrapper);
             page.setTotal(total);
         } else {
-            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
+            page = service.page(page, wrapper);
         }
         return ResultHelper.multi(page.getRecords(), page.getTotal());
     }
 
     @PostMapping("/createOne")
     public CommonResult<ObjectRelation> create(@Valid @RequestBody ObjectRelation vo){
-        //基础参数校验
         ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
-        List<ObjectRelation> voList = CollUtil.newArrayList(vo);
-        //创建默认条件
-        createDefaultValue(voList);
-        //新增对象关系
-        service.insert(voList);
+        service.save(CollUtil.newArrayList(vo));
         return ResultHelper.single(vo);
     }
 
     @PostMapping("/create")
     public CommonResult<List<ObjectRelation>> createBatch(@Valid @RequestBody List<ObjectRelation> voList){
-        //基础参数校验
         ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
-        //创建默认条件
-        createDefaultValue(voList);
-        voList = service.insert(voList);
-        //批量新增对象关系
+        service.save(voList);
         return ResultHelper.multi(voList);
     }
 
     @PostMapping("/updateOne")
-    public CommonResult<ObjectRelation> update(@Valid @RequestBody ObjectRelation vo) throws Exception{
-        //基础参数校验
+    public CommonResult<ObjectRelation> update(@Valid @RequestBody ObjectRelation vo) {
         ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
-        List<ObjectRelation> voList = CollUtil.newArrayList(vo);
-        //修改默认条件
-        createDefaultValue(voList);
-        //修改对象关系
-        service.update(voList);
+        service.save(CollUtil.newArrayList(vo));
         return ResultHelper.single(vo);
     }
 
     @PostMapping("/update")
-    public CommonResult<List<ObjectRelation>> updateBatch(@Valid @RequestBody List<ObjectRelation> voList) throws Exception{
-        //基础参数校验
+    public CommonResult<List<ObjectRelation>> updateBatch(@Valid @RequestBody List<ObjectRelation> voList){
         ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
-        //修改默认条件
-        createDefaultValue(voList);
-        //批量修改对象关系
-        voList = service.update(voList);
+        service.save(voList);
         return ResultHelper.multi(voList);
     }
 
     @PostMapping("/deleteOne")
     public CommonResult delete(@RequestBody String id){
-        QueryCriteria queryCriteria = new QueryCriteria();
-        ObjectMapper objectMapper = SpringUtil.getBean(ObjectMapper.class);
-        ObjectNode objectNode = objectMapper.createObjectNode();
-        objectNode.put(ObjectRelation.PROP_ID,id);
-        queryCriteria.setCriteria(objectNode);
-        this.deleteBatch(queryCriteria);
+        if(StrUtil.isBlank(id)) {
+            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
+        }
+        service.removeById(id);
         return ResultHelper.success();
     }
 
@@ -139,30 +111,6 @@ public class ObjectRelationController {
         // 转换查询条件
         QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelation.class);
         List<ObjectRelation> delList = service.delete(wrapper);
-        return ResultHelper.multi(delList,delList.size());
-    }
-
-    /**
-     * 创建默认条件
-     * @param voList
-     */
-    private void createDefaultValue(List<ObjectRelation> voList){
-        String groupCode = AppContext.getContext().getGroupCode();
-        if(StringUtils.isEmpty(groupCode)){
-            //默认集团
-            groupCode = CommonConstant.DEFAULT_ID;
-        }
-        String projectId = AppContext.getContext().getProjectId();
-        if(StringUtils.isEmpty(projectId)){
-            //默认项目
-            projectId = CommonConstant.DEFAULT_ID;
-        }
-        for (ObjectRelation vo : voList) {
-            vo.setGroupCode(groupCode);
-            vo.setProjectId(projectId);
-            if(vo.getValid() == null) {
-                vo.setValid(ValidEnum.TRUE.getType());
-            }
-        }
+        return ResultHelper.multi(delList);
     }
 }

+ 8 - 1
dmp-business/dmp-rwd/src/main/java/com/persagy/dmp/rwd/digital/dao/ObjectRelationMapper.java

@@ -1,7 +1,6 @@
 package com.persagy.dmp.rwd.digital.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.persagy.dmp.basic.dto.RelationCalDTO;
 import com.persagy.dmp.digital.entity.ObjectDigital;
 import com.persagy.dmp.digital.entity.ObjectRelation;
 import com.persagy.dmp.rwd.digital.domain.CalculatingDTO;
@@ -17,6 +16,14 @@ import java.util.Set;
  * @date 2021-07-08
  */
 public interface ObjectRelationMapper extends BaseMapper<ObjectRelation> {
+
+    /**
+     * 保存
+     * @param vos
+     * @return
+     */
+    void save(@Param("vos") List<ObjectRelation> vos);
+
     /***
      * Description: 根据对象id查询关系
      * @param objIds : 对象id

+ 3 - 33
dmp-business/dmp-rwd/src/main/java/com/persagy/dmp/rwd/digital/service/IObjectRelationService.java

@@ -1,11 +1,9 @@
 package com.persagy.dmp.rwd.digital.service;
 
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.persagy.dmp.basic.dto.RelationCalDTO;
 import com.persagy.dmp.digital.entity.ObjectRelation;
+
 import java.util.List;
 
 /**
@@ -16,44 +14,16 @@ import java.util.List;
 public interface IObjectRelationService extends IService<ObjectRelation> {
 
     /**
-     * 按条件查询
-     * @param queryWrapper 查询条件
-     * @return 列表
-     */
-    List<ObjectRelation> queryByCondition(Wrapper<ObjectRelation> queryWrapper);
-
-    /**
-     * 按条件计数
-     * @param queryWrapper 查询条件
-     * @return 结果数量
-     */
-    Integer queryCount(Wrapper<ObjectRelation> queryWrapper);
-
-    /**
-     * 按条件查询
-     * @param page 分页条件
-     * @param queryWrapper 查询条件
-     * @return 分页结果
-     */
-    Page queryByCondition(Page page, Wrapper<ObjectRelation> queryWrapper);
-
-    /**
      * 新增
      * @param voList 待新增数据
      * @return 新增后数据
      */
-    List<ObjectRelation> insert(List<ObjectRelation> voList);
-
-    /**
-     * 修改
-     * @param voList 待修改数据
-     * @return 修改后数据
-     */
-    List<ObjectRelation> update(List<ObjectRelation> voList) throws Exception;
+    List<ObjectRelation> save(List<ObjectRelation> voList);
 
     /**
      * 删除
      * @param wrapper 删除条件
+     * @return 已删除的数据
      */
     List<ObjectRelation> delete(QueryWrapper wrapper);
 }

+ 27 - 112
dmp-business/dmp-rwd/src/main/java/com/persagy/dmp/rwd/digital/service/impl/ObjectRelationServiceImpl.java

@@ -2,13 +2,12 @@ package com.persagy.dmp.rwd.digital.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.IdUtil;
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import cn.hutool.core.util.StrUtil;
 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.persagy.dmp.basic.constant.DigitalMessageConstant;
 import com.persagy.dmp.common.constant.ValidEnum;
+import com.persagy.dmp.common.context.AppContext;
 import com.persagy.dmp.digital.entity.ObjectRelation;
 import com.persagy.dmp.rwd.basic.utils.DigitalCommonUtils;
 import com.persagy.dmp.rwd.basic.utils.DigitalMessageHelper;
@@ -37,43 +36,20 @@ public class ObjectRelationServiceImpl extends ServiceImpl<ObjectRelationMapper,
     @Autowired
     private DigitalMessageHelper messageSender;
 
-    @Autowired
-    private IObjectRelationService iService;
-
-    /**
-     * 查询对象关系数据
-     * @param queryWrapper 查询条件
-     * @return
-     */
-    @Override
-    public List<ObjectRelation> queryByCondition(Wrapper<ObjectRelation> queryWrapper) {
-        return dao.selectList(queryWrapper);
-    }
-
-    @Override
-    public Integer queryCount(Wrapper<ObjectRelation> queryWrapper) {
-        return dao.selectCount(queryWrapper);
-    }
-
-    @Override
-    public Page queryByCondition(Page page, Wrapper<ObjectRelation> queryWrapper) {
-        return dao.selectPage(page, queryWrapper);
-    }
-
     /**
      * 新增关系数据
      * @param voList 待新增数据
      * @return
      */
     @Override
-    public List<ObjectRelation> insert(List<ObjectRelation> voList) {
-        //创建默认的图实例id
-        createGraphId(voList);
-        //数据重复性校验,若存在重复数据,填充关系ID
-        checkRepeatData(voList,false);
-        //新增关系数据
-        iService.saveOrUpdateBatch(voList);
-        //发送新增消息
+    public List<ObjectRelation> save(List<ObjectRelation> voList) {
+        if(CollUtil.isEmpty(voList)) {
+            return null;
+        }
+        ensureDefaultValue(voList);
+        // 新增关系数据
+        dao.save(voList);
+        // 发送新增消息 -- 不重新查询了,主键不保真。应基于objFrom、objTo等信息判定唯一
         voList.forEach(vo->{
             messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_INSERT, null, vo, false);
         });
@@ -81,80 +57,24 @@ public class ObjectRelationServiceImpl extends ServiceImpl<ObjectRelationMapper,
     }
 
     /**
-     * 数据重复性校验,存在重复数据时,自动填充关系ID,作更新操作
+     * 补充默认值
      * @param voList
      */
-    private Boolean checkRepeatData(List<ObjectRelation> voList,Boolean isUpdate){
-        if(CollUtil.isEmpty(voList)){
-            return true;
-        }
-        Boolean flag = true;
-        for (ObjectRelation relation : voList) {
-            //条件查询关系数据
-            LambdaQueryWrapper<ObjectRelation> wrapper = new LambdaQueryWrapper<>();
-            wrapper.eq(ObjectRelation::getGroupCode,relation.getGroupCode());
-            wrapper.eq(ObjectRelation::getProjectId,relation.getProjectId());
-            wrapper.eq(ObjectRelation::getGraphCode, relation.getGraphCode());
-            wrapper.eq(ObjectRelation::getRelCode,relation.getRelCode());
-            wrapper.eq(ObjectRelation::getObjFrom,relation.getObjFrom());
-            wrapper.eq(ObjectRelation::getObjTo,relation.getObjTo());
-            wrapper.eq(ObjectRelation::getValid,true);
-            List<ObjectRelation> relationList = queryByCondition(wrapper);
-
-            if(CollUtil.isNotEmpty(relationList)){
-                //新增关系时数据已存在,待添加数据填充已存在的ID
-                if(!isUpdate){
-                    relation.setId(relationList.get(0).getId());
-                    continue;
-                }
-                //判断更新时关系数据是否重复
-                String existId = relationList.get(0).getId();
-                if(isUpdate && !relation.getId().equals(existId)){
-                    flag = false;
-                }
-            }else{
-                if(!isUpdate){
-                    //新增时关系数据不存在,设置自增ID
-                    relation.setId(IdUtil.simpleUUID());
-                }
+    private void ensureDefaultValue(List<ObjectRelation> voList) {
+        String groupCode = AppContext.getContext().getGroupCode();
+        String projectId = AppContext.getContext().getProjectId();
+        String appId = AppContext.getContext().getAppId();
+        for (ObjectRelation vo : voList) {
+            // 图实例id
+            vo.setGraphId(DigitalCommonUtils.getDefaultGraphIdByGraphCode(vo.getGraphCode()));
+            vo.setGroupCode(groupCode);
+            vo.setProjectId(projectId);
+            vo.setCreateApp(appId);
+            vo.setValid(ValidEnum.TRUE.getType());
+            if(StrUtil.isBlank(vo.getId())) {
+                vo.setId(IdUtil.simpleUUID());
             }
         }
-        return flag;
-    }
-    /**
-     * 创建默认的图实例id
-     * @param voList
-     */
-    private void createGraphId(List<ObjectRelation> voList){
-        if(CollUtil.isEmpty(voList)){
-            return;
-        }
-        for (ObjectRelation objectRelation : voList) {
-            String graphCode = objectRelation.getGraphCode();
-            String graphId = DigitalCommonUtils.getDefaultGraphIdByGraphCode(graphCode);
-            objectRelation.setGraphId(graphId);
-        }
-    }
-
-    /**
-     * 修改关系数据
-     * @param voList 修改条件
-     * @return
-     */
-    @Override
-    public List<ObjectRelation> update(List<ObjectRelation> voList) throws Exception{
-        //数据重复性校验
-        Boolean flag = checkRepeatData(voList, true);
-        if(!flag){
-            throw new Exception("存在重复的关系数据");
-        }
-        //修改关系数据
-        iService.saveOrUpdateBatch(voList);
-        //发送修改消息
-        voList.forEach(vo->{
-            messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_UPDATE, null, vo, false);
-        });
-        return voList;
     }
 
     /**
@@ -164,17 +84,12 @@ public class ObjectRelationServiceImpl extends ServiceImpl<ObjectRelationMapper,
      */
     @Override
     public List<ObjectRelation> delete(QueryWrapper wrapper) {
-        //根据条件查询有效关系数据
-        wrapper.eq("valid",ValidEnum.TRUE.getType());
         List<ObjectRelation> list = dao.selectList(wrapper);
-        if(list == null || list.size()<=0){
+        if(CollUtil.isEmpty(list)){
             return new ArrayList<>();
         }
-        //批量删除关系数据
-        for (ObjectRelation relation : list) {
-            relation.setValid(ValidEnum.FALSE.getType());
-        }
-        iService.saveOrUpdateBatch(list);
+        // 20211201 不使用逻辑删除了,改为真删除
+        dao.delete(wrapper);
         //发送删除消息
         list.forEach(vo->{
             messageSender.sendMessage(DigitalMessageConstant.OPERATE_AFTER_DELETE, null, vo, false);

+ 1 - 1
dmp-business/dmp-rwd/src/main/resources/db/init/schema.sql

@@ -164,7 +164,7 @@ CREATE TABLE IF NOT EXISTS `dt_relation` (
   `ts` timestamp default current_timestamp on update current_timestamp NOT NULL COMMENT '乐观锁',
   PRIMARY KEY (`id`) USING BTREE,
   KEY `idx_graph_code` (`graph_code`) USING BTREE,
-  KEY `idx_1` (`graph_code`,`rel_code`,`obj_from`,`obj_to`,`project_id`) USING BTREE,
+  UNIQUE KEY `idx_1`(`graph_code`,`rel_code`,`obj_from`,`obj_to`,`rel_value`,`project_id`) USING BTREE,
   KEY `idx_2` (`graph_id`,`rel_code`,`obj_from`,`obj_to`,`project_id`) USING BTREE,
   KEY `idx_3` (`graph_code`,`rel_code`,`obj_to`,`project_id`) USING BTREE,
   KEY `idx_4` (`graph_id`,`rel_code`,`obj_to`,`project_id`) USING BTREE,

+ 14 - 0
dmp-business/dmp-rwd/src/main/resources/mapper/ObjectRelationMapper.xml

@@ -57,6 +57,20 @@
         creation_time,modifier,modified_time,
         valid,ts
     </sql>
+    <insert id="save">
+        INSERT INTO dt_relation(id,group_code,project_id,graph_id,graph_code,rel_code,rel_value,
+            obj_from,obj_to,create_app,update_app,creator,creation_time,modifier,modified_time,valid)
+        VALUES
+        <foreach collection="vos" item="vo" open="(" close=")" separator="),(">
+            #{vo.id},#{vo.groupCode},#{vo.projectId},#{vo.graphId},#{vo.graphCode},#{vo.relCode},#{vo.relValue},
+            #{vo.objFrom},#{vo.objTo},#{vo.createApp},#{vo.updateApp},#{vo.creator},#{vo.creationTime},
+            #{vo.modifier},#{vo.modifiedTime},#{vo.valid}
+        </foreach>
+        ON DUPLICATE KEY UPDATE
+            graph_id = VALUES(graph_id), graph_code = VALUES(graph_code), rel_code = VALUES(rel_code),
+            rel_value = VALUES(rel_value), obj_from = VALUES(obj_from), obj_to = VALUES(obj_to),
+            update_app = VALUES(update_app), modifier = VALUES(modifier), modified_time = VALUES(modified_time)
+    </insert>
     <insert id="insertObjectRelationByRelChain">
         INSERT INTO dt_relation (
             id,