Blame view

DepartmentDictServiceImpl.java 6.02 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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;
    }

}