员工加角色(查询、交接待完善)

This commit is contained in:
xiao-fajia 2024-08-14 20:01:43 +08:00
parent dc74f8f0b0
commit b9e17e0568
11 changed files with 212 additions and 52 deletions

View File

@ -9,4 +9,5 @@ public interface CommonErrorCodeConstants extends ErrorCodeConstants {
ErrorCode UNIQUE_CODE_CREATE_REPEAT = new ErrorCode(2_002_000_000, "唯一推广码生成失败");
ErrorCode STAFF_CHANGE_CREATE_REPEAT = new ErrorCode(2_002_000_001, "该员工工作已交接");
ErrorCode STAFF_NOT_CHANGE = new ErrorCode(2_002_000_003, "该员工还有工作未交接,不可删除");
ErrorCode LOGIN_ACCOUNT_EXIST = new ErrorCode(2_002_000_004, "登录账号已存在");
}

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.staff.entity.CompanyStaff;
import cn.iocoder.yudao.module.staff.service.CompanyStaffService;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffReqVO;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffRespVO;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
@ -200,4 +201,16 @@ public class CompanyStaffController {
staffService.resetPassword(staffRespVO);
return CommonResult.ok();
}
/**
* 获取角色列表
* @author 小李
* @date 15:08 2024/8/14
**/
@GetMapping("/roleList")
@Operation(summary = "获取角色列表")
@PreAuthorize("@ss.hasPermission('company:staff:query')")
public CommonResult<List<RoleReqDTO>> getRoleList(){
return success(staffService.getRoleList());
}
}

View File

@ -1,12 +1,14 @@
package cn.iocoder.yudao.module.staff.entity;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
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 com.fasterxml.jackson.annotation.JsonFormat;
@ -15,7 +17,9 @@ import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static cn.iocoder.yudao.common.DictBaseConstants.*;
@ -98,4 +102,12 @@ public class CompanyStaff extends TenantBaseDO {
/** 附件urlsinfra_file表中的url多个英文逗号拼接 */
private String fileUrls;
/** 员工角色ID集合 */
@TableField(exist = false)
private List<Long> roleIds;
/** 员工角色名称们(,分隔) */
@TableField(exist = false)
private String roleNames;
}

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.module.staff.entity.CompanyStaff;
import cn.iocoder.yudao.module.label.entity.Label;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffReqVO;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffRespVO;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
@ -94,4 +95,11 @@ public interface CompanyStaffService extends IService<CompanyStaff> {
* @date 11:41 2024/8/13
**/
void updateStaffWorkAndJoinedYears();
/**
* 获取当前租户有些什么角色可以分配
* @author 小李
* @date 14:53 2024/8/14
**/
List<RoleReqDTO> getRoleList();
}

View File

@ -4,9 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.common.BaseConstants;
import cn.iocoder.yudao.common.CommonErrorCodeConstants;
import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.company.service.CompanyService;
import cn.iocoder.yudao.module.label.entity.BusiLabel;
import cn.iocoder.yudao.module.label.entity.Label;
import cn.iocoder.yudao.module.label.service.BusiLabelService;
@ -20,6 +18,9 @@ import cn.iocoder.yudao.module.staff.vo.CompanyStaffReqVO;
import cn.iocoder.yudao.module.staff.vo.CompanyStaffRespVO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserDTO;
@ -37,6 +38,7 @@ import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.config.CommonStr.USER_TYPE_STAFF;
@ -72,6 +74,12 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
@Resource
private CompanyStaffChangeService staffChangeService;
@Resource
private RoleApi roleApi;
@Resource
private PermissionApi permissionApi;
/**
* 获得企业管理-员工信息表分页
*
@ -98,6 +106,12 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
// 获取当前登录用户的详细信息
AdminUserRespDTO loginUser = getLoginUser();
// 验证登录账号是否重复
AdminUserRespDTO checkUserName = adminUserApi.getUserByUsername(staffRespVO.getLoginAccount());
if (ObjectUtil.isNotEmpty(checkUserName)){
throw exception(CommonErrorCodeConstants.LOGIN_ACCOUNT_EXIST);
}
/* 创建UserDTO用于给sys_user插入数据 */
UserDTO userDTO = new UserDTO();
userDTO.setUsername(staffRespVO.getLoginAccount());
@ -142,6 +156,10 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
// 3 保存员工信息到数据库
this.save(staffRespVO);
// 新增角色权限
Set<Long> roleIds = staffRespVO.getRoleIds().stream().collect(Collectors.toSet());
permissionApi.assignUserRoleByRoleIds(userId, roleIds);
/* 插入标签到业务标签表 */
if (ObjectUtil.isNotEmpty(staffRespVO.getLabelsArray())) {
// 1 获取所有标签信息
@ -198,6 +216,12 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
}
}
/* 修改角色 */
if (ObjectUtil.isNotEmpty(staffRespVO.getRoleIds())){
Set<Long> roleIdes = staffRespVO.getRoleIds().stream().collect(Collectors.toSet());
permissionApi.assignUserRoleByRoleIds(staffRespVO.getUserId(), roleIdes);
}
/* 修改员工表 */
baseMapper.updateById(staffRespVO);
}
@ -227,6 +251,7 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
/* 删除业务标签表记录 */
busiLabelService.remove(new LambdaQueryWrapper<BusiLabel>().eq(BusiLabel::getMainId, staff.getId()));
/* 删除员工表记录 */
baseMapper.deleteById(id);
}
@ -251,6 +276,9 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
List<String> labelsArray = busiLabels.stream().map(item -> item.getLabelName()).collect(Collectors.toList());
staffRespVO.setLabelsArray(labelsArray);
}
// 查角色ID给响应对象
List<Long> roleIds = permissionApi.getRoleIdsByUserId(staff.getUserId());
staffRespVO.setRoleIds(roleIds);
return staffRespVO;
}
@ -435,4 +463,15 @@ public class CompanyStaffServiceImpl extends ServiceImpl<CompanyStaffMapper, Com
queryWrapper.gt(CompanyStaff::getId, page.getRecords().get(page.getRecords().size() - 1).getId());
}
}
/**
* 获取当前租户有些什么角色可以分配
*
* @author 小李
* @date 14:53 2024/8/14
**/
@Override
public List<RoleReqDTO> getRoleList() {
return roleApi.getRoleList();
}
}

