package com.xhkjedu.smarking.controller.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.smarking.mapper.system.UserMapper;
import com.xhkjedu.smarking.model.exam.MsExam;
import com.xhkjedu.smarking.service.exam.MsExamService;
import com.xhkjedu.smarking.service.notice.NoticeService;
import com.xhkjedu.smarking.utils.MarkingUtil;
import com.xhkjedu.smarking.vo.exam.EBaseVo;
import com.xhkjedu.smarking.vo.exam.ExamVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.system.UserVo;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description 阅卷考试表相关操作
 * @Author auto
 * @Date 2024-10-15
 */
@RestController
@RequestMapping("/mexam")
public class MsExamController {
    @Resource
    private MsExamService msExamService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private UserMapper userMapper;

    /**
     * @Description 新增
     * @Param [msExam]
     * @Author auto
     * @Date 2024-10-15
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save")
    public ResultVo save(@RequestBody MsExam msExam) {
        N_Utils.validation(new Object[]{
                msExam.getExamname(), "考试名称", 2
                , msExam.getExamdate(), "考试日期", 2
                , msExam.getGradeid(), "考试年级", 1
                , msExam.getExamtype(), "考试类型", 1
                , msExam.getExammode(), "阅卷模式", 1
                , msExam.getSubjects(), "考试科目", 3
                , msExam.getCreateid(), "创建人id", 1
                , msExam.getSchoolid(), "学校id", 1
        });
        msExamService.add(msExam);
        return new ResultVo(0, "保存成功");
    }

    /**
     * @Description 列表
     * @Param [msExam]
     * @Author auto
     * @Date 2024-10-15
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list")
    public ResultVo queryList(@RequestBody MsExam msExam) {
        Integer page = msExam.getPage();
        Integer size = msExam.getPageSize();
        N_Utils.validation(new Object[]{page, "显示页码", 1, size, "显示条数", 1});
        PageHelper.startPage(page, size);
        List<Map> list = msExamService.queryList(msExam);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "查询成功", pageResult);
    }

    /**
     * @Description 发布考试
     * @Date 2024/10/17 17:57
     * @Author YWX
     * @Param [vo]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/fb")
    public ResultVo postExam(@RequestBody ExamVo vo) {
        Integer notice = vo.getNotice();//是否发送通知1发送2不发送
        Integer msg = vo.getMsg();//是否发送短信1发送2不发送
        String examcomm = vo.getExamcomm();
        Integer createid = vo.getHandleid();
        Integer examid = vo.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1, examcomm, "考试公告", 2, createid, "操作人id", 1});
        EBaseVo exam = msExamService.getExamStateById(examid);//根据考试id获取考试状态
        String examname = exam.getExamname();
        Integer schoolid = exam.getSchoolid();
        if (exam.getExammode() == 3) {
            if (" :00".equals(exam.getStrtime())) return new ResultVo(1, "还有考试试卷未设置时间");
            Long timestamp = Long.valueOf(N_Utils.getSecondTimestamp() + 30 * 60);
            Long begintime = MarkingUtil.strToTimestamp(exam.getStrtime());
            if (begintime.compareTo(timestamp) != 1) return new ResultVo(1, "发布时间必须早于最早科目开始考试之前30分钟");
        }
        if (exam.getWwcnum() != 0) return new ResultVo(1, "还有考试试卷未设置完成");
        if (exam.getPbstate() != 2) return new ResultVo(1, "还有考试试卷题块未设置完成");
        if (exam.getExamstate() != 0) return new ResultVo(0, "发布成功");

        List<UserVo> students = userMapper.listStuForExamByExamId(examid);//获取考生
        Integer code = msExamService.updateExamStateStart(examid, examcomm, students);//发布考试
        if (code == 1) return new ResultVo(1, "发布考试失败");

        if (notice == 2 && msg == 2) return new ResultVo(0, "发布成功");

        vo.setSchoolid(schoolid);
        vo.setHandleid(createid);
        vo.setExamname(examname);
        vo.setGradeid(exam.getGradeid());
        noticeService.batchSave2(vo, userMapper.listStuForNoticeByExamId(examid));
        return new ResultVo(0, "保存成功");
    }

    /**
     * @Description 班级选择列表（创建考试用）
     * @Date 2024/11/6 17:15
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_class")
    public ResultVo listClass(@RequestBody MsExam msExam) {
        Integer createid = msExam.getCreateid();
        N_Utils.validation(new Object[]{createid, "登录人id", 1});
        return new ResultVo(0, "查询成功", msExamService.listClass(createid));
    }

    /**
     * @Description 删除
     * @Date 2024/11/25 16:46
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/delete")
    public ResultVo delete(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.deleteById(examid);
        return new ResultVo(0, "删除成功");
    }

    /**
     * @Description 取消发布
     * @Date 2024/11/25 16:58
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/qx_fb")
    public ResultVo qxFb(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.qxFb(examid);
        return new ResultVo(0, "取消发布成功");
    }

    /**
     * @Description 修改
     * @Date 2024/11/25 17:13
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/update")
    public ResultVo update(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        return msExamService.update(msExam);
    }

    /**
     * @Description 详情
     * @Date 2024/11/26 8:51
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        return new ResultVo(0, "查询成功", msExamService.findById(examid));
    }


    /**
     * @Description 获取考试关联学校
     * @Date 2024/11/26 11:44
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_school")
    public ResultVo listSchool(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        return new ResultVo(0, "查询成功", msExamService.listSchool(examid));
    }

    /**
     * @Description 考试科目列表
     * @Date 2024/11/26 15:17
     * @Author YWX
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_subject")
    public ResultVo listSubject(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        return new ResultVo(0, "查询成功", msExamService.listSubject(examid));
    }

    /*
     * @Description 考试状态
     * @Date 2025/1/7 17:19:25
     * @Author WN
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_state")
    public ResultVo getExamstate(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        return new ResultVo(0, "查询成功", msExamService.getExamstate(examid));
    }

    /*
     * @Description 一键统分
     * @Date 2025/1/8 16:49:38
     * @Author WN
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/yjtf")
    public ResultVo updateExamStateForYjtf(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.updateExamStateForYjtf(examid);
        return new ResultVo(0, "保存成功");
    }
    /*
     * @Description 取消一键统分
     * @Date 2025/1/8 16:49:38
     * @Author WN
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/unyjtf")
    public ResultVo updateExamStateForUnYjtf(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.updateExamStateForUnYjtf(examid);
        return new ResultVo(0, "取消成功");
    }

    /*
     * @Description 发布成绩
     * @Date 2025/1/8 16:52:37
     * @Author WN
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/fbcj")
    public ResultVo updateExamStateForfbcj(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.updateExamStateForfbcj(examid);
        return new ResultVo(0, "发布成功");
    }

    /*
     * @Description 取消发布成绩
     * @Date 2025/1/8 16:53:05
     * @Author WN
     * @Param [msExam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/unfbcj")
    public ResultVo updateExamStateForUnfbcj(@RequestBody MsExam msExam) {
        Integer examid = msExam.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        msExamService.updateExamStateForUnfbcj(examid);
        return new ResultVo(0, "取消成功");
    }


}
