package com.infoepoch.pms.agent.common.utils;

import com.infoepoch.pms.agent.domain.baisc.organization.Organization;
import com.infoepoch.pms.agent.domain.baisc.user.User;
import org.apache.commons.collections4.CollectionUtils;

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

public class OrganizationTool {

    /**
     * 组织排序
     * 排序规则：先根据级别排序，再根据displayOrder排序，最后根据ID排序
     *
     * @param organizationList
     * @return
     */
    public static List<Organization> organizationSort(List<Organization> organizationList) {
        if (CollectionUtils.isEmpty(organizationList))
            return new ArrayList<>();
        return organizationList.stream().sorted(Comparator.comparing(Organization::getLevel)
                        .thenComparing(Organization::getDisplayOrderValue)
                        .thenComparing(Organization::getOaOrgCodeNotNull))
                .collect(Collectors.toList());
    }

    /**
     * 组织列表转换成树形结构
     *
     * @param organizationList
     * @return
     */
    public static List<Organization> changeOrganizationListToTree(List<Organization> organizationList) {
        if (CollectionUtils.isEmpty(organizationList))
            return new ArrayList<>();
        // 组织排序
        organizationList = organizationSort(organizationList);
        Integer topLevel = null;
        Map<String, List<Organization>> organizationListMap = new HashMap<>();
        for (Organization organization : organizationList) {
            List<Organization> children = organizationListMap.get(organization.getpId());
            if (children == null) {
                children = new ArrayList<>();
            }
            children.add(organization);
            organizationListMap.put(organization.getpId(), children);
            if (topLevel == null) {
                topLevel = organization.getLevel();
            } else {
                if (topLevel > organization.getLevel()) {
                    topLevel = organization.getLevel();
                }
            }
        }
        List<Organization> topLevelOrganizationList = new ArrayList<>();
        for (Organization organization : organizationList) {
            if (Objects.equals(organization.getLevel(), topLevel)) {
                topLevelOrganizationList.add(organization);
            }
        }
        // 将组织列表转换为树形结构
        fillTreeOrganizationData(topLevelOrganizationList, organizationListMap);
        return topLevelOrganizationList;
    }

    /**
     * 将组织列表转换为树形结构
     *
     * @param organizationList 组织列表
     * @param userList         用户列表
     * @return
     */
    public static List<Organization> changeOrganizationListToTree(List<Organization> organizationList, List<User> userList) {
        if (CollectionUtils.isEmpty(organizationList))
            return new ArrayList<>();
        if (userList == null) {
            userList = new ArrayList<>();
        }
        // 组织排序
        organizationList = organizationSort(organizationList);
        Map<String, List<User>> userListMap = LambdaTool.changeCollectionToHashListMap(userList, User::getOrganizationId);
        Integer topLevel = null;
        Map<String, List<Organization>> organizationListMap = new HashMap<>();
        for (Organization organization : organizationList) {
            List<Organization> children = organizationListMap.get(organization.getpId());
            if (children == null) {
                children = new ArrayList<>();
            }
            children.add(organization);
            organizationListMap.put(organization.getpId(), children);
            organization.setUserList(userListMap.get(organization.getId()));
            if (topLevel == null) {
                topLevel = organization.getLevel();
            } else {
                if (topLevel > organization.getLevel()) {
                    topLevel = organization.getLevel();
                }
            }
        }
        List<Organization> topLevelOrganizationList = new ArrayList<>();
        for (Organization organization : organizationList) {
            if (Objects.equals(organization.getLevel(), topLevel)) {
                topLevelOrganizationList.add(organization);
            }
        }
        // 将组织列表转换为树形结构
        fillTreeOrganizationData(topLevelOrganizationList, organizationListMap);
        return topLevelOrganizationList;
    }

    /**
     * 将组织列表转换为树形结构
     *
     * @param organizationList
     * @param organizationListMap
     */
    public static void fillTreeOrganizationData(List<Organization> organizationList, Map<String, List<Organization>> organizationListMap) {
        if (CollectionUtils.isEmpty(organizationList))
            return;
        for (Organization organization : organizationList) {
            // 防止无限循环
            if(organization.getpId().equals(organization.getId())) {
                continue;
            }
            List<Organization> children = organizationListMap.get(organization.getId());
            if (CollectionUtils.isNotEmpty(children)) {
                fillTreeOrganizationData(children, organizationListMap);
                organization.addChildren(children);
            }
        }
    }

}
