Commit f9f79002 authored by huyuchen's avatar huyuchen

明细接口改造

parent 72b47c4b
...@@ -85,6 +85,12 @@ public class PersonVo { ...@@ -85,6 +85,12 @@ public class PersonVo {
@Attribute @Attribute
protected Date updateTime; protected Date updateTime;
/**
* 部门id
*/
@Attribute
protected Long deptId;
/** /**
* 状态 * 状态
*/ */
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.yifu.cloud.plus.v1.yifu.admin.controller; package com.yifu.cloud.plus.v1.yifu.admin.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
...@@ -45,6 +46,7 @@ import org.springframework.security.access.prepost.PreAuthorize; ...@@ -45,6 +46,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import javax.validation.Valid;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -493,4 +495,16 @@ public class UserController { ...@@ -493,4 +495,16 @@ public class UserController {
UserInfo info = userService.getUserInfo(user); UserInfo info = userService.getUserInfo(user);
return info; return info;
} }
/**
* 从ldap实时更新用户和部门
*
* @return R
*/
@SysLog("从ldap实时更新用户和部门")
@PostMapping("/updateUserAndDept")
public R updateUserAndDept(HttpServletRequest request,@RequestBody JSONObject json) {
return R.ok(userService.updateUserAndDept(request,json));
}
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.yifu.cloud.plus.v1.yifu.admin.service; package com.yifu.cloud.plus.v1.yifu.admin.service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
...@@ -29,6 +30,7 @@ import com.yifu.cloud.plus.v1.yifu.admin.api.vo.UserVO; ...@@ -29,6 +30,7 @@ import com.yifu.cloud.plus.v1.yifu.admin.api.vo.UserVO;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R; import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -178,4 +180,9 @@ public interface SysUserService extends IService<SysUser> { ...@@ -178,4 +180,9 @@ public interface SysUserService extends IService<SysUser> {
**/ **/
void ldapLogin(String username,String password,SysUser user); void ldapLogin(String username,String password,SysUser user);
/**
* 从ldap实时更新用户和部门
* @return R
*/
R updateUserAndDept(HttpServletRequest request, JSONObject json);
} }
...@@ -19,7 +19,9 @@ package com.yifu.cloud.plus.v1.yifu.admin.service.impl; ...@@ -19,7 +19,9 @@ package com.yifu.cloud.plus.v1.yifu.admin.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
...@@ -51,6 +53,7 @@ import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil; ...@@ -51,6 +53,7 @@ import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
...@@ -59,6 +62,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -59,6 +62,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
...@@ -96,6 +100,12 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -96,6 +100,12 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
private final PermissionDaprUtil permissionDaprUtil; private final PermissionDaprUtil permissionDaprUtil;
@Value("${hrms.id}")
private String hcmId;
@Value("${hrms.secret}")
private String hcmSecret;
/** /**
* 保存用户信息 * 保存用户信息
* @param userDto DTO 对象 * @param userDto DTO 对象
...@@ -490,8 +500,20 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -490,8 +500,20 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
try { try {
List<PersonVo> list = ldapUtil.getAllPersons(); List<PersonVo> list = ldapUtil.getAllPersons();
if (null != list) { if (null != list) {
List<SysUser> listUser = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDelFlag, CommonConstants.STATUS_NORMAL)); List<SysUser> listUser = this.list(Wrappers.<SysUser>query().lambda()
.eq(SysUser::getType, CommonConstants.STATUS_NORMAL)
.eq(SysUser::getDelFlag, CommonConstants.STATUS_NORMAL));
List<String> list1 = listUser.stream().map(SysUser::getUsername).collect(Collectors.toList()); List<String> list1 = listUser.stream().map(SysUser::getUsername).collect(Collectors.toList());
//筛选出不存在的用户重置为不可用
List<String> list2 = list.stream().map(PersonVo::getUid).collect(Collectors.toList());
List<String> collect = list1.stream().filter(s -> !list2.contains(s)
).collect(Collectors.toList());
log.info("要重置为不可用的用户列表:" + collect);
LambdaUpdateWrapper<SysUser> settleUpdate = new LambdaUpdateWrapper<>();
settleUpdate.set(SysUser::getDelFlag,CommonConstants.ONE_STRING);
settleUpdate.in(SysUser::getUsername,collect);
this.update(settleUpdate);
List<PersonVo> updateList = new ArrayList<>(); List<PersonVo> updateList = new ArrayList<>();
List<PersonVo> insertList = new ArrayList<>(); List<PersonVo> insertList = new ArrayList<>();
for (PersonVo personVo : list) { for (PersonVo personVo : list) {
...@@ -709,6 +731,197 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -709,6 +731,197 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return this.updateById(sysUser); return this.updateById(sysUser);
} }
/**
* 从ldap实时更新用户和部门
* @return R
*/
@Override
public R updateUserAndDept(HttpServletRequest request,JSONObject json) {
//权限验证
String appId = request.getHeader("HRBS-APP-ID");
String appSecret = request.getHeader("HRBS-APP-SECRET");
if(Common.isEmpty(appId) || Common.isEmpty(appSecret)){
return R.failed("操作失败,权限标识错误!");
}
if(!hcmId.equals(appId) || !hcmSecret.equals(appSecret) ){
return R.failed("操作失败,无权限!");
}
if(Common.isEmpty(json)) {
return R.failed("传参不能为空");
}
boolean isAll = (boolean) json.get("isAll");
String dataType = json.get("isAll").toString();
//获取人员和部门信息
JSONArray updateParamsArr=json.getJSONArray("data");
if (Common.isEmpty(updateParamsArr)) {
return R.failed("传参不能为空");
}
//全量更新直接调用定时任务接口
if (isAll) {
this.updateByLdap();
//用户同步操作
} else if ("user".equals(dataType)) {
//更新或者新增用户信息
updateOrSaveUser(updateParamsArr);
} else {
//更新或者新增部门信息
updateOrSaveDept(updateParamsArr);
}
return R.ok();
}
/**
* 更新或者新增用户信息
* @return
*/
private void updateOrSaveUser(JSONArray updateParamsArr) {
List<SysUser> listUser = this.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getDelFlag, CommonConstants.STATUS_NORMAL));
List<String> list1 = listUser.stream().map(SysUser::getUsername).collect(Collectors.toList());
List<PersonVo> updateList = new ArrayList<>();
List<PersonVo> insertList = new ArrayList<>();
SysDept sysDeptCount;
List<String> saveDnList;
JSONObject updateJson;
for (int i = 0; i < updateParamsArr.size(); i++) {
updateJson=updateParamsArr.getJSONObject(i);
PersonVo personVo = new PersonVo();
personVo.setUid(updateJson.get("uid")==null?"":updateJson.getString("uid"));
personVo.setPersonName(updateJson.get("cn")==null?"":updateJson.getString("cn"));
personVo.setTelephoneNumber(updateJson.get("telephoneNumber")==null?"":updateJson.getString("telephoneNumber"));
personVo.setEmail(updateJson.get("mail")==null?"":updateJson.getString("mail"));
String dn = updateJson.get("dn")==null?"":updateJson.getString("dn");
log.info("更新人员信息:" + dn + "_" +personVo.getUid());
if (dn.contains("uid")) {
dn = dn.substring(dn.indexOf(",", 1) + 1);
}
saveDnList = Arrays.stream(dn.split(",")).filter(e -> (e.contains("ou=") && !e.equals("ou=wanxin"))).collect(Collectors.toList());
String deptName = saveDnList.get(0).replace("ou=", "");
personVo.setDeptName(deptName);
sysDeptCount = sysDeptMapper.selectDeptDn(dn);
if (Common.isEmpty(sysDeptCount)) {
continue;
} else {
personVo.setDeptName(deptName);
personVo.setDeptId(sysDeptCount.getDeptId());
}
if (list1.stream().anyMatch(e -> e.equals(personVo.getUid()))) {
updateList.add(personVo);
} else {
//新增用户赋初始密码
personVo.setPassword(ENCODER.encode(CommonConstants.DEFAULT_PASSWORD));
insertList.add(personVo);
}
}
if (!updateList.isEmpty()) {
baseMapper.batchUpdateUser(updateList);
}
if (!insertList.isEmpty()) {
baseMapper.batchInsertUser(insertList);
//新增用户的初始化角色
JSONObject jsonObject;
for (PersonVo personVo :insertList) {
if (Common.isNotNull(personVo.getRoleIds())) {
SysUser user = this.getOne(Wrappers.<SysUser>query().lambda().
eq(SysUser::getUsername, personVo.getUid()).last(CommonConstants.LAST_ONE_SQL));
jsonObject = JSONObject.parseObject(personVo.getRoleIds());
//获取mvp用户的角色
String userRole=jsonObject.get("hro")==null?"":jsonObject.getString("hro");
if (!userRole.isEmpty()) {
Common.getList(userRole).stream().map(roleId -> {
SysUserRole role = new SysUserRole();
role.setUserId(user.getUserId());
role.setRoleId(Long.valueOf(roleId));
return role;
}).forEach(sysUserRoleMapper::insert);
}
//获取ekp用户的角色
String ekpUserRole=jsonObject.get("ekp")==null?"":jsonObject.getString("ekp");
if (!ekpUserRole.isEmpty()) {
for (String roleId : Common.getList(ekpUserRole)) {
HcmInitRoleInfoVo vo = new HcmInitRoleInfoVo();
vo.setLoginName(user.getUsername());
vo.setRoleId(roleId);
permissionDaprUtil.saveSysUserRole(vo);
}
}
}
}
}
}
/**
* 更新或者新增部门信息
* @return
*/
private void updateOrSaveDept(JSONArray updateParamsArr) {
SysDept sysDeptCount;
List<String> saveDnList;
JSONObject updateJson;
for (int i = 0; i < updateParamsArr.size(); i++) {
updateJson = updateParamsArr.getJSONObject(i);
String dn = updateJson.get("dn")==null?"":updateJson.getString("dn");
String deptId = updateJson.get("x-ouid")==null?"":updateJson.getString("x-ouid");
String deptName = updateJson.get("ou")==null?"":updateJson.getString("ou");
log.info("更新部门信息:" + dn);
saveDnList = Arrays.stream(dn.split(",")).filter(e -> (e.contains("ou=") && !e.equals("ou=wanxin"))).collect(Collectors.toList());
int size = saveDnList.size();
if (size == 1) {
SysDept sysDept = sysDeptMapper.selectDeptDn(dn);
if (null == sysDept) {
SysDept ins = sysDeptMapper.selectCountId(Long.valueOf(deptId));
if (Common.isNotNull(ins)) {
ins.setName(deptName);
ins.setDeptDn(dn);
sysDeptMapper.updateDeptById(ins);
} else {
SysDept insertSysDept = new SysDept();
insertSysDept.setName(deptName);
insertSysDept.setParentId(0L);
insertSysDept.setDeptDn(dn);
if (Common.isNotNull(deptId)) {
insertSysDept.setDeptId(Long.valueOf(deptId));
}
insertSysDept.setDelFlag(CommonConstants.ZERO_STRING);
sysDeptMapper.insert(insertSysDept);
}
} else {
sysDept.setName(deptName);
sysDeptMapper.updateDeptById(sysDept);
}
} else {
SysDept sysDept = sysDeptMapper.selectDeptDn(dn.substring(dn.indexOf("ou=", 2), dn.length()));
if (null != sysDept) {
sysDeptCount = sysDeptMapper.selectDeptDn(dn);
if (null == sysDeptCount) {
SysDept ins = sysDeptMapper.selectCountId(Long.valueOf(deptId));
if (Common.isNotNull(ins)) {
ins.setName(deptName);
ins.setDeptDn(dn);
ins.setParentId(sysDept.getDeptId());
sysDeptMapper.updateDeptById(ins);
} else {
SysDept insertSysDept = new SysDept();
insertSysDept.setName(deptName);
insertSysDept.setParentId(sysDept.getDeptId());
insertSysDept.setDeptDn(dn);
if (Common.isNotNull(deptId)) {
insertSysDept.setDeptId(Long.valueOf(deptId));
}
insertSysDept.setDelFlag(CommonConstants.ZERO_STRING);
sysDeptMapper.insert(insertSysDept);
}
} else {
sysDeptCount.setName(deptName);
sysDeptCount.setDeptDn(dn);
sysDeptCount.setParentId(sysDept.getDeptId());
sysDeptMapper.updateDeptById(sysDeptCount);
}
}
}
}
}
private void clenrUserCache(String username,String wxUserId,String phone){ private void clenrUserCache(String username,String wxUserId,String phone){
//用户名登录用户缓存清除 //用户名登录用户缓存清除
if(Common.isNotNull(username)){ if(Common.isNotNull(username)){
......
...@@ -97,3 +97,7 @@ springdoc: ...@@ -97,3 +97,7 @@ springdoc:
- group: upms - group: upms
#按包路径匹配 #按包路径匹配
packagesToScan: com.yifu.cloud.plus.v1.yifu.admin.controller packagesToScan: com.yifu.cloud.plus.v1.yifu.admin.controller
hrms:
id: HRBS
secret: d8a3649587afe0fc2045741f84cd15221b8404f1ae5c3e9c8c301a4caba07d54
...@@ -214,7 +214,7 @@ ...@@ -214,7 +214,7 @@
<insert id="batchInsertUser" parameterType="java.util.List"> <insert id="batchInsertUser" parameterType="java.util.List">
insert into sys_user insert into sys_user
(user_id,username,password,phone,email,nickname,create_time) (user_id,username,password,phone,email,nickname,create_time,dept_id,dept_name)
VALUES VALUES
<foreach collection="list" item="item" index="index" separator=","> <foreach collection="list" item="item" index="index" separator=",">
( (
...@@ -224,7 +224,9 @@ ...@@ -224,7 +224,9 @@
#{item.telephoneNumber}, #{item.telephoneNumber},
#{item.email}, #{item.email},
#{item.personName}, #{item.personName},
now() now(),
#{item.deptId},
#{item.deptName}
) )
</foreach> </foreach>
</insert> </insert>
...@@ -265,6 +267,20 @@ ...@@ -265,6 +267,20 @@
when username=#{i.uid} then now() when username=#{i.uid} then now()
</foreach> </foreach>
</trim> </trim>
<trim prefix="dept_id =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.deptId!=null">
when username=#{i.userId} then #{i.deptId}
</if>
</foreach>
</trim>
<trim prefix="dept_name =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.deptName!=null">
when username=#{i.userId} then #{i.deptName}
</if>
</foreach>
</trim>
</trim> </trim>
where where
<foreach collection="list" separator="or" item="i" index="index" > <foreach collection="list" separator="or" item="i" index="index" >
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment