package com.dsk.cscec.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.cscec.domain.DProjectType;
import com.dsk.cscec.domain.TreeSelect;
import com.dsk.cscec.mapper.DProjectTypeMapper;
import com.dsk.cscec.service.DProjectTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维表：工程类型，源于IPM(DProjectType)表服务实现类
 *
 * @author sxk
 * @since 2024-01-05 11:45:49
 */
@Service("dProjectTypeService")
public class DProjectTypeServiceImpl extends ServiceImpl<DProjectTypeMapper, DProjectType> implements DProjectTypeService {
    @Resource
    private DProjectTypeMapper baseMapper;

    @Override
    public List<TreeSelect> getList(DProjectType bo) {

        return buildTree(baseMapper.selectList(buildQueryWrapper(bo))).stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<DProjectType> getChildren(String projectTypeId) {
        DProjectType bo = new DProjectType();
        if (StringUtils.isNotBlank(projectTypeId)) {
            bo.setParentId(projectTypeId);
        } else {
            bo.setLevelNo(1l);
        }

        return baseMapper.selectList(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<DProjectType> buildQueryWrapper(DProjectType bo) {
        LambdaQueryWrapper<DProjectType> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getProjectTypeKey()), DProjectType::getProjectTypeKey, bo.getProjectTypeKey());
//        lqw.like(StringUtils.isNotBlank(bo.getProjectTypeName()), DProjectType::getProjectTypeName, bo.getProjectTypeName());
        lqw.eq(ObjectUtil.isNotNull(bo.getLevelNo()), DProjectType::getLevelNo, bo.getLevelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectTypeId()), DProjectType::getProjectTypeId, bo.getProjectTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), DProjectType::getParentId, bo.getParentId());
        return lqw;
    }

    public List<DProjectType> buildTree(List<DProjectType> types) {
        List<DProjectType> returnList = new ArrayList<DProjectType>();
        List<String> tempList = new ArrayList<String>();
        for (DProjectType type : types) {
            tempList.add(type.getProjectTypeId());
        }
        for (DProjectType type : types) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId())) {
                recursionFn(types, type);
                returnList.add(type);
            }
        }
        if (returnList.isEmpty()) {
            returnList = types;
        }
        return returnList;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<DProjectType> list, DProjectType t) {
        // 得到子节点列表
        List<DProjectType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (DProjectType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<DProjectType> getChildList(List<DProjectType> list, DProjectType t) {
        List<DProjectType> tlist = new ArrayList<DProjectType>();
        for (DProjectType n : list) {
            if (ObjectUtil.isNotNull(n.getParentId()) && n.getParentId().equals(t.getProjectTypeId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<DProjectType> list, DProjectType t) {
        return getChildList(list, t).size() > 0;
    }
}
