package com.cool.store.dto.enterprise;

import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.RegionDO;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

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


/**
 * @ClassName SysDepartmentDO
 * @Description
 * @author 王春辉
 */
@Data
public class SysDepartmentDTO {
    /**
     * 部门id
     */
    private String id;

    /**
     * 部门名称
     */
    private String name;

    /**
     * 父部门id，根部门为1
     */
    private String parentId;

    /**
     * 在父部门中的次序值
     */
    private Integer departOrder;

    /**
     * 是否同步创建一个关联此部门的企业群, true表示是, false表示不是
     */
    private Boolean createDeptGroup;

    /**
     * 当群已经创建后，是否有新人加入部门会自动加入该群, true表示是, false表示不是
     */
    private Boolean autoAddUser;

    /**
     * 是否隐藏部门, true表示隐藏, false表示显示
     */
    private Boolean depHiding;

    /**
     * 可以查看指定隐藏部门的其他部门列表，如果部门隐藏，则此值生效，取值为其他的部门id组成的的字符串，使用|符号进行分割
     */
    private String deptPerimits;

    /**
     * 可以查看指定隐藏部门的其他人员列表，如果部门隐藏，则此值生效，取值为其他的人员userid组成的的字符串，使用|符号进行分割
     */
    private String userPerimits;

    /**
     * 是否本部门的员工仅可见员工自己, 为true时，本部门员工默认只能看到员工自己
     */
    private Boolean outerDept;

    /**
     * 本部门的员工仅可见员工自己为true时，可以配置额外可见部门，值为部门id组成的的字符串，使用|符号进行分割
     */
    private String outerPermitDepts;

    /**
     * 本部门的员工仅可见员工自己为true时，可以配置额外可见人员，值为userid组成的的字符串，使用| 符号进行分割
     */
    private String outerPermitUsers;

    /**
     * 企业群群主
     */
    private String orgDeptOwner;

    /**
     * 部门的主管列表,取值为由主管的userid组成的字符串，不同的userid使用|符号进行分割
     */
    private List<String> deptManagerUseridList;


    /**
     * 该部门下所有的用户数，包括子部门
     */
    private Integer userCount;

    /**
     * 部门下所有未激活的用户数，包括子部门
     */
    private Integer unactiveUserCount;

    /**
     * 父级部门ids
     */
    private String parentIds;

    /**
     * 子部门ids
     */
    private String subIds;

    private String leaderUserId;


    public SysDepartmentDTO() {

    }

    /**
     * 构造函数
     *
     * @param nodeId 节点Id
     */
    public SysDepartmentDTO(String nodeId) {
        this.id = nodeId;
    }

    /**
     * 构造函数
     *
     * @param nodeId   节点Id
     * @param parentId 父节点Id
     */

    public SysDepartmentDTO(String nodeId, String parentId) {
        this.id = nodeId;
        this.parentId = parentId;
    }


    /**
     * 设置部门名称
     *
     * @param name 部门名称
     */
    public void setName(String name) {
        if (StringUtils.isNotBlank(name)) {
            this.name = name.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
            if (StringUtils.isBlank(this.name)) {
                this.name = "空";
            }
        }
    }

    /**
     * 获取父部门id，根部门为1
     *
     * @return parent_id - 父部门id，根部门为1
     */
    public String getParentId() {
        if (id.equals(CommonConstants.ROOT_DEPT_ID_STR)) {
            return null;
        }
        return parentId;
    }

    @Override
    public String toString() {
        return "SysDepartment{" +
                "id=" + id +
                ", parentId=" + parentId + ",name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        SysDepartmentDTO that = (SysDepartmentDTO) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public static List<RegionDO> convertRegionDO(List<SysDepartmentDTO> departmentList, Multimap<String, String> leaderDeptMap){
        if(CollectionUtils.isEmpty(departmentList)){
            return Lists.newArrayList();
        }
        Map<String, String> parentMap = departmentList.stream().collect(Collectors.toMap(SysDepartmentDTO::getId, SysDepartmentDTO::getParentId));
        List<RegionDO> resultList = new ArrayList<>();
        for (SysDepartmentDTO dept : departmentList) {
            RegionDO region = new RegionDO();
            region.setRegionId(dept.getId());
            region.setName(dept.getName());
            region.setParentId(dept.getParentId());
            region.setUnclassifiedFlag(CommonConstants.ZERO);
            region.setLeaderUserId(dept.getLeaderUserId());
            region.setOrderNum(dept.getDepartOrder());
            region.setThirdDeptId(dept.getId());
            region.setCreateTime(new Date());
            region.setUpdateTime(new Date());
            List<String> pathIds = new ArrayList<>();
            pathIds.add(region.getRegionId());
            String parentId = parentMap.get(region.getRegionId());
            while (Objects.nonNull(parentId)){
                pathIds.add(parentId);
                parentId = parentMap.get(parentId);
            }
            Collections.reverse(pathIds);
            String regionPath = CommonConstants.PATH_SPILT + String.join(CommonConstants.PATH_SPILT, pathIds) + CommonConstants.PATH_SPILT;
            region.setRegionPath(regionPath);
            region.setDeleted(Boolean.FALSE);
            if(CollectionUtils.isNotEmpty(dept.getDeptManagerUseridList())){
                for (String leader : dept.getDeptManagerUseridList()) {
                    leaderDeptMap.put(leader, dept.getId());
                }
            }
            resultList.add(region);
        }
        return resultList;
    }


    public static RegionDO convertRegionDO(SysDepartmentDTO dept, Multimap<String, String> leaderDeptMap, RegionDO parentRegion){
        RegionDO region = new RegionDO();
        region.setRegionId(dept.getId());
        region.setName(dept.getName());
        region.setParentId(dept.getParentId());
        region.setUnclassifiedFlag(CommonConstants.ZERO);
        region.setLeaderUserId(dept.getLeaderUserId());
        region.setOrderNum(dept.getDepartOrder());
        region.setThirdDeptId(dept.getId());
        region.setCreateTime(new Date());
        region.setUpdateTime(new Date());
        String regionPath = parentRegion.getRegionPath() + region.getRegionId() + CommonConstants.PATH_SPILT;
        region.setRegionPath(regionPath);
        region.setDeleted(Boolean.FALSE);
        if(CollectionUtils.isNotEmpty(dept.getDeptManagerUseridList())){
            for (String leader : dept.getDeptManagerUseridList()) {
                leaderDeptMap.put(leader, dept.getId());
            }
        }
        return region;
    }

    public static RegionDO convertRegionDO(SysDepartmentDTO dept, Multimap<String, String> leaderDeptMap){
        RegionDO region = new RegionDO();
        region.setRegionId(dept.getId());
        region.setName(dept.getName());
        region.setParentId(dept.getParentId());
        region.setUnclassifiedFlag(CommonConstants.ZERO);
        region.setLeaderUserId(dept.getLeaderUserId());
        region.setOrderNum(dept.getDepartOrder());
        region.setThirdDeptId(dept.getId());
        region.setCreateTime(new Date());
        region.setUpdateTime(new Date());
        region.setDeleted(Boolean.FALSE);
        if(CollectionUtils.isNotEmpty(dept.getDeptManagerUseridList())){
            for (String leader : dept.getDeptManagerUseridList()) {
                leaderDeptMap.put(leader, dept.getId());
            }
        }
        return region;
    }
}
