package jnpf.cultivate.controller.web.question;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateQuestionBankService;
import jnpf.model.cultivate.po.question.FtbCultivateQuestionBank;
import jnpf.model.cultivate.req.paper.PaperConfigReq;
import jnpf.model.cultivate.req.questionbank.AddQuestionBankReq;
import jnpf.model.cultivate.req.questionbank.EditQuestionBankReq;
import jnpf.model.cultivate.req.questionbank.QueryQuestionBankReq;
import jnpf.model.cultivate.req.questionbank.UnbindQuestionBankReq;
import jnpf.model.cultivate.resp.CanDeleteMsg;
import jnpf.model.cultivate.resp.QuestionBankVo;
import jnpf.model.enums.CourseEnums;
import jnpf.util.FtbUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web题库模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/questionbank")
public class FtbCultivateQuestionBankController {

    /**
     * 服务对象
     */
    @Resource
    private FtbCultivateQuestionBankService questionBankService;


    /**
     * 分页列出题库列表
     *
     * @param req
     * @return
     */
    @GetMapping("/pageLists")
    public ActionResult<PageListVO<QuestionBankVo>> pageLists(QueryQuestionBankReq req) {

        PageInfo<QuestionBankVo> pageVo = questionBankService.getPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 添加题库
     *
     * @param addQuestionBank
     * @return
     */
    @PostMapping("/add")
    public ActionResult<Boolean> add(@RequestBody @Valid AddQuestionBankReq addQuestionBank) {

        //1、检测名称是否重复
        QueryWrapper<FtbCultivateQuestionBank> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbCultivateQuestionBank::getBankContent, addQuestionBank.getBankContent())
                .eq(FtbCultivateQuestionBank::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        Long count = questionBankService.count(wrapper);
        if (null != count && count > 0) {
            throw new RuntimeException("题库已经存在");
        }
        //2、新增
        questionBankService.insertData(addQuestionBank);
        return ActionResult.success(true);
    }

    /**
     * 编辑题库
     *
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public ActionResult<Boolean> edit(@RequestBody @Valid EditQuestionBankReq req) {
        //1、检测名称是否重复
        QueryWrapper<FtbCultivateQuestionBank> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbCultivateQuestionBank::getBankContent, req.getBankContent())
                .ne(FtbCultivateQuestionBank::getId, req.getId())
                .eq(FtbCultivateQuestionBank::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        Long count = questionBankService.count(wrapper);
        if (null != count && count > 0) {
            throw new RuntimeException("题库名称重复");
        }
        //2修改
        questionBankService.updateData(req);
        return ActionResult.success(true);
    }


    /**
     * 查询题库详情，包括题库关联的课程
     *
     * @param id 题库ID
     * @return
     */
    @GetMapping("/get/{id}")
    public ActionResult<QuestionBankVo> queryInfo(@PathVariable("id") String id) {
        return ActionResult.success(questionBankService.getInfo(id));
    }

    /**
     * 删除题库
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ActionResult<Boolean> del(@PathVariable("id") String id) {
        //1、删除题库
        questionBankService.deleteData(id);
        return ActionResult.success(true);
    }


    /**
     * 检测题库是否可以删除
     *
     * @param id 题库ID
     * @return
     */
    @GetMapping("/check-can-del/{id}")
    public ActionResult<CanDeleteMsg> checkCanDel(@PathVariable("id") String id) {
        return ActionResult.success(questionBankService.checkQuestionBankCanDelete(id));
    }

    /**
     * 题库和课程解绑
     *
     * @param req
     * @return
     */
    @PostMapping("/unbind")
    public ActionResult<Boolean> unbind(@RequestBody @Valid UnbindQuestionBankReq req) {
        questionBankService.unbind(req);
        return ActionResult.success(true);
    }


    /**
     * 查询题库中题目类型分析数据，当选题、多选题多少数据
     *
     * @param id 题库ID
     * @return Map<Integer, QuestionNum>   题目类型->数量
     */
    @GetMapping("/analys/question-count/{id}")
    public ActionResult<Map<String, PaperConfigReq.QuestionNum>> analysQuestionCount(@PathVariable("id") String id) {
        return ActionResult.success(questionBankService.analysQuestionCount(id));
    }


}
