package com.xhkjedu.smarking.controller.paper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.smarking.model.exam.MsSubject;
import com.xhkjedu.smarking.model.paper.MsPaper;
import com.xhkjedu.smarking.model.paper.MsPaperBack;
import com.xhkjedu.smarking.model.paper.MsPaperFile;
import com.xhkjedu.smarking.service.paper.MsPaperService;
import com.xhkjedu.smarking.vo.paper.MsPaperParams;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
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;

/**
 * @Description 阅卷试卷表相关操作
 * @Author auto
 * @Date 2024-10-15
 */
@RestController
@RequestMapping("/mp")
public class MsPaperController {
    @Resource
    private MsPaperService msPaperService;

    /*
     * @Description 教师出卷列表
     * @Date 2024/10/18 10:52:58
     * @Author WN
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_teacher")
    public ResultVo examTeacherPaperList(@RequestBody MsSubject subject){
        Integer page = subject.getPage();
        Integer pageSize = subject.getPageSize();
        N_Utils.validation(new Object[]{subject.getTeacherid(),"教师id",1,page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page, pageSize);
        List<Map<String,Object>> rtnlst = msPaperService.listExamPapersByTeacherid(subject.getTeacherid(), subject.getPstate());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(rtnlst));
        return new ResultVo(0,"成功获取教师出卷信息",pageResult);
    }

    /*
     * @Description 试卷--退回
     * @Date 2024/10/18 11:02:17
     * @Author WN
     * @Param [paperBack]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/back_save")
    @Action("考试测评-退回试卷")
    public ResultVo saveExamPaperBack(@RequestBody MsPaperBack paperBack){
        N_Utils.validation(new Object[]{paperBack.getMpid(),"试卷id",1});
        msPaperService.savePaperBack(paperBack);
        return new ResultVo(0,"成功退回试卷",null);
    }

    /*
     * @Description 试卷--退回原因列表
     * @Date 2024/10/18 11:02:22
     * @Author WN
     * @Param [paperBack]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/back_list")
    public ResultVo listPaperBacks(@RequestBody MsPaperBack paperBack){
        N_Utils.validation(new Object[]{paperBack.getMpid(),"试卷id",1});
        List<Map<String,Object>> list = msPaperService.listPaperBacksByEpid(paperBack.getMpid());
        return new ResultVo(0,"成功获取试卷退回原因",list);
    }

    /*
     * @Description 试卷试题-原卷试题及切割坐标
     * @Date 2024/12/4 14:41:19
     * @Author WN
     * @Param [paperBack]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/que_coor")
    public ResultVo listPaperQuestions(@RequestBody MsPaperBack paperBack){
        N_Utils.validation(new Object[]{paperBack.getMpid(),"试卷id",1});
        List<Map<String,Object>> list = msPaperService.listPaperQuestionCoors(paperBack.getMpid());
        return new ResultVo(0,"成功获取试题",list);
    }

    /*
     * @Description 试卷试题-保存切割
     * @Date 2024/12/4 15:21:19
     * @Author WN
     * @Param [paperBack]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/que_cut")
    public ResultVo updatePaperQuestionCoor(@RequestBody MsPaperParams params){
        N_Utils.validation(new Object[]{params.getMpid(),"试卷id",1});
        if(params.getQuestions() == null || params.getQuestions().size() == 0){
            return new ResultVo(1,"未切割试题");
        }
        msPaperService.savePaperQuestionCoors(params);
        return new ResultVo(0,"保存成功");
    }

    /*
     * @Description 试卷文件-保存
     * @Date 2024/10/17 16:01:07
     * @Author WN
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/file_save")
    public ResultVo savePaperFiles(@RequestBody MsPaper paper){
        N_Utils.validation(new Object[]{paper.getMpid(),"试卷id",1});
        List<MsPaperFile> pfiles = paper.getPfiles();
        if(pfiles == null || pfiles.isEmpty()){
            return new ResultVo(1,"未上传文件");
        }
        msPaperService.savePaperFiles(paper);
        return new ResultVo(0,"保存成功");
    }
    /*
     * @Description 试卷文件-删除
     * @Date 2024/12/4 15:35:16
     * @Author WN
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/file_del")
    public ResultVo deletePaperFiles(@RequestBody MsPaper paper){
        N_Utils.validation(new Object[]{paper.getMpid(),"试卷id",1});
        msPaperService.deletePaperFiles(paper.getMpid());
        return new ResultVo(0,"删除成功");
    }
}
