DepartmentDictServiceImpl.java 11.7 KB
package com.subsidy.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.subsidy.common.exception.HttpException;
import com.subsidy.dto.department.GetDepartmentMembersDTO;
import com.subsidy.dto.department.GetDepartmentsVO;
import com.subsidy.mapper.MemberDepartmentMappingMapper;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.model.AdministerDO;
import com.subsidy.model.DepartmentDictDO;
import com.subsidy.mapper.DepartmentDictMapper;
import com.subsidy.model.MemberDO;
import com.subsidy.model.MemberDepartmentMappingDO;
import com.subsidy.service.DepartmentDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.department.GetMembersVO;
import com.subsidy.vo.member.MemberVodVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
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;

    @Autowired
    private MemberMapper memberMapper;

    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())
            .eq(MemberDepartmentMappingDO::getMemberStatus,1));
            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) {

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

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

        List<String> depNames = new ArrayList<>();

        StringBuilder fullName = new StringBuilder();
        //fullName 遍历生成全名称
        do {
            depNames.add("/" + departmentDictDO.getDepartmentName());
            departmentDictDO = this.baseMapper.selectById(departmentDictDO.getParentId());
        } while (null != departmentDictDO.getParentId());

        depNames.add(departmentDictDO.getDepartmentName());

        for (int i = depNames.size() - 1; i >= 0; i--) {
            fullName.append(depNames.get(i));
        }
        departmentDictDO1.setFullName(fullName.toString());
        this.baseMapper.insert(departmentDictDO1);

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

    public IPage getDepartmentMembers(GetDepartmentMembersDTO getDepartmentMembersDTO) {

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

        if (null != getDepartmentMembersDTO.getDepartmentId()) {
            //查询某个部门
            IPage iPage = memberMapper.getMembers(pager, getDepartmentMembersDTO.getDepartmentId(), getDepartmentMembersDTO.getUserName(), null, getDepartmentMembersDTO.getStartDate(), getDepartmentMembersDTO.getEndDate());
            return iPage;
        } else {
            //查询整个公司
            IPage iPage = memberMapper.manageMember(pager, getDepartmentMembersDTO.getCompanyId(), getDepartmentMembersDTO.getUserName(), getDepartmentMembersDTO.getStartDate(), getDepartmentMembersDTO.getEndDate());
            return iPage;
        }
    }

    public List<GetMembersVO> getMembers(GetDepartmentMembersDTO getDepartmentMembersDTO) {

        List<GetMembersVO> getMembersVOS = new ArrayList<>();

//        GetMembersVO getMembersVO = new GetMembersVO();

        //找到父节点
        DepartmentDictDO departmentDictDO = this.baseMapper.selectOne(new QueryWrapper<DepartmentDictDO>()
                .lambda()
                .eq(DepartmentDictDO::getCompanyId, getDepartmentMembersDTO.getCompanyId())
                .isNull(DepartmentDictDO::getParentId));
//        BeanUtils.copyProperties(departmentDictDO,getMembersVO);

//        List<DepartmentDictDO> departmentDictDOS = new ArrayList<>();
//        departmentDictDOS.add(departmentDictDO);

//        for (DepartmentDictDO dpt : departmentDictDOS){
        GetMembersVO gmv = new GetMembersVO();
        BeanUtils.copyProperties(departmentDictDO, gmv);
//            if (null!=departmentDictDO.getParentId()){
//                DepartmentDictDO departmentDictDO1 =this.baseMapper.selectById(departmentDictDO.getParentId());
//                gmv.setParentDepartmentName(departmentDictDO1.getDepartmentName());
//            }
        gmv.setChildren(getChildNodes(gmv));

        //父节点人员
        List<MemberDO> memberDOS = memberMapper.getMemberWithoutPage(departmentDictDO.getId(), getDepartmentMembersDTO.getUserName(), null, null, null);
        if (memberDOS.size() > 0) {
            gmv.setMemberDOS(memberDOS);
        }
//            if (dpt.getParentId()!=null){
//                //部门人员
//                List<MemberDO> memberDOS1 = memberMapper.getMemberWithoutPage(dpt.getId(),getMembersVO.getUserName(),null,null,null);
//                gmv.setMemberDOS(memberDOS1);
//                getMembersVOS.add(gmv);
//            }else {
        //公司人员
//                List<MemberDO> memberDOS = memberMapper.selectList(new QueryWrapper<MemberDO>()
//                .lambda()
//                .eq(MemberDO::getCompanyId,getDepartmentMembersDTO.getCompanyId()));
//                gmv.setMemberDOS(memberDOS);
//                getMembersVOS.add(gmv);
//            }
        getMembersVOS.add(gmv);
//        }
        return getMembersVOS;
    }


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


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

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

        //递归
        for (DepartmentDictDO dd : departmentDictDOS) {
            GetMembersVO gmv = new GetMembersVO();
            BeanUtils.copyProperties(dd, gmv);
            if (null != dd.getParentId()) {
                DepartmentDictDO departmentDictDO = this.baseMapper.selectById(dd.getParentId());
                gmv.setParentDepartmentName(departmentDictDO.getDepartmentName());
            }
            gmv.setChildren(getChildNodes(gmv));


            //部门人员
            List<MemberDO> memberDOS = memberMapper.getMemberWithoutPage(dd.getId(), getMembersVO.getUserName(), null, null, null);
            if (memberDOS.size() > 0) {
                gmv.setMemberDOS(memberDOS);
            }
            getAllDepartmentVOS.add(gmv);
        }
        if (departmentDictDOS.size() == 0) {
            return null;
        }

        return getAllDepartmentVOS;
    }
}