DepartmentDictServiceImpl.java 12.1 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.common.exception.HttpException;
import com.meishu.dto.department.BatchOprDTO;
import com.meishu.dto.department.GetAllDepartmentVO;
import com.meishu.dto.department.GetTeachersDTO;
import com.meishu.dto.subject.GetSubjectTreeDTO;
import com.meishu.mapper.AdministerDepartmentMappingMapper;
import com.meishu.mapper.AdministerMapper;
import com.meishu.model.*;
import com.meishu.mapper.DepartmentDictMapper;
import com.meishu.service.DepartmentDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.administer.GetTeachersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门字典表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-04-25
 */
@Service
public class DepartmentDictServiceImpl extends ServiceImpl<DepartmentDictMapper, DepartmentDictDO> implements DepartmentDictService {

    @Autowired
    private AdministerMapper administerMapper;

    @Autowired
    private AdministerDepartmentMappingMapper administerDepartmentMappingMapper;

    public List<GetAllDepartmentVO> getDepartmentInfo() {

        //返回结果
        List<GetAllDepartmentVO> getAllDepartmentVOS = new ArrayList<>();

        //获取所有的父节点
        List<DepartmentDictDO> parentNodes = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .isNull(DepartmentDictDO::getParentId));

        //遍历父节点
        for (DepartmentDictDO departmentDictDO : parentNodes) {
            GetAllDepartmentVO getAllDepartmentVO = new GetAllDepartmentVO();
            BeanUtils.copyProperties(departmentDictDO, getAllDepartmentVO);
            if (null != departmentDictDO.getParentId()) {
                DepartmentDictDO departmentDictDO1 = this.baseMapper.selectById(departmentDictDO.getParentId());
                getAllDepartmentVO.setParentDepartmentName(departmentDictDO1.getDepartmentName());
            }
            getAllDepartmentVO.setChildren(getChildNodes(getAllDepartmentVO));
            AdministerDO administerDO = administerMapper.selectById(getAllDepartmentVO.getDepartmentLeaderId());
            if (null != administerDO) {
                List<AdministerDepartmentMappingDO> administerDepartmentMappingDOS = administerDepartmentMappingMapper.selectList(new QueryWrapper<AdministerDepartmentMappingDO>()
                        .lambda()
                        .eq(AdministerDepartmentMappingDO::getAdministerId, administerDO.getId()));
                getAllDepartmentVO.setAdministerDepartmentMappingDOS(administerDepartmentMappingDOS);
                getAllDepartmentVO.setAdministerId(administerDO.getId());
                getAllDepartmentVO.setUserName(administerDO.getUserName());
                getAllDepartmentVO.setImg(administerDO.getImg());
            }
            Integer count = administerDepartmentMappingMapper.selectCount(new QueryWrapper<AdministerDepartmentMappingDO>()
                    .lambda()
                    .eq(AdministerDepartmentMappingDO::getDepartmentId, departmentDictDO.getId()));
            getAllDepartmentVO.setCount(count);
            //部门人员
            List<AdministerDO> administerDOS = administerMapper.getAdministerByDepartmentId(departmentDictDO.getId());
            getAllDepartmentVO.setAdministerDOS(administerDOS);

            getAllDepartmentVOS.add(getAllDepartmentVO);
        }
        return getAllDepartmentVOS;
    }

    public List<DepartmentDictDO> getDepartmentList(GetSubjectTreeDTO getSubjectTreeDTO) {

        //查看角色所在部门
        List<DepartmentDictDO> departmentDictDOS = administerMapper.getDepartmentByAdministerId(getSubjectTreeDTO.getUserId());

        List<Long> ids = new ArrayList<>();
        for (DepartmentDictDO departmentDictDO : departmentDictDOS) {
            ids.add(departmentDictDO.getId());
        }

        List<DepartmentDictDO> parentNodes = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getParentId, 1)
                .in(DepartmentDictDO::getId, ids));

        for (DepartmentDictDO departmentDictDO : parentNodes) {
            departmentDictDO.setChildren(getChildNodes(departmentDictDO));
        }
        return parentNodes;
    }

    public String addDepartment(DepartmentDictDO departmentDictDO) {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getDepartmentName, departmentDictDO.getDepartmentName()));

        if (count > 0) {
            throw new HttpException(20001);
        }

        this.baseMapper.insert(departmentDictDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public IPage<GetTeachersVO> getTeachers(GetTeachersDTO getTeachersDTO) {

        Page pager = new Page(getTeachersDTO.getPageNum(), getTeachersDTO.getPageSize());

        List<Long> userIds = new ArrayList<>();

        //查看该角色所在部门
        if (null == getTeachersDTO.getDepartmentId()){
            List<DepartmentDictDO> departmentDictDOS = administerMapper.getDepartmentByAdministerId(getTeachersDTO.getUserId());
            for (DepartmentDictDO departmentDictDO : departmentDictDOS){
                userIds.add(departmentDictDO.getId());
            }
        }

        IPage<GetTeachersVO> getTeachersVOIPage = administerMapper.getDepartmentTeachers(pager, getTeachersDTO.getDepartmentId(), getTeachersDTO.getCampusId(), getTeachersDTO.getUserName(),userIds);
        List<GetTeachersVO> getTeachersVOS = getTeachersVOIPage.getRecords();
        for (GetTeachersVO getTeachersVO : getTeachersVOS) {
//            List<CampusDictDO> campusDictDOS = administerMapper.getTeacherCampus(getTeachersVO.getId());
//            getTeachersVO.setCampusDictDOS(campusDictDOS);
            //部门
            List<DepartmentDictDO> departmentDictDOS = administerMapper.getDepartmentByAdministerId(getTeachersVO.getId());
            getTeachersVO.setDepartmentDictDOS(departmentDictDOS);

            //科目
            List<KnowledgeSubjectDictDO> knowledgeSubjectDicts = administerMapper.getTeacherSubjects(getTeachersVO.getId());
            getTeachersVO.setKnowledgeSubjectDicts(knowledgeSubjectDicts);

            //角色
            List<RoleDictDO> roleDictDOS = administerMapper.getTeacherRoles(getTeachersVO.getId());
            getTeachersVO.setRoleDictDOS(roleDictDOS);

            //多个在读班级
            List<SubjectDictDO> subjectDictDOS = administerMapper.getTeacherClasses(getTeachersVO.getId());
            getTeachersVO.setSubjectDictDOS(subjectDictDOS);

        }
        return getTeachersVOIPage;
    }

    public String batchOpr(BatchOprDTO batchOprDTO) {

        List<Long> longs = batchOprDTO.getIds();
        for (Long lg : longs) {
            DepartmentDictDO departmentDictDO = this.baseMapper.selectById(lg);
            if (null != batchOprDTO.getDepartmentLeaderId()) {
                departmentDictDO.setDepartmentLeaderId(batchOprDTO.getDepartmentLeaderId());
            }
            if (null != batchOprDTO.getParentId()) {
                departmentDictDO.setParentId(batchOprDTO.getParentId());
            }
            this.baseMapper.updateById(departmentDictDO);
        }
        return ConstantUtils.SUCCESS_UPDATE;
    }


    public String deleteDepartment(BatchOprDTO batchOprDTO) {

        List<Long> longs = batchOprDTO.getIds();

        for (Long lg : longs) {
            List<DepartmentDictDO> departmentDictDOS = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                    .lambda()
                    .eq(DepartmentDictDO::getParentId, lg)
            );

            for (DepartmentDictDO departmentDictDO1 : departmentDictDOS) {
                deleteChildNode(departmentDictDO1);
            }
            this.baseMapper.deleteById(lg);
        }

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateDepartment(DepartmentDictDO departmentDictDO) {
        this.baseMapper.updateById(departmentDictDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    void deleteChildNode(DepartmentDictDO departmentDictDO) {

        List<DepartmentDictDO> departmentDictDOS = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getParentId, departmentDictDO.getId()));

        for (DepartmentDictDO departmentDictDO1 : departmentDictDOS) {
            deleteChildNode(departmentDictDO1);
        }
        this.baseMapper.deleteById(departmentDictDO.getId());
    }


    /**
     * 递归  带上部门总数,部门负责人等参数
     *
     * @param getAllDepartmentVO 父节点查找该节点的children
     * @return 子节点
     */
    public List<GetAllDepartmentVO> getChildNodes(GetAllDepartmentVO getAllDepartmentVO) {

        List<GetAllDepartmentVO> getAllDepartmentVOS = new ArrayList<>();

        //查询该节点的子节点
        List<DepartmentDictDO> departmentDictDOS = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getParentId, getAllDepartmentVO.getId()));

        //递归
        for (DepartmentDictDO dd : departmentDictDOS) {
            GetAllDepartmentVO getAllDepartmentVO1 = new GetAllDepartmentVO();
            BeanUtils.copyProperties(dd, getAllDepartmentVO1);
            if (null != dd.getParentId()) {
                DepartmentDictDO departmentDictDO = this.baseMapper.selectById(dd.getParentId());
                getAllDepartmentVO1.setParentDepartmentName(departmentDictDO.getDepartmentName());
            }
            getAllDepartmentVO1.setChildren(getChildNodes(getAllDepartmentVO1));
            AdministerDO administerDO = administerMapper.selectById(getAllDepartmentVO1.getDepartmentLeaderId());
            if (null != administerDO) {
                List<AdministerDepartmentMappingDO> administerDepartmentMappingDOS = administerDepartmentMappingMapper.selectList(new QueryWrapper<AdministerDepartmentMappingDO>()
                        .lambda()
                        .eq(AdministerDepartmentMappingDO::getAdministerId, administerDO.getId()));
                getAllDepartmentVO1.setAdministerDepartmentMappingDOS(administerDepartmentMappingDOS);
                getAllDepartmentVO1.setUserName(administerDO.getUserName());
                getAllDepartmentVO1.setImg(administerDO.getImg());
                getAllDepartmentVO1.setAdministerId(administerDO.getId());
            }
            Integer count = administerDepartmentMappingMapper.selectCount(new QueryWrapper<AdministerDepartmentMappingDO>()
                    .lambda()
                    .eq(AdministerDepartmentMappingDO::getDepartmentId, dd.getId()));
            getAllDepartmentVO1.setCount(count);
            //部门人员
            List<AdministerDO> administerDOS = administerMapper.getAdministerByDepartmentId(dd.getId());
            getAllDepartmentVO1.setAdministerDOS(administerDOS);
            getAllDepartmentVOS.add(getAllDepartmentVO1);
        }
        if (departmentDictDOS.size() == 0) {
            return null;
        }

        return getAllDepartmentVOS;
    }

    /**
     * 递归部门列表  不包含老师信息和人数信息
     *
     * @param departmentDictDO
     * @return
     */
    public List<DepartmentDictDO> getChildNodes(DepartmentDictDO departmentDictDO) {

        List<DepartmentDictDO> departmentDictDOS = this.baseMapper.selectList(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getParentId, departmentDictDO.getId()));

        for (DepartmentDictDO departmentDictDO1 : departmentDictDOS) {
            departmentDictDO1.setChildren(getChildNodes(departmentDictDO1));
        }

        if (departmentDictDOS.size() == 0) {
            return null;
        }
        return departmentDictDOS;
    }


}