Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / WarnTeamBossServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.pc.dao.WarnTeamBossMapper;
import com.newfiber.api.pc.model.entity.TaskOrgCount;
import com.newfiber.api.pc.model.entity.UserInfoVo;
import com.newfiber.api.pc.model.entity.WarnTeamBoss;
import com.newfiber.api.pc.model.entity.WarnTeamSup;
import com.newfiber.api.pc.model.vo.UserTree;
import com.newfiber.api.pc.service.WarnTeamBossService;
import com.newfiber.api.pc.service.WarnTeamSupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WarnTeamBossServiceImpl
 * @Description TODO
 * @Author 张鸿志
 * @Date sj2 19:20
 * Version 1.0
 **/
@Service
public class WarnTeamBossServiceImpl extends ServiceImpl<WarnTeamBossMapper, WarnTeamBoss> implements WarnTeamBossService {

    @Autowired
    private WarnTeamBossService warnTeamBossService;

    @Autowired
    private WarnTeamSupService warnTeamSupService;

    @Autowired
    private WarnTeamBossMapper warnTeamBossMapper;

    @Override
    public Set<UserInfoVo> allteamUser() {
        List<UserInfoVo> userInfoVos = new ArrayList<>();
        //应急队伍人员
        List<WarnTeamBoss> warnTeamBoss = warnTeamBossService.selectList(null);
        if(!warnTeamBoss.isEmpty()){
            warnTeamBoss.stream().forEach(e->{
                UserInfoVo vo = new UserInfoVo();
                vo.setUserNo(e.getUserNo());
                vo.setUserName(e.getName());
                vo.setPhoneNumber(e.getPhone());
                userInfoVos.add(vo);
            });
        }
        //应急队伍人员
        List<WarnTeamSup> warnTeamSups = warnTeamSupService.selectList(null);
        if(!warnTeamSups.isEmpty()){
            warnTeamSups.stream().forEach(e->{
                UserInfoVo vo1 = new UserInfoVo();
                vo1.setUserNo(e.getUserNo());
                vo1.setPhoneNumber(e.getSupPhone());
                vo1.setUserName(e.getSupName());
                userInfoVos.add(vo1);
            });
        }
        Set<UserInfoVo> result = new HashSet<>(userInfoVos);
        return result;
    }

    @Override
    public List<UserTree> userTree() {
        List<UserTree> root = new ArrayList<>();
        List<UserTree> result = new ArrayList<>();
        //应急队伍人员
        List<UserTree> child = new ArrayList<>();
        List<WarnTeamBoss> warnTeamBoss = warnTeamBossService.selectList(null);
        if(!warnTeamBoss.isEmpty()){
            Map<Integer, List<WarnTeamBoss>> listMap = warnTeamBoss.stream().collect(Collectors.groupingBy(WarnTeamBoss::getOrgType));
            for (Integer integer : listMap.keySet()) {
                WarnTeamBoss e = listMap.get(integer).get(0);
                UserTree vo = new UserTree();
                vo.setId(e.getId());
                vo.setUserName(e.getOrgName());
                vo.setUnit(e.getOrgName());
                vo.setUserType(3);
                vo.setOrgType(e.getOrgType());
                root.add(vo);
            }
            warnTeamBoss.stream().forEach(e->{
                UserTree vo = new UserTree();
                vo.setId(e.getId());
                vo.setUserName(e.getUnit());
                vo.setUnit(e.getUnit());
                vo.setUserType(3);
                vo.setType(e.getOrgType());
                vo.setOrgType(e.getOrgType());
                vo.setOrgName(e.getOrgName());
                result.add(vo);
                UserTree vo1 = new UserTree();
                vo1.setId(e.getId());
                vo1.setUserNo(e.getUserNo());
                vo1.setUserName(e.getName());
                vo1.setUnit(e.getUnit());
                vo1.setJob(e.getJob());
                vo1.setPhone(e.getPhone());
                vo1.setUserType(1);
                vo.setType(0);
                vo1.setOrgType(e.getOrgType());
                vo1.setOrgName(e.getOrgName());
                child.add(vo1);
            });
        }

        List<WarnTeamSup> warnTeamSups = warnTeamSupService.selectList(null);
        if(!warnTeamSups.isEmpty()){
            warnTeamSups.stream().forEach(e->{
                UserTree vo = new UserTree();
                vo.setId(e.getBossId());
                vo.setUserNo(e.getUserNo());
                vo.setUserName(e.getSupName());
                vo.setJob(e.getSupJob());
                vo.setPhone(e.getSupPhone());
                vo.setUserType(e.getUserType());
                vo.setType(0);
                vo.setOrgType(e.getOrgType());
                vo.setOrgName(e.getOrgName());
                child.add(vo);
            });
        }
        //组装普通成员
        setChild(result, child);
        //组装root
        setRoot(root, result);
        return root;
    }

