package com.ruoyi.web.controller.test.gl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.test.domain.DO.SubjectKnowledgeDO;
import com.ruoyi.test.domain.KnowledgeTree;
import com.ruoyi.test.domain.Vo.KnowledgeTreeVo;
import com.ruoyi.test.seriver.IKnowledgeTreeService;
import com.ruoyi.test.seriver.ISubjectKnowledgeDOService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理端知识点管理")
@RestController
@RequestMapping("/gl/knowledgeTree")
public class KnowledgeTreeController {
    @Autowired
    private IKnowledgeTreeService iKnowledgeTreeService;

    @Autowired
    private ISubjectKnowledgeDOService iSubjectKnowledgeDOService;

//    @ApiOperation("知识点列表")
    @GetMapping("selectList")
    public R<List<SubjectKnowledgeDO>> selectList(){
        return R.ok(iSubjectKnowledgeDOService.selectList());
    }

    @ApiOperation("知识点树")
    @GetMapping("selectAll")
    public R<List<KnowledgeTree>> selectAll(String grade,String subject){
        if(grade==null||subject==null){
            return R.fail("操作失败");
        }
        return R.ok(iKnowledgeTreeService.selectAll(grade,subject));
    }

    @ApiOperation("添加知识点")
    @PostMapping("addKnowledgeTree")
    public R<List<KnowledgeTree>> addKnowledgeTree(@RequestBody KnowledgeTreeVo knowledgeTreeVo){
//        System.out.println(knowledgeTreeVo);
        if(iKnowledgeTreeService.addKnowledgeTree(knowledgeTreeVo)>0){
            return R.ok(iKnowledgeTreeService.selectAll(knowledgeTreeVo.getGrade(),knowledgeTreeVo.getSubject()));
        }else if(iKnowledgeTreeService.addKnowledgeTree(knowledgeTreeVo)==-1){
            return R.fail("无该父节点");
        }
        return R.fail("添加失败");
    }

    @ApiOperation("重置知识点树")
    @GetMapping("resetKnowledgeTree")
    public R<String> resetKnowledgeTree(String grade, String subject){
        if(iKnowledgeTreeService.resetKnowledgeTree(grade,subject)!=0){
            return R.ok("重置成功！");
        }
        return R.fail("重置失败！");
    }

    //？如果删除父节点，则子节点无用，在数据库中可能造成冗余
    @ApiOperation("删除知识点树")
    @DeleteMapping("deleteKnowledgeTree")
    public R<String> deleteKnowledgeTree(String grade, String subject,Long id){
        if(iKnowledgeTreeService.deleteKnowledgeTree(grade,subject,id)!=0){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }

    @ApiOperation("修改知识点")
    @GetMapping("update")
    public R<Integer> update(String grade,String subject,Long id,String content){
        return R.ok(iKnowledgeTreeService.update(grade,subject,id,content));
    }
}
