RoleDictServiceImpl.java 5.89 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meishu.dto.role.AddPermissionDTO;
import com.meishu.dto.role.DeletePermissionDTO;
import com.meishu.dto.role.GetRoleTeachersDTO;
import com.meishu.dto.role.RoleTeacherDTO;
import com.meishu.dto.rule.GetRuleReportDTO;
import com.meishu.mapper.*;
import com.meishu.model.*;
import com.meishu.service.RoleDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.role.GetAllRolesVO;
import com.meishu.vo.role.GetRoleTeachersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-04-26
 */
@Service
public class RoleDictServiceImpl extends ServiceImpl<RoleDictMapper, RoleDictDO> implements RoleDictService {

    @Autowired
    private RoleAdministerMappingMapper roleAdministerMappingMapper;

    @Autowired
    private RolePermissionMappingMapper rolePermissionMappingMapper;

    @Autowired
    private AdministerMapper administerMapper;

    public List<GetAllRolesVO> getAllRoles(){

        List<GetAllRolesVO> getAllRolesVOS = new ArrayList<>();

        List<RoleDictDO> roleDictDOS = this.baseMapper.selectList(null);
        for (RoleDictDO roleDictDO : roleDictDOS){
            GetAllRolesVO getAllRolesVO = new GetAllRolesVO();
            List<PermissionsDictDO> list = rolePermissionMappingMapper.getRolePermission(roleDictDO.getId());
            //树形结构
            List<PermissionsDictDO> treeList = new ArrayList();
            if (list != null) {
                list.forEach(permission -> {
                    if (permission.getParentId() == null) {
                        treeList.add(permission);
                    }

                    list.forEach(p -> {
                        if (null != p.getParentId() && p.getParentId().equals(permission.getId())) {
                            if (permission.getChildren() == null) {
                                permission.setChildren(new ArrayList<>());
                            }
                            permission.getChildren().add(p);
                        }
                    });
                });
            }

            BeanUtils.copyProperties(roleDictDO,getAllRolesVO);
            getAllRolesVO.setPermissionsDictDOS(treeList);
            getAllRolesVOS.add(getAllRolesVO);
        }
        return getAllRolesVOS;
    }

    public IPage<GetRoleTeachersVO> getRoleTeachers(GetRoleTeachersDTO getRoleTeachersDTO){
        Page pager = new Page(getRoleTeachersDTO.getPageNum(), getRoleTeachersDTO.getPageSize());
        IPage<GetRoleTeachersVO> getRoleTeachersVOIPage = this.baseMapper.getRoleTeachers(pager,getRoleTeachersDTO.getRoleId(),getRoleTeachersDTO.getUserName());
        List<GetRoleTeachersVO> getRoleTeachersVOS = getRoleTeachersVOIPage.getRecords();
        for (GetRoleTeachersVO getRoleTeachersVO : getRoleTeachersVOS){
            List<DepartmentDictDO> departmentDictDOS  = administerMapper.getDepartmentByAdministerId(getRoleTeachersVO.getAdministerId());
            getRoleTeachersVO.setDepartmentDictDOS(departmentDictDOS);
        }
        return getRoleTeachersVOIPage;
    }

    public String removeTeacher(RoleTeacherDTO roleTeacherDTO){

        roleAdministerMappingMapper.delete(new QueryWrapper<RoleAdministerMappingDO>()
        .lambda()
        .eq(RoleAdministerMappingDO::getRoleId,roleTeacherDTO.getRoleId())
        .in(RoleAdministerMappingDO::getId,roleTeacherDTO.getIds()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String roleTeacher(RoleTeacherDTO roleTeacherDTO){

        roleAdministerMappingMapper.delete(new QueryWrapper<RoleAdministerMappingDO>()
        .lambda()
        .eq(RoleAdministerMappingDO::getRoleId,roleTeacherDTO.getRoleId()));

        List<Long> longs = roleTeacherDTO.getIds();
        for (Long lg : longs){
            RoleAdministerMappingDO roleAdministerMappingDO = new RoleAdministerMappingDO();
            roleAdministerMappingDO.setAdministerId(lg);
            roleAdministerMappingDO.setRoleId(roleTeacherDTO.getRoleId());
            roleAdministerMappingMapper.insert(roleAdministerMappingDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String addPermission(AddPermissionDTO addPermissionDTO){
        List<Long> longs = addPermissionDTO.getPermissionIds();

        rolePermissionMappingMapper.delete(new QueryWrapper<RolePermissionMappingDO>()
        .lambda()
        .eq(RolePermissionMappingDO::getRoleId,addPermissionDTO.getRoleId()));

        for (Long lg : longs){
            RolePermissionMappingDO  rolePermissionMappingDO = new RolePermissionMappingDO();
            rolePermissionMappingDO.setRoleId(addPermissionDTO.getRoleId());
            rolePermissionMappingDO.setPermissionId(lg);
            rolePermissionMappingMapper.insert(rolePermissionMappingDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String deletePermission(DeletePermissionDTO deletePermissionDTO){

        List<Long> longs = deletePermissionDTO.getRoleIds();
        for (Long lg : longs){
            rolePermissionMappingMapper.delete(new QueryWrapper<RolePermissionMappingDO>()
            .lambda()
            .eq(RolePermissionMappingDO::getPermissionId,deletePermissionDTO.getPermissionId())
            .eq(RolePermissionMappingDO::getRoleId,lg));
        }
        return ConstantUtils.DELETE_SUCCESS;
    }

}