同步代码编写完毕
This commit is contained in:
@@ -6,12 +6,16 @@ import cn.hutool.http.HttpRequest;
|
||||
import com.adc.da.sync.service.dao.mysql.SyncIAMLogDao;
|
||||
import com.adc.da.sync.service.entity.SyncIAMLogEO;
|
||||
import com.adc.da.sync.service.enums.SyncTypeEnum;
|
||||
import com.adc.da.sys.constant.SysConstants;
|
||||
import com.adc.da.sys.constant.UserBelongEnum;
|
||||
import com.adc.da.sys.dao.mysql.OrgEODao;
|
||||
import com.adc.da.sys.entity.OrgEO;
|
||||
import com.adc.da.sys.entity.UserEO;
|
||||
import com.adc.da.sys.service.OrgEOService;
|
||||
import com.adc.da.sys.service.iservice.IUserEoService;
|
||||
import com.adc.da.sys.util.EncryptUtil;
|
||||
import com.adc.da.sys.vo.iam.OrgVoIAM;
|
||||
import com.adc.da.sys.vo.iam.OrgsShortVoIAM;
|
||||
import com.adc.da.sys.vo.iam.UserVoIAM;
|
||||
import com.adc.da.util.utils.GsonUtil;
|
||||
import com.auth0.jwt.JWT;
|
||||
@@ -70,13 +74,18 @@ public class SyncIAMTimer {
|
||||
@Resource
|
||||
IUserEoService iUserEoService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SyncIAMTimer syncIAMTimer = new SyncIAMTimer();
|
||||
syncIAMTimer.syncOrgFullData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步组织机构-全量数据并入库
|
||||
* @Param syncModel 同步模式
|
||||
*/
|
||||
public void syncOrgFullData(){
|
||||
String syncUrl = this.iamUrl + ORG_FULL_URL;
|
||||
String jwtToken = this.getJwtToken(appkey, appSecret);
|
||||
String jwtToken = this.getJwtToken(this.appkey, this.appSecret);
|
||||
|
||||
Date syncStartTime = new Date();
|
||||
|
||||
@@ -95,8 +104,8 @@ public class SyncIAMTimer {
|
||||
orgEOList.add(orgEO);
|
||||
}
|
||||
|
||||
// TODO 需要处理 ,比较后再更新
|
||||
this.orgEOService.saveBatch(orgEOList);
|
||||
// 以 部门编码 为主键,所以直接调整即可
|
||||
this.orgEOService.saveOrUpdateBatch(orgEOList);
|
||||
|
||||
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_FULL_DATA.getValue());
|
||||
}
|
||||
@@ -130,8 +139,8 @@ public class SyncIAMTimer {
|
||||
OrgEO orgEO = this.transOrgIAMVoToOrgEO(orgVoIAM);
|
||||
orgEOList.add(orgEO);
|
||||
}
|
||||
// TODO 需要处理 ,比较后再更新
|
||||
this.orgEOService.saveBatch(orgEOList);
|
||||
|
||||
this.orgEOService.saveOrUpdateBatch(orgEOList);
|
||||
|
||||
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_INCREMENT_DATA.getValue());
|
||||
}
|
||||
@@ -157,13 +166,44 @@ public class SyncIAMTimer {
|
||||
// 同步的VO 转换成 数据库EO,并存库
|
||||
for (UserVoIAM userVoIAM : userVoIAMList){
|
||||
UserEO userEO = this.transUserIAMVoToUserEO(userVoIAM);
|
||||
// TODO 需要处理 ,比较后再更新
|
||||
this.iUserEoService.save(userEO);
|
||||
this.iUserEoService.syncSaveOrUpdate(userEO);
|
||||
}
|
||||
|
||||
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_FULL_DATA.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步组织机构-增量数据并入库
|
||||
* @Param syncModel 同步模式
|
||||
*/
|
||||
public void syncUserIncrData(){
|
||||
String syncUrl = this.iamUrl + USER_INCREMENT_URL;
|
||||
String jwtToken = this.getJwtToken(appkey, appSecret);
|
||||
|
||||
Date syncStartTime = new Date();
|
||||
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.set(Calendar.HOUR_OF_DAY, (c.get(Calendar.HOUR_OF_DAY) - 24));//HOUR_OF_DAY 指一天中的小时
|
||||
Long incrementStartTime = c.getTimeInMillis(); // 每天同步前一天的数据,需要当前时间往前诺一天
|
||||
|
||||
String userDataStr = HttpRequest.post(syncUrl)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", jwtToken)//头信息,多个头信息多次调用此方法即可
|
||||
.form("startTime", incrementStartTime)
|
||||
.timeout(10 * 60 * 1000) //超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
List<UserVoIAM> userVoIAMList = GsonUtil.json2Collection(userDataStr, UserVoIAM.class);
|
||||
|
||||
// 同步的VO 转换成 数据库EO,并存库
|
||||
for (UserVoIAM userVoIAM : userVoIAMList){
|
||||
UserEO userEO = this.transUserIAMVoToUserEO(userVoIAM);
|
||||
this.iUserEoService.syncSaveOrUpdate(userEO);
|
||||
}
|
||||
|
||||
this.addSyncLog(null, syncStartTime, SyncTypeEnum.USER_INCREMENT_DATA.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key和secret获取JWT_TOKEN
|
||||
* @param appKey
|
||||
@@ -199,6 +239,7 @@ public class SyncIAMTimer {
|
||||
*/
|
||||
public OrgEO transOrgIAMVoToOrgEO(OrgVoIAM orgVoIAM) {
|
||||
OrgEO orgEO = new OrgEO();
|
||||
orgEO.setId(orgVoIAM.getOrgCode());
|
||||
orgEO.setOrgCode(orgVoIAM.getOrgCode());
|
||||
orgEO.setOrgName(orgVoIAM.getOrgName());
|
||||
orgEO.setOrgLevel(orgVoIAM.getOrgLevel());
|
||||
@@ -218,11 +259,50 @@ public class SyncIAMTimer {
|
||||
* @return
|
||||
*/
|
||||
public UserEO transUserIAMVoToUserEO(UserVoIAM userVoIAM) {
|
||||
UserEO userEO = new UserEO();
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
UserEO userEO = new UserEO();
|
||||
// 转换基础数据
|
||||
userEO.setUsid(userVoIAM.getUserUiD());
|
||||
userEO.setIsSelf(UserBelongEnum.IAM.getValue());
|
||||
userEO.setPassword(EncryptUtil.encrypt(SysConstants.DEFAULT_PASSWORD));
|
||||
userEO.setDelFlag(0);
|
||||
userEO.setAccount(userVoIAM.getWorkNo());
|
||||
userEO.setCellPhoneNumber(userVoIAM.getMobile() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userVoIAM.getMobile()));
|
||||
userEO.setCreateTime(userVoIAM.getUpdateTime());
|
||||
userEO.setUpdateTime(userVoIAM.getUpdateTime());
|
||||
userEO.setEmail(userVoIAM.getEmail() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userVoIAM.getEmail()));
|
||||
userEO.setUsname(userVoIAM.getUserName());
|
||||
userEO.setDutyCode(userVoIAM.getDutyCode());
|
||||
userEO.setDutyName(userVoIAM.getDutyName());
|
||||
userEO.setStatus(userVoIAM.getStatus());
|
||||
userEO.setLevelId(userVoIAM.getLevelId());
|
||||
userEO.setZj(userVoIAM.getZj());
|
||||
|
||||
// 转换org的关联数据
|
||||
List<OrgEO> orgList = new ArrayList<>();
|
||||
if (userVoIAM.getOrgs() != null){
|
||||
for (OrgsShortVoIAM orgsShortVoIAM :userVoIAM.getOrgs()) {
|
||||
OrgEO orgEO = new OrgEO();
|
||||
orgEO.setId(orgsShortVoIAM.getOrgCode());
|
||||
orgEO.setOrgCode(orgsShortVoIAM.getOrgCode());
|
||||
orgEO.setOrgName(orgsShortVoIAM.getOrgName());
|
||||
orgEO.setOrgShortName(orgsShortVoIAM.getOrgShortName());
|
||||
|
||||
orgList.add(orgEO);
|
||||
}
|
||||
}
|
||||
userEO.setOrgList(orgList);
|
||||
|
||||
// 固定角色数据
|
||||
List<String> roleIdList = new ArrayList<>();
|
||||
// TODO 根据level固定为一般管理人员 或 总监
|
||||
if ("0.5".equals(userEO.getLevelId())){
|
||||
roleIdList.add("1");
|
||||
} else {
|
||||
roleIdList.add("2");
|
||||
}
|
||||
userEO.setRoleIdList(roleIdList);
|
||||
|
||||
return userEO;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ package com.adc.da.sys.constant;
|
||||
public enum UserBelongEnum {
|
||||
|
||||
LOCAL("1","本地新增用户"),
|
||||
IAM("0","IAm同步用户");
|
||||
IAM("0","IAM同步用户");
|
||||
|
||||
private String label;
|
||||
private String value;
|
||||
|
||||
@@ -16,5 +16,5 @@ public interface OrgEODao extends BaseMapper<OrgEO> {
|
||||
|
||||
IPage<OrgEO> selectPageByVO(Page<OrgEO> page,@Param("pageVO")OrgEOPage orgEOPage);
|
||||
|
||||
long selectCountByUUID(@Param("pageVO")OrgEO orgEO);
|
||||
long selectCountByOrgCode(@Param("pageVO")OrgEO orgEO);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.adc.da.sys.entity;
|
||||
|
||||
import com.adc.da.base.entity.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -16,17 +18,15 @@ public class OrgEO extends BaseEntity implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2386892031753294731L;
|
||||
|
||||
@TableId(value = "ID",type = IdType.INPUT)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 部门编码
|
||||
*/
|
||||
@TableField("org_code")
|
||||
private String orgCode;
|
||||
|
||||
/**
|
||||
* 唯一值
|
||||
*/
|
||||
@TableField("UUID")
|
||||
private String UUID;
|
||||
|
||||
/**
|
||||
* 部门名称
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.aspectj.weaver.ast.Or;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
@@ -35,6 +36,11 @@ import java.util.List;
|
||||
public class UserEO extends BaseEntity implements Serializable {
|
||||
private static final long serialVersionUID = 3658632939727891047L;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@TableId(value = "USID",type =IdType.INPUT)
|
||||
private String usid;
|
||||
|
||||
/**
|
||||
* 区分本系统自建用户和同步过来的用户
|
||||
@@ -44,13 +50,6 @@ public class UserEO extends BaseEntity implements Serializable {
|
||||
@TableField("ISSELF")
|
||||
private String isSelf;
|
||||
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@TableId(value = "USID",type =IdType.INPUT)
|
||||
private String usid;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@@ -97,9 +96,45 @@ public class UserEO extends BaseEntity implements Serializable {
|
||||
@TableField("USNAME")
|
||||
private String usname;
|
||||
|
||||
/**
|
||||
* 职位编码
|
||||
*/
|
||||
@TableField("DUTY_CODE")
|
||||
private String dutyCode;
|
||||
|
||||
/**
|
||||
* 职位名称
|
||||
*/
|
||||
@TableField("DUTY_NAME")
|
||||
private String dutyName;
|
||||
|
||||
/**
|
||||
* 员工状态:0停用,1启用,2密码错误锁定,3管理员强制锁定
|
||||
*/
|
||||
@TableField("STATUS")
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 级别ID
|
||||
*/
|
||||
@TableField("LEVEL_ID")
|
||||
private String levelId;
|
||||
|
||||
/**
|
||||
* 职级
|
||||
*/
|
||||
@TableField("ZJ")
|
||||
private String zj;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String orgId;
|
||||
|
||||
/**
|
||||
* 部门EO列表
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private List<OrgEO> orgList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 角色ID列表
|
||||
*/
|
||||
@@ -112,15 +147,7 @@ public class UserEO extends BaseEntity implements Serializable {
|
||||
@TableField(exist = false)
|
||||
private List<RoleEO> roleEOList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 部门EO列表
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private List<RoleEO> orgList = new ArrayList<>();
|
||||
|
||||
@TableField(exist = false)
|
||||
private String roleId;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -63,13 +63,12 @@ public class OrgEOService extends ServiceImpl<OrgEODao, OrgEO> implements IOrgEo
|
||||
if (StringUtils.isEmpty(orgEO.getOrgCode())){
|
||||
return Result.error("部门编码不能为空");
|
||||
}
|
||||
long count = baseMapper.selectCountByUUID(orgEO);
|
||||
long count = baseMapper.selectCountByOrgCode(orgEO);
|
||||
if(count>0){
|
||||
return Result.error("部门编码重复");
|
||||
}
|
||||
if(StringUtils.isBlank(orgEO.getUUID())){
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||||
orgEO.setUUID(uuid);
|
||||
if(StringUtils.isBlank(orgEO.getId())){
|
||||
orgEO.setId(orgEO.getOrgCode());
|
||||
baseMapper.insert(orgEO);
|
||||
}else{
|
||||
baseMapper.updateById(orgEO);
|
||||
|
||||
@@ -3,9 +3,11 @@ package com.adc.da.sys.service;
|
||||
import com.adc.da.sys.constant.SysConstants;
|
||||
import com.adc.da.sys.constant.UserBelongEnum;
|
||||
import com.adc.da.sys.dao.mysql.UserEODao;
|
||||
import com.adc.da.sys.dao.mysql.UserOrgEODao;
|
||||
import com.adc.da.sys.dao.mysql.UserRoleEODao;
|
||||
import com.adc.da.sys.entity.RoleEO;
|
||||
import com.adc.da.sys.entity.UserEO;
|
||||
import com.adc.da.sys.entity.UserOrgEO;
|
||||
import com.adc.da.sys.entity.UserRoleEO;
|
||||
import com.adc.da.sys.service.iservice.IUserEoService;
|
||||
import com.adc.da.sys.util.EncryptUtil;
|
||||
@@ -49,6 +51,9 @@ public class UserEOServiceImpl implements IUserEoService {
|
||||
@Resource
|
||||
private UserRoleEODao userRoleEODao;
|
||||
|
||||
@Resource
|
||||
private UserOrgEODao userOrgEODao;
|
||||
|
||||
@Resource
|
||||
private RoleEOService roleEOService;
|
||||
|
||||
@@ -74,6 +79,7 @@ public class UserEOServiceImpl implements IUserEoService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(UserEO eo) {
|
||||
// TODO 组织机构和角色
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
if (StringUtils.isEmpty(eo.getUsid())) {
|
||||
eo.setDelFlag(0);
|
||||
@@ -104,6 +110,8 @@ public class UserEOServiceImpl implements IUserEoService {
|
||||
deleteWapper.eq("user_id",userEO.getUsid());
|
||||
userRoleEODao.delete(deleteWapper);
|
||||
}
|
||||
|
||||
// 加入角色关联表
|
||||
eo.getRoleIdList().forEach(c -> {
|
||||
UserRoleEO userRoleEO = new UserRoleEO();
|
||||
userRoleEO.setUserId(eo.getUsid());
|
||||
@@ -111,8 +119,54 @@ public class UserEOServiceImpl implements IUserEoService {
|
||||
userRoleEODao.insert(userRoleEO);
|
||||
});
|
||||
|
||||
// // 插入组织关联表 暂时用不上,页面不维护组织机构
|
||||
// eo.getOrgList().forEach(c -> {
|
||||
// UserOrgEO userOrgEO = new UserOrgEO();
|
||||
// userOrgEO.setUserId(eo.getUsid());
|
||||
// userOrgEO.setOrgId(c.getOrgCode());
|
||||
// userOrgEODao.insert(userOrgEO);
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步保存或更新
|
||||
* @param eo
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncSaveOrUpdate(UserEO eo) {
|
||||
UserEO userEO = dao.selectById(eo.getUsid());
|
||||
if (userEO == null) {
|
||||
this.save(userEO);
|
||||
} else {
|
||||
//删除旧的角色
|
||||
QueryWrapper<UserRoleEO> deleteRoleWapper =new QueryWrapper<>();
|
||||
deleteRoleWapper.eq("user_id",userEO.getUsid());
|
||||
userRoleEODao.delete(deleteRoleWapper);
|
||||
|
||||
//删除旧的角色
|
||||
QueryWrapper<UserOrgEO> deleteOrgWapper =new QueryWrapper<>();
|
||||
deleteOrgWapper.eq("user_id",userEO.getUsid());
|
||||
userOrgEODao.delete(deleteOrgWapper);
|
||||
}
|
||||
|
||||
// 加入角色关联表
|
||||
eo.getRoleIdList().forEach(c -> {
|
||||
UserRoleEO userRoleEO = new UserRoleEO();
|
||||
userRoleEO.setUserId(eo.getUsid());
|
||||
userRoleEO.setRoleId(c);
|
||||
userRoleEODao.insert(userRoleEO);
|
||||
});
|
||||
|
||||
// 插入组织关联表
|
||||
eo.getOrgList().forEach(c -> {
|
||||
UserOrgEO userOrgEO = new UserOrgEO();
|
||||
userOrgEO.setUserId(eo.getUsid());
|
||||
userOrgEO.setOrgId(c.getOrgCode());
|
||||
userOrgEODao.insert(userOrgEO);
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void saveBatch(List<UserEO> userEOList) {
|
||||
@@ -321,11 +375,6 @@ public class UserEOServiceImpl implements IUserEoService {
|
||||
"tester".equals(userImportVO.getAccount())) {
|
||||
throw new AdcDaBaseException("第"+(i+1)+"行"+"用户名不符合规则");
|
||||
}
|
||||
if (StringUtils.isNotBlank(userImportVO.getDepartment())) {
|
||||
if (userImportVO.getDepartment().length() > 50) {
|
||||
throw new AdcDaBaseException("第"+(i+1)+"行"+"部门不超过50字符");
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(userImportVO.getEmail())) {
|
||||
userImportVO.setEmail(userImportVO.getEmail().replace("amp;", ""));
|
||||
}
|
||||
|
||||
@@ -21,21 +21,17 @@ public interface IUserEoService {
|
||||
*/
|
||||
UserEO getUserByLoginName(String userName,String usid);
|
||||
|
||||
// /**
|
||||
// * 根据邮箱和id查询用户数量
|
||||
// * @param email
|
||||
// * @param usid
|
||||
// * @return
|
||||
// */
|
||||
// int getUserCountByEmail(String email,String usid);
|
||||
|
||||
/**
|
||||
* 新增或者编辑用户
|
||||
* @param eo
|
||||
*/
|
||||
void save(UserEO eo);
|
||||
|
||||
// void setIsUse(String usid,String type);
|
||||
/**
|
||||
* 从同步处新增或编辑用户
|
||||
* @param eo
|
||||
*/
|
||||
void syncSaveOrUpdate(UserEO eo);
|
||||
|
||||
UserEO getUserById(String usid);
|
||||
|
||||
|
||||
@@ -55,11 +55,6 @@ public class UserImportVO {
|
||||
@Excel(name = "是否启用(0禁用、1启用)", width = 20)
|
||||
private String isUse;
|
||||
|
||||
|
||||
@Excel(name = "部门", width = 20)
|
||||
private String department;
|
||||
|
||||
|
||||
@Excel(name = "姓名", width = 20)
|
||||
private String usname;
|
||||
|
||||
|
||||
@@ -43,11 +43,6 @@ public class UserRoleVO extends BaseEntity {
|
||||
*/
|
||||
private String email;
|
||||
|
||||
|
||||
|
||||
private String department;
|
||||
|
||||
|
||||
private String usname;
|
||||
|
||||
}
|
||||
|
||||
@@ -45,12 +45,9 @@
|
||||
<include refid="Base_Where_Clause"/>
|
||||
</where>
|
||||
</select>
|
||||
<select id="selectCountByUUID" resultType="java.lang.Long">
|
||||
select count(1) from TS_ORG
|
||||
<select id="selectCountByOrgCode" resultType="java.lang.Long">
|
||||
select count(1) from TS_ORG
|
||||
<where>
|
||||
<if test="pageVO.UUID != null and pageVO.UUID != ''">
|
||||
and UUID != #{pageVO.UUID}
|
||||
</if>
|
||||
<if test="pageVO.orgCode != null and pageVO.orgCode != ''">
|
||||
and org_code = #{pageVO.orgCode}
|
||||
</if>
|
||||
|
||||
Reference in New Issue
Block a user