package com.xhkjedu.sexam.controller.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sexam.model.exam.ESubject;
import com.xhkjedu.sexam.model.exam.ESubjectInvigilate;
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.service.exam.ESubjectInvigilateService;
import com.xhkjedu.sexam.service.notice.NoticeService;
import com.xhkjedu.sexam.vo.exam.EInVigilateVo;
import com.xhkjedu.sexam.vo.paper.EPTeacherVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
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 ESubjectInvigilateController
 * @description 监考设置
 * @date 2022/7/18 17:38
 **/
@RestController
@RequestMapping("/ei")
public class ESubjectInvigilateController {
    @Resource
    private ESubjectInvigilateService eSubjectInvigilateService;
    @Resource
    private NoticeService noticeService;

    /**
     * 设置监考人--设置
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/19 17:32
     **/
    @PostMapping("/set")
    @Action("考试测评-监考设置")
    public ResultVo setTeacher(@RequestBody EInVigilateVo vo) {
        Integer notice = vo.getNotice();//是否发送通知1发送2不发送
        Integer msg = vo.getMsg();//是否发送短信1发送2不发送
        Integer esid = vo.getEsid();
        String subjectid = vo.getSubjectid();
        Integer createid = vo.getCreateid();
        String subjectname = vo.getSubjectname();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, subjectid, "科目id", 2
                , createid, "操作人id", 1, subjectname, "科目名称", 2});
        List<ESubjectInvigilate> teachers = vo.getTeachers();
        if (N_Utils.isListEmpty(teachers)) return new ResultVo(1, "监考教师不能为空");
        eSubjectInvigilateService.setTeacher(esid, subjectid, createid, teachers);
        if (notice == 2 && msg == 2) return new ResultVo(0, "设置成功");
        
        List<TNotice> noticeList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        Map s = eSubjectInvigilateService.getESubjectByEsId(esid);
        Integer schoolid = (Integer) s.get("schoolid");
        String noticetitle = s.get("examname") + "-监考设置";
        Integer gradeid = N_Utils.str2Int(s.get("gradeid").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 (ESubjectInvigilate ei : teachers) {
            Integer teacherid = ei.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, "设置成功");
    }

    /**
     * 监考设置列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [subject]
     * @Author ywx
     * @Date 2022/7/20 17:43
     **/
    @PostMapping("/list")
    public ResultVo listSubject(@RequestBody ESubject subject) {
        Integer examid = subject.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eSubjectInvigilateService.listSubject(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 监考人查看
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/21 9:11
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody ESubject vo) {
        Integer esid = vo.getEsid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1});
        Map list = eSubjectInvigilateService.listByEsId(esid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * 监考人删除
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [vo]
     * @Author ywx
     * @Date 2022/7/21 9:17
     **/
    @PostMapping("/del")
    @Action("考试测评-删除监考人")
    public ResultVo delete(@RequestBody ESubject vo) {
        Integer esid = vo.getEsid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1});
        eSubjectInvigilateService.delByEsId(esid);
        return new ResultVo(0, "删除成功");
    }

    /**
     * 教师监考-监控列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [subject]
     * @Author ywx
     * @Date 2022/7/22 10:10
     **/
    @PostMapping("/list_jk")
    public ResultVo listJkSubject(@RequestBody ESubject subject) {
        Integer teacherid = subject.getTeacherid();
        Integer esstate = subject.getEsstate();
        Integer page = subject.getPage();
        Integer pageSize = subject.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectInvigilateService.listJkSubject(0, teacherid, esstate);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * @Description 教师监考-监控列表（兼容集团校）
     * @Date 2023/8/30 15:24
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_jkjt")
    public ResultVo listJtJkSubject(@RequestBody ESubject subject) {
        Integer teacherid = subject.getTeacherid();
        Integer esstate = subject.getEsstate();
        Integer schoolid = subject.getSchoolid();
        Integer page = subject.getPage();
        Integer pageSize = subject.getPageSize();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1, schoolid, "学校id", 1
                , page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectInvigilateService.listJtJkSubject(0, teacherid, esstate, schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * @Description 获取待监考数量
     * @Date 2024/9/18 11:07
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/get_jk_num")
    public ResultVo getJtJkSubjectNum(@RequestBody ESubject subject) {
        Integer teacherid = subject.getTeacherid();
        N_Utils.validation(new Object[]{teacherid, "教师id", 1});
        return new ResultVo(0, "获取成功", eSubjectInvigilateService.getJtJkSubjectNum(teacherid));
    }

    /*
     * 管理员监考-监控列表
     * @Param [subject]
     * @Author ywx
     * @Date 10:25 2022/8/2
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_ejk")
    public ResultVo listExamJkSubject(@RequestBody ESubject subject) {
        Integer examid = subject.getExamid();
        Integer esstate = subject.getEsstate();
        Integer page = subject.getPage();
        Integer pageSize = subject.getPageSize();
        N_Utils.validation(new Object[]{examid, "考试id", 1, page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectInvigilateService.listJkSubject(examid, 0, esstate);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * @Description 管理员监考-监控列表（兼容集团校）
     * @Date 2023/8/30 15:44
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_ejkjt")
    public ResultVo listExamJtJkSubject(@RequestBody ESubject subject) {
        Integer examid = subject.getExamid();
        Integer esstate = subject.getEsstate();
        Integer schoolid = subject.getSchoolid();
        Integer page = subject.getPage();
        Integer pageSize = subject.getPageSize();
        N_Utils.validation(new Object[]{examid, "考试id", 1, schoolid, "学校id", 1
                , page, "页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eSubjectInvigilateService.listJtJkSubject(examid, 0, esstate, schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /*
     * 缺考学生列表
     * @Param [vo]
     * @Author ywx
     * @Date 2022/11/3 8:13
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_qkxs")
    public ResultVo listMissStu(@RequestBody EPTeacherVo vo) {
        String classids = vo.getClassids();
        Integer esid = vo.getEsid();
        N_Utils.validation(new Object[]{classids, "班级ids", 1, esid, "考试科目id", 1});
        List<Map> list = eSubjectInvigilateService.listMissStu(vo);
        return new ResultVo(0, "获取成功", list);
    }
}
