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,23 +59,24 @@ 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");
TsUser userEO = tsUserService.getById(userid);
String userState = "";
if(userEO != null){
userState = "UPDATE";
}else{
userState = "ADD";
userEO = new TsUser();
}
//先获取岗位以及岗位id //先获取岗位以及岗位id
TsUser tsUser = new TsUser();
UserOrgEO userOrgEO = new UserOrgEO();
//设置岗位 //设置岗位
if (null != jsonObject.getString("title")) { if (null != jsonObject.getString("title")) {
//通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增 //通过岗位名称查询系统表中是否有岗位,有则设定用户的岗位为系统中的岗位,否则新增
@@ -86,62 +88,47 @@ public class DataSyncServiceImpl implements IDataSyncService {
//放入岗位的map集合中 //放入岗位的map集合中
positionMap.put(tsPosition.getName(), tsPosition.getId()); positionMap.put(tsPosition.getName(), tsPosition.getId());
//放入类中 //放入类中
tsUser.setPositionName(tsPosition.getName()); userEO.setPositionName(tsPosition.getName());
tsUser.setPositionId(tsPosition.getId()); userEO.setPositionId(tsPosition.getId());
} else { } else {
tsUser.setPositionName(jsonObject.getString("title").trim()); userEO.setPositionName(jsonObject.getString("title").trim());
tsUser.setPositionId(positionMap.get(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()); Timestamp createTime = new Timestamp(new Date().getTime());
tsUser.setState(jsonObject.getString("userStatus")); userEO.setState(jsonObject.getString("userStatus"));
tsUser.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):""); userEO.setUname(null!=jsonObject.getString("name")?jsonObject.getString("name"):"");
tsUser.setValidFlag("0"); userEO.setCreationTime(createTime);
tsUser.setDisableFlag("0"); userEO.setUserId(jsonObject.getString("userid"));
tsUser.setCreationTime(createTime); userEO.setAccount(jsonObject.getString("userid"));
tsUser.setUserId(jsonObject.getString("userid")); userEO.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" );
tsUser.setAccount(jsonObject.getString("userid")); userEO.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" );
tsUser.setInstitutionId(null!=jsonObject.getString("orgNumber")?jsonObject.getString("orgNumber"):"" );
tsUser.setInstitutionName(null!=jsonObject.getString("orgName")?jsonObject.getString("orgName"):"" );
//2022-05-23:用户同步增加状态标识,将停用的用户过滤掉 //2022-05-23:用户同步增加状态标识,将停用的用户过滤掉
if (null == useDistinct.get(jsonObject.getString("userid")) && "1".equals(jsonObject.getString("userStatus"))) { if("1".equals(jsonObject.getString("userStatus"))){
tsUsers.add(tsUser); 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 (! "anbin".equals(jsonObject.getString("userid"))){
//
// //测试数据修改
// if ("anbing".equals(jsonObject.getString("userid"))){
// tsUser.setUname("DDDDDD");
// tsUsers.add(tsUser);
// } else if (null == useDistinct.get(jsonObject.getString("userid"))) {
// tsUsers.add(tsUser);
//
// }
// }
useDistinct.put(tsUser.getUserId(),tsUser.getUserId());
}); });
tsUserService.saveBatch(tsUsers);
} }
}
//测试数据添加 if(!addUserList.isEmpty()){
// TsUser addUser = new TsUser(); tsUserService.saveBatch(addUserList);
// addUser.setState("FFFFFFF"); }
// addUser.setUname("zhaokaiyao"); if(!upUserList.isEmpty()){
// addUser.setValidFlag("0"); tsUserService.updateBatchById(upUserList);
// 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;
} }