package com.xhkjedu.sstudy.controller.question;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.model.question.TQuestion;
import com.xhkjedu.sstudy.model.question.TQuestionCollect;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.model.subjectbook.TPoint;
import com.xhkjedu.sstudy.service.question.QuestionService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.sstudy.service.subjectbook.PointService;
import com.xhkjedu.sstudy.vo.question.QuestionCollectVo;
import com.xhkjedu.sstudy.vo.question.QuestionVo;
import com.xhkjedu.sstudy.vo.system.CollectVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* @author ywx
* @classname TQuestionController
* @description
* @date 2020/06/06 15:54
**/
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService tQuestionService;
    @Resource
    private DirectorService directorService;
    @Resource
    private PointService pointService;

    @PostMapping("/update_state")
    @Action("试题管理-修改状态")
    public ResultVo updateState(@RequestBody TQuestion tQuestion) {
        String questionid = tQuestion.getQuestionid();
        Integer qstate = tQuestion.getQstate();
        N_Utils.validation(new Object[]{questionid,"试题id",2,qstate,"试题状态",1});
        tQuestionService.updateState(questionid, qstate);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/update")
    @Action("试题管理-修改")
    public ResultVo update(@RequestBody TQuestion question) {
        tQuestionService.updateQuestion(question);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TQuestion tQuestion) {
        N_Utils.validation(new Object[]{tQuestion.getQuestionid(),"试题id",2});
        QuestionVo question = tQuestionService.findById(tQuestion.getQuestionid());
        return new ResultVo(0,"获取详情成功",question);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TQuestion tQuestion) {
        Integer page = tQuestion.getPage();
        Integer pageSize = tQuestion.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        //获取所选章节信息
        if(N_Utils.isNotEmpty(tQuestion.getDirectorid()) && !"0".equals(tQuestion.getDirectorid())){
            TDirector director = directorService.getDirectorById(tQuestion.getDirectorid());
            tQuestion.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tQuestion.setDirectorall(director.getDirectorall());
            }
        }
        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> list = tQuestionService.findAll(tQuestion);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取列表成功",pageResult);
    }

    @PostMapping("/list_director")
    public ResultVo listDirector(@RequestBody TQuestion tQuestion) {
        Integer page = tQuestion.getPage();
        Integer pageSize = tQuestion.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});

        //获取所选章节信息
        if(N_Utils.isNotEmpty(tQuestion.getDirectorid()) && !"0".equals(tQuestion.getDirectorid())){
            TDirector director = directorService.getDirectorById(tQuestion.getDirectorid());
            tQuestion.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                tQuestion.setDirectorall(director.getDirectorall());
            }
        }

        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> list = tQuestionService.listForSelect(tQuestion);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取章节选题列表成功",pageResult);
    }

    @PostMapping("/list_point")
    public ResultVo listByPoint(@RequestBody TQuestion tQuestion) {
        Integer page = tQuestion.getPage();
        Integer pageSize = tQuestion.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        //获取所选知识点信息
        if(N_Utils.isNotEmpty(tQuestion.getPointid()) && !"0".equals(tQuestion.getPointid())){
            TPoint point = pointService.getPointInfoById(tQuestion.getPointid());

            if(point.getSonnum() == 0){
                //没有子知识点，属于叶子节点
                tQuestion.setPointLeaf(1);
            }else{
                //说明有子知识点，直接获取
                tQuestion.setPointLeaf(0);
                tQuestion.setPointall(point.getPointall());
            }
        }

        PageHelper.startPage(page, pageSize);
        tQuestion.setLsbid(null);
        List<Map<String, Object>> list = tQuestionService.listForSelect(tQuestion);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取知识点选题列表成功",pageResult);
    }

    /**
     * 添加
     * @Param [question]
     * @Author ywx
     * @Date 2020/6/8 17:36
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/add")
    @Action("试题管理-增加")
    public ResultVo add(@RequestBody TQuestion question) {
        N_Utils.validation(new Object[]{
                question.getQstem(),"试题题干",2,question.getQlevel(),"试题类型",1
                ,question.getScore(),"试题分值",1,question.getComplexity(),"试题难易度",1
                ,question.getQtypeid(),"试题题型id",2,question.getBelong(),"试题归属",1
        });
        if (CollectionUtils.isEmpty(question.getQuestionPoints())){
            return new ResultVo(1, "试题关联知识点不能为空");
        }
        String id = tQuestionService.save(question);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/save_collect")
    public ResultVo saveCollect(@RequestBody TQuestionCollect collect) {
        N_Utils.validation(new Object[]{collect.getQuestionid(),"试题id",2,collect.getUserid(),"用户id",1});
        tQuestionService.saveCollect(collect);
        return new ResultVo(0,"试题收藏成功");
    }

    @PostMapping("/cancel_collect")
    public ResultVo cancelCollect(@RequestBody TQuestionCollect collect) {
        N_Utils.validation(new Object[]{collect.getQuestionid(),"试题id",2,collect.getUserid(),"用户id",1});
        tQuestionService.cancelCollect(collect);
        return new ResultVo(0,"试题取消收藏成功");
    }

    @PostMapping("/list_collect")
    public ResultVo listCollect(@RequestBody CollectVo collect) {
        List<String> questionids = collect.getIds();
        Integer userid = collect.getUserid();
        if (N_Utils.isListEmpty(questionids)){
            return new ResultVo(1,"试题id集合不能为空");
        }
        N_Utils.validation(new Object[]{userid,"用户id",1});
        List<QuestionCollectVo> list = tQuestionService.listCollect(questionids, userid);
        return new ResultVo(0,"获取试题是否收藏列表成功",list);
    }

    /**
     * @Description 编辑试题,保存为教师个人题库新试题
     * @Param [question]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/7/19 16:55
     **/
    @PostMapping("/suq")
    @Action("试题管理-修改")
    public ResultVo saveUpdateQuestion(@RequestBody TQuestion question) {
        N_Utils.validation(new Object[]{question.getQuestionid(),"试题id",2,
                question.getQstem(),"试题题干",2,question.getScore(),"试题分值",1
                ,question.getComplexity(),"试题难易度",1,question.getQtypeid(),"试题题型id",2,question.getCreateid(),"教师id",1 });

        Map map = tQuestionService.saveUpdateQuestion(question);
        return new ResultVo(0, "保存成功",map);
    }

    /*
     * @Description 获取试题关联知识点
     * @Date 2023/8/28 15:32:38
     * @Author WN
     * @Param [question]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/qpoints")
    public ResultVo listQuestionPoints(@RequestBody TQuestion question) {
        N_Utils.validation(new Object[]{question.getQuestionid(),"试题id",2 });

        List<Map> list = tQuestionService.listPointsByQuestionid(question.getQuestionid());
        return new ResultVo(0, "保存成功",list);
    }

    /**
     * @Description 分享集团校
     * @Date 2023/8/29 9:25
     * @Author YWX
     * @Param [question]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/share_gs")
    @Action("试题管理-分享伙伴校")
    public ResultVo shareGroupSchool(@RequestBody TQuestion question) {
        N_Utils.validation(new Object[]{question.getQuestionid(), "试题id", 2, question.getCreateid(), "用户id", 1});
        return tQuestionService.shareGroupSchool(question);
    }

    /**
     * @Description 获取分享的伙伴校
     * @Date 2023/8/29 11:22
     * @Author YWX
     * @Param [question]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_share_ps")
    public ResultVo listSharePartnerSchool(@RequestBody TQuestion question) {
        String questionid = question.getQuestionid();
        N_Utils.validation(new Object[]{questionid, "试题id", 2});
        List<Map<String, Object>> list=tQuestionService.listSharePartnerSchool(questionid);
        return new ResultVo(0,"获取分享的伙伴校成功",list);
    }

    /*
     * @Description获取一组试题知识点
     * @Date 2023/8/31 15:26:40
     * @Author WN
     * @Param [questionids]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/qids_points")
    public ResultVo listQuestionPoints(@RequestBody List<String> questionids) {
        if(N_Utils.isListEmpty(questionids)){
            return new ResultVo(1,"试题id不能为空");
        }else{
            List<Map> qlist = tQuestionService.listQuestionPoints(questionids);
            return new ResultVo(0,"获取成功",qlist);
        }
    }

}
