DepartmentDictServiceImpl.java 6.02 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.department.GetDepartmentsVO;
import com.subsidy.mapper.MemberDepartmentMappingMapper;
import com.subsidy.model.DepartmentDictDO;
import com.subsidy.mapper.DepartmentDictMapper;
import com.subsidy.model.MemberDepartmentMappingDO;
import com.subsidy.service.DepartmentDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
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 DengMin
 * @since 2021-10-11
 */
@Service
public class DepartmentDictServiceImpl extends ServiceImpl<DepartmentDictMapper, DepartmentDictDO> implements DepartmentDictService {

    @Autowired
    private MemberDepartmentMappingMapper memberDepartmentMappingMapper;

    public List<GetDepartmentsVO> getDepartments(DepartmentDictDO departmentDictDO) {

        //返回结果
        List<GetDepartmentsVO> getDepartmentsVOS = new ArrayList<>();

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

        for (DepartmentDictDO dd : parentNodes) {

            GetDepartmentsVO getDepartmentsVO = new GetDepartmentsVO();
            BeanUtils.copyProperties(dd,getDepartmentsVO);
            Integer count = memberDepartmentMappingMapper.selectCount(new QueryWrapper<MemberDepartmentMappingDO>()
            .lambda()
            .eq(MemberDepartmentMappingDO::getDepartmentId,dd.getId()));
            getDepartmentsVO.setCnt(count);
            getDepartmentsVO.setChildren(getChildNodes(getDepartmentsVO));
            DepartmentDictDO departmentDictDO1 = this.baseMapper.selectById(dd.getParentId());
            if (null != departmentDictDO1){
                getDepartmentsVO.setParentDepartmentName(departmentDictDO1.getDepartmentName());
            }
            getDepartmentsVOS.add(getDepartmentsVO);
        }

        return getDepartmentsVOS;
    }

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

        List<GetDepartmentsVO> getDepartmentsVOS = new ArrayList<>();

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

        for (DepartmentDictDO departmentDictDO1 : departmentDictDOS) {

            GetDepartmentsVO getDepartmentsVO = new GetDepartmentsVO();
            BeanUtils.copyProperties(departmentDictDO1,getDepartmentsVO);
            getDepartmentsVO.setChildren(getChildNodes(getDepartmentsVO));

            Integer count = memberDepartmentMappingMapper.selectCount(new QueryWrapper<MemberDepartmentMappingDO>()
            .lambda()
            .eq(MemberDepartmentMappingDO::getDepartmentId,getDepartmentsVO.getId()));
            getDepartmentsVO.setCnt(count);
            DepartmentDictDO departmentDictDO2 = this.baseMapper.selectById(departmentDictDO1.getParentId());
            if (null !=  departmentDictDO2){
                getDepartmentsVO.setParentDepartmentName(departmentDictDO2.getDepartmentName());
            }
            getDepartmentsVOS.add(getDepartmentsVO);
        }

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

    public String addDepartment(DepartmentDictDO departmentDictDO) {

        Integer counter = this.baseMapper.selectCount(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getCompanyId, departmentDictDO.getCompanyId())
                .eq(DepartmentDictDO::getDepartmentName, departmentDictDO.getDepartmentName()));

        if (counter > 0) {
            throw new HttpException(30001);
        }

        this.baseMapper.insert(departmentDictDO);

        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteDepartment(DepartmentDictDO departmentDictDO) {
        this.baseMapper.deleteById(departmentDictDO.getId());

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

        for (DepartmentDictDO departmentDictDO1:departmentDictDOS){
            deleteChildNode(departmentDictDO1);
        }

        return ConstantUtils.DELETE_SUCCESS;
    }

    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());
    }

    public String updateDepartment(DepartmentDictDO departmentDictDO) {

        Integer counter = this.baseMapper.selectCount(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getCompanyId, departmentDictDO.getCompanyId())
                .eq(DepartmentDictDO::getDepartmentName, departmentDictDO.getDepartmentName())
                .ne(DepartmentDictDO::getId, departmentDictDO.getId()));

        if (counter > 0) {
            throw new HttpException(30001);
        }

        this.baseMapper.updateById(departmentDictDO);
        return ConstantUtils.SET_SUCCESS;
    }

}