package com.xhkjedu.sexam.controller.paper;

import com.xhkjedu.sexam.model.notice.TMsg;
import com.xhkjedu.sexam.model.notice.TNotice;
import com.xhkjedu.sexam.model.notice.TNoticeTeacher;
import com.xhkjedu.sexam.model.paper.EPaper;
import com.xhkjedu.sexam.model.paper.EPaperCorrectquestion;
import com.xhkjedu.sexam.service.notice.NoticeService;
import com.xhkjedu.sexam.service.paper.EPaperCorrectquestionService;
import com.xhkjedu.sexam.vo.paper.EPQuestionVo;
import com.xhkjedu.utils.N_Utils;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/epq")
public class EPaperCorrectquestionController {
    @Resource
    private EPaperCorrectquestionService ePaperCorrectquestionService;
    @Resource
    private NoticeService noticeService;

    /**
     * 设置批阅人
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/20 10:14
     **/
    @PostMapping("/set")
    public ResultVo setTeacher(@RequestBody EPQuestionVo vo) {
        Integer notice = vo.getNotice();//是否发送通知1发送2不发送
        Integer msg = vo.getMsg();//是否发送短信1发送2不发送
        Integer epid = vo.getEpid();
        Integer createid = vo.getCreateid();
        String subjectname = vo.getSubjectname();
        N_Utils.validation(new Object[]{epid, "试卷id", 1, createid, "操作人id", 1, subjectname, "科目名称", 2});
        List<EPaperCorrectquestion> teachers = vo.getTeachers();
        if (N_Utils.isListEmpty(teachers)) return new ResultVo(1, "教师不能为空");
        ePaperCorrectquestionService.setTeacher(epid, createid, teachers);
        if (notice == 2 && msg == 2) return new ResultVo(0, "设置成功");

        List<TNotice> noticeList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        Map s = ePaperCorrectquestionService.getESubjectByEpId(epid);
        Integer schoolid = (Integer) s.get("schoolid");
        String noticetitle = s.get("examname") + "-阅卷按题设置";
        for (EPaperCorrectquestion q : teachers) {
            Integer teacherid = q.getTeacherid();
            if (N_Utils.isEmptyInteger(teacherid)) continue;
            String noticecontent = "【" + subjectname + "】" + s.get("sdate") + " "
                    + s.get("begintime") + "-" + s.get("endtime") + "\n请在规定时间段阅卷";
            if (notice == 1) {
                TNotice n = new TNotice();
                n.setNoticetitle(noticetitle);
                n.setNoticecontent(noticecontent);
                TNoticeTeacher nt = new TNoticeTeacher();
                nt.setTeacherid(teacherid);
                n.setTeachers(Arrays.asList(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, schoolid, createid, 2);
        return new ResultVo(0, "设置成功");
    }

    /**
     * 查看批阅人
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/20 13:38
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody EPQuestionVo vo) {
        Integer epid = vo.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        List<Map> list = ePaperCorrectquestionService.listByEpId(epid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * 获取主观题题号
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/21 16:38
     **/
    @PostMapping("/list_qn")
    public ResultVo listQn(@RequestBody EPaper vo) {
        Integer epid = vo.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        List<Map> list = ePaperCorrectquestionService.listQnByEpId(epid);
        return new ResultVo(0, "获取成功", list);
    }
}
