package com.ruoyi.web.controller.ie;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.PaperquesType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ie.domain.KnowledgeTree;
import com.ruoyi.ie.domain.Paperques;
import com.ruoyi.ie.domain.Questions;
import com.ruoyi.ie.service.IPaperquesService;
import com.ruoyi.ie.service.impl.KnowledgeTreeService;
import com.ruoyi.ie.service.IQuestionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 知识分类树Controller
 *
 * @author mingxue
 * @date 2021-06-14
 */
@Api(tags = "前端V2 知识点分类树")
@RestController
@RequestMapping("/front/knowledgeTree")
public class FrontKnowledgeTreeController extends BaseController {
    @Autowired
    private KnowledgeTreeService knowledgeTreeService;
    @Autowired
    private IPaperquesService paperquesService;
    @Autowired
    private IQuestionsService questionsService;

    /**
     * 查询知识分类树列表
     */
    @ApiOperation("获取知识点树")
    @GetMapping("/list")
    public AjaxResult list(@ApiParam("科目Id") @RequestParam Long subjectId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        KnowledgeTree query = new KnowledgeTree();
        query.setSubjectId(subjectId);
        query.setLocation(user.getProvinceName());
        query.setExamineeType(user.getExamType());
        List<KnowledgeTree> list = knowledgeTreeService.selectKnowledgeTreeList(query);

        Map<Long, KnowledgeTree> map = new HashMap<Long, KnowledgeTree>();
        for (KnowledgeTree t : list) {
            map.put(t.getId(), t);
        }
        List<KnowledgeTree> top = new ArrayList<KnowledgeTree>();
        for (KnowledgeTree t : list) {
            if (t.getPid() != null && t.getPid() != 0) {
                KnowledgeTree parent = map.get(t.getPid());
                if (parent != null) {
                    parent.getChildren().add(t);
                }
            } else {
                top.add(t);
            }
        }
        return AjaxResult.success(top);
    }

    @ApiOperation("获取最底层的知识点")
    @GetMapping("/listBottoms")
    public AjaxResult listBottoms(Long knowledgeId) {
        List<KnowledgeTree> oldids = new ArrayList<>();
        KnowledgeTree top = getTop(knowledgeId);
        getKnowledgeTreeOldids(top, oldids);
        return AjaxResult.success(oldids);
    }
    private KnowledgeTree getTop(Long knowledgeId) {
        KnowledgeTree t = knowledgeTreeService.selectKnowledgeTreeById(knowledgeId);
        while (t.getPid() != null && t.getPid() != 0) {
            t = knowledgeTreeService.selectKnowledgeTreeById(t.getPid());
        }
        return t;
    }

    public void getKnowledgeTreeOldids(KnowledgeTree knowledgeTree, List<KnowledgeTree> oldids) {
        // 找出章节的所有末节点
        KnowledgeTree query = new KnowledgeTree();
        query.setPid(knowledgeTree.getId());
        List<KnowledgeTree> children = knowledgeTreeService.selectKnowledgeTreeList(query);
        if (CollectionUtils.isEmpty(children)) {
            // 如果没有子，说明自己就是末节点
            oldids.add(knowledgeTree);
        } else {
            Map<Long, KnowledgeTree> objMap = new HashMap<>();
            Map<Long, List<Long>> map = new LinkedHashMap<>();
            while (CollectionUtils.isNotEmpty(children)) {
                for (KnowledgeTree obj : children) {
                    objMap.put(obj.getId(), obj);
                }
                groupChildrenIds(children, map);
                List<Long> pids = children.stream().map(KnowledgeTree::getId).collect(Collectors.toList());
                query = new KnowledgeTree();
                query.setPids(pids);
                children = knowledgeTreeService.selectKnowledgeTreeList(query);
            }

            for (Long pid : map.keySet()) {
                if (pid == 0L) {
                    continue;
                }
                List<Long> childrenIds = map.get(pid);
                if (CollectionUtils.isEmpty(childrenIds)) {
                    oldids.add(objMap.get(pid));
                }
            }
        }
    }

    private void groupChildrenIds(List<KnowledgeTree> children, Map<Long, List<Long>> map) {
        // 放parent
        for (KnowledgeTree c : children) {
            List<Long> childrenIds = map.get(c.getPid());
            if (childrenIds == null) {
                childrenIds = new ArrayList<>();
                map.put(c.getPid(), childrenIds);
            }
            childrenIds.add(c.getId());
        }
        // 放自己
        for (KnowledgeTree c : children) {
            map.put(c.getId(), new ArrayList<>());
        }
    }
}
