add:添加全量部门同步

This commit is contained in:
wxyclub
2023-07-24 17:18:22 +08:00
parent 41023597d4
commit d60bc2c9da
4 changed files with 145 additions and 8 deletions
@@ -50,7 +50,7 @@ public class DataSyncController {
@ApiOperation("同步组织机构")
@PostMapping("/importOrgData")
public String importOrgData() throws Exception {
return iDataSyncService.orgDataSync();
return iDataSyncService.orgDataSync2();
}
@ApiOperation("同步组织机构,添加参数")
@@ -8,8 +8,10 @@ public interface IDataSyncService {
public String dataSync();
public String dataSync2(Long chuo);
// 定时增量同步
public String orgDataSync();
// 手动全量同步
public String orgDataSync2();
public String updateByOrgs();
}
@@ -469,7 +469,7 @@ public class DataSyncServiceImpl implements IDataSyncService {
}
// 增量同步部门数据
@Transactional(rollbackFor = {RuntimeException.class,Exception.class})
public String orgDataSync(){
try{
@@ -532,7 +532,7 @@ public class DataSyncServiceImpl implements IDataSyncService {
tsInstitutionService.updateBatchById(updateTsInstitutionList);
}
//需要删除的数据
// 先行注释,会删除所有部门
// 增量同步,不作删除
// if(!delTsInstitutionList.isEmpty()){
// logger.debug("本次删除的组织机构为:"+JSONObject.toJSONString(delTsInstitutionList));
// List<String> delIdList = new ArrayList<>();
@@ -562,6 +562,99 @@ public class DataSyncServiceImpl implements IDataSyncService {
return "1";
}
// 手动全量同步部门信息
@Transactional(rollbackFor = {RuntimeException.class,Exception.class})
public String orgDataSync2(){
try{
SyncUserService syncUserService = new SyncUserService();
List<String> res = syncUserService.syncFotonOrg2();
if(res!=null && !res.isEmpty()){
res.forEach(each -> logger.info("同步部门数据:"+each));
//获取到所有用户数据
List<String> 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<TsInstitution> allTsInstitutionList = tsInstitutionService.list();
List<TsInstitution> addTsInstitutionList = new ArrayList<>();
List<TsInstitution> updateTsInstitutionList = new ArrayList<>();
List<TsInstitution> delTsInstitutionList = new ArrayList<>();
Set<String> 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());
}
});
}
}
//遍历出来需要删除的数据
for(TsInstitution data :allTsInstitutionList){
if(!syncAllIdList.contains(data.getId())){
delTsInstitutionList.add(data);
}
}
//需要新增的数据
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<String> delIdList = new ArrayList<>();
for(TsInstitution data : delTsInstitutionList){
delIdList.add(data.getId());
}
tsInstitutionService.removeByIds(delIdList);
}
//全部部门
List<TsInstitution> allTsInstitutionListForPids = new ArrayList<>(addTsInstitutionList);
allTsInstitutionListForPids.addAll(updateTsInstitutionList);
for (TsInstitution tsInstitution : allTsInstitutionListForPids){
List<String> parentIds = new ArrayList<>();
getParentTaxCompanyIds(allTsInstitutionListForPids, tsInstitution.getId(), parentIds);
tsInstitution.setParentIds(String.join(",",parentIds));
}
if (! allTsInstitutionListForPids.isEmpty()) {
tsInstitutionService.updateBatchById(allTsInstitutionListForPids);
}
}else{
logger.info("本次获取组织机构的数据为空,原因有2种:1、没有同步数据 2、请求接口报错");
}
}catch(Exception e){
logger.error(e.getMessage(),e);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
return "1";
}
public String updateByOrgs(){
QueryWrapper<TsInstitution> queryWrapper = new QueryWrapper<>();
queryWrapper.ne("id","");
@@ -128,10 +128,9 @@ public class SyncUserService {
Map<String, Object> params = new HashMap<String, Object>();
params.put("cookie", Base64.encodeBase64String(cookie));
// params.put("timestamp", System.currentTimeMillis());
// Calendar calendar = Calendar.getInstance();
// String timestamp = sdf.format(calendar.getTime());
// params.put("timestamp", timestamp);
params.put("timestamp","");
Calendar calendar = Calendar.getInstance();
String timestamp = sdf.format(calendar.getTime());
params.put("timestamp", timestamp);
// 组织参数
params.put("filter", "(orgNumber=*)");
params.put("basedn", "ou=Organizations,o=foton.com.cn,o=isp");
@@ -162,4 +161,47 @@ public class SyncUserService {
} while (cookie != null);
return json;
}
// 不添加时间戳,全量同步部门信息
public List<String> syncFotonOrg2()throws Exception{
String appuser = "app_slrs";
String appkey = "Fxi5LHbI5yGbQQDpVp86GcCdXeC5Bjfe";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
AuthUtils util = new AuthUtils(appuser, appkey, null);
byte[] cookie = null;
List<String> json=new ArrayList<>();
do {
Map<String, Object> params = new HashMap<String, Object>();
params.put("cookie", Base64.encodeBase64String(cookie));
params.put("timestamp","");
// 组织参数
params.put("filter", "(orgNumber=*)");
params.put("basedn", "ou=Organizations,o=foton.com.cn,o=isp");
// 组织测试
String rs = util.getResponseFromServer("http://idmsync.foton.com.cn/rest/orgs/getOrgList", params);
log.info("远程调用同步的部门数据:"+rs);
JSONObject responseStr = JSONObject.parseObject(rs);
if(responseStr.containsKey("status") && responseStr.getBoolean("status")){
json.add(rs);
}
if (responseStr.containsKey("cookie")) {
try {
JSONArray entries = responseStr.getJSONArray("cookie");
if(entries==null){
break;
}
cookie = new byte[entries.size()];
for (int i = 0; i < entries.size(); i++) {
cookie[i] = (byte) entries.getByte(i);
}
} catch (Exception e) {
log.error(e.getMessage(),e);
if (e.getMessage().contains("is not a JSONArray")) {
cookie = null;
}
}
}
} while (cookie != null);
return json;
}
}