    @Override
    public List<UserTree> userSearchTree(String searchStr) {
        List<UserTree> root = new ArrayList<>();
        List<UserTree> result = new ArrayList<>();
        List<UserTree> child = new ArrayList<>();
        List<WarnTeamSup> warnTeamSups = new ArrayList<>();
        List<WarnTeamBoss> warnTeamBoss = warnTeamBossMapper.userSearchTree(searchStr);
        if(!warnTeamBoss.isEmpty()) {
            Map<Integer, List<WarnTeamBoss>> listMap = warnTeamBoss.stream().collect(Collectors.groupingBy(WarnTeamBoss::getOrgType));
            for (Integer integer : listMap.keySet()) {
                WarnTeamBoss e = listMap.get(integer).get(0);
                UserTree vo = new UserTree();
                vo.setId(e.getId());
                vo.setUserName(e.getOrgName());
                vo.setUnit(e.getOrgName());
                vo.setUserType(3);
                vo.setOrgType(e.getOrgType());
                root.add(vo);
            }
            warnTeamBoss.stream().forEach(e->{
                if(e!=null && e.getWarnTeamSups()!=null && e.getWarnTeamSups().size()>0){
                    List<WarnTeamSup> supList = e.getWarnTeamSups();
                    for (WarnTeamSup warnTeamSup : supList) {
                        if(warnTeamSup!=null && warnTeamSup.getSupId()!=null){
                            warnTeamSups.add(warnTeamSup);
                        }
                    }
                }
                UserTree vo = new UserTree();
                vo.setId(e.getId());
                vo.setUserName(e.getUnit());
                vo.setUnit(e.getUnit());
                vo.setUserType(3);
                vo.setType(e.getOrgType());
                vo.setOrgType(e.getOrgType());
                vo.setOrgName(e.getOrgName());
                result.add(vo);
                UserTree vo1 = new UserTree();
                vo1.setId(e.getId());
                vo1.setUserNo(e.getUserNo());
                vo1.setUserName(e.getName());
                vo1.setUnit(e.getUnit());
                vo1.setJob(e.getJob());
                vo1.setPhone(e.getPhone());
                vo1.setUserType(1);
                vo.setType(0);
                vo1.setOrgType(e.getOrgType());
                vo1.setOrgName(e.getOrgName());
                child.add(vo1);
            });
        }
        if(!warnTeamSups.isEmpty()){
            warnTeamSups.stream().forEach(e->{
                UserTree vo = new UserTree();
                vo.setId(e.getBossId());
                vo.setUserNo(e.getUserNo());
                vo.setUserName(e.getSupName());
                vo.setJob(e.getSupJob());
                vo.setPhone(e.getSupPhone());
                vo.setUserType(e.getUserType());
                vo.setType(0);
                vo.setOrgType(e.getOrgType());
                vo.setOrgName(e.getOrgName());
                child.add(vo);
            });
        }
        //组装普通成员
        setChild(result, child);
        //组装root
        setRoot(root, result);
        return root;
    }

    @Override
    public List<TaskOrgCount> taskOrgType() {
        return warnTeamBossMapper.taskOrgType();
    }

    @Override
    public Boolean isExist(String userNo) {
        List<WarnTeamBoss> bosses =  warnTeamBossMapper.isExist(userNo);
        if(bosses!=null && bosses.size()>0){
            return true;
        }
        return false;
    }

    private void setChild(List<UserTree> result, List<UserTree> child) {
        if(child!=null && child.size()>0){
            Map<Integer, List<UserTree>> listMap = child.stream().collect(Collectors.groupingBy(UserTree::getId));
            for (UserTree userTree : result) {
                if(listMap.get(userTree.getId())!=null){
                    List<UserTree> chiled = listMap.get(userTree.getId());
                    for (UserTree tree : chiled) {
                        tree.setUnit(userTree.getUnit());
                    }
                    userTree.setChildren(chiled);
                }
            }
        }
    }

    private void setRoot(List<UserTree> result, List<UserTree> child) {
        if(child!=null && child.size()>0){
            Map<Integer, List<UserTree>> listMap = child.stream().collect(Collectors.groupingBy(UserTree::getOrgType));
            for (UserTree userTree : result) {
                if(listMap.get(userTree.getOrgType())!=null){
                    List<UserTree> chiled = listMap.get(userTree.getOrgType());
                    for (UserTree tree : chiled) {
                        tree.setUnit(userTree.getUnit());
                    }
                    userTree.setChildren(chiled);
                }
            }
        }
    }
}