package jnpf.cultivate.controller.web.paper;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateTestPaperService;
import jnpf.model.cultivate.req.paper.QueryPaperReq;
import jnpf.model.cultivate.req.paper.SavePaperReq;
import jnpf.model.cultivate.resp.CanDeleteMsg;
import jnpf.model.cultivate.resp.PaperDetailVo;
import jnpf.model.cultivate.resp.PaperListVo;
import jnpf.util.FtbUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * web试卷表模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/paper")
public class FtbCultivateTestPaperController {

    /**
     * 服务对象
     */
    @Resource
    private FtbCultivateTestPaperService paperService;


    /**
     * 分页列出试卷列表
     *
     * @param req
     * @return
     */
    @GetMapping("/pageLists")
    public ActionResult<PageListVO<PaperListVo>> pageLists(QueryPaperReq req) {

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

    /**
     * 添加/修改试卷
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public ActionResult<Boolean> save(@RequestBody @Valid SavePaperReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            //新增试卷
            paperService.insertData(req);
        } else {
            //修改试卷
            paperService.updatePaperAndQuestion(req);
        }
        return ActionResult.success(true);
    }


    /**
     * 修改试卷
     *
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public ActionResult<Boolean> edit(@RequestBody @Valid SavePaperReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            throw new RuntimeException("参数错误,试卷ID不能够为空");
        }
        paperService.updatePaperAndQuestion(req);

        return ActionResult.success(true);
    }


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

    /**
     * 修改试卷（新）
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public ActionResult<Boolean> update(@RequestBody @Valid SavePaperReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            //新增试卷
            throw new RuntimeException("试卷ID不能为空");
        }
        //修改试卷
        paperService.updatePaperAndQuestion(req);

        return ActionResult.success(true);
    }

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


    /**
     * 启用禁用试卷
     *
     * @param id     试卷ID
     * @param status 状态（0启用，1禁用）
     * @return
     */
    @PutMapping("/switch-status/{id}/{status}")
//    @SaCheckPermission(PAPER_PERMISSION_PRE + "btn_paper_switch_status")
    public ActionResult<Boolean> switchEnabledMark(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        paperService.switchEnabledMark(id, status);
        return ActionResult.success(true);
    }


    /**
     * 设置正式试卷
     *
     * @param id
     * @return
     */
    @PutMapping("/set-formal-paper/{id}")
    public ActionResult<Boolean> setFormalPaper(@PathVariable("id") String id) {
        paperService.setFormalPaper(id);
        return ActionResult.success(true);
    }

    /**
     * web试卷列表-查看试卷基本信息，主要用于复制回显
     *
     * @param id 试卷ID
     * @return
     */
    @GetMapping("/get/{id}")
    public ActionResult<PaperDetailVo> queryDetail(@PathVariable("id") String id) {
        return ActionResult.success(paperService.getInfo(id));
    }


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

    /**
     * 查询试卷详情和试卷题目列表
     *
     * @param id 试卷id
     * @return
     */
    @RequestMapping("/queryPaperInfoAndQuestionList/{id}")
    public ActionResult<PaperDetailVo> queryQuestionList(@PathVariable("id") String id) {
        return ActionResult.success(paperService.queryPaperInfoAndQuestionList(id));
    }

}
