package com.ho.user.service.impl;

import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.OrderDept;
import com.ho.user.api.entity.SysDept;
import com.ho.user.api.entity.SysUser;
import com.ho.user.service.DeptOrderUserService;
import com.ho.user.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author fancl
 * @desc:
 * @date 2023/2/13
 */
@Service
@Slf4j
public class DeptOrderUserServiceImpl implements DeptOrderUserService {

    @Autowired
    DeptService deptService;

    @Override
    public OrderDept getDeptUser(SysDept topDept, List<SysUser> sysUsers) {

        //以下是人的集合
        Map<Integer, List<OrderDept>> userMap = sysUsers.stream().map(user -> {
            OrderDept order = new OrderDept();
            //
            String deptId = user.getDeptId().toString();
            order.setId(user.getId());
            order.setDeptId(user.getDeptId());
            order.setType("person");
            order.setUserId(user.getId());
            order.setName(user.getRealName());
            return order;
        }).collect(Collectors.groupingBy(OrderDept::getDeptId));
        //过滤部门
        List<Integer> deptIdList = new ArrayList<>();
        for (SysUser userDatum : sysUsers) {
            if (!deptIdList.contains(userDatum.getDeptId())) {
                deptIdList.add(userDatum.getDeptId());
            }
        }

        //根据顶级部门(集团)查到所有有效的部门
        //String relationCode = topDept.getRelationCode();
        String enableStatus = "enableStatus";
        List<OrderDept> orderDepts = new ArrayList<>();
        List<SysDept> depts =new ArrayList<>();
        depts.add(topDept);
        List<SysDept> sysDepts = deptService.getSubsidiaryDepts(depts, CommonConstant.DEPT_KEY);
        for (SysDept sysDept : sysDepts) {
            OrderDept orderDept = new OrderDept();
            orderDept.setPid(sysDept.getPid());
            orderDept.setId(String.valueOf(sysDept.getId()));
            orderDept.setDeptId(sysDept.getId());
            orderDept.setType("dept");
            orderDept.setName(sysDept.getName());
            orderDepts.add(orderDept);
        }
        //顶级的那个数据
        OrderDept orderDeptTop = new OrderDept();
        orderDeptTop.setPid(topDept.getPid());
        orderDeptTop.setId(String.valueOf(topDept.getId()));
        orderDeptTop.setDeptId(topDept.getId());
        orderDeptTop.setType("dept");
        orderDeptTop.setName(topDept.getName());
        List<OrderDept> child = new ArrayList<>();

        //去除没有子部门并且没有人员的机构元素
        while (true) {
            //一次只能找到最下层结构没有子部门并且没有挂人的
            List<OrderDept> noSubList = new ArrayList<>();
            for (OrderDept orderDept : orderDepts) {
                boolean hasSub =false;
                for(OrderDept dept: orderDepts){
                    if(orderDept.getDeptId().equals(dept.getPid())){
                        hasSub =true;
                        break;
                    }
                }
                if(!hasSub && !userMap.containsKey(orderDept.getDeptId())){
                    noSubList.add(orderDept);
                }
            }
            //没有
            if (noSubList.isEmpty()) {
                break;
            }
            Map<Integer, OrderDept> noSubMap = noSubList.stream().collect(Collectors.toMap(OrderDept::getDeptId, orderDept -> orderDept));
            //下一次循环时
            List<OrderDept> newOrderDepts = new ArrayList<>();
            for (OrderDept orderDept : orderDepts) {
                if(noSubMap.containsKey(orderDept.getDeptId())){
                    continue;
                }
                newOrderDepts.add(orderDept);
            }
            orderDepts = newOrderDepts;
        }

        //生成部门树
        for (OrderDept orderDept : orderDepts) {
            //
            if (orderDept.getPid().equals(orderDeptTop.getDeptId())) {
                //拿到所有的子部门
                List<OrderDept> deptChild = getChild(orderDept.getDeptId(), orderDepts, userMap);
                //顶级部门下一层的二级部门增加人员
                if(userMap.containsKey(orderDept.getDeptId())){
                    deptChild.addAll(userMap.get(orderDept.getDeptId()));
                }
                orderDept.setChildren(deptChild);
                child.add(orderDept);

            }
        }
        //顶层部门下的人员集合
        if (userMap.containsKey(orderDeptTop.getDeptId())) {
            List<OrderDept> persons = userMap.get(orderDeptTop.getDeptId());
            child.addAll(persons);
        }
        //设置顶级部门下的child
        orderDeptTop.setChildren(child);
        return orderDeptTop;
    }

    //递归方法
    public List<OrderDept> getChild(Integer pid, List<OrderDept> all, Map<Integer, List<OrderDept>> userMap) {
        List<OrderDept> list = new ArrayList<>();
        for (OrderDept orderDept : all) {
            if (orderDept.getPid().equals(pid)) {
                List<OrderDept> deptChild = getChild(orderDept.getDeptId(), all, userMap);
                //如果
                if (userMap.containsKey(orderDept.getDeptId())) {
                    deptChild.addAll(userMap.get(orderDept.getDeptId()));
                }
                orderDept.setChildren(deptChild);
                list.add(orderDept);
            }
        }
        return list;
    }



}
