package com.xhkjedu.smarking.controller.scan;

import com.xhkjedu.smarking.model.paper.MsTemplate;
import com.xhkjedu.smarking.model.scan.MsScanbatchPaper;
import com.xhkjedu.smarking.model.scan.MsScanbatchPaperBlock;
import com.xhkjedu.smarking.service.scan.MsScanbatchPaperService;
import com.xhkjedu.smarking.vo.stupaper.PaperStudentWebVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
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
 */
@Slf4j
@RestController
@RequestMapping("/mscanp")
public class MsScanbatchPaperController {
    @Resource
    private MsScanbatchPaperService msScanbatchPaperService;
    

    /*
     * @Description 扫描记录-查看原卷
     * @Date 2024/10/25 17:24:18
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/paper")
    public ResultVo getScanPaper(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        Integer mspid = msScanbatchPaper.getMspid();
        N_Utils.validation(new Object[]{mspid, "扫描试卷id", 1});
        Map<String, Object> map = msScanbatchPaperService.getScanPaperQuestion(mspid);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 扫描试卷-试卷模版信息
     * @Date 2024/11/6 16:50:23
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/paper_temps")
    public ResultVo listTemplates(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        Integer mpid = msScanbatchPaper.getMpid();
        N_Utils.validation(new Object[]{mpid, "试卷id", 1});
        List<MsTemplate> temps = msScanbatchPaperService.listTemplates(mpid);
        return new ResultVo(0, "获取成功", temps);
    }

    /*
     * @Description 保存扫描试卷
     * @Date 2024/11/6 15:58:50
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save")
    public ResultVo saveScanPaper(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        N_Utils.validation(new Object[]{msScanbatchPaper.getBatchid(), "扫描批次id", 1,msScanbatchPaper.getMpid(), "试卷id", 1});
        try{
            msScanbatchPaperService.savePaperIdentify(msScanbatchPaper);
            return new ResultVo(0, "保存成功");
        }catch (Exception e){
            String message = e.getMessage();
            if(!message.contains("禁止扫描")){
                message = "保存失败";
                log.error("保存扫描试卷失败：" + e.getMessage());
            }
            return new ResultVo(1, message);
        }
    }

    /*
     * @Description 扫描记录-查看原卷-客观题-修改客观题题块
     * @Date 2024/11/7 17:10:10
     * @Author WN
     * @Param [msScanbatchPaperBlock]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save_obj")
    public ResultVo saveScanPaperObjective(@RequestBody MsScanbatchPaperBlock msScanbatchPaperBlock) {
        N_Utils.validation(new Object[]{msScanbatchPaperBlock.getMspid(), "扫描试卷id", 1,msScanbatchPaperBlock.getMblockid(), "题块id", 1});
        return msScanbatchPaperService.updateObjectiveQuestionAnswer(msScanbatchPaperBlock);
    }

    /*
     * @Description 扫描异常-异常数量
     * @Date 2024/11/8 16:00:22
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_num")
    public ResultVo getScanErrorNum(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        N_Utils.validation(new Object[]{msScanbatchPaper.getMpid(), "试卷id", 1,msScanbatchPaper.getBatchid(), "批次id", 1});
        Map<String, Object> map = msScanbatchPaperService.getScanErrorNum(msScanbatchPaper.getMpid(),msScanbatchPaper.getBatchid());
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 扫描异常-异常列表
     * @Date 2024/11/8 15:11:01
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_list")
    public ResultVo listScanPaperErrors(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        N_Utils.validation(new Object[]{msScanbatchPaper.getMpid(), "试卷id", 1});
        List<Map<String, Object>> list = msScanbatchPaperService.listScanPaperErrors(msScanbatchPaper);
        return new ResultVo(0, "获取成功", list);
    }

    /*
     * @Description 扫描异常-异常列表-数量
     * @Date 2024/11/11 10:30:02
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_list_num")
    public ResultVo listScanPaperErrorsNum(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        N_Utils.validation(new Object[]{msScanbatchPaper.getMpid(), "试卷id", 1});
        Map<String, Object> map = msScanbatchPaperService.listScanPaperErrorNum(msScanbatchPaper.getMpid());
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * @Description 扫描异常-删除
     * @Date 2024/11/8 15:31:23
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_del")
    public ResultVo updateScanPaperMsstateForDel(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        Integer mspid = msScanbatchPaper.getMspid();
        N_Utils.validation(new Object[]{mspid, "扫描试卷id", 1});
        msScanbatchPaperService.updateScanPaperMsstate(mspid,10);
        return new ResultVo(0, "删除成功");
    }

    /*
     * @Description 扫描异常-还原
     * @Date 2024/11/8 15:33:23
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_res")
    public ResultVo updateScanPaperMsstateForRes(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        Integer mspid = msScanbatchPaper.getMspid();
        N_Utils.validation(new Object[]{mspid, "扫描试卷id", 1});
        msScanbatchPaperService.updateScanPaperMsstate(mspid,1);
        return new ResultVo(0, "还原成功");
    }

    /*
     * @Description 扫描异常-已删除数量
     * @Date 2024/11/8 15:39:36
     * @Author WN
     * @Param [msScanbatchPaper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_delnum")
    public ResultVo getDelScanPaperCount(@RequestBody MsScanbatchPaper msScanbatchPaper) {
        Integer mpid = msScanbatchPaper.getMpid();
        N_Utils.validation(new Object[]{mpid, "试卷id", 1});
        int result = msScanbatchPaperService.getDelScanPaperCount(mpid);
        return new ResultVo(0, "获取成功", result);
    }

    /*
     * @Description 扫描异常--考号异常-考号确认
     * @Date 2024/11/8 17:37:47
     * @Author WN
     * @Param [ps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_eno")
    public ResultVo updateExamnoConfirm(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{ ps.getMspid(), "扫描试卷id", 1, ps.getMpid(), "试卷id", 1, ps.getExamno(), "考号", 2});

        try {
            msScanbatchPaperService.updateExamnoConfirm(ps);
            return new ResultVo(0, "考号确认成功");
        } catch (Exception e) {
            log.error("考号确认失败：" + e.getMessage());
            return new ResultVo(1, "考号确认失败");
        }
    }

    /*
     * @Description 扫描异常--考号异常-考号重复确认
     * @Date 2024/11/11 9:36:32
     * @Author WN
     * @Param [ps]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_eno_repeat")
    public ResultVo updateExamnoRepeatConfirm(@RequestBody PaperStudentWebVo ps) {
        N_Utils.validation(new Object[]{ ps.getMspid(), "扫描试卷id", 1, ps.getMpid(), "试卷id", 1, ps.getExamno(), "考号", 2});

        try {
            msScanbatchPaperService.updateExamnoRepeatConfirm(ps);
            return new ResultVo(0, "考号重复确认成功");
        } catch (Exception e) {
            log.error("考号重复确认失败：" + e.getMessage());
            return new ResultVo(1, "考号重复确认失败");
        }
    }

    /*
     * @Description 扫描异常--缺考异常确认
     * @Date 2024/11/11 10:12:17
     * @Author WN
     * @Param [list]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/error_miss")
    public ResultVo updateExamnoMissConfirm(@RequestBody List<PaperStudentWebVo> list) {
        if (N_Utils.isListEmpty(list)) return new ResultVo(1, "处理数据不能为空");

        for (PaperStudentWebVo ps : list) {
            N_Utils.validation(new Object[]{ps.getMspid(), "扫描试卷id", 1});
        }

        msScanbatchPaperService.updateMissExamConfirm(list);
        return new ResultVo(0, "缺考确认成功");
    }


}
