package com.xhkjedu.sexam.controller.paperstudent;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sexam.model.exam.ESubject;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudent;
import com.xhkjedu.sexam.model.paperstudent.EScanbatch;
import com.xhkjedu.sexam.model.paperstudent.EScanerror;
import com.xhkjedu.sexam.service.paperstudent.EScanbatchService;
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: TODO 答题卡扫描
 * @Author: wn
 * @CreateTime: 2022-08-11  09:43
 */
@RestController
@RequestMapping("/scan")
public class EScanbatchController {
    @Resource
    private EScanbatchService eScanbatchService;

    //保持批次信息
    @PostMapping("/save")
    @Action("考试测评-增加扫描批次")
    public ResultVo saveEscanbatch(@RequestBody EScanbatch eScanbatch) {
        N_Utils.validation(new Object[]{eScanbatch.getEpid(),"试卷id",1,eScanbatch.getDevice(),"设备",2});
        Map map = eScanbatchService.saveScanBatch(eScanbatch);
        return new ResultVo(0, "成功保存", map);
    }

    /**
     * 扫描答题卡列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [subject]
     * @Author ywx
     * @Date 2022/8/11 11:28
     **/
    @PostMapping("/list_sm")
    public ResultVo listScan(@RequestBody ESubject subject) {
        Integer examid = subject.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eScanbatchService.listScan(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 扫描答题卡详情
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eps]
     * @Author ywx
     * @Date 2022/8/11 15:47
     **/
    @PostMapping("/detail")
    public ResultVo getScanDetail(@RequestBody EPaperStudent eps) {
        Integer epid = eps.getEpid();
        Integer examid = eps.getExamid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1, examid, "考试id", 1});
        Map map = eScanbatchService.getScanDetail(epid, examid);
        return new ResultVo(0, "获取详情成功", map);
    }

    /**
     * @description: 试卷扫描答题卡
     * @param: [eScanbatch]
     * @return: com.xhkjedu.vo.ResultVo
     * @author: wn
     * @date:
     **/
    @PostMapping("/sp")
    public ResultVo scanPaperDetail(@RequestBody EScanbatch eScanbatch) {
        Integer epid = eScanbatch.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        Map map = eScanbatchService.scanPaperDetail(epid);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 扫描答题卡详情-学生列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eps]
     * @Author ywx
     * @Date 2022/8/15 16:15
     **/
    @PostMapping("/list_ss")
    public ResultVo listScanStu(@RequestBody EPaperStudent eps) {
        Integer epid = eps.getEpid();
        Integer sstate = eps.getSstate();
        Integer page = eps.getPage();
        Integer pageSize = eps.getPageSize();
        N_Utils.validation(new Object[]{epid, "试卷id", 1, sstate, "扫描状态", 1});
        if (N_Utils.isEmptyInteger(page)) {
            List<Map> list = eScanbatchService.listScanStu(epid, sstate, eps.getCreatename(), eps.getClassid());
            return new ResultVo(0, "获取成功", list);
        } else {
            PageHelper.startPage(page, pageSize);
            List<Map> list = eScanbatchService.listScanStu(epid, sstate, eps.getCreatename(), eps.getClassid());
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
            return new ResultVo(0, "获取成功", pageResult);
        }
    }

    /**
     * 获取扫描答卷识别异常详情
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eps]
     * @Author ywx
     * @Date 2022/8/17 9:44
     **/
    @PostMapping("/edetail")
    public ResultVo getScanErrorDetail(@RequestBody EPaperStudent eps) {
        Integer epid = eps.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        Map map = eScanbatchService.getScanErrorDetail(epid);
        return new ResultVo(0, "获取详情成功", map);
    }
    /**
     * @Description 获取扫描答卷识别异常详情--分类获取
     * @Param [er]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/8/26 14:45
     **/
    @PostMapping("/edetail_type")
    public ResultVo getScanErrorDetailByErrorType(@RequestBody EScanerror er) {
        Integer epid = er.getEpid();
        Integer errortype = er.getErrortype();
        N_Utils.validation(new Object[]{epid, "试卷id", 1,errortype,"错误类型",1});
        Map map = eScanbatchService.getScanErrorDetailByErrorType(epid,errortype);
        return new ResultVo(0, "获取错误信息成功", map);
    }


    /**
     * 删除
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [scanerror]
     * @Author ywx
     * @Date 2022/8/19 14:50
     **/
    @PostMapping("/del")
    @Action("考试测评-删除扫描批次")
    public ResultVo delete(@RequestBody EScanerror scanerror) {
        Integer serrorid = scanerror.getSerrorid();
        N_Utils.validation(new Object[]{serrorid, "扫描出错id", 1});
        eScanbatchService.updateErrorState(serrorid, 10);
        return new ResultVo(0, "删除成功");
    }

    /**
     * 还原
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [scanerror]
     * @Author ywx
     * @Date 2022/8/19 14:51
     **/
    @PostMapping("/back")
    @Action("考试测评-还原扫描批次")
    public ResultVo back(@RequestBody EScanerror scanerror) {
        Integer serrorid = scanerror.getSerrorid();
        N_Utils.validation(new Object[]{serrorid, "扫描出错id", 1});
        eScanbatchService.updateErrorState(serrorid, 0);
        return new ResultVo(0, "还原成功");
    }

    /**
     * 获取批次扫描出错数量
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [scanbatch]
     * @Author ywx
     * @Date 2022/9/16 11:01
     **/
    @PostMapping("/get_errnum")
    public ResultVo getErrNum(@RequestBody EScanbatch scanbatch) {
        String batchname = scanbatch.getBatchname();
        N_Utils.validation(new Object[]{batchname, "批次名称", 2});
        Integer errnum = eScanbatchService.getErrNumByBatchName(batchname);
        return new ResultVo(0, "获取成功", errnum);
    }
}
