package jnpf.cultivate.v2.controller.web.exam;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.v2.service.V2CultivateExamService;
import jnpf.cultivate.v2.service.V2CultivatePositionService;
import jnpf.cultivate.v2.service.V2CultivateTaskService;
import jnpf.model.cultivate.req.exam.WebRestartExamReq;
import jnpf.model.cultivate.v2.exam.req.*;
import jnpf.model.cultivate.v2.exam.vo.*;
import jnpf.util.FtbUtil;
import jnpf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 考试管理[web]
 *
 * @author yanwenfu
 */
@RestController
@RequestMapping("/v2/web/exam")
public class V2CultivateExamController {

    @Autowired
    private V2CultivateExamService v2CultivateExamService;

    @Autowired
    private V2CultivatePositionService v2CultivatePositionService;

    @Autowired
    private V2CultivateTaskService cultivateTaskService;

    @PutMapping(value = "/test/del-question")
    public void testDelQuestion(@RequestBody List<String> questionIds) {
        v2CultivateExamService.questionChangeDeal(questionIds);
    }

    /**
     * 考试列表[分页]
     *
     * @param req 查询参数
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO < jnpf.model.cultivate.v2.exam.vo.V2ExamListVo>>
     */
    @GetMapping("/list/page")
    public ActionResult<PageListVO<V2ExamListVo>> getPage(V2QueryExamReq req) {

        PageInfo<V2ExamListVo> pageVo = v2CultivateExamService.getPage(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 我的考试列表[web](分页)
     *
     * @param queryDto 查询参数
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO < jnpf.model.cultivate.v2.exam.vo.V2ExamListVo>>
     */
    @GetMapping("/my-exam/page")
    public ActionResult<PageListVO<MyExamWebVo>> getMyExamWebPage(MyExamWebQueryDto queryDto) {

        PageInfo<MyExamWebVo> pageVo = v2CultivateExamService.getMyExamWebPage(queryDto);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 新增岗位/自定义考试
     *
     * @param req 考试参数
     * @return jnpf.base.ActionResult<java.lang.Boolean>
     */
    @PostMapping
    public ActionResult<Boolean> addExam(@RequestBody @Valid V2SaveExamReq req) throws Exception {

        v2CultivateExamService.addExam(req);
        return ActionResult.success(true);
    }

    /**
     * 编辑岗位/自定义考试
     *
     * @param id  考试id
     * @param req 考试参数
     * @return jnpf.base.ActionResult<java.lang.Boolean>
     */
    @PutMapping("/{id}")
    public ActionResult<Boolean> updateExam(@PathVariable(value = "id") String id, @RequestBody @Valid V2SaveExamReq req) throws Exception {

        v2CultivateExamService.updateExam(id, req);
        return ActionResult.success(true);
    }

    /**
     * 查询考试详情
     *
     * @param id   考试id
     * @param copy 是否复制(1: 是, 0: 否)
     * @return jnpf.base.ActionResult<java.lang.Boolean>
     */
    @GetMapping("/detail/{id}")
    public ActionResult<V2ExamVo> getDetail(@PathVariable("id") String id, @RequestParam(value = "copy") Integer copy) {

        V2ExamVo vo = v2CultivateExamService.getDetail(id, copy);
        return ActionResult.success(vo);
    }

    /**
     * 获取考试绑定状态-
     *
     * @param examId 考试id
     * @return jnpf.base.ActionResult<java.lang.Boolean> true-有绑定关系  false-无绑定关系
     */
    @GetMapping("/checkBindingStatus/{examId}")
    public ActionResult<Boolean> checkBindingStatus(@PathVariable("examId") String examId) {
        //查询是否有岗位学习绑定有考试
        boolean hasPositionBinding = v2CultivatePositionService.checkExamBinding(examId);
        if (hasPositionBinding) {
            return ActionResult.success(true);
        }

        //查询是否有学习任务绑定有考试
        boolean hasTaskBinding = cultivateTaskService.checkExamBinding(examId);
        if (hasTaskBinding) {
            return ActionResult.success(true);
        }
        //只要有一个是绑定关系，则返回true
        return ActionResult.success(false);
    }

    /**
     * 批阅详情
     *
     * @param id 考试id
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.V2ExamDetailVo>
     */
    @GetMapping("/mark-detail/{id}")
    public ActionResult<V2ExamDetailVo> getMarkDetail(@PathVariable("id") String id) {

        V2ExamDetailVo v2ExamDetail = v2CultivateExamService.getMarkDetail(id);
        return ActionResult.success(v2ExamDetail);
    }

    /**
     * 查看考卷详情
     *
     * @param userExamId 用户的考试id
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.V2ExamDetailVo>
     */
    @GetMapping("/paper-detail/{userExamId}")
    public ActionResult<PaperDetailVo> getMarkPaperDetail(@PathVariable("userExamId") String userExamId) throws Exception {

        PaperDetailVo paperDetail = v2CultivateExamService.getMarkPaperDetail(userExamId);
        return ActionResult.success(paperDetail);
    }

    /**
     * 删除用户的考试
     * @param userExamId 用户的考试id
     * @return java.lang.Object
     */
    @DeleteMapping(value = "/user-exam/{userExamId}")
    public Object delUserExam(@PathVariable(value = "userExamId") String userExamId) throws Exception {

        v2CultivateExamService.delUserExam(userExamId);
        return ActionResult.success();
    }

    /**
     * 批阅详情 - 列表[分页]
     *
     * @param req 考试id
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.V2ExamDetailVo>
     */
    @GetMapping("/mark-detail/page")
    public ActionResult<PageListVO<V2TestPaperVo>> getMarkDetailPage(MarkDetailQueryReq req) {

        PageInfo<V2TestPaperVo> page = v2CultivateExamService.getMarkDetailPage(req);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * 删除考试
     *
     * @param id 考试id
     * @return jnpf.base.ActionResult<java.lang.Boolean>
     */
    @DeleteMapping("/{id}")
    public ActionResult<Boolean> delExam(@PathVariable("id") String id) throws Exception {

        v2CultivateExamService.delExam(id);
        return ActionResult.success(true);
    }

    /**
     * 抽取题目数量及分值
     *
     * @param bankIds 题库ids(英文逗号分隔)
     * @return jnpf.base.ActionResult<jnpf.model.cultivate.v2.exam.vo.BankAnalysisVo>
     */
    @GetMapping(value = "/bank-analysis")
    public ActionResult<BankAnalysisVo> getBankAnalysis(@RequestParam(value = "bankIds") String bankIds) throws Exception {

        if (StringUtil.isBlank(bankIds)) {
            throw new Exception("请选择题库");
        }
        BankAnalysisVo vo = v2CultivateExamService.getBankAnalysis(List.of(bankIds.split(",")));
        return ActionResult.success(vo);
    }

    /**
     * 选择题目
     *
     * @param req 查询条件
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO < jnpf.model.cultivate.v2.exam.vo.QuestionVo>>
     */
    @GetMapping(value = "/question/list")
    public ActionResult<PageListVO<QuestionVo>> getQuestionPage(QuestionReq req) {

        PageInfo<QuestionVo> page = v2CultivateExamService.getQuestionPage(req);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }


    /**
     * 设置重新考试
     * 用于批量重置用户的考试状态，允许用户重新参加考试
     *
     * @param req 重新考试请求参数
     *            - examId: 考试主键 ID（必填）
     *            - startTime: 常规考试开始时间，格式 yyyy-MM-dd HH:mm:ss
     *            - endTime: 常规考试结束时间，格式 yyyy-MM-dd HH:mm:ss
     *            - userExamIdList: 需要重考的用户考试 ID 集合
     *            - versionBatch: 版本批次（必填）
     * @return ActionResult<Boolean> 操作结果，成功返回 true
     */
    @PostMapping("/setRestartExam")
    public ActionResult<Boolean> setRestartExam(@Valid @RequestBody WebRestartExamReq req) {
        v2CultivateExamService.webRestartExam(req);
        return ActionResult.success("操作成功", true);
    }
}
