From b415133f5ef645ac121c890a995de6d43625b364 Mon Sep 17 00:00:00 2001 From: tiger_zhou <123456> Date: Fri, 4 Nov 2022 10:56:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtss/controller/user/UserController.java | 3 + .../dao/permission/PermissionSubjectDAO.java | 14 ++ .../PermissionSystemAbilityDAO.java | 13 +- .../RtsMapFunctionServiceImpl.java | 1 + .../OldPermissionDataSyncService.java | 184 ++++++++++++------ .../vo/permission/sync/UserPermissionVO.java | 2 +- .../mybatis/mapper/permission/SyncDAO.xml | 2 +- .../services/permission/SyncServiceTest.java | 1 + 8 files changed, 154 insertions(+), 66 deletions(-) diff --git a/src/main/java/club/joylink/rtss/controller/user/UserController.java b/src/main/java/club/joylink/rtss/controller/user/UserController.java index 04581e68b..a91e45f3a 100644 --- a/src/main/java/club/joylink/rtss/controller/user/UserController.java +++ b/src/main/java/club/joylink/rtss/controller/user/UserController.java @@ -66,6 +66,9 @@ public class UserController { @Role({RoleEnum.SuperAdmin, RoleEnum.Admin}) @GetMapping(path = "sync") public String sync( HttpServletResponse response){ + if(!this.syncService.initData()){ + return "正在同步。。。"; + } File file = this.syncService.sync(); if(Objects.isNull(file) || !file.exists()){ return "未找到统计的同步文件"; diff --git a/src/main/java/club/joylink/rtss/dao/permission/PermissionSubjectDAO.java b/src/main/java/club/joylink/rtss/dao/permission/PermissionSubjectDAO.java index 9678800c9..136bcb247 100644 --- a/src/main/java/club/joylink/rtss/dao/permission/PermissionSubjectDAO.java +++ b/src/main/java/club/joylink/rtss/dao/permission/PermissionSubjectDAO.java @@ -5,6 +5,7 @@ import club.joylink.rtss.entity.permission.PermissionSubjectExample; import club.joylink.rtss.vo.permission.PermissionSubjectStatusEnum; import club.joylink.rtss.vo.permission.subject.PermissionSubjectQueryVO; import club.joylink.rtss.vo.permission.subject.PermissionSubjectVO; +import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @@ -81,6 +82,19 @@ public interface PermissionSubjectDAO { int insert(PermissionSubject record); + @Insert("") + void batchInsert(@Param("list") List list); int insertSelective(PermissionSubject record); List selectByExample(PermissionSubjectExample example); diff --git a/src/main/java/club/joylink/rtss/dao/permission/PermissionSystemAbilityDAO.java b/src/main/java/club/joylink/rtss/dao/permission/PermissionSystemAbilityDAO.java index d6be054d7..dfd3b95cb 100644 --- a/src/main/java/club/joylink/rtss/dao/permission/PermissionSystemAbilityDAO.java +++ b/src/main/java/club/joylink/rtss/dao/permission/PermissionSystemAbilityDAO.java @@ -1,8 +1,11 @@ package club.joylink.rtss.dao.permission; +import club.joylink.rtss.entity.permission.PermissionSubject; import club.joylink.rtss.entity.permission.PermissionSystemAbility; import club.joylink.rtss.entity.permission.PermissionSystemAbilityExample; import java.util.List; + +import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @@ -16,6 +19,14 @@ public interface PermissionSystemAbilityDAO { int deleteByPrimaryKey(Long id); + @Insert("") + void batchInsert(@Param("list") List list); int insert(PermissionSystemAbility record); int insertSelective(PermissionSystemAbility record); @@ -31,4 +42,4 @@ public interface PermissionSystemAbilityDAO { int updateByPrimaryKeySelective(PermissionSystemAbility record); int updateByPrimaryKey(PermissionSystemAbility record); -} \ No newline at end of file +} diff --git a/src/main/java/club/joylink/rtss/services/mapFunction/RtsMapFunctionServiceImpl.java b/src/main/java/club/joylink/rtss/services/mapFunction/RtsMapFunctionServiceImpl.java index 067d48fb7..02d7b8acd 100644 --- a/src/main/java/club/joylink/rtss/services/mapFunction/RtsMapFunctionServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/mapFunction/RtsMapFunctionServiceImpl.java @@ -135,6 +135,7 @@ public class RtsMapFunctionServiceImpl implements RtsMapFunctionService { return msgList; } + @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW) @Override public List generateLpf(long mapId, long creatorId) { List pfDataList = mapPassengerFlowDataService.queryAllPassengerFlowBaseDataOfMap(mapId); diff --git a/src/main/java/club/joylink/rtss/services/permission/OldPermissionDataSyncService.java b/src/main/java/club/joylink/rtss/services/permission/OldPermissionDataSyncService.java index 491aa6576..bf0ce9b03 100644 --- a/src/main/java/club/joylink/rtss/services/permission/OldPermissionDataSyncService.java +++ b/src/main/java/club/joylink/rtss/services/permission/OldPermissionDataSyncService.java @@ -38,6 +38,7 @@ import java.io.FileWriter; import java.io.IOException; import java.time.LocalDateTime; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.stream.Collectors; @@ -68,14 +69,16 @@ public class OldPermissionDataSyncService { @Autowired private MapInfoDAO mapInfoDAO; - static final Map MAPID_TYPE_MAP = Maps.newHashMap(); + private final static AtomicBoolean ISRUN = new AtomicBoolean(false); + static final Map MAPID_TYPE_MAP = Maps.newHashMap(); static final List SYNC_RESULT_LIST = Lists.newArrayList(); static{ - MAPID_TYPE_MAP.put(154L,Simulation.Type.RAILWAY); //大铁标准站 -// MAPID_TYPE_MAP.put(81L,Simulation.Type.RAILWAY); //北交大客流 - MAPID_TYPE_MAP.put(161L,Simulation.Type.EMERGENCY); //武汉8号线应急调度 + MAPID_TYPE_MAP.put(154L,GgenerateType.RAILWAY); //大铁标准站 + MAPID_TYPE_MAP.put(81L,GgenerateType.DAKE_STREAM); //北交大客流 + MAPID_TYPE_MAP.put(161L,GgenerateType.EMERGENCY); //武汉8号线应急调度 } + private void truncateAllTable(){ syncDAO.clearRtsPermission(); syncDAO.clearRtsDistribute(); @@ -83,42 +86,68 @@ public class OldPermissionDataSyncService { syncDAO.clearRtsPermissionSystemAbility(); syncDAO.clearRtsPermissionSubject(); } + + public boolean initData(){ + if(ISRUN.compareAndSet(false,true)){ + SYNC_RESULT_LIST.clear(); + try{ + MapInfoExample example = new MapInfoExample(); + example.createCriteria().andStatusEqualTo("1" ); + List mapInfoList = this.mapInfoDAO.selectByExample(example); + for (MapInfo mapInfo : mapInfoList) { + this.generate(mapInfo); + } + this.abilityService.autoSynchMapSystemData(0L); + }catch (Exception e){ + log.error("用户权限初始化失败 msg:" + e.getMessage(),e); + ISRUN.compareAndSet(true,false); + } + + return true; + }else{ + return false; + } + } + @Transactional(rollbackFor = Exception.class) public File sync(){ - SYNC_RESULT_LIST.clear(); - truncateAllTable(); - List userPermissionList = syncDAO.findCanUsedData(); - List syncVOList = findAllPermission(userPermissionList); - List distributeDataVOList = distributeData(syncVOList); - List psList = this.userPermission(userPermissionList); - insertPermission(syncVOList); - insertDistribute(distributeDataVOList); - insertUserPermission(psList); - return outErrorMsg(); + + try{ + truncateAllTable(); + List userPermissionList = syncDAO.findCanUsedData(); + Map> funMapIdMap = this.findSystemAbilityGroupMapId(); + List syncVOList = findAllPermission(userPermissionList,funMapIdMap); + List distributeDataVOList = distributeData(syncVOList); + List psList = this.userPermission(userPermissionList,funMapIdMap); + insertPermission(syncVOList); + insertDistribute(distributeDataVOList); + insertUserPermission(psList); + File file = outErrorMsg(); + return file; + }catch (Exception e){ + log.error("用户权限同步失败 msg:" + e.getMessage(),e); + throw e; + }finally { + ISRUN.compareAndSet(true,false); + } + } private File outErrorMsg(){ - List createMapErrorList = SYNC_RESULT_LIST.stream().filter(d->d.errorType == ErrorType.CREATE_MAP).map(d->d.getPermissionVO().getMapId()).distinct().collect(Collectors.toList()); - MapInfoExample example = new MapInfoExample(); - example.createCriteria().andIdIn(createMapErrorList); - List mapInfoList = this.mapInfoDAO.selectByExample(example); - - List xiajiaList = mapInfoList.stream().filter(d->!Objects.equals(d.getStatus(),"1")).map(d->String.format("%s [%s] [%s]",d.getId(),d.getName(),Objects.equals(d.getStatus(),"1")?"正常":"下架")).collect(Collectors.toList()); - List zhengchangList = mapInfoList.stream().filter(d->Objects.equals(d.getStatus(),"1")).map(d->String.format("%s [%s] [%s]",d.getId(),d.getName(),Objects.equals(d.getStatus(),"1")?"正常":"下架")).collect(Collectors.toList()); BufferedWriter bw = null; try{ File file = File.createTempFile("error",".txt"); bw = new BufferedWriter(new FileWriter(file)); bw.write("------------------------------\n"); - bw.write("创建子系统错误【下架】的数据\n"); - bw.write(Joiner.on(",").join(xiajiaList)); - bw.write("\n\n"); - bw.write("------------------------------\n"); - bw.write("创建子系统正常【创建失败】的数据\n"); - bw.write(Joiner.on(",").join(zhengchangList)); - bw.write("\n\n\n\n"); +// bw.write("创建子系统错误【下架】的数据\n"); +// bw.write(Joiner.on(",").join(xiajiaList)); +// bw.write("\n\n"); +// bw.write("------------------------------\n"); +// bw.write("创建子系统正常【创建失败】的数据\n"); +// bw.write(Joiner.on(",").join(zhengchangList)); +// bw.write("\n\n\n\n"); bw.write("------------------------------\n"); for (ErrorMsgVO errorMsgVO : SYNC_RESULT_LIST) { bw.write(errorMsgVO.toString()); @@ -144,11 +173,10 @@ public class OldPermissionDataSyncService { for (SyncVO syncVO : syncVOList) { syncVO.permission.setIsSync(true); this.acPermissionDAO.customerSyncInsert(syncVO.permission); - for (PermissionSystemAbility sa : syncVO.getSystemAbility()) { - sa.setIsSync(true); - permissionSystemAbilityDAO.insert(sa); - } + syncVO.getSystemAbility().forEach(d->d.setIsSync(true)); } + List tmpList = syncVOList.stream().map(d->d.getSystemAbility()).flatMap(d->d.stream()).collect(Collectors.toList()); + this.permissionSystemAbilityDAO.batchInsert(tmpList); } private void insertDistribute(List distributeDataVOList){ @@ -164,15 +192,19 @@ public class OldPermissionDataSyncService { } } private void insertUserPermission(List psList){ + if(CollectionUtils.isEmpty(psList)){ + return; + } for (PermissionSubject ps : psList) { ps.setIsSync(true); - this.subjectDAO.insert(ps); +// this.subjectDAO.insert(ps); } + + this.subjectDAO.batchInsert(psList); } - private List userPermission(List voList){ + private List userPermission(List voList,Map> funMapIdMap){ List psList = Lists.newArrayList(); - Map> funMapIdMap = this.findSystemAbilityGroupMapId(); for (UserPermissionVO userPermissionVO : voList) { List mapList = funMapIdMap.get(userPermissionVO.getMapId()); if(CollectionUtils.isEmpty(mapList)){ @@ -197,18 +229,15 @@ public class OldPermissionDataSyncService { return datavoList; } - private List findAllPermission(List userPermissionList){ + private List findAllPermission(List userPermissionList,Map> funMapIdMap){ List handleDataList = this.easyBeforehand(userPermissionList); - this.abilityService.autoSynchMapSystemData(0L); - - Map> funMapIdMap = this.findSystemAbilityGroupMapId(); List syncVOList = Lists.newArrayList(); Set existPermissionIdSet = Sets.newHashSet(); for (UserPermissionVO permissionVO : handleDataList) { List mapList = funMapIdMap.get(permissionVO.getMapId()); if(CollectionUtils.isEmpty(mapList)){ log.error("{} 为找到对应的功能",permissionVO.getMapId()); - SYNC_RESULT_LIST.add(new ErrorMsgVO(ErrorType.PERSSION_NOTFIND_MAP,permissionVO,"")); + SYNC_RESULT_LIST.add(new ErrorMsgVO(ErrorType.PERSSION_NOTFIND_MAP,permissionVO,String.format("mapid[%s] 未在 rts_permission_system_ability 表中找到对应的数据",permissionVO.getMapId()))); continue; } if(!existPermissionIdSet.add(permissionVO.getPermissionId())){ @@ -251,29 +280,50 @@ public class OldPermissionDataSyncService { return null; } - private List easyBeforehand(List userPermissionList){ - Set existMapIdSet = Sets.newHashSet(); - userPermissionList.forEach(d-> { - if(Objects.isNull(d.getMapId())){ - d.setErrorData(true); - }else{ - if(existMapIdSet.add(d.getMapId())){ - Simulation.Type type = Objects.isNull(MAPID_TYPE_MAP.get(d.getMapId())) ? Simulation.Type.METRO : MAPID_TYPE_MAP.get(d.getMapId()); - RtsMapFunctionGenerateParamVO paramVO = new RtsMapFunctionGenerateParamVO(); - paramVO.setSimTypes(Arrays.asList(type)); - try{ - this.rtsMapFunctionService.generate(d.getMapId(),paramVO,0L); - }catch(Exception e){ + private void generate(MapInfo mi){ + Simulation.Type type = Simulation.Type.METRO; + GgenerateType gt = MAPID_TYPE_MAP.get(mi.getId()); + if(gt == GgenerateType.RAILWAY){ + type = Simulation.Type.RAILWAY; + this.generate(type,mi); + }else if(gt == GgenerateType.EMERGENCY){ + type = Simulation.Type.EMERGENCY; + this.generate(type,mi); + }else if(gt == GgenerateType.DAKE_STREAM){ + this.generateLpf(mi); + return; + }else{ + this.generate(type,mi); + } + } - d.setErrorData(true); - SYNC_RESULT_LIST.add(new ErrorMsgVO(ErrorType.CREATE_MAP,d,e.getMessage())); - } - } - if(Objects.isNull(d.getIsPackage())) d.setIsPackage(false); + private void generateLpf(MapInfo mi){ + try{ + this.rtsMapFunctionService.generateLpf(mi.getId(),0L); + }catch(Exception e){ + SYNC_RESULT_LIST.add(new ErrorMsgVO(ErrorType.CREATE_MAP,mi,e.getMessage())); + } + } + private void generate(Simulation.Type type,MapInfo mi){ + RtsMapFunctionGenerateParamVO paramVO = new RtsMapFunctionGenerateParamVO(); + paramVO.setSimTypes(Arrays.asList(type)); + try{ + this.rtsMapFunctionService.generate(mi.getId(),paramVO,0L); + }catch(Exception e){ + + SYNC_RESULT_LIST.add(new ErrorMsgVO(ErrorType.CREATE_MAP,mi,e.getMessage())); + } + } + private List easyBeforehand(List userPermissionList){ +// Set existMapIdSet = Sets.newHashSet(); + for (UserPermissionVO d : userPermissionList) { + if(Objects.isNull(d.getIsPackage())) d.setIsPackage(false); + if(Objects.isNull(d.getMapId())){ +// d.setErrorData(true); + continue; } - }); - userPermissionList.stream().filter(UserPermissionVO::isErrorData).collect(Collectors.toList()); - Map> userPermissionMapList = userPermissionList.stream().filter(d->!d.getIsPackage()).collect(Collectors.groupingBy(UserPermissionVO::getIsPackage)); + } + Map> userPermissionMapList = userPermissionList.stream().collect(Collectors.groupingBy(UserPermissionVO::getIsPackage)); List voList = userPermissionMapList.get(false); List packAgeVOList = userPermissionMapList.get(true); if(!CollectionUtils.isEmpty(packAgeVOList)){ @@ -390,7 +440,15 @@ public class OldPermissionDataSyncService { } } - + public enum GgenerateType { + /** 地铁 */ + METRO, + /** 铁路/大铁 */ + RAILWAY, + /** 应急调度(Emergency dispatching command system) */ + EMERGENCY, + DAKE_STREAM, + } public enum ErrorType{ CREATE_MAP, PERSSION_NOTFIND_MAP; @@ -400,7 +458,7 @@ public class OldPermissionDataSyncService { @AllArgsConstructor public static class ErrorMsgVO{ ErrorType errorType; - UserPermissionVO permissionVO; + Object permissionVO; String msg; @Override public String toString(){ diff --git a/src/main/java/club/joylink/rtss/vo/permission/sync/UserPermissionVO.java b/src/main/java/club/joylink/rtss/vo/permission/sync/UserPermissionVO.java index f3ce6a251..9b126fee9 100644 --- a/src/main/java/club/joylink/rtss/vo/permission/sync/UserPermissionVO.java +++ b/src/main/java/club/joylink/rtss/vo/permission/sync/UserPermissionVO.java @@ -34,6 +34,6 @@ public class UserPermissionVO { private Integer amount; private Integer remains; private String mapName; - private boolean errorData; +// private boolean errorData; private List permissionVO; } diff --git a/src/main/resources/mybatis/mapper/permission/SyncDAO.xml b/src/main/resources/mybatis/mapper/permission/SyncDAO.xml index 345142c89..d2f06500b 100644 --- a/src/main/resources/mybatis/mapper/permission/SyncDAO.xml +++ b/src/main/resources/mybatis/mapper/permission/SyncDAO.xml @@ -7,6 +7,6 @@ ,A.forever,A.amount,A.remains,C.name as map_name from user_permission A left join permission B on A.permission_id = B.id left join map_info C on B.map_id = C.id - where 1 = 1 and C.id is not null + where 1 = 1 and C.id is not null and C.status = '1' diff --git a/src/test/java/club/joylink/rtss/services/permission/SyncServiceTest.java b/src/test/java/club/joylink/rtss/services/permission/SyncServiceTest.java index 68f7a3f7c..c82c49d67 100644 --- a/src/test/java/club/joylink/rtss/services/permission/SyncServiceTest.java +++ b/src/test/java/club/joylink/rtss/services/permission/SyncServiceTest.java @@ -14,6 +14,7 @@ public class SyncServiceTest { private OldPermissionDataSyncService syncService; @Test public void sync(){ + this.syncService.initData(); File file = this.syncService.sync(); if(Objects.isNull(file)){