package com.xhkjedu.smarking.controller.stupaper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentScore;
import com.xhkjedu.smarking.service.stupaper.MsPaperStudentScoreService;
import com.xhkjedu.smarking.vo.stupaper.PaperStuScoreVo;
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;

/**
 * @Description：补录成绩
 * @Author: WN
 * @Date: 2024/11/12 10:57:15
 **/
@Slf4j
@RestController
@RequestMapping("/mstuc")
public class MsPaperStudentScoreController {
    @Resource
    private MsPaperStudentScoreService msPaperStudentScoreService;

    /*
     * @Description 补录成绩-列表
     * @Date 2024/11/12 11:45:44
     * @Author WN
     * @Param [msPaperStudentScore]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list")
    public ResultVo list(@RequestBody MsPaperStudentScore msPaperStudentScore) {
        N_Utils.validation(new Object[]{msPaperStudentScore.getExamid(), "考试id", 1});
        List<Map<String, Object>> list = msPaperStudentScoreService.listPaperStudentScores(msPaperStudentScore.getExamid());
        return new ResultVo(0, "获取成功",list);
    }


    /*
     * @Description 补录成绩-保存单科
     * @Date 2024/11/12 11:00:38
     * @Author WN
     * @Param [msPaperStudentScore]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save")
    public ResultVo save(@RequestBody MsPaperStudentScore msPaperStudentScore) {
        N_Utils.validation(new Object[]{msPaperStudentScore.getMpsid(), "学生试卷id", 1,msPaperStudentScore.getFillscore(),"补录分数",1});
        try {
            msPaperStudentScoreService.saveStudentScore(msPaperStudentScore);
            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/12 11:02:42
     * @Author WN
     * @Param [stuScoreVo]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save_more")
    public ResultVo saveMoreSubject(@RequestBody PaperStuScoreVo stuScoreVo) {
        N_Utils.validation(new Object[]{stuScoreVo.getExamid(), "考试id", 1,stuScoreVo.getStudentid(), "学生id", 1});
        if(N_Utils.isListEmpty(stuScoreVo.getStuscores())){
            return new ResultVo(1, "补录科目不能为空");
        }
        try {
            msPaperStudentScoreService.saveBatchStudentScores(stuScoreVo);
            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/12 16:29:29
     * @Author WN
     * @Param [classStudent]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/stu")
    public ResultVo listClassStudentForScore(@RequestBody MsClassStudent classStudent) {
        Integer examid = classStudent.getExamid();
        Integer classid = classStudent.getClassid();
        N_Utils.validation(new Object[]{examid, "考试id", 1,classid, "班级id", 1});
        List<Map<String, Object>> list = msPaperStudentScoreService.listStudentsForAdd(examid,classid);
        return new ResultVo(0, "获取成功",list);
    }

    /*
     * @Description 补录成绩-新增补录-学生科目分数
     * @Date 2024/11/12 16:38:29
     * @Author WN
     * @Param [classStudent]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/stu_score")
    public ResultVo listStudentSubjectScore(@RequestBody MsClassStudent classStudent) {
        Integer examid = classStudent.getExamid();
        Integer studentid = classStudent.getStudentid();
        N_Utils.validation(new Object[]{examid, "考试id", 1,studentid, "学生id", 1});
        List<Map<String, Object>> list = msPaperStudentScoreService.listStudentSubjectScores(examid,studentid);
        return new ResultVo(0, "获取成功",list);
    }

    /*
     * @Description 补录成绩-删除
     * @Date 2024/11/12 15:40:59
     * @Author WN
     * @Param [stuScore]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/del")
    public ResultVo deleteStuScore(@RequestBody MsPaperStudentScore stuScore) {
        Integer examid = stuScore.getExamid();
        Integer studentid = stuScore.getStudentid();
        N_Utils.validation(new Object[]{examid, "考试id", 1,studentid, "学生id", 1});
        msPaperStudentScoreService.deleteStuScore(examid,studentid);
        return new ResultVo(0, "删除成功");
    }

    /*
     * @Description 补录成绩-补录历史
     * @Date 2024/11/12 11:33:02
     * @Author WN
     * @Param [stuScoreVo]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_history")
    public ResultVo listStuScoreHistory(@RequestBody MsClassStudent stuScoreVo) {
        Integer page = stuScoreVo.getPage();
        Integer size = stuScoreVo.getPageSize();
        N_Utils.validation(new Object[]{stuScoreVo.getExamid(), "考试id", 1,page, "显示页码", 1, size, "显示条数", 1});
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = msPaperStudentScoreService.listStuScoresHistory(stuScoreVo);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功",pageResult);
    }

    /*
     * @Description 补录成绩-导入
     * @Date 2024/11/13 16:09:32
     * @Author WN
     * @Param [stuScoreVo, file]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save_import")
    public ResultVo saveBatchForImport(MsPaperStudentScore stuScoreVo, MultipartFile file) {
        N_Utils.validation(new Object[]{stuScoreVo.getExamid(), "考试id", 1});

        try {
            return msPaperStudentScoreService.saveBatch(stuScoreVo, file);
        }catch (Exception e){
            log.error("补录成绩导入失败", e.getMessage());
            return new ResultVo(1, "保存失败");
        }
    }
}
