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.paperstudent.EPsqbatch;
import com.xhkjedu.sexam.model.paperstudent.EPsqbatchDetail;
import com.xhkjedu.sexam.service.paperstudent.EPsqbatchService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Description 考试补录
 * @Author ywx
 * Date 2022/11/21 14:12
 **/
@RestController
@RequestMapping("/epsb")
@Slf4j
public class EPsqbatchController {
    @Resource
    private EPsqbatchService ePsqbatchService;

    //获取考试关联的科目
    @PostMapping("/subjects")
    public ResultVo listHistorys(@RequestBody EPsqbatch ePsqbatch) {
        try {
            N_Utils.validation(new Object[]{ePsqbatch.getExamid(), "考试id", 1});
            List<Map> list = ePsqbatchService.listSubjectsByExamid(ePsqbatch.getExamid());
            return new ResultVo(0, "成功获取考试科目",list);
        }catch (Exception e){
            log.error("获取考试科出错："+e.getMessage());
            return new ResultVo(0, "获取考试科目失败");
        }
    }

    //补录成绩列表
    @PostMapping("/list")
    public ResultVo listEpsBatchs(@RequestBody EPsqbatch ePsqbatch) {
        try {
            Integer page = ePsqbatch.getPage();
            Integer pageSize = ePsqbatch.getPageSize();
            Integer examid= ePsqbatch.getExamid();
            N_Utils.validation(new Object[]{examid, "考试id", 1,page,"当前页",1,pageSize,"每页条数",1});
            PageHelper.startPage(page, pageSize);
            List<Map> rtnlst = ePsqbatchService.listPsqBatchsByExamid(examid);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(rtnlst));
            Map map = ePsqbatchService.getExamInfo(examid);
            Map rtnmap = new TreeMap();
            rtnmap.put("total",pageResult.getTotal());
            rtnmap.put("list",pageResult.getList());
            rtnmap.put("exam",map);
            return new ResultVo(0, "成功获取补录成绩信息",rtnmap);
        }catch (Exception e){
            log.error("获取获取补录成绩信息出错：" + e.getMessage());
            return new ResultVo(0, "获取失败");
        }
    }

    //补录成绩详情
    @PostMapping("/detail")
    public ResultVo listPsqBatchDetail(@RequestBody EPsqbatchDetail ePsqbatchDetail) {
        try {
            Integer page = ePsqbatchDetail.getPage();
            Integer pageSize = ePsqbatchDetail.getPageSize();
            N_Utils.validation(new Object[]{ePsqbatchDetail.getBatchid(), "补录批次id", 1,page,"当前页",1,pageSize,"每页条数",1});
            PageHelper.startPage(page, pageSize);
            List<Map> rtnlst = ePsqbatchService.listPsqBatchDetail(ePsqbatchDetail);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(rtnlst));
            return new ResultVo(0, "成功获取补录成绩详细信息",pageResult);
        }catch (Exception e){
            log.error("获取获取补录成绩详细信息出错：" + e.getMessage());
            return new ResultVo(0, "获取失败");
        }
    }

    /*
     * 补录成绩导入
     * @Param [file, batch]
     * @Author ywx
     * @Date 2022/11/21 15:14
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/import")
    @Action("考试测评-导入补录成绩")
    public ResultVo importRecord(EPsqbatch batch, MultipartFile file) {
        N_Utils.validation(new Object[]{batch.getExamid(), "考试id", 1, batch.getSubjectname(), "科目名称", 2
                , batch.getEpid(), "试卷id", 1, batch.getTeacherid(), "教师id", 1});
        return ePsqbatchService.importRecord(file, batch);
    }


}
