package com.ruoyi.test.seriver.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.KnowledgeTree;
import com.ruoyi.test.domain.Vo.KnowledgeTreeVo;
import com.ruoyi.test.mapper.KnowledgeTreeMapper;
import com.ruoyi.test.seriver.IKnowledgeTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class KnowledgeTreeServiceImpl extends ServiceImpl<KnowledgeTreeMapper,KnowledgeTree> implements IKnowledgeTreeService {
    @Autowired
    private KnowledgeTreeMapper knowledgeTreeMapper;

    public List<KnowledgeTree> selectAll(String grade,String subject) {
        LambdaQueryWrapper<KnowledgeTree> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(KnowledgeTree::getId, KnowledgeTree::getPid, KnowledgeTree::getContent); // 仅选择数据库表中存在的列
        queryWrapper.eq(KnowledgeTree::getGrade,grade);
        queryWrapper.eq(KnowledgeTree::getSubject,subject);
        queryWrapper.eq(KnowledgeTree::getPid, 0L);
        List<KnowledgeTree> topLevelList = knowledgeTreeMapper.selectList(queryWrapper);
        // 为顶级元素递归获取子元素并设置到list属性中
        for (KnowledgeTree knowledgeTree : topLevelList) {
            knowledgeTree.setList(getList(knowledgeTree));
        }
        return topLevelList;
    }

    @Override
    public int addKnowledgeTree(KnowledgeTreeVo knowledgeTreeVo) {
        Long pid = knowledgeTreeVo.getPid();
        LambdaQueryWrapper<KnowledgeTree> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(KnowledgeTree::getSubject,KnowledgeTree::getGrade,KnowledgeTree::getPid,KnowledgeTree::getContent,KnowledgeTree::getId);
        queryWrapper.eq(KnowledgeTree::getId,pid);
        System.out.println(knowledgeTreeMapper.selectList(queryWrapper));
        if(knowledgeTreeMapper.selectList(queryWrapper).size()==0){
            return -1;
        }
        String content = knowledgeTreeVo.getContent();
        return knowledgeTreeMapper.addKnowledgeTree(knowledgeTreeVo.getGrade(),knowledgeTreeVo.getSubject(),pid,content);
    }

    @Override
    public int resetKnowledgeTree(String grade,String subject) {
        return knowledgeTreeMapper.resetKnowledgeTree(grade,subject);
    }

    @Override
    public int deleteKnowledgeTree(String grade, String subject, Long id) {
        return knowledgeTreeMapper.deleteKnowledgeTree(grade,subject,id);
    }

    @Override
    public int update(String grade, String subject, Long id, String content) {
        return knowledgeTreeMapper.update(grade,subject,id,content);
    }

    private List<KnowledgeTree> getList(KnowledgeTree parent) {
        LambdaQueryWrapper<KnowledgeTree> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(KnowledgeTree::getId, KnowledgeTree::getPid, KnowledgeTree::getContent); // 仅选择数据库表中存在的列
        queryWrapper.eq(KnowledgeTree::getPid, parent.getId());
        List<KnowledgeTree> children = knowledgeTreeMapper.selectList(queryWrapper);
        // 为子元素递归获取孙元素并设置到list属性中
        for (KnowledgeTree child : children) {
            child.setList(getList(child));
        }
        return children;
    }
}
