package cn.stylefeng.guns.sys.modular.system.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.Relation;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.mapper.RelationMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.RoleMapper;
import cn.stylefeng.guns.sys.modular.system.model.RoleDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
*
* 角色表 服务实现类
*
*
* @author stylefeng
* @since 2018-12-07
*/
@Service
public class RoleService extends ServiceImpl {
@Resource
private RoleMapper roleMapper;
@Resource
private RelationMapper relationMapper;
@Resource
private UserService userService;
/**
* 添加角色
*
* @author fengshuonan
* @Date 2018/12/23 6:40 PM
*/
@Transactional(rollbackFor = Exception.class)
public void addRole(Role role) {
if (ToolUtil.isOneEmpty(role, role.getName(), role.getPid(), role.getDescription())) {
throw new RequestEmptyException();
}
role.setRoleId(null);
this.save(role);
}
/**
* 编辑角色
*
* @author fengshuonan
* @Date 2018/12/23 6:40 PM
*/
@Transactional(rollbackFor = Exception.class)
public void editRole(RoleDto roleDto) {
if (ToolUtil.isOneEmpty(roleDto, roleDto.getName(), roleDto.getPid(), roleDto.getDescription())) {
throw new RequestEmptyException();
}
Role old = this.getById(roleDto.getRoleId());
BeanUtil.copyProperties(roleDto, old);
this.updateById(old);
}
/**
* 设置某个角色的权限
*
* @param roleId 角色id
* @param ids 权限的id
* @date 2017年2月13日 下午8:26:53
*/
@Transactional(rollbackFor = Exception.class)
public void setAuthority(Long roleId, String ids) {
// 删除该角色所有的权限
this.roleMapper.deleteRolesById(roleId);
// 添加新的权限
for (Long id : Convert.toLongArray(ids.split(","))) {
Relation relation = new Relation();
relation.setRoleId(roleId);
relation.setMenuId(id);
this.relationMapper.insert(relation);
}
// 刷新当前用户的权限
userService.refreshCurrentUser();
}
/**
* 删除角色
*
* @author stylefeng
* @Date 2017/5/5 22:24
*/
@Transactional(rollbackFor = Exception.class)
public void delRoleById(Long roleId) {
if (ToolUtil.isEmpty(roleId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
}
//不能删除超级管理员角色
if (roleId.equals(Const.ADMIN_ROLE_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
//缓存被删除的角色名称
LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));
//删除角色
this.roleMapper.deleteById(roleId);
//删除该角色所有的权限
this.roleMapper.deleteRolesById(roleId);
}
/**
* 根据条件查询角色列表
*
* @return
* @date 2017年2月12日 下午9:14:34
*/
public Page