gasWellWatch/src/main/java/com/isu/gaswellwatch/service/impl/RoleServiceImpl.java

108 lines
4.5 KiB
Java
Raw Normal View History

2024-10-30 10:34:26 +08:00
package com.isu.gaswellwatch.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isu.gaswellwatch.config.SnowflakeConfig;
import com.isu.gaswellwatch.constants.UserConstant;
import com.isu.gaswellwatch.dao.RoleDao;
import com.isu.gaswellwatch.dto.RoleDTO;
import com.isu.gaswellwatch.dto.RoleEditDTO;
import com.isu.gaswellwatch.dto.RoleMenuDTO;
import com.isu.gaswellwatch.entity.Role;
import com.isu.gaswellwatch.entity.RoleMenu;
import com.isu.gaswellwatch.entity.UserRole;
import com.isu.gaswellwatch.exception.BusinessException;
import com.isu.gaswellwatch.service.RoleMenuService;
import com.isu.gaswellwatch.service.RoleService;
import com.isu.gaswellwatch.service.UserRoleService;
import com.isu.gaswellwatch.utils.ConverterUtil;
import com.isu.gaswellwatch.vo.RoleVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Service("roleService")
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
@Resource
private SnowflakeConfig snowflakeConfig;
@Resource
private RoleMenuService roleMenuService;
@Resource
private UserRoleService userRoleService;
@Override
public Page<Role> page(Integer currentPage, Integer pageSize, String name){
return page(new Page<>(currentPage, pageSize), new LambdaQueryWrapper<Role>().like(StringUtils.isNotBlank(name), Role::getName, name).orderByDesc(Role::getCreateTime));
2024-10-30 10:34:26 +08:00
}
@Override
public void add(RoleDTO roleDTO){
//查重
List<Role> list = list(new LambdaQueryWrapper<Role>().eq(Role::getName, roleDTO.getName()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("已有角色,请重新输入");
}
Long roleId = snowflakeConfig.snowflakeId();
save(Role.builder().id(roleId).name(roleDTO.getName()).description(roleDTO.getDescription()).build());
//关联
addRelation(roleDTO.getRoleMenuDTOList(), roleId);
}
@Override
public void edit(RoleEditDTO roleEditDTO){
List<Role> list = list(new LambdaQueryWrapper<Role>().eq(Role::getName, roleEditDTO.getName()).ne(Role::getId, roleEditDTO.getId()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("角色名称已存在,请重新输入");
}
updateById(ConverterUtil.convert(roleEditDTO, Role.class));
//关联
roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId,roleEditDTO.getId()));
addRelation(roleEditDTO.getRoleMenuDTOList(), roleEditDTO.getId());
}
@Override
public void delete(Long id){
//如果角色绑定了用户 不能删除
List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, id));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("该角色已和用户绑定,不能删除");
}
//如果是超管角色 不能删除
if(Objects.equals(id, UserConstant.SUPER_ADMIN_ID)) {
throw new BusinessException("该角色为超管角色,不能删除");
}
//删除
removeById(id);
roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId,id));
}
private void addRelation(List<RoleMenuDTO> roleMenuDTOList, Long roleId) {
if(!CollectionUtil.isEmpty(roleMenuDTOList)){
List<RoleMenu> roleMenus = new ArrayList<>();
roleMenuDTOList.forEach(roleMenuDTO -> {
if(CollectionUtil.isEmpty(roleMenuDTO.getIdentifier())){
roleMenus.add(RoleMenu.builder().id(snowflakeConfig.snowflakeId()).roleId(roleId).menuId(roleMenuDTO.getMenuId()).build());
}else {
roleMenuDTO.getIdentifier().forEach(identifier -> {
roleMenus.add(RoleMenu.builder().id(snowflakeConfig.snowflakeId()).roleId(roleId).menuId(roleMenuDTO.getMenuId()).identifier(identifier).build());
});
}
});
roleMenuService.saveBatch(roleMenus);
}
}
}