diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/DataSync/service/impl/DataSyncServiceImpl.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/DataSync/service/impl/DataSyncServiceImpl.java index fd69cea1..b1e8cbd9 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/DataSync/service/impl/DataSyncServiceImpl.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/DataSync/service/impl/DataSyncServiceImpl.java @@ -47,89 +47,93 @@ public class DataSyncServiceImpl implements IDataSyncService { try{ SyncUserService syncUserService = new SyncUserService(); List res = syncUserService.syncFotonUser(); - //存放岗位信息 - Map positionMap = new HashMap<>(); - TsPosition position = new TsPosition(); - position.setCurrent(1); - position.setPageSize(100000); - IPage iPage = tsPositionService.getPosition(position); - List positions=iPage.getRecords(); - if (!positions.isEmpty()) { - positions.forEach(tsPosition -> { - positionMap.put(tsPosition.getName(), tsPosition.getId()); - }); - } - List addUserList = new ArrayList<>(); - List upUserList = new ArrayList<>(); - for (String s : res) { - JSONObject userData = JSONObject.parseObject(s); - if(userData.containsKey("results")){ - JSONArray jsonArray = userData.getJSONArray("results"); - jsonArray.forEach(object -> { - JSONObject jsonObject = JSONObject.parseObject(object.toString()); - String userid = jsonObject.getString("userid"); - TsUser userEO = tsUserService.getById(userid); - String userState = ""; - if(userEO != null){ - userState = "UPDATE"; - }else{ - userState = "ADD"; - userEO = new TsUser(); - } - //先获取岗位以及岗位id - //设置岗位 - if (null != jsonObject.getString("title")) { - //通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增 - if (null == positionMap.get(jsonObject.getString("title"))) { - TsPosition tsPosition = new TsPosition(); - tsPosition.setId(String.valueOf(UUID.randomUUID())); - tsPosition.setName(jsonObject.getString("title")); - tsPositionService.addPosition(tsPosition); - //放入岗位的map集合中 - positionMap.put(tsPosition.getName(), tsPosition.getId()); - //放入类中 - userEO.setPositionName(tsPosition.getName()); - userEO.setPositionId(tsPosition.getId()); - } else { - userEO.setPositionName(jsonObject.getString("title").trim()); - userEO.setPositionId(positionMap.get(jsonObject.getString("title").trim())); - } - }else{ - userEO.setPositionId(null); - userEO.setPositionName(null); - } - //设置其他数据 - Timestamp createTime = new Timestamp(new Date().getTime()); - userEO.setState(jsonObject.getString("userStatus")); - userEO.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):""); - userEO.setCreationTime(createTime); - userEO.setUserId(jsonObject.getString("userid")); - userEO.setAccount(jsonObject.getString("userid")); - userEO.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" ); - userEO.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" ); - //2022-05-23:用户同步增加状态标识,将停用的用户过滤掉 - if("1".equals(jsonObject.getString("userStatus"))){ - userEO.setValidFlag(ValidFlagEnum.VALID_TRUE.getValue()+""); - userEO.setDisableFlag(ValidFlagEnum.VALID_TRUE.getValue()+""); - }else{ - userEO.setValidFlag(ValidFlagEnum.VALID_FALSE.getValue()+""); - userEO.setDisableFlag(ValidFlagEnum.VALID_FALSE.getValue()+""); - } - if("ADD".equals(userState)){ - addUserList.add(userEO); - }else { - upUserList.add(userEO); - } + if(res!=null && !res.isEmpty()){ + //存放岗位信息 + Map positionMap = new HashMap<>(); + TsPosition position = new TsPosition(); + position.setCurrent(1); + position.setPageSize(100000); + IPage iPage = tsPositionService.getPosition(position); + List positions=iPage.getRecords(); + if (!positions.isEmpty()) { + positions.forEach(tsPosition -> { + positionMap.put(tsPosition.getName(), tsPosition.getId()); }); } - } - if(!addUserList.isEmpty()){ - logger.debug("本次新增的用户为:"+JSONObject.toJSONString(addUserList)); - tsUserService.saveBatch(addUserList); - } - if(!upUserList.isEmpty()){ - logger.debug("本次更新的用户为:"+JSONObject.toJSONString(upUserList)); - tsUserService.updateBatchById(upUserList); + List addUserList = new ArrayList<>(); + List upUserList = new ArrayList<>(); + for (String s : res) { + JSONObject userData = JSONObject.parseObject(s); + if(userData.containsKey("results")){ + JSONArray jsonArray = userData.getJSONArray("results"); + jsonArray.forEach(object -> { + JSONObject jsonObject = JSONObject.parseObject(object.toString()); + String userid = jsonObject.getString("userid"); + TsUser userEO = tsUserService.getById(userid); + String userState = ""; + if(userEO != null){ + userState = "UPDATE"; + }else{ + userState = "ADD"; + userEO = new TsUser(); + } + //先获取岗位以及岗位id + //设置岗位 + if (null != jsonObject.getString("title")) { + //通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增 + if (null == positionMap.get(jsonObject.getString("title"))) { + TsPosition tsPosition = new TsPosition(); + tsPosition.setId(String.valueOf(UUID.randomUUID())); + tsPosition.setName(jsonObject.getString("title")); + tsPositionService.addPosition(tsPosition); + //放入岗位的map集合中 + positionMap.put(tsPosition.getName(), tsPosition.getId()); + //放入类中 + userEO.setPositionName(tsPosition.getName()); + userEO.setPositionId(tsPosition.getId()); + } else { + userEO.setPositionName(jsonObject.getString("title").trim()); + userEO.setPositionId(positionMap.get(jsonObject.getString("title").trim())); + } + }else{ + userEO.setPositionId(null); + userEO.setPositionName(null); + } + //设置其他数据 + Timestamp createTime = new Timestamp(new Date().getTime()); + userEO.setState(jsonObject.getString("userStatus")); + userEO.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):""); + userEO.setCreationTime(createTime); + userEO.setUserId(jsonObject.getString("userid")); + userEO.setAccount(jsonObject.getString("userid")); + userEO.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" ); + userEO.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" ); + //2022-05-23:用户同步增加状态标识,将停用的用户过滤掉 + if("1".equals(jsonObject.getString("userStatus"))){ + userEO.setValidFlag(ValidFlagEnum.VALID_TRUE.getValue()+""); + userEO.setDisableFlag(ValidFlagEnum.VALID_TRUE.getValue()+""); + }else{ + userEO.setValidFlag(ValidFlagEnum.VALID_FALSE.getValue()+""); + userEO.setDisableFlag(ValidFlagEnum.VALID_FALSE.getValue()+""); + } + if("ADD".equals(userState)){ + addUserList.add(userEO); + }else { + upUserList.add(userEO); + } + }); + } + } + if(!addUserList.isEmpty()){ + logger.debug("本次新增的用户为:"+JSONObject.toJSONString(addUserList)); + tsUserService.saveBatch(addUserList); + } + if(!upUserList.isEmpty()){ + logger.debug("本次更新的用户为:"+JSONObject.toJSONString(upUserList)); + tsUserService.updateBatchById(upUserList); + } + }else{ + logger.info("本次获取用户的数据为空,原因有2种:1、没有同步数据 2、请求接口报错"); } }catch(Exception e){ logger.error(e.getMessage(),e); @@ -145,68 +149,72 @@ public class DataSyncServiceImpl implements IDataSyncService { try{ SyncUserService syncUserService = new SyncUserService(); List res = syncUserService.syncFotonOrg(); - //获取到所有用户数据 - List strings=new ArrayList<>(); - //tsInstitutionService.clearData(); - for (String s : res) { - JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(s.toString()).getString("results")); - jsonArray.forEach(object -> { - JSONObject jsonObject = JSONObject.parseObject(object.toString()); - strings.add(jsonObject.getString("parentOrgNumber")); - }); - } - List allTsInstitutionList = tsInstitutionService.list(); - List addTsInstitutionList = new ArrayList<>(); - List updateTsInstitutionList = new ArrayList<>(); - List delTsInstitutionList = new ArrayList<>(); - Set syncAllIdList = new HashSet<>(); - for (String s : res) { - JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(s.toString()).getString("results")); - if(jsonArray!=null){ + if(res!=null && !res.isEmpty()){ + //获取到所有用户数据 + List strings=new ArrayList<>(); + //tsInstitutionService.clearData(); + for (String s : res) { + JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(s.toString()).getString("results")); jsonArray.forEach(object -> { JSONObject jsonObject = JSONObject.parseObject(object.toString()); - TsInstitution tsInstitution=new TsInstitution(); - tsInstitution.setId(jsonObject.getString("orgNumber")); - tsInstitution.setName(jsonObject.getString("orgName")); - tsInstitution.setHasChild(strings.contains(jsonObject.getString("orgNumber"))?"1":"0"); - tsInstitution.setParentId(jsonObject.getString("parentOrgNumber")); - //部门为"null"的数据不保存 - if (!"null".equals(jsonObject.getString("orgName"))){ - TsInstitution institution = tsInstitutionService.getById(tsInstitution.getId()); - if(institution!=null){ - updateTsInstitutionList.add(tsInstitution); - }else{ - addTsInstitutionList.add(tsInstitution); - } - syncAllIdList.add(tsInstitution.getId()); - } + strings.add(jsonObject.getString("parentOrgNumber")); }); } - } - //遍历出来需要删除的数据 - for(TsInstitution data :allTsInstitutionList){ - if(!syncAllIdList.contains(data.getId())){ - delTsInstitutionList.add(data); + List allTsInstitutionList = tsInstitutionService.list(); + List addTsInstitutionList = new ArrayList<>(); + List updateTsInstitutionList = new ArrayList<>(); + List delTsInstitutionList = new ArrayList<>(); + Set syncAllIdList = new HashSet<>(); + for (String s : res) { + JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(s.toString()).getString("results")); + if(jsonArray!=null){ + jsonArray.forEach(object -> { + JSONObject jsonObject = JSONObject.parseObject(object.toString()); + TsInstitution tsInstitution=new TsInstitution(); + tsInstitution.setId(jsonObject.getString("orgNumber")); + tsInstitution.setName(jsonObject.getString("orgName")); + tsInstitution.setHasChild(strings.contains(jsonObject.getString("orgNumber"))?"1":"0"); + tsInstitution.setParentId(jsonObject.getString("parentOrgNumber")); + //部门为"null"的数据不保存 + if (!"null".equals(jsonObject.getString("orgName"))){ + TsInstitution institution = tsInstitutionService.getById(tsInstitution.getId()); + if(institution!=null){ + updateTsInstitutionList.add(tsInstitution); + }else{ + addTsInstitutionList.add(tsInstitution); + } + syncAllIdList.add(tsInstitution.getId()); + } + }); + } } - } - //需要新增的数据 - if(!addTsInstitutionList.isEmpty()){ - logger.debug("本次新增的组织机构为:"+JSONObject.toJSONString(addTsInstitutionList)); - tsInstitutionService.saveBatch(addTsInstitutionList); - } - //需要更新的数据 - if(!updateTsInstitutionList.isEmpty()){ - logger.debug("本次更新的组织机构为:"+JSONObject.toJSONString(updateTsInstitutionList)); - tsInstitutionService.updateBatchById(updateTsInstitutionList); - } - //需要删除的数据 - if(!delTsInstitutionList.isEmpty()){ - logger.debug("本次删除的组织机构为:"+JSONObject.toJSONString(delTsInstitutionList)); - List delIdList = new ArrayList<>(); - for(TsInstitution data : delTsInstitutionList){ - delIdList.add(data.getId()); + //遍历出来需要删除的数据 + for(TsInstitution data :allTsInstitutionList){ + if(!syncAllIdList.contains(data.getId())){ + delTsInstitutionList.add(data); + } } - tsInstitutionService.removeByIds(delIdList); + //需要新增的数据 + if(!addTsInstitutionList.isEmpty()){ + logger.debug("本次新增的组织机构为:"+JSONObject.toJSONString(addTsInstitutionList)); + tsInstitutionService.saveBatch(addTsInstitutionList); + } + //需要更新的数据 + if(!updateTsInstitutionList.isEmpty()){ + logger.debug("本次更新的组织机构为:"+JSONObject.toJSONString(updateTsInstitutionList)); + tsInstitutionService.updateBatchById(updateTsInstitutionList); + } + //需要删除的数据 + if(!delTsInstitutionList.isEmpty()){ + logger.debug("本次删除的组织机构为:"+JSONObject.toJSONString(delTsInstitutionList)); + List delIdList = new ArrayList<>(); + for(TsInstitution data : delTsInstitutionList){ + delIdList.add(data.getId()); + } + tsInstitutionService.removeByIds(delIdList); + } + }else{ + logger.info("本次获取组织机构的数据为空,原因有2种:1、没有同步数据 2、请求接口报错"); } }catch(Exception e){ logger.error(e.getMessage(),e);