View File

@ -24,4 +24,6 @@ public class CompanyStaffReqVO extends CompanyStaff {
@Schema(description = "入职日期查询范围")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private Date[] joinedDateArray;
}

View File

@ -5,57 +5,60 @@
<mapper namespace="cn.iocoder.yudao.module.staff.mapper.CompanyStaffMapper">
<resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.staff.entity.CompanyStaff">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
<result property="userId" column="user_id" jdbcType="BIGINT"/>
<result property="deptId" column="dept_id" jdbcType="BIGINT"/>
<result property="workNo" column="work_no" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="tel" column="tel" jdbcType="VARCHAR"/>
<result property="sex" column="sex" jdbcType="VARCHAR"/>
<result property="address" column="address" jdbcType="VARCHAR"/>
<result property="workDate" column="work_date" jdbcType="DATE"/>
<result property="workYear" column="work_year" jdbcType="DECIMAL"/>
<result property="joinedDate" column="joined_date" jdbcType="DATE"/>
<result property="joinedYear" column="joined_year" jdbcType="DECIMAL"/>
<result property="education" column="education" jdbcType="VARCHAR"/>
<result property="content" column="content" jdbcType="VARCHAR"/>
<result property="uniqueCode" column="unique_code" jdbcType="VARCHAR"/>
<result property="fileUrls" column="file_urls" jdbcType="VARCHAR"/>
<result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
<result property="deleted" column="deleted" jdbcType="BIT"/>
<result property="creator" column="creator" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updater" column="updater" jdbcType="VARCHAR"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<id property="id" column="cs_id" jdbcType="VARCHAR"/>
<result property="corpId" column="cs_corp_id" jdbcType="VARCHAR"/>
<result property="userId" column="cs_user_id" jdbcType="BIGINT"/>
<result property="deptId" column="cs_dept_id" jdbcType="BIGINT"/>
<result property="workNo" column="cs_work_no" jdbcType="VARCHAR"/>
<result property="name" column="cs_name" jdbcType="VARCHAR"/>
<result property="tel" column="cs_tel" jdbcType="VARCHAR"/>
<result property="sex" column="cs_sex" jdbcType="VARCHAR"/>
<result property="address" column="cs_address" jdbcType="VARCHAR"/>
<result property="workDate" column="cs_work_date" jdbcType="DATE"/>
<result property="workYear" column="cs_work_year" jdbcType="DECIMAL"/>
<result property="joinedDate" column="cs_joined_date" jdbcType="DATE"/>
<result property="joinedYear" column="cs_joined_year" jdbcType="DECIMAL"/>
<result property="education" column="cs_education" jdbcType="VARCHAR"/>
<result property="content" column="cs_content" jdbcType="VARCHAR"/>
<result property="uniqueCode" column="cs_unique_code" jdbcType="VARCHAR"/>
<result property="fileUrls" column="cs_file_urls" jdbcType="VARCHAR"/>
<result property="roleNames" column="role_names" />
</resultMap>
<sql id="Base_SQL">
select id,
corp_id,
user_id,
dept_id,
work_no,
name,
tel,
sex,
address,
work_date,
work_year,
joined_date,
joined_year,
education,
content,
unique_code,
file_urls,
tenant_id,
deleted,
creator,
create_time,
updater,
update_time
from company_staff cs
where deleted = '0'
SELECT cs.id AS cs_id,
cs.corp_id AS cs_corp_id,
cs.user_id AS cs_user_id,
cs.dept_id AS cs_dept_id,
cs.work_no AS cs_work_no,
cs.name AS cs_name,
cs.tel AS cs_tel,
cs.sex AS cs_sex,
cs.address AS cs_address,
cs.work_date AS cs_work_date,
cs.work_year AS cs_work_year,
cs.joined_date AS cs_joined_date,
cs.joined_year AS cs_joined_year,
cs.education AS cs_education,
cs.content AS cs_content,
cs.unique_code AS cs_unique_code,
cs.file_urls AS cs_file_urls,
rn.role_names AS role_names
FROM company_staff cs
LEFT JOIN
(
SELECT cs.user_id,
sr.id,
GROUP_CONCAT(DISTINCT sr.name ORDER BY sr.name SEPARATOR ',') AS role_names
FROM company_staff cs
LEFT JOIN
system_user_role sur ON cs.user_id = sur.user_id
LEFT JOIN
system_role sr ON sur.role_id = sr.id
WHERE cs.deleted = '0' AND sur.deleted = '0'
GROUP BY cs.user_id
) rn ON cs.user_id = rn.user_id
WHERE cs.deleted = '0'
</sql>
<select id="selectListPage" resultMap="BaseResultMap">
@ -93,6 +96,12 @@
<if test="map.joinedDateArray.length > 0">
and cs.joined_date between #{map.joinedDateArray[0]} and #{map.joinedDateArray[1]}
</if>
order by create_time desc
<if test="map.roleIds != null and map.roleIds.size > 0">
and rn.id in
<foreach collection="map.roleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
order by cs.create_time desc
</select>
</mapper>

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
@ -54,4 +55,21 @@ public interface PermissionApi {
*/
void assignUserRole(Long userId, Set<String> roleCodes);
/**
* 设置用户角色
*
* @author 小李
* @date 18:58 2024/8/14
* @param userId 用户D
* @param roleIds 角色IDs
**/
void assignUserRoleByRoleIds(Long userId, Set<Long> roleIds);
/**
* 获取某个用户的角色IDs
* @author 小李
* @date 19:24 2024/8/14
* @param userId 用户ID
**/
List<Long> getRoleIdsByUserId(Long userId);
}

