Blame view

DepartmentDictServiceImpl.java 11.6 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
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.mapper.MemberDepartmentMappingMapper;
import com.subsidy.mapper.MemberMapper;
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.GetDepartmentsVO;
import com.subsidy.vo.department.GetMembersVO;
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 2024-01-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;
    }

}