fix: 重构底层基础数据同步的代码逻辑,去掉清空表数据的操作,更换为更新或添加操作

This commit is contained in:
zyd
2022-06-08 10:25:55 +08:00
parent b427520d47
commit fb72b31b43
2 changed files with 80 additions and 101 deletions
@@ -9,11 +9,14 @@ import com.adc.da.slrs.sarPosition.service.ITsPositionService;
import com.adc.da.slrs.sarUser.entity.TsUser; import com.adc.da.slrs.sarUser.entity.TsUser;
import com.adc.da.slrs.sarUser.service.ITsUserService; import com.adc.da.slrs.sarUser.service.ITsUserService;
import com.adc.da.sync.service.SyncUserService; import com.adc.da.sync.service.SyncUserService;
import com.adc.da.sys.constant.ValidFlagEnum;
import com.adc.da.sys.entity.UserOrgEO; import com.adc.da.sys.entity.UserOrgEO;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.log4j.Log4j; import lombok.extern.log4j.Log4j;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -40,14 +43,12 @@ public class DataSyncServiceImpl implements IDataSyncService {
@Transactional(rollbackFor = {RuntimeException.class,Exception.class}) @Transactional(rollbackFor = {RuntimeException.class,Exception.class})
@Override
public String dataSync(){ public String dataSync(){
try{ try{
SyncUserService syncUserService = new SyncUserService(); SyncUserService syncUserService = new SyncUserService();
List<String> res = syncUserService.syncFotonUser(); List<String> res = syncUserService.syncFotonUser();
//存放岗位信息 //存放岗位信息
Map<String, String> positionMap = new HashMap<>(); Map<String, String> positionMap = new HashMap<>();
Map<String, String> useDistinct = new HashMap<>();
TsPosition position = new TsPosition(); TsPosition position = new TsPosition();
position.setCurrent(1); position.setCurrent(1);
position.setPageSize(100000); position.setPageSize(100000);
@@ -58,90 +59,76 @@ public class DataSyncServiceImpl implements IDataSyncService {
positionMap.put(tsPosition.getName(), tsPosition.getId()); positionMap.put(tsPosition.getName(), tsPosition.getId());
}); });
} }
List<TsUser> addUserList = new ArrayList<>();
/** List<TsUser> upUserList = new ArrayList<>();
* 得到数据时,删除系统表中所有用户信息
*/
if (res.size()>0){
tsUserService.deleteUser();
}
for (String s : res) { for (String s : res) {
//获取到所有用户数据 JSONObject userData = JSONObject.parseObject(s);
List<TsUser> tsUsers = new ArrayList<>(); if(userData.containsKey("results")){
JSONArray jsonArray = userData.getJSONArray("results");
JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(s.toString()).getString("results")); jsonArray.forEach(object -> {
jsonArray.forEach(object -> { JSONObject jsonObject = JSONObject.parseObject(object.toString());
JSONObject jsonObject = JSONObject.parseObject(object.toString()); String userid = jsonObject.getString("userid");
//先获取岗位以及岗位id TsUser userEO = tsUserService.getById(userid);
TsUser tsUser = new TsUser(); String userState = "";
UserOrgEO userOrgEO = new UserOrgEO(); if(userEO != null){
//设置岗位 userState = "UPDATE";
if (null != jsonObject.getString("title")) { }else{
//通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增 userState = "ADD";
if (null == positionMap.get(jsonObject.getString("title"))) { userEO = new TsUser();
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());
//放入类中
tsUser.setPositionName(tsPosition.getName());
tsUser.setPositionId(tsPosition.getId());
} else {
tsUser.setPositionName(jsonObject.getString("title").trim());
tsUser.setPositionId(positionMap.get(jsonObject.getString("title").trim()));
} }
} //先获取岗位以及岗位id
//设置其他数据 //设置岗位
Timestamp createTime = new Timestamp(new Date().getTime()); if (null != jsonObject.getString("title")) {
tsUser.setState(jsonObject.getString("userStatus")); //通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增
tsUser.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):""); if (null == positionMap.get(jsonObject.getString("title"))) {
tsUser.setValidFlag("0"); TsPosition tsPosition = new TsPosition();
tsUser.setDisableFlag("0"); tsPosition.setId(String.valueOf(UUID.randomUUID()));
tsUser.setCreationTime(createTime); tsPosition.setName(jsonObject.getString("title"));
tsUser.setUserId(jsonObject.getString("userid")); tsPositionService.addPosition(tsPosition);
tsUser.setAccount(jsonObject.getString("userid")); //放入岗位的map集合中
tsUser.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" ); positionMap.put(tsPosition.getName(), tsPosition.getId());
tsUser.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" ); //放入类中
//2022-05-23:用户同步增加状态标识,将停用的用户过滤掉 userEO.setPositionName(tsPosition.getName());
if (null == useDistinct.get(jsonObject.getString("userid")) && "1".equals(jsonObject.getString("userStatus"))) { userEO.setPositionId(tsPosition.getId());
tsUsers.add(tsUser); } else {
userEO.setPositionName(jsonObject.getString("title").trim());
} userEO.setPositionId(positionMap.get(jsonObject.getString("title").trim()));
}
// //测试数据删除 }else{
// if (! "anbin".equals(jsonObject.getString("userid"))){ userEO.setPositionId(null);
// userEO.setPositionName(null);
// //测试数据修改 }
// if ("anbing".equals(jsonObject.getString("userid"))){ //设置其他数据
// tsUser.setUname("DDDDDD"); Timestamp createTime = new Timestamp(new Date().getTime());
// tsUsers.add(tsUser); userEO.setState(jsonObject.getString("userStatus"));
// } else if (null == useDistinct.get(jsonObject.getString("userid"))) { userEO.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):"");
// tsUsers.add(tsUser); userEO.setCreationTime(createTime);
// userEO.setUserId(jsonObject.getString("userid"));
// } userEO.setAccount(jsonObject.getString("userid"));
// } userEO.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" );
useDistinct.put(tsUser.getUserId(),tsUser.getUserId()); userEO.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" );
}); //2022-05-23:用户同步增加状态标识,将停用的用户过滤掉
tsUserService.saveBatch(tsUsers); if("1".equals(jsonObject.getString("userStatus"))){
userEO.setValidFlag(ValidFlagEnum.VALID_FALSE.getValue()+"");
userEO.setDisableFlag(ValidFlagEnum.VALID_FALSE.getValue()+"");
}else{
userEO.setValidFlag(ValidFlagEnum.VALID_TRUE.getValue()+"");
userEO.setDisableFlag(ValidFlagEnum.VALID_TRUE.getValue()+"");
}
if("ADD".equals(userState)){
addUserList.add(userEO);
}else {
upUserList.add(userEO);
}
});
}
}
if(!addUserList.isEmpty()){
tsUserService.saveBatch(addUserList);
}
if(!upUserList.isEmpty()){
tsUserService.updateBatchById(upUserList);
} }
//测试数据添加
// TsUser addUser = new TsUser();
// addUser.setState("FFFFFFF");
// addUser.setUname("zhaokaiyao");
// addUser.setValidFlag("0");
// addUser.setDisableFlag("0");
// addUser.setCreationTime(new Timestamp(new Date().getTime()));
// addUser.setUserId("FFFFFFF");
// addUser.setAccount("zhaokaiyao");
// addUser.setInstitutionId("10022745");
// addUser.setInstitutionName("福田营销其他");
//
// List<TsUser> addUserList = Arrays.asList(addUser);
// tsUserService.saveBatch(addUserList);
}catch(Exception e){ }catch(Exception e){
logger.error(e.getMessage(),e); logger.error(e.getMessage(),e);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@@ -19,10 +19,6 @@ import java.util.Map;
@Slf4j @Slf4j
public class SyncUserService { public class SyncUserService {
public static void main(String[] args) throws Exception {
SyncUserService syncUserService=new SyncUserService();
syncUserService.syncFotonUser();
}
/** /**
* 同步福田IDM数据 * 同步福田IDM数据
@@ -46,12 +42,11 @@ public class SyncUserService {
System.out.println("RequestBody:" + params.toString()); System.out.println("RequestBody:" + params.toString());
// 用户测试 // 用户测试
String rs = util.getResponseFromServer("http://idmsync.foton.com.cn/rest/users/getUserList", params); String rs = util.getResponseFromServer("http://idmsync.foton.com.cn/rest/users/getUserList", params);
log.debug(rs);
json.add(rs);
JSONObject responseStr = JSON.parseObject(rs); JSONObject responseStr = JSON.parseObject(rs);
System.out.println(rs); if(responseStr.containsKey("status") && responseStr.getBoolean("status")){
log.info(rs); json.add(rs);
}
if (responseStr.containsKey("cookie")) { if (responseStr.containsKey("cookie")) {
try { try {
JSONArray entries = responseStr.getJSONArray("cookie"); JSONArray entries = responseStr.getJSONArray("cookie");
@@ -63,6 +58,7 @@ public class SyncUserService {
cookie[i] = (byte) entries.getByte(i); cookie[i] = (byte) entries.getByte(i);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e);
if (e.getMessage().contains("is not a JSONArray")) { if (e.getMessage().contains("is not a JSONArray")) {
cookie = null; cookie = null;
} }
@@ -92,13 +88,11 @@ public class SyncUserService {
params.put("basedn", "ou=Organizations,o=foton.com.cn,o=isp"); params.put("basedn", "ou=Organizations,o=foton.com.cn,o=isp");
// 组织测试 // 组织测试
String rs = util.getResponseFromServer("http://idmsync.foton.com.cn/rest/orgs/getOrgList", params); String rs = util.getResponseFromServer("http://idmsync.foton.com.cn/rest/orgs/getOrgList", params);
json.add(rs);
JSONObject responseStr = JSONObject.parseObject(rs);
System.out.println(responseStr);
log.info(rs); log.info(rs);
JSONObject responseStr = JSONObject.parseObject(rs);
if(responseStr.containsKey("status") && responseStr.getBoolean("status")){
json.add(rs);
}
if (responseStr.containsKey("cookie")) { if (responseStr.containsKey("cookie")) {
try { try {
JSONArray entries = responseStr.getJSONArray("cookie"); JSONArray entries = responseStr.getJSONArray("cookie");
@@ -110,14 +104,12 @@ public class SyncUserService {
cookie[i] = (byte) entries.getByte(i); cookie[i] = (byte) entries.getByte(i);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); log.error(e.getMessage(),e);
e.printStackTrace();
if (e.getMessage().contains("is not a JSONArray")) { if (e.getMessage().contains("is not a JSONArray")) {
cookie = null; cookie = null;
} }
} }
} }
} while (cookie != null); } while (cookie != null);
return json; return json;
} }