View File

@ -1,6 +1,9 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import java.util.Collection;
import java.util.List;
/**
* 角色 API 接口
@ -18,4 +21,11 @@ public interface RoleApi {
*/
void validRoleList(Collection<Long> ids);
/**
* 查询角色列表
* @author 小李
* @date 15:03 2024/8/14
**/
List<RoleReqDTO> getRoleList();
}

View File

@ -6,7 +6,9 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 权限 API 实现类
@ -50,4 +52,29 @@ public class PermissionApiImpl implements PermissionApi {
permissionService.assignUserRoleByCodes(userId, roleCodes);
}
/**
* 设置用户角色
*
* @author 小李
* @date 18:58 2024/8/14
* @param userId 用户D
* @param roleIds 角色IDs
**/
@Override
public void assignUserRoleByRoleIds(Long userId, Set<Long> roleIds){
permissionService.assignUserRole(userId, roleIds);
}
/**
* 获取某个用户的角色IDs
* @author 小李
* @date 19:24 2024/8/14
* @param userId 用户ID
**/
@Override
public List<Long> getRoleIdsByUserId(Long userId){
Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(userId);
return roleIds.stream().collect(Collectors.toList());
}
}

View File

@ -1,10 +1,15 @@
package cn.iocoder.yudao.module.system.api.permission;
import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* 角色 API 实现类
@ -21,4 +26,20 @@ public class RoleApiImpl implements RoleApi {
public void validRoleList(Collection<Long> ids) {
roleService.validateRoleList(ids);
}
/**
* 查询角色列表
* @author 小李
* @date 15:03 2024/8/14
**/
@Override
public List<RoleReqDTO> getRoleList(){
List<RoleDO> roleList = roleService.getRoleList();
List<RoleReqDTO> result = roleList.stream().map(item -> {
RoleReqDTO roleReqDTO = new RoleReqDTO();
BeanUtil.copyProperties(item, roleReqDTO);
return roleReqDTO;
}).collect(Collectors.toList());
return result;
}
}