浏览代码

枣阳中台角色信息同步,增加子系统标识

欧阳劲驰 2 周之前
父节点
当前提交
adbc5846ee

+ 2 - 2
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/config/PrimaryMybatisConfig.java

@@ -63,8 +63,8 @@ public class PrimaryMybatisConfig {
      * @param dataSource 数据源
      * @return 事物管理器
      */
-    @Bean(PRIMARY + "DbTransactionManager")
-    public DataSourceTransactionManager mainDbTransactionManager(@Qualifier(PRIMARY + "DataSource") DataSource dataSource) {
+    @Bean(PRIMARY + "TransactionManager")
+    public DataSourceTransactionManager mainTransactionManager(@Qualifier(PRIMARY + "DataSource") DataSource dataSource) {
         return new DataSourceTransactionManager(dataSource);
     }
 }

+ 20 - 0
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/domain/po/FunctionInfo.java

@@ -51,4 +51,24 @@ public class FunctionInfo {
      * 修改时间
      */
     private LocalDateTime updateTime;
+
+    /**
+     * 废弃删除标识:0--未废弃;1--已废弃
+     */
+    private Short disused;
+
+    /**
+     * 显示顺序
+     */
+    private Short ordering;
+
+    /**
+     * 父id
+     */
+    private String puid;
+
+    /**
+     * 职能类型
+     */
+    private String kind;
 }

+ 15 - 1
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/mapper/primary/FunctionInfoMapper.java

@@ -2,9 +2,12 @@ package com.shkpr.service.bespokegateway.core.mapper.primary;
 
 import com.shkpr.service.bespokegateway.core.domain.po.FunctionInfo;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
- * 用户信息mapper
+ * 职能信息mapper
  *
  * @author 欧阳劲驰
  * @since 1.0.0
@@ -12,10 +15,21 @@ import org.apache.ibatis.annotations.Mapper;
 @Mapper
 public interface FunctionInfoMapper {
     /**
+     * 查询全部
+     * @return  实体集合
+     */
+    List<FunctionInfo> findAll();
+    /**
      * 合并
      *
      * @param record 对象
      * @return 保存数量
      */
     int upsert(FunctionInfo record);
+    /**
+     * 根据id删除
+     * @param uids id集合
+     * @return 删除项
+     */
+    int deleteByUids(@Param("uids") List<String> uids);
 }

+ 3 - 3
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/service/FunctionInfoService.java

@@ -13,10 +13,10 @@ import java.util.List;
  */
 public interface FunctionInfoService {
     /**
-     * 批量合并操作
+     * 批量同步
      *
      * @param dates 数据集合
-     * @return 合并状态
+     * @return 同步状态
      */
-    Boolean upsertAll(List<FunctionInfo> dates);
+    Boolean syncAll(List<FunctionInfo> dates);
 }

+ 20 - 4
bespoke-gateway-core/src/main/java/com/shkpr/service/bespokegateway/core/service/impl/FunctionInfoServiceImpl.java

@@ -13,9 +13,12 @@ import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.sql.Connection;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * 职能信息service实现
@@ -33,21 +36,32 @@ public class FunctionInfoServiceImpl implements FunctionInfoService {
 
     final
     SqlSessionFactory sqlSessionFactory;
+    final
+    FunctionInfoMapper functionInfoMapper;
 
-    public FunctionInfoServiceImpl(@Qualifier(DataSourceNames.PRIMARY + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
+    public FunctionInfoServiceImpl(@Qualifier(DataSourceNames.PRIMARY + "SqlSessionFactory") SqlSessionFactory sqlSessionFactory, FunctionInfoMapper functionInfoMapper) {
         this.sqlSessionFactory = sqlSessionFactory;
+        this.functionInfoMapper = functionInfoMapper;
     }
 
     /**
-     * 批量合并操作
+     * 批量同步
      *
      * @param dates 数据集合
-     * @return 合并状态
+     * @return 同步状态
      */
     @Override
-    public Boolean upsertAll(List<FunctionInfo> dates) {
+    @Transactional(DataSourceNames.PRIMARY + "TransactionManager")
+    public Boolean syncAll(List<FunctionInfo> dates) {
         if (CollectionUtils.isEmpty(dates)) return true;
 
+        //uid集合
+        Set<String> uids = dates.stream().map(FunctionInfo::getUid).collect(Collectors.toSet());
+        //过滤需要删除的uid
+        List<String> toDelete = functionInfoMapper.findAll().stream().map(FunctionInfo::getUid)
+                .filter(uid -> !uids.contains(uid))
+                .collect(Collectors.toList());
+
         LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
                 , String.format(
                         "开始批量写入职能信息,开启批处理 数据量:%s"
@@ -66,6 +80,8 @@ public class FunctionInfoServiceImpl implements FunctionInfoService {
                 //从session获取mapper
                 FunctionInfoMapper mapper = sqlSession.getMapper(FunctionInfoMapper.class);
 
+                //批量删除
+                if (CollectionUtils.isNotEmpty(toDelete)) mapper.deleteByUids(toDelete);
                 //批量写入
                 dates.forEach(mapper::upsert);
 

+ 27 - 3
bespoke-gateway-core/src/main/resources/mapper/FunctionInfoMapper.xml

@@ -1,14 +1,38 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.shkpr.service.bespokegateway.core.mapper.primary.FunctionInfoMapper">
+    <select id="findAll" resultType="com.shkpr.service.bespokegateway.core.domain.po.FunctionInfo">
+        select id,
+               uid,
+               name,
+               remark,
+               param1,
+               param2,
+               create_time,
+               update_time,
+               disused,
+               ordering,
+               puid,
+               kind
+        from k4_bs_sys_function_info
+    </select>
     <insert id="upsert">
-        insert into k4_bs_sys_function_info (uid, "name", remark, update_time)
+        insert into k4_bs_sys_function_info (uid, "name", remark, update_time, puid)
         values (#{uid,jdbcType=VARCHAR},
                 #{name,jdbcType=VARCHAR},
                 #{remark,jdbcType=VARCHAR},
-                timezone('utc', localtimestamp(0) at time zone current_setting('timezone')))
+                localtimestamp(0),
+                #{puid,jdbcType=VARCHAR})
         on conflict (uid) do update set "name"      = excluded."name",
                                         remark      = excluded.remark,
-                                        update_time = excluded.update_time
+                                        update_time = excluded.update_time,
+                                        puid        = excluded.puid
     </insert>
+    <delete id="deleteByUids">
+        delete from k4_bs_sys_function_info
+        where uid in
+        <foreach collection="uids" item="uid" open="(" close=")" separator=",">
+            #{uid, jdbcType=VARCHAR}
+        </foreach>
+    </delete>
 </mapper>

+ 27 - 18
bespoke-gateway-zydma/src/main/java/com/shkpr/service/bespokegateway/zydma/components/InfoSynchronizer.java

@@ -3,7 +3,6 @@ package com.shkpr.service.bespokegateway.zydma.components;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
-import com.shkpr.service.bespokegateway.zydma.domain.*;
 import com.shkpr.service.bespokegateway.core.constants.LogFlagBusiType;
 import com.shkpr.service.bespokegateway.core.domain.CallingEndpoint;
 import com.shkpr.service.bespokegateway.core.domain.IntegrationKey;
@@ -15,7 +14,9 @@ import com.shkpr.service.bespokegateway.core.service.FunctionInfoService;
 import com.shkpr.service.bespokegateway.core.service.PersonnelInfoService;
 import com.shkpr.service.bespokegateway.core.utils.CallingUtil;
 import com.shkpr.service.bespokegateway.zydma.constants.MiddlePlatformMetadata;
+import com.shkpr.service.bespokegateway.zydma.domain.*;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.http.Header;
 import org.apache.http.message.BasicHeader;
@@ -123,14 +124,28 @@ public class InfoSynchronizer {
         CallingEndpoint endpoint = callingProperties.getEndpoint(MiddlePlatformMetadata.NAME);
 
         //获取角色
-        List<MiddlePlatformRole> roles = getRoles(endpoint);
+        List<MiddlePlatformSubsystem> roles = getRoles(endpoint);
 
         //转换职能对象
-        List<FunctionInfo> dates = roles.stream()
-                .map(MiddlePlatformRole::toFunctionInfo).collect(Collectors.toList());
+        List<FunctionInfo> dates = new ArrayList<>(roles.stream()
+                .flatMap(subsystem -> subsystem.getRoleList().stream()
+                        .map(role -> {
+                            FunctionInfo functionInfo = role.toFunctionInfo();
+                            functionInfo.setPuid(subsystem.getVisibleValue());
+                            return functionInfo;
+                        }))
+                //根据uid排序
+                .sorted(Comparator.comparingLong(role -> Long.parseLong(role.getUid())))
+                //根据uid去重
+                .collect(Collectors.toMap(
+                        FunctionInfo::getUid,
+                        Function.identity(),
+                        (existing, replacement) -> existing,
+                        LinkedHashMap::new
+                )).values());
 
         //批量写入职能
-        Boolean upserted = functionInfoService.upsertAll(dates);
+        Boolean upserted = functionInfoService.syncAll(dates);
         long end = System.currentTimeMillis();
         LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
                 , String.format(
@@ -181,9 +196,9 @@ public class InfoSynchronizer {
      * @param endpoint 对接点
      * @return 角色列表
      */
-    private List<MiddlePlatformRole> getRoles(CallingEndpoint endpoint) {
+    private List<MiddlePlatformSubsystem> getRoles(CallingEndpoint endpoint) {
         //结果
-        List<MiddlePlatformRole> result = new ArrayList<>();
+        List<MiddlePlatformSubsystem> result = new ArrayList<>();
         //请求地址
         String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_GROUP_ROLES;
         //参数
@@ -214,22 +229,16 @@ public class InfoSynchronizer {
         subsystems.forEach(new Consumer<MiddlePlatformSubsystem>() {
             @Override
             public void accept(MiddlePlatformSubsystem subsystem) {
-                //填入角色
-                if (subsystem.getRoleList() != null) result.addAll(subsystem.getRoleList());
+                //填入结果
+                if (MiddlePlatformMetadata.SUBSYSTEMS.contains(subsystem.getVisibleValue()) &&
+                        CollectionUtils.isNotEmpty(subsystem.getRoleList()))
+                    result.add(subsystem);
                 //递归子系统
                 if (subsystem.getChild() != null) subsystem.getChild().forEach(this);
             }
         });
 
-        //根据角色id排序去重
-        return new ArrayList<>(result.stream()
-                .sorted(Comparator.comparingLong(role -> Long.parseLong(role.getRoleId()))) // 如果是int类型
-                .collect(Collectors.toMap(
-                        MiddlePlatformRole::getRoleId,
-                        Function.identity(),
-                        (existing, replacement) -> existing,
-                        LinkedHashMap::new
-                )).values());
+        return result;
     }
 
     /**

+ 4 - 0
bespoke-gateway-zydma/src/main/java/com/shkpr/service/bespokegateway/zydma/constants/MiddlePlatformMetadata.java

@@ -6,6 +6,8 @@ import org.apache.commons.codec.digest.DigestUtils;
 
 import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.util.Arrays;
+import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
@@ -18,6 +20,8 @@ import java.util.stream.Collectors;
 public abstract class MiddlePlatformMetadata extends IntegrationMetadata {
     //系统名称
     public static String NAME = "zaoyang-middle";
+    //子系统
+    public static List<String> SUBSYSTEMS = Arrays.asList("DMA", "gonggongyongshui", "loushunjihe");
 
     /**
      * 获取密钥