package cn.stylefeng.guns.sys.modular.rest.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.rest.entity.RestRelation;
import cn.stylefeng.guns.sys.modular.rest.entity.RestRole;
import cn.stylefeng.guns.sys.modular.rest.mapper.RestRelationMapper;
import cn.stylefeng.guns.sys.modular.rest.mapper.RestRoleMapper;
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.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 RestRoleService extends ServiceImpl {
@Resource
private RestRoleMapper restRoleMapper;
@Resource
private RestRelationMapper restRelationMapper;
@Resource
private RestUserService restUserService;
/**
* 添加角色
*
* @author fengshuonan
* @Date 2018/12/23 6:40 PM
*/
@Transactional(rollbackFor = Exception.class)
public void addRole(RestRole 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();
}
RestRole 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.restRoleMapper.deleteRolesById(roleId);
// 添加新的权限
for (Long id : Convert.toLongArray(ids.split(","))) {
RestRelation relation = new RestRelation();
relation.setRoleId(roleId);
relation.setMenuId(id);
this.restRelationMapper.insert(relation);
}
// 刷新当前用户的权限
restUserService.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.restRoleMapper.deleteById(roleId);
//删除该角色所有的权限
this.restRoleMapper.deleteRolesById(roleId);
}
/**
* 根据条件查询角色列表
*
* @return
* @date 2017年2月12日 下午9:14:34
*/
public Page