package jnpf.cultivate.controller.web.exam;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateExamService;
import jnpf.model.cultivate.po.learn.FtbCultivateLearnTask;
import jnpf.model.cultivate.req.exam.QueryExamForPostReq;
import jnpf.model.cultivate.req.exam.QueryExamReq;
import jnpf.model.cultivate.req.exam.SaveExamReq;
import jnpf.model.cultivate.resp.*;
import jnpf.model.enums.CourseEnums;
import jnpf.permission.entity.PositionEntity;
import jnpf.permission.vo.v2.position.PositionAndGradesVO;
import jnpf.permission.vo.v2.position.PositionVO;
import jnpf.util.FtbUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * web考试模块
 *
 * @author xuguilin
 */
@RestController
@RequestMapping("/exam")
public class FtbCultivateExamController {
    /**
     * 考试服务
     */
    @Resource
    private FtbCultivateExamService examService;


    /**
     * 分页列出考试列表
     *
     * @param req
     * @return
     */
    @GetMapping("/pageLists")
    public ActionResult<PageListVO<ExamListVo>> pageLists(QueryExamReq req) {
        PageInfo<ExamListVo> pageVo = examService.getPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 根据岗位和职能查询考试列表
     *
     * @param queryPaper
     * @return
     */
    @GetMapping("/pageListsForPostAndPosition")
    public ActionResult<PageListVO<ExamListVo>> pageListsForPostAndPosition(QueryExamForPostReq queryPaper) {
        QueryExamReq req = new QueryExamReq();
        req.setPostId(queryPaper.getPostId());
        req.setPostionId(queryPaper.getPostionId());
        req.setKeyword(queryPaper.getKeyword());
        req.setCurrentPage(queryPaper.getCurrentPage());
        req.setPageSize(queryPaper.getPageSize());
        req.setExamType(CourseEnums.ExamType.POSITION.getCode());
        PageInfo<ExamListVo> pageVo = examService.getPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 查询所有批阅角色
     *
     * @return
     */
    @Deprecated
    @GetMapping("/query-all-reviewer-role")
    public ActionResult<List<ReviewerRole>> queryRoleReviewerList() {
        return ActionResult.success( examService.queryRoleReviewerList());
    }


    /**
     * 查询所有批阅人
     *
     * @return
     */
    @GetMapping("/query-all-reviewer")
    public ActionResult<List<ExamBaseUser>> queryReviewerUserList() {
        return ActionResult.success(examService.queryReviewerUserList());
    }


    /**
     * 考试列表-编辑-回显接口
     *
     * @param examId 考试id
     * @return
     */
    @GetMapping("/get/{examId}")
    public ActionResult<ExamVo> get(@PathVariable("examId") String examId) {
        return ActionResult.success(examService.getInfo(examId));
    }


    /**
     * 考试管理-考试列表-批阅-考试详情【考试信息，试卷信息】
     *
     * @param examId 考试ID
     * @return
     */
    @RequestMapping("/queryExamDetail/{examId}")
    public ActionResult<ExamAndPaperDetailVo> queryExamDetail(@PathVariable("examId") String examId) {
        return ActionResult.success(examService.queryExamDetail(examId));
    }


    /**
     * 查询当前考试的试卷信息包括试卷的题目数量
     *
     * @param examId 考试ID
     * @return
     */
    @GetMapping("/queryCurrExamPaperInfo/{examId}")
    public ActionResult<PaperDetailVo> queryCurrExamPaperInfo(@PathVariable("examId") String examId) {
        return ActionResult.success(examService.queryCurrExamPaperInfo(examId));
    }


    /**
     * 添加考试
     *
     * @param req
     * @return
     */
    @PostMapping("/add")
    public ActionResult<Boolean> add(@RequestBody @Valid SaveExamReq req) {
        //新增考试
        examService.insertData(req);
        return ActionResult.success(true);
    }


    /**
     * 编辑考试
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public ActionResult<Boolean> update(@RequestBody @Valid SaveExamReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            //新增考试
            throw new RuntimeException("考试ID为空");
        }
        //修改考试
        examService.updateData(req);

        return ActionResult.success(true);
    }


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


    /**
     * 删除考试
     *
     * @param id 考试ID
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ActionResult<Boolean> del(@PathVariable("id") String id) {
        examService.deleteData(id);
        return ActionResult.success(true);
    }

    /**
     * 查询所有的岗位和职等
     *
     * @return
     */
    @GetMapping("/query-post-and-rank")
    public ActionResult<List<PositionAndGradesVO>> queryPostAndRank() {
        return ActionResult.success(examService.queryPostAndRank());
    }

    /**
     * 根据当前用户所属组织查询有批阅权限的用户列表
     *
     * @return
     */
    @GetMapping("/query-exam-review-list")
    public ActionResult<List<String>> queryExamReviewList() {
        return ActionResult.success("成功", examService.queryExamReviewList(UserProvider.getLoginUserId()));
    }

    /**
     * 查询所有的已经配置的岗位学习
     */
    @GetMapping("/query-all-positon")
    public ActionResult<List<PositionVO>> queryAllPostion() {
        return ActionResult.success("成功", examService.queryAllPostion());
    }


    /**
     * 考试作废-常规考试
     *
     * @param id 考试ID
     * @return
     */
    @PutMapping("/nullify/{id}")
    public ActionResult<Boolean> nullify(@PathVariable("id") String id) {
        examService.nullify(id);
        return ActionResult.success(true);
    }


    /**
     * 考试作废-校验前置接口，判断是否有关联任务
     *
     * @param id 考试ID
     * @return
     */
    @GetMapping("/checkNullify/{id}")
    public ActionResult<List<FtbCultivateLearnTask>> checkNullify(@PathVariable("id") String id) {
        return ActionResult.success(examService.checkNullify(id));
    }
}
