package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.analysis.PcRaProblemType;
import cn.cecep.talroad.mapper.analysis.PcRaProblemTypeMapper;
import cn.cecep.talroad.service.analysis.IPcRaProblemTypeService;
import cn.cecep.talroad.vo.analysis.PcRaProblemTypeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 问题分类Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class PcRaProblemTypeServiceImpl extends ServiceImpl<PcRaProblemTypeMapper,PcRaProblemType> implements IPcRaProblemTypeService
{
    @Autowired
    private PcRaProblemTypeMapper pcRaProblemTypeMapper;

    /**
     * 查询问题分类列表
     *
     * @param pcRaProblemType 问题分类
     * @return 问题分类
     */
    @Override
    public List<PcRaProblemType> selectPcRaProblemTypeList(PcRaProblemType pcRaProblemType)
    {
        return pcRaProblemTypeMapper.selectPcRaProblemTypeList(pcRaProblemType);
    }

    @Override
    public List<Map<String, Object>> cascader() {
        List<PcRaProblemType> problemTypes = list(new QueryWrapper<PcRaProblemType>().eq("is_status", "1").orderByAsc("sort"));

        // 转为map列表
        List<Map<String, Object>> mapList = problemTypes.stream().map(o -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", o.getId());
            map.put("label", o.getName());
            map.put("parentId", o.getParentId());
            return map;
        }).collect(Collectors.toList());

        // 以问题id为key, 转为map
        Map<String, Map<String, Object>> keyMap = mapList.stream().collect(Collectors.toMap(o -> (String) o.get("value"), o -> o));

        // 添加到父节点children属性下
        for (Map<String, Object> map : mapList) {
            Map<String, Object> parent = keyMap.get((String) map.get("parentId"));
            if (parent != null) {
                List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
                if (children == null) {
                    children = new ArrayList<>();
                }
                children.add(map);
                parent.put("children", children);
            }
        }

        // 返回所有顶级节点
        return mapList.stream().filter(o -> o.get("parentId") == null).collect(Collectors.toList());
    }

    /**
     * 查询所有的问题分类，并设置问题分类路径
     * @return
     */
    @Override
    public List<PcRaProblemTypeVo> selectProblemTypeVoList() {
        List<PcRaProblemType> problemTypes = list(new QueryWrapper<PcRaProblemType>().orderByAsc("sort"));

        Map<String, PcRaProblemType> typeMap = problemTypes.stream().collect(Collectors.toMap(PcRaProblemType::getId, o -> o));

        // 转为vo, 并且设置path属性
        return problemTypes.stream().map(o -> {
            PcRaProblemTypeVo vo = new PcRaProblemTypeVo();
            BeanUtils.copyProperties(o, vo);

            List<PcRaProblemType> path = new ArrayList<>();
            path.add(o);

            PcRaProblemType parent = typeMap.get(o.getParentId());
            while (parent != null) {
                path.add(parent);
                parent = typeMap.get(parent.getParentId());
            }
            Collections.reverse(path);

            vo.setPath(path);
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取指定问题路径名称
     * @param problemType
     * @return
     */
    @Override
    public String getPathName(String problemType) {
        if (StringUtils.isBlank(problemType)) {
            return null;
        }
        PcRaProblemType type = baseMapper.selectById(problemType);
        if (type == null) {
            return null;
        }
        List<PcRaProblemType> path = new ArrayList<>();
        path.add(type);

        String parentId = type.getParentId();
        while (parentId != null) {
            PcRaProblemType parent = baseMapper.selectById(parentId);
            if (parent == null) {
                break;
            }
            path.add(parent);
            parentId = parent.getParentId();
        }
        Collections.reverse(path);
        return path.stream().map(PcRaProblemType::getName).collect(Collectors.joining("-"));
    }

}
