package com.xhkjedu.smarking.controller.exam;

import com.xhkjedu.exception.ServiceException;
import com.xhkjedu.smarking.mapper.exam.MsExamMapper;
import com.xhkjedu.smarking.model.exam.MsExam;
import com.xhkjedu.smarking.model.exam.MsSubject;
import com.xhkjedu.smarking.model.notice.*;
import com.xhkjedu.smarking.service.exam.MsSubjectService;
import com.xhkjedu.smarking.service.notice.NoticeService;
import com.xhkjedu.smarking.vo.exam.ExamVo;
import com.xhkjedu.utils.DatesUtil;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description 阅卷考试科目表相关操作
 * @Author auto
 * @Date 2024-10-15
 */
@RestController
@RequestMapping("/msubject")
public class MsSubjectController {
    @Resource
    private MsSubjectService msSubjectService;
    @Resource
    private MsExamMapper msExamMapper;
    @Resource
    private NoticeService noticeService;

    /**
     * @return com.xhkjedu.vo.ResultVo
     * @Description 考试进度总览
     * @Param [msSubject]
     * @Author auto
     * @Date 2024-10-15
     **/
    @PostMapping("/exam_overview")
    public ResultVo examOverview(@RequestBody MsSubject msSubject) {
        N_Utils.validation(new Object[]{msSubject.getExamid(), "考试id", 1});
        return new ResultVo(0, "查询成功", msSubjectService.examOverview(msSubject));
    }

    /**
     * @Description 设置学科组长
     * @Date 2024/10/17 15:48
     * @Author YWX
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/set_subject_leader")
    public ResultVo setSubjectLeader(@RequestBody MsSubject msSubject) {
        N_Utils.validation(new Object[]{
                msSubject.getMsid(), "考试科目id", 1
                , msSubject.getLeaderid(), "学科组长id", 1
                , msSubject.getHandleid(), "设置人id", 1
        });
        msSubjectService.updateById(msSubject);
        return new ResultVo(0, "设置成功");
    }

    /**
     * @Description 设置考试时间
     * @Date 2024/10/17 15:52
     * @Author YWX
     * @Param [exam]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/set_exam_date")
    public ResultVo setExamDate(@RequestBody ExamVo exam) {
        Integer handleid = exam.getHandleid();
        Integer examid = exam.getExamid();
        N_Utils.validation(new Object[]{
                examid, "考试id", 1
                , exam.getSubjects(), "考试科目", 3
                , handleid, "设置人id", 1
        });
        MsExam msExam = msExamMapper.selectByPrimaryKey(examid);
        if (2 <= msExam.getExamstate()) throw new ServiceException("考试已结束禁止操作");
        List<MsSubject> subjects = exam.getSubjects();
        for (MsSubject subject : subjects) {
            subject.setExamid(examid);
            String sdate = subject.getSdate();
            if (N_Utils.isEmpty(sdate) || N_Utils.isEmpty(subject.getBegintime()) || N_Utils.isEmpty(subject.getEndtime())) {
                throw new ServiceException("考试时间不能为空");
            } else if (DatesUtil.dateNan(sdate)) {
                throw new ServiceException("日期格式不正确");
            } else if (N_Utils.isEmpty(subject.getSubjectid()) || N_Utils.isEmpty(subject.getSubjectname())) {
                throw new ServiceException("科目不能为空");
            }
        }
        msSubjectService.setExamDate(subjects, handleid);

        //通知
        String noticetitle = msExam.getExamname() + "-考试设置";
        Integer gradeid = msExam.getGradeid();
        Integer notice = exam.getNotice();
        Integer msg = exam.getMsg();
        List<TNotice> noticeList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        for (MsSubject s : subjects) {
            Integer teacherid = s.getTeacherid();
            if (N_Utils.isEmptyInteger(teacherid)) continue;
            String noticecontent = "【" + s.getSubjectname() + "】";
            if (N_Utils.isNotEmpty(s.getSdate())) {
                noticecontent += s.getSdate() + " " + s.getBegintime() + "-" + s.getEndtime() + "\n请在规定时间段出卷";
            }
            if (notice == 1) {
                TNotice n = new TNotice();
                n.setNoticetitle(noticetitle);
                n.setNoticecontent(noticecontent);
                TNoticeUser nt = new TNoticeUser();
                nt.setUserid(teacherid);
                nt.setGradeid(gradeid);
                nt.setSubjectid(s.getSubjectid());
                n.setUsers(Collections.singletonList(nt));
                noticeList.add(n);
            }
            if (msg == 1) {
                TMsg m = new TMsg();
                m.setMsgtxt(noticetitle + "\n" + noticecontent);
                m.setUserid(teacherid);
                msgList.add(m);
            }
        }
        noticeService.batchSave(noticeList, msgList, msExam.getSchoolid(), handleid, 2);
        return new ResultVo(0, "设置成功");
    }

    /*
     * @Description 考试学科组长权限科目
     * @Date 2024/10/18 15:15:48
     * @Author WN
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/exam_leader_subject")
    public ResultVo getExamSubjectsByLeader(@RequestBody MsSubject msSubject) {
        N_Utils.validation(new Object[]{
                msSubject.getExamid(), "考试id", 1
                , msSubject.getLeaderid(), "学科组长id", 1
        });
        Map<String, Object> map = msSubjectService.getExamAndSubjectByLeaderid(msSubject.getExamid(),msSubject.getLeaderid());
        return new ResultVo(0, "获取成功",map);
    }

    /**
     * @Description 开始阅卷
     * @Date 2024/10/25 9:53
     * @Author YWX
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/start_check")
    public ResultVo startCheck(@RequestBody MsSubject msSubject) {
        Integer msid = msSubject.getMsid();
        Integer handleid = msSubject.getHandleid();
        N_Utils.validation(new Object[]{
                msid, "考试科目id", 1
                , handleid, "设置人id", 1
        });
        msSubjectService.updateCheckState(msid, handleid, 1);
        return new ResultVo(0, "开始成功");
    }

    /**
     * @Description 停止阅卷
     * @Date 2024/10/25 9:54
     * @Author YWX
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/stop_check")
    public ResultVo stopCheck(@RequestBody MsSubject msSubject) {
        Integer msid = msSubject.getMsid();
        Integer handleid = msSubject.getHandleid();
        N_Utils.validation(new Object[]{
                msid, "考试科目id", 1
                , handleid, "设置人id", 1
        });
        msSubjectService.updateCheckState(msid, handleid, 2);
        return new ResultVo(0, "停止成功");
    }

    /**
     * @Description 考试科目选择列表
     * @Date 2024/12/3 11:27
     * @Author YWX
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_sel")
    public ResultVo listForSel(@RequestBody MsSubject msSubject) {
        Integer examid = msSubject.getExamid();
        Integer handleid = msSubject.getHandleid();
        N_Utils.validation(new Object[]{
                examid, "考试id", 1
                , handleid, "操作人id", 1
        });
        return new ResultVo(0, "获取成功", msSubjectService.listForSel(examid, handleid));
    }

    /**
     * @Description 单科设置信息
     * @Date 2024/12/3 17:56
     * @Author YWX
     * @Param [msSubject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_set")
    public ResultVo getSet(@RequestBody MsSubject msSubject) {
        Integer examid = msSubject.getExamid();
        String subjectid = msSubject.getSubjectid();
        N_Utils.validation(new Object[]{
                examid, "考试id", 1
                , subjectid, "科目id", 2
        });
        return new ResultVo(0, "获取成功", msSubjectService.getSet(examid, subjectid));
    }
}
