mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 10:56: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 java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
@ -50,7 +51,7 @@ public interface RoleEmployeeDao extends BaseMapper<RoleEmployeeEntity> {
|
||||
/**
|
||||
* 查询角色下的人员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.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 角色的员工更新
|
||||
@ -25,6 +25,6 @@ public class RoleEmployeeUpdateForm {
|
||||
|
||||
@Schema(description = "员工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 net.lab1024.sa.admin.module.system.role.dao.RoleEmployeeDao;
|
||||
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.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 角色员工 manager
|
||||
@ -16,19 +12,9 @@ import java.util.List;
|
||||
* @Date 2022-04-08 21:53:04
|
||||
* @Wechat zhuoda1024
|
||||
* @Email lab1024@163.com
|
||||
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>
|
||||
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>
|
||||
*/
|
||||
@Service
|
||||
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;
|
||||
|
||||
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.domain.entity.DepartmentEntity;
|
||||
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 org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -88,7 +89,6 @@ public class RoleEmployeeService {
|
||||
* 移除员工角色
|
||||
*
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResponseDTO<String> removeRoleEmployee(Long employeeId, Long roleId) {
|
||||
if (null == employeeId || null == roleId) {
|
||||
return ResponseDTO.userErrorParam();
|
||||
@ -110,21 +110,23 @@ public class RoleEmployeeService {
|
||||
* 批量添加角色的成员员工
|
||||
*
|
||||
*/
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
public ResponseDTO<String> batchAddRoleEmployee(RoleEmployeeUpdateForm roleEmployeeUpdateForm) {
|
||||
Long roleId = roleEmployeeUpdateForm.getRoleId();
|
||||
List<Long> employeeIdList = roleEmployeeUpdateForm.getEmployeeIdList();
|
||||
// 保存新的角色员工
|
||||
List<RoleEmployeeEntity> roleEmployeeList = null;
|
||||
if (CollectionUtils.isNotEmpty(employeeIdList)) {
|
||||
roleEmployeeList = employeeIdList.stream()
|
||||
|
||||
// 已选择的员工id列表
|
||||
Set<Long> selectedEmployeeIdList = roleEmployeeUpdateForm.getEmployeeIdList();
|
||||
// 数据库里已有的员工id列表
|
||||
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))
|
||||
.collect(Collectors.toList());
|
||||
roleEmployeeManager.saveBatch(roleEmployeeList);
|
||||
}
|
||||
// 防重,删除此次角色员工数据
|
||||
roleEmployeeDao.batchDeleteEmployeeRole(roleId, employeeIdList);
|
||||
// 保存数据
|
||||
roleEmployeeManager.saveRoleEmployee(roleEmployeeList);
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
|
@ -65,6 +65,11 @@ public class RoleService {
|
||||
if (null == roleEntity) {
|
||||
return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
|
||||
}
|
||||
// 当没有员工绑定这个角色时才可以删除
|
||||
Integer exists = roleEmployeeDao.existsByRoleId(roleId);
|
||||
if (exists != null) {
|
||||
return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "该角色下存在员工,无法删除");
|
||||
}
|
||||
roleDao.deleteById(roleId);
|
||||
roleMenuDao.deleteByRoleId(roleId);
|
||||
roleEmployeeDao.deleteByRoleId(roleId);
|
||||
@ -86,7 +91,7 @@ public class RoleService {
|
||||
}
|
||||
|
||||
existRoleEntity = roleDao.getByRoleCode(roleUpdateForm.getRoleCode());
|
||||
if (null != existRoleEntity) {
|
||||
if (null != existRoleEntity && !existRoleEntity.getRoleId().equals(roleUpdateForm.getRoleId())) {
|
||||
return ResponseDTO.userErrorParam("角色编码重复,重复的角色为:" + existRoleEntity.getRoleName());
|
||||
}
|
||||
|
||||
|
@ -138,4 +138,12 @@
|
||||
#{item}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
<select id="existsByRoleId" resultType="java.lang.Integer">
|
||||
SELECT 1
|
||||
FROM t_role_employee er
|
||||
WHERE er.role_id = #{roleId}
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
</mapper>
|
@ -40,7 +40,7 @@
|
||||
</a-row>
|
||||
</a-form>
|
||||
<a-table
|
||||
:row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
|
||||
:row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange, getCheckboxProps: getCheckboxProps }"
|
||||
:loading="tableLoading"
|
||||
size="small"
|
||||
:columns="columns"
|
||||
@ -95,6 +95,7 @@
|
||||
|
||||
const visible = ref(false);
|
||||
async function showModal(selectEmployeeId) {
|
||||
originalRowKeyList.value = selectEmployeeId || [];
|
||||
selectedRowKeyList.value = selectEmployeeId || [];
|
||||
visible.value = true;
|
||||
onSearch();
|
||||
@ -144,8 +145,9 @@
|
||||
}
|
||||
|
||||
// ----------------------- 员工表格选择 ---------------------
|
||||
const originalRowKeyList = ref([]);
|
||||
let selectedRowKeyList = ref([]);
|
||||
const hasSelected = computed(() => selectedRowKeyList.value.length > 0);
|
||||
const hasSelected = computed(() => selectedRowKeyList.value.length !== originalRowKeyList.value.length);
|
||||
|
||||
function onSelectChange(selectedRowKeys) {
|
||||
selectedRowKeyList.value = selectedRowKeys;
|
||||
@ -156,10 +158,19 @@
|
||||
message.warning('请选择角色人员');
|
||||
return;
|
||||
}
|
||||
emits('selectData', selectedRowKeyList.value);
|
||||
// 过滤出新选择的人员id
|
||||
const newEmployeeIdList = selectedRowKeyList.value.filter((id) => !originalRowKeyList.value.includes(id));
|
||||
emits('selectData', newEmployeeIdList);
|
||||
closeModal();
|
||||
}
|
||||
|
||||
function getCheckboxProps(record) {
|
||||
return {
|
||||
// 角色员工列表的添加员工弹窗中 禁止添加选择已存在该角色的员工
|
||||
disabled: originalRowKeyList.value.includes(record.employeeId),
|
||||
};
|
||||
}
|
||||
|
||||
// ----------------------- 员工表格渲染 ---------------------
|
||||
const tableData = ref([]);
|
||||
//字段
|
||||
|
@ -168,7 +168,7 @@
|
||||
|
||||
async function addRoleEmployee() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user