package com.xhkjedu.sexam.controller.paper;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sexam.model.notice.TMsg;
import com.xhkjedu.sexam.model.notice.TNotice;
import com.xhkjedu.sexam.model.notice.TNoticeUser;
import com.xhkjedu.sexam.model.paper.EPaperCorrectclass;
import com.xhkjedu.sexam.service.notice.NoticeService;
import com.xhkjedu.sexam.service.paper.EPaperCorrectclassService;
import com.xhkjedu.sexam.vo.paper.EPClassVo;
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.List;
import java.util.Map;

/**
 * @author ywx
 * @classname EPaperCorrectclassController
 * @description 按班批阅
 * @date 2022/7/20 9:49
 **/
@RestController
@RequestMapping("/epc")
public class EPaperCorrectclassController {
    @Resource
    private EPaperCorrectclassService ePaperCorrectclassService;
    @Resource
    private NoticeService noticeService;

    /**
     * 设置批阅人
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/20 10:01
     **/
    @PostMapping("/set")
    @Action("考试测评-设置批阅人")
    public ResultVo setTeacher(@RequestBody EPClassVo 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<EPaperCorrectclass> teachers = vo.getTeachers();
        if (N_Utils.isListEmpty(teachers)) return new ResultVo(1, "教师不能为空");
        ePaperCorrectclassService.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 = ePaperCorrectclassService.getESubjectByEpId(epid);
        Integer schoolid = (Integer) s.get("schoolid");
        String noticetitle = s.get("examname") + "-阅卷按班设置";
        Integer gradeid = N_Utils.str2Int(s.get("gradeid").toString());
        String subjectid = s.get("subjectid").toString();
        String noticecontent = "【" + subjectname + "】";
        if (N_Utils.isNotEmpty(s.get("sdate"))) {
            noticecontent += s.get("sdate") + " " + s.get("begintime") + "-" + s.get("endtime") + "\n请在规定时间段阅卷";
        }
        List<TNoticeUser> users = new ArrayList<>();
        for (EPaperCorrectclass c : teachers) {
            Integer teacherid = c.getTeacherid();
            if (N_Utils.isEmptyInteger(teacherid)) continue;
            if (notice == 1) {
                TNoticeUser nt = new TNoticeUser();
                nt.setUserid(teacherid);
                nt.setGradeid(gradeid);
                nt.setSubjectid(subjectid);
                users.add(nt);
            }
            if (msg == 1) {
                TMsg m = new TMsg();
                m.setMsgtxt(noticetitle + "\n" + noticecontent);
                m.setUserid(teacherid);
                msgList.add(m);
            }
        }
        if (notice == 1) {
            TNotice n = new TNotice();
            n.setNoticetitle(noticetitle);
            n.setNoticecontent(noticecontent);
            n.setUsers(users);
            noticeList.add(n);
        }
        noticeService.batchSave(noticeList, msgList, schoolid, createid, 2);
        return new ResultVo(0, "设置成功");
    }

    /*
     * @Description 批阅人--学校班级批阅人(兼容集团校)
     * @Date 2023/8/29 15:32:44
     * @Author WN
     * @Param [vo]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/detail")
    public ResultVo listSchoolClassCorrect(@RequestBody EPClassVo vo) {
        Integer epid = vo.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        List<Map> list = ePaperCorrectclassService.listClassCorrectByEpId(epid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * 删除批阅人
     * @Param [vo]
     * @Author ywx
     * @Date 17:38 2022/8/1
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/del")
    @Action("考试测评-删除批阅人")
    public ResultVo deleteByEpId(@RequestBody EPClassVo vo) {
        Integer epid = vo.getEpid();
        N_Utils.validation(new Object[]{epid, "试卷id", 1});
        ePaperCorrectclassService.deleteByEpId(epid);
        return new ResultVo(0, "删除成功");
    }
}
