package com.storlead.user.pojo.vo;

import com.storlead.user.pojo.entity.CompanyEntity;
import com.storlead.user.pojo.entity.DeptEntity;
import com.storlead.user.pojo.entity.SubCompanyEntity;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-cloud
 * @description:
 * @author: chenkq
 * @create: 2022-05-13 15:55
 */
@Data
public class DepartmentTree {

    private Long id;
    private String treeId;
    private Long parentId;
    private String title;
    private String code;
    private Object arg;
    private String photo;
    private String avatar;
    private Long leaderId;
    private String leaderName;
    private Boolean checked = false;
    private Boolean _showChildren = true;
    private Boolean expand = false;
    private Boolean disableCheckbox = false;
    private Boolean disabled = true;
    /**
     * 类型:10: 集团,20:公司 30:部门 40:小组,99:人员
     */
    private Integer type;
    private List<DepartmentTree> children;


    /**
     * 公司转换
     * @param vals
     * @return
     */
    public static List<DepartmentTree> companyListToDepartmentTreeVoList(List<CompanyEntity> vals) {
        if (CollectionUtils.isEmpty(vals)) {
            return null;
        }
        List<DepartmentTree> departmentTrees = vals.stream().map(DepartmentTree::companyToDepartmentTreeVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return departmentTrees;
    }

    public static DepartmentTree  companyToDepartmentTreeVo(CompanyEntity val) {
        if (null == val) {
            return null;
        }
        DepartmentTree departmentTree = new DepartmentTree();

        departmentTree.setId(val.getId());
        departmentTree.setParentId(0L);
        departmentTree.setTitle(val.getName());
        departmentTree.set_showChildren(true);
        // departmentTree.setCode(d.getOrgCode());
        departmentTree.setArg(val);
        departmentTree.setType(Integer.valueOf(10));
        departmentTree.setTreeId(departmentTree.getType()+"-"+val.getId());
        return departmentTree;
    }

    /**
     * 子公司转换
     * @param vals
     * @return
     */
    public static List<DepartmentTree> subCompanyListToDepartmentTreeVoList(List<SubCompanyEntity> vals) {
        if (CollectionUtils.isEmpty(vals)) {
            return null;
        }
        List<DepartmentTree> departmentTrees = vals.stream().map(DepartmentTree::subCompanyToDepartmentTreeVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return departmentTrees;
    }

    public static DepartmentTree  subCompanyToDepartmentTreeVo(SubCompanyEntity val) {
        if (null == val) {
            return null;
        }
        DepartmentTree departmentTree = new DepartmentTree();
        departmentTree.setId(val.getId());
        departmentTree.setParentId(val.getCompanyId());
        departmentTree.setTitle(val.getName());
        departmentTree.set_showChildren(false);
        departmentTree.setLeaderName(val.getLeaderName());
        departmentTree.setLeaderId(val.getLeaderId());
        departmentTree.setArg(val);
        departmentTree.setType(Integer.valueOf(20));
        departmentTree.setTreeId(departmentTree.getType()+"-"+val.getId());

        return departmentTree;
    }


    /**
     * 部门转换
     * @param vals
     * @return
     */
    public static List<DepartmentTree> deptListToDepartmentTreeVoList(List<DeptEntity> vals) {
        if (CollectionUtils.isEmpty(vals)) {
            return null;
        }
        List<DepartmentTree> departmentTrees = vals.stream().map(DepartmentTree::deptToDepartmentTreeVo).filter(obj -> !Objects.isNull(obj)).collect(Collectors.toList());
        return departmentTrees;
    }

    public static DepartmentTree  deptToDepartmentTreeVo(DeptEntity val) {
        if (null == val) {
            return null;
        }
        DepartmentTree departmentTree = new DepartmentTree();
        departmentTree.setId(val.getId());
        if(Long.valueOf(0).equals(val.getPid())) {
            departmentTree.setParentId(val.getSubCompanyId());
        } else {
            departmentTree.setParentId(val.getPid());
        }
        departmentTree.set_showChildren(false);
        departmentTree.setTitle(val.getName());
        departmentTree.setLeaderName(val.getLeaderName());
        departmentTree.setLeaderId(val.getManagerId());
        departmentTree.setArg(val);
        departmentTree.setType(Integer.valueOf(30));
        departmentTree.setTreeId(departmentTree.getType()+"-"+val.getId());

        return departmentTree;
    }

}
