mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 19:06:39 +08:00
优化:1、角色员工列表的添加员工弹窗中禁止添加选择已存在该角色的员工;2、禁止删除已存在员工的角色
This commit is contained in:
parent
949f7a209c
commit
8fd02fc519
@ -12,6 +12,7 @@ import net.lab1024.sa.admin.module.system.role.domain.form.RoleEmployeeQueryForm
|
|||||||
import net.lab1024.sa.admin.module.system.role.domain.vo.RoleEmployeeVO;
|
import net.lab1024.sa.admin.module.system.role.domain.vo.RoleEmployeeVO;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,7 +51,7 @@ public interface RoleEmployeeDao extends BaseMapper<RoleEmployeeEntity> {
|
|||||||
/**
|
/**
|
||||||
* 查询角色下的人员id
|
* 查询角色下的人员id
|
||||||
*/
|
*/
|
||||||
List<Long> selectEmployeeIdByRoleIdList(@Param("roleIdList") List<Long> roleIdList);
|
Set<Long> selectEmployeeIdByRoleIdList(@Param("roleIdList") List<Long> roleIdList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -79,5 +80,10 @@ public interface RoleEmployeeDao extends BaseMapper<RoleEmployeeEntity> {
|
|||||||
/**
|
/**
|
||||||
* 批量删除某个角色下的某批用户的关联关系
|
* 批量删除某个角色下的某批用户的关联关系
|
||||||
*/
|
*/
|
||||||
void batchDeleteEmployeeRole(@Param("roleId") Long roleId,@Param("employeeIds")List<Long> employeeIds);
|
void batchDeleteEmployeeRole(@Param("roleId") Long roleId, @Param("employeeIds") Set<Long> employeeIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断某个角色下是否存在用户
|
||||||
|
*/
|
||||||
|
Integer existsByRoleId(@Param("roleId") Long roleId);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色的员工更新
|
* 角色的员工更新
|
||||||
@ -25,6 +25,6 @@ public class RoleEmployeeUpdateForm {
|
|||||||
|
|
||||||
@Schema(description = "员工id集合")
|
@Schema(description = "员工id集合")
|
||||||
@NotEmpty(message = "员工id不能为空")
|
@NotEmpty(message = "员工id不能为空")
|
||||||
protected List<Long> employeeIdList;
|
protected Set<Long> employeeIdList;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,7 @@ package net.lab1024.sa.admin.module.system.role.manager;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import net.lab1024.sa.admin.module.system.role.dao.RoleEmployeeDao;
|
import net.lab1024.sa.admin.module.system.role.dao.RoleEmployeeDao;
|
||||||
import net.lab1024.sa.admin.module.system.role.domain.entity.RoleEmployeeEntity;
|
import net.lab1024.sa.admin.module.system.role.domain.entity.RoleEmployeeEntity;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色员工 manager
|
* 角色员工 manager
|
||||||
@ -21,14 +17,4 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class RoleEmployeeManager extends ServiceImpl<RoleEmployeeDao, RoleEmployeeEntity> {
|
public class RoleEmployeeManager extends ServiceImpl<RoleEmployeeDao, RoleEmployeeEntity> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存 角色员工
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Transactional(rollbackFor = Throwable.class)
|
|
||||||
public void saveRoleEmployee(List<RoleEmployeeEntity> roleEmployeeList) {
|
|
||||||
if (CollectionUtils.isNotEmpty(roleEmployeeList)) {
|
|
||||||
this.saveBatch(roleEmployeeList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.lab1024.sa.admin.module.system.role.service;
|
package net.lab1024.sa.admin.module.system.role.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import net.lab1024.sa.admin.module.system.department.dao.DepartmentDao;
|
import net.lab1024.sa.admin.module.system.department.dao.DepartmentDao;
|
||||||
import net.lab1024.sa.admin.module.system.department.domain.entity.DepartmentEntity;
|
import net.lab1024.sa.admin.module.system.department.domain.entity.DepartmentEntity;
|
||||||
import net.lab1024.sa.admin.module.system.employee.domain.vo.EmployeeVO;
|
import net.lab1024.sa.admin.module.system.employee.domain.vo.EmployeeVO;
|
||||||
@ -20,12 +21,12 @@ import net.lab1024.sa.base.common.util.SmartBeanUtil;
|
|||||||
import net.lab1024.sa.base.common.util.SmartPageUtil;
|
import net.lab1024.sa.base.common.util.SmartPageUtil;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,7 +89,6 @@ public class RoleEmployeeService {
|
|||||||
* 移除员工角色
|
* 移除员工角色
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public ResponseDTO<String> removeRoleEmployee(Long employeeId, Long roleId) {
|
public ResponseDTO<String> removeRoleEmployee(Long employeeId, Long roleId) {
|
||||||
if (null == employeeId || null == roleId) {
|
if (null == employeeId || null == roleId) {
|
||||||
return ResponseDTO.userErrorParam();
|
return ResponseDTO.userErrorParam();
|
||||||
@ -110,21 +110,23 @@ public class RoleEmployeeService {
|
|||||||
* 批量添加角色的成员员工
|
* 批量添加角色的成员员工
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Throwable.class)
|
|
||||||
public ResponseDTO<String> batchAddRoleEmployee(RoleEmployeeUpdateForm roleEmployeeUpdateForm) {
|
public ResponseDTO<String> batchAddRoleEmployee(RoleEmployeeUpdateForm roleEmployeeUpdateForm) {
|
||||||
Long roleId = roleEmployeeUpdateForm.getRoleId();
|
Long roleId = roleEmployeeUpdateForm.getRoleId();
|
||||||
List<Long> employeeIdList = roleEmployeeUpdateForm.getEmployeeIdList();
|
|
||||||
// 保存新的角色员工
|
// 已选择的员工id列表
|
||||||
List<RoleEmployeeEntity> roleEmployeeList = null;
|
Set<Long> selectedEmployeeIdList = roleEmployeeUpdateForm.getEmployeeIdList();
|
||||||
if (CollectionUtils.isNotEmpty(employeeIdList)) {
|
// 数据库里已有的员工id列表
|
||||||
roleEmployeeList = employeeIdList.stream()
|
Set<Long> dbEmployeeIdList = roleEmployeeDao.selectEmployeeIdByRoleIdList(Lists.newArrayList(roleId));
|
||||||
|
// 从已选择的员工id列表里 过滤数据库里不存在的 即需要添加的员工 id
|
||||||
|
Set<Long> addEmployeeIdList = selectedEmployeeIdList.stream().filter(id -> !dbEmployeeIdList.contains(id)).collect(Collectors.toSet());
|
||||||
|
|
||||||
|
// 添加角色员工
|
||||||
|
if (CollectionUtils.isNotEmpty(addEmployeeIdList)) {
|
||||||
|
List<RoleEmployeeEntity> roleEmployeeList = addEmployeeIdList.stream()
|
||||||
.map(employeeId -> new RoleEmployeeEntity(roleId, employeeId))
|
.map(employeeId -> new RoleEmployeeEntity(roleId, employeeId))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
roleEmployeeManager.saveBatch(roleEmployeeList);
|
||||||
}
|
}
|
||||||
// 防重,删除此次角色员工数据
|
|
||||||
roleEmployeeDao.batchDeleteEmployeeRole(roleId, employeeIdList);
|
|
||||||
// 保存数据
|
|
||||||
roleEmployeeManager.saveRoleEmployee(roleEmployeeList);
|
|
||||||
return ResponseDTO.ok();
|
return ResponseDTO.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +65,11 @@ public class RoleService {
|
|||||||
if (null == roleEntity) {
|
if (null == roleEntity) {
|
||||||
return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
|
return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
|
||||||
}
|
}
|
||||||
|
// 当没有员工绑定这个角色时才可以删除
|
||||||
|
Integer exists = roleEmployeeDao.existsByRoleId(roleId);
|
||||||
|
if (exists != null) {
|
||||||
|
return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "该角色下存在员工,无法删除");
|
||||||
|
}
|
||||||
roleDao.deleteById(roleId);
|
roleDao.deleteById(roleId);
|
||||||
roleMenuDao.deleteByRoleId(roleId);
|
roleMenuDao.deleteByRoleId(roleId);
|
||||||
roleEmployeeDao.deleteByRoleId(roleId);
|
roleEmployeeDao.deleteByRoleId(roleId);
|
||||||
@ -86,7 +91,7 @@ public class RoleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
existRoleEntity = roleDao.getByRoleCode(roleUpdateForm.getRoleCode());
|
existRoleEntity = roleDao.getByRoleCode(roleUpdateForm.getRoleCode());
|
||||||
if (null != existRoleEntity) {
|
if (null != existRoleEntity && !existRoleEntity.getRoleId().equals(roleUpdateForm.getRoleId())) {
|
||||||
return ResponseDTO.userErrorParam("角色编码重复,重复的角色为:" + existRoleEntity.getRoleName());
|
return ResponseDTO.userErrorParam("角色编码重复,重复的角色为:" + existRoleEntity.getRoleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,4 +138,12 @@
|
|||||||
#{item}
|
#{item}
|
||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
<select id="existsByRoleId" resultType="java.lang.Integer">
|
||||||
|
SELECT 1
|
||||||
|
FROM t_role_employee er
|
||||||
|
WHERE er.role_id = #{roleId}
|
||||||
|
LIMIT 1
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
@ -40,7 +40,7 @@
|
|||||||
</a-row>
|
</a-row>
|
||||||
</a-form>
|
</a-form>
|
||||||
<a-table
|
<a-table
|
||||||
:row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
|
:row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange, getCheckboxProps: getCheckboxProps }"
|
||||||
:loading="tableLoading"
|
:loading="tableLoading"
|
||||||
size="small"
|
size="small"
|
||||||
:columns="columns"
|
:columns="columns"
|
||||||
@ -95,6 +95,7 @@
|
|||||||
|
|
||||||
const visible = ref(false);
|
const visible = ref(false);
|
||||||
async function showModal(selectEmployeeId) {
|
async function showModal(selectEmployeeId) {
|
||||||
|
originalRowKeyList.value = selectEmployeeId || [];
|
||||||
selectedRowKeyList.value = selectEmployeeId || [];
|
selectedRowKeyList.value = selectEmployeeId || [];
|
||||||
visible.value = true;
|
visible.value = true;
|
||||||
onSearch();
|
onSearch();
|
||||||
@ -144,8 +145,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------- 员工表格选择 ---------------------
|
// ----------------------- 员工表格选择 ---------------------
|
||||||
|
const originalRowKeyList = ref([]);
|
||||||
let selectedRowKeyList = ref([]);
|
let selectedRowKeyList = ref([]);
|
||||||
const hasSelected = computed(() => selectedRowKeyList.value.length > 0);
|
const hasSelected = computed(() => selectedRowKeyList.value.length !== originalRowKeyList.value.length);
|
||||||
|
|
||||||
function onSelectChange(selectedRowKeys) {
|
function onSelectChange(selectedRowKeys) {
|
||||||
selectedRowKeyList.value = selectedRowKeys;
|
selectedRowKeyList.value = selectedRowKeys;
|
||||||
@ -156,10 +158,19 @@
|
|||||||
message.warning('请选择角色人员');
|
message.warning('请选择角色人员');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emits('selectData', selectedRowKeyList.value);
|
// 过滤出新选择的人员id
|
||||||
|
const newEmployeeIdList = selectedRowKeyList.value.filter((id) => !originalRowKeyList.value.includes(id));
|
||||||
|
emits('selectData', newEmployeeIdList);
|
||||||
closeModal();
|
closeModal();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCheckboxProps(record) {
|
||||||
|
return {
|
||||||
|
// 角色员工列表的添加员工弹窗中 禁止添加选择已存在该角色的员工
|
||||||
|
disabled: originalRowKeyList.value.includes(record.employeeId),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------- 员工表格渲染 ---------------------
|
// ----------------------- 员工表格渲染 ---------------------
|
||||||
const tableData = ref([]);
|
const tableData = ref([]);
|
||||||
//字段
|
//字段
|
||||||
|
@ -168,7 +168,7 @@
|
|||||||
|
|
||||||
async function addRoleEmployee() {
|
async function addRoleEmployee() {
|
||||||
let res = await roleApi.getRoleAllEmployee(selectRoleId.value);
|
let res = await roleApi.getRoleAllEmployee(selectRoleId.value);
|
||||||
let selectedIdList = res.data.map((e) => e.roleId) || [];
|
let selectedIdList = res.data.map((e) => e.employeeId) || [];
|
||||||
selectEmployeeModal.value.showModal(selectedIdList);
|
selectEmployeeModal.value.showModal(selectedIdList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user