package com.engine.organization.entity.extend.bo;

import com.engine.organization.entity.extend.po.ExtendGroupPO;
import com.engine.organization.entity.extend.po.ExtendTitlePO;
import com.engine.organization.entity.fieldset.vo.TypeTreeVO;
import com.engine.organization.mapper.extend.ExtendTitleMapper;
import com.engine.organization.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/6/14
 * @Version V1.0
 **/
public class ExtendGroupBO {

    public static List<TypeTreeVO> buildTypeTree(List<ExtendGroupPO> extendGroupPOS) {

        if (CollectionUtils.isEmpty(extendGroupPOS)) {
            return Collections.emptyList();
        }

        List<TypeTreeVO> typeTreeVOS = extendGroupPOS.stream().map(e ->
                TypeTreeVO.builder()
                        .canClick(true)
                        .addChild(e.getPid() == null)
                        .isSelected(isSelect(e.getId()))
                        .domid(String.valueOf(e.getId()))
                        .childs(Lists.newArrayList())
                        .hasGroup(isHasGroup(e.getId()))
                        .isShow(e.getIsShow())
                        .key(e.getId())
                        .name(e.getGroupName())
                        .viewAttr(e.getIsSystemDefault() == 0 ? 1 : 2)
                        .pid(e.getPid())
                        .build()).collect(Collectors.toList());

        //获取非一级
        Map<Long, List<TypeTreeVO>> collects = typeTreeVOS.stream().filter(item -> item.getPid() != null).collect(Collectors.groupingBy(TypeTreeVO::getPid));

        return typeTreeVOS.stream().peek(e -> {
            e.setChilds(collects.get(e.getKey()));
            e.setHaschild(collects.get(e.getKey()) != null && collects.get(e.getKey()).size() > 0);
        }).filter(item -> item.getPid() == null).collect(Collectors.toList());
    }


    private static boolean isHasGroup(Long id) {
        List<ExtendTitlePO> titlesByGroupID = MapperProxyFactory.getProxy(ExtendTitleMapper.class).getTitlesByGroupID(id,null);
        return titlesByGroupID.size() > 0;
    }

    /**
     * 获取对应表名
     *
     * @param extendGroupPO
     * @return
     */
    public static String getTableNameByGroupPO(ExtendGroupPO extendGroupPO) {
        String tableName = "";
        switch (extendGroupPO.getExtendType()) {
            case 1:
                tableName = "JCL_ORG_COMP";
                break;
            case 2:
                tableName = "JCL_ORG_DEPT";
                break;
            case 3:
                tableName = "JCL_ORG_JOB";
                break;
            case 4:
                tableName = "JCL_ORG_HRMRESOURCE";
                break;
            default:
                break;
        }

        if (extendGroupPO.getIsSystemDefault() == 0) {
            if (null != extendGroupPO.getPid()) {
                tableName += "EXT_DT1";
            }
        } else if (null == extendGroupPO.getPid()) {
            tableName += "EXT";
        } else {
            tableName += "EXT_DT1";
        }
        return tableName;
    }


    private static boolean isSelect(Long id) {
        if (id == 1 || id == 2 || id == 3 || id == 4) {
            return true;
        } else {
            return false;
        }
    }
}
