Files
java-servers/servers/org-user-sync/src/main/java/com/xboe/StartRunner.java
joshen@zcwytd.com f9685a9094 'pageadd'
2023-06-07 23:10:14 +08:00

165 lines
5.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.xboe;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.xboe.basic.entity.OldOrganization;
import com.xboe.basic.service.IOldService;
import com.xboe.primary.entity.MainOrganization;
import com.xboe.primary.service.IMainDbSyncService;
import lombok.extern.slf4j.Slf4j;
/**
* 启动执行一次
* @author seastar
*
*/
@Slf4j
@Component
public class StartRunner implements ApplicationRunner {
@Autowired
IOldService oldService;
@Autowired
IMainDbSyncService mainService;
@Override
public void run(ApplicationArguments args) throws Exception {
//用于存放 kid=newId
Map<String,String> mainOrgMap=new HashMap<String,String>();
try {
//同步机构
List<OldOrganization> allList =oldService.listAll();
Set<String> oldIds=new HashSet<String>();
for(OldOrganization org :allList){
oldIds.add(org.getKid());
MainOrganization mainOrg = mainService.findByKid(org.getKid());
if(mainOrg==null) {
//添加
mainOrg=organizationToEntity(org);
mainService.save(mainOrg);
}else {
//更新
copyOrganizationToEntity(mainOrg,org);
mainService.update(mainOrg);
}
mainOrgMap.put(org.getKid(),mainOrg.getId());//
}
//本地的检查 ,如果老库中没有,新库中有,也要设置为删除
List<MainOrganization> localList=mainService.getAllSysIdAndId();
for(MainOrganization mainOrg : localList) {
if(!oldIds.contains(mainOrg.getSysId())) {
//把本地的设置为已删除
if(StringUtils.isNotBlank(mainOrg.getId())) {
mainService.deletedOrg(mainOrg.getId());
}else {
log.error("id错误【"+mainOrg.getSysId()+"");
}
}
}
//同步用户信息,2022/11/04同步用户学习时长
//查询出本地用户
// List<MainUser> allUsers=mainService.findAll();
// for(MainUser mainUser : allUsers) {
//
// OldUser oldUser = oldService.getByUserKid(mainUser.getSysId());
// if(oldUser!=null) {
// String newId=mainOrgMap.get(oldUser.getOrgnizationId());
//
// if(StringUtils.isBlank(newId)) {
// log.error("本地未找到【"+oldUser.getKid()+"】对应的机构id,不更新用户");
// }else {
// mainUser.setSysDepartId(oldUser.getOrgnizationId());
// mainUser.setCompanyId(oldUser.getCompanyId());
// mainUser.setDepartId(newId);
//
// mainUser.setLearningDuration(oldUser.getLearningDuration());
// mainUser.setStatus(oldUser.getStatus());
// if(oldUser.getIsDeleted()!=null) {
// mainUser.setDeleted(oldUser.getIsDeleted()==0? false:true);
// }
// if(mainUser.getDeleted()) {
// if(StringUtils.isNotBlank(oldUser.getEmployeeStatus())){
// mainUser.setDeleted(oldUser.getEmployeeStatus().equals("3")? true:false);
// }
// }
// mainService.updateUser(mainUser);
// }
// }else {
// //本地应该删除
// log.error("原系统中无【"+mainUser.getSysId()+"】对应的用户id,本地标识删除此用户");
// mainService.deleteAccount(mainUser.getId());
//
// }
// }
System.exit(0);
} catch (Exception e) {
log.error("执行失败",e);
e.printStackTrace();
}
}
private void copyOrganizationToEntity(MainOrganization ov ,OldOrganization org) {
ov.setCode(org.getOrgnizationCode());
ov.setName(org.getOrgnizationName());
ov.setSysId(org.getKid());
ov.setSysParentId(org.getParentOrgnizationId());
ov.setDescription(org.getDescription());
ov.setCompanyId(org.getCompanyId());
ov.setDomainId(org.getDomainId());
ov.setNamePath(org.getNamePath());
ov.setOrgnizationManagerId(org.getOrgnizationManagerId());
ov.setOrganizationLevel(org.getOrganizationLevel());
if(StringUtils.isNotBlank(org.getIsMakeOrg())){
ov.setIsMakeOrg("1".equals(org.getIsMakeOrg()));//01
}
if(StringUtils.isNotBlank(org.getIsServiceSite())) {
ov.setIsServiceSite("1".equals(org.getIsServiceSite()));//01
}
if(StringUtils.isNotBlank(org.getIsDefaultOrganization())) {
ov.setIsDefaultOrganization("1".equals(org.getIsDefaultOrganization()));//01
}
if(StringUtils.isNotBlank(org.getStatus())) {
ov.setStatus(Integer.parseInt(org.getStatus()));
}else{
ov.setStatus(1);
}
if(org.getIsDeleted()!=null) {
ov.setDeleted(org.getIsDeleted()==0? false:true); //0正常1已删除
}else {
ov.setDeleted(false);
}
}
/**
* 转化对象
* @param org
* @return
*/
private MainOrganization organizationToEntity(OldOrganization org) {
MainOrganization ov = new MainOrganization();
copyOrganizationToEntity(ov,org);
return ov;
}
}