package com.xhkjedu.smarking.controller.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.smarking.model.exam.MsSubject;
import com.xhkjedu.smarking.model.exam.MsSubjectInvigilate;
import com.xhkjedu.smarking.model.notice.*;
import com.xhkjedu.smarking.service.exam.MsSubjectInvigilateService;
import com.xhkjedu.smarking.service.notice.NoticeService;
import com.xhkjedu.smarking.vo.exam.EInVigilateVo;
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.*;

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

/**
 * @Description 阅卷科目监考分配表相关操作
 * @Author auto
 * @Date 2024-10-15
 */
@RestController
@RequestMapping("/minvigilate")
public class MsSubjectInvigilateController {
    @Resource
    private MsSubjectInvigilateService msSubjectInvigilateService;
    @Resource
    private NoticeService noticeService;

    /**
     * @return com.xhkjedu.vo.ResultVo
     * @Description 新增
     * @Param [msSubjectInvigilate]
     * @Author auto
     * @Date 2024-10-15
     **/
    @PostMapping("/set")
    public ResultVo setTeacher(@RequestBody EInVigilateVo vo) {
        Integer notice = vo.getNotice();//是否发送通知1发送2不发送
        Integer msg = vo.getMsg();//是否发送短信1发送2不发送
        Integer msid = vo.getMsid();
        String subjectid = vo.getSubjectid();
        Integer createid = vo.getCreateid();
        String subjectname = vo.getSubjectname();
        N_Utils.validation(new Object[]{msid, "考试科目id", 1, subjectid, "科目id", 2
                , createid, "操作人id", 1, subjectname, "科目名称", 2});
        List<MsSubjectInvigilate> teachers = vo.getTeachers();
        if (N_Utils.isListEmpty(teachers)) return new ResultVo(1, "监考教师不能为空");
        msSubjectInvigilateService.setTeacher(msid, subjectid, createid, teachers);
        if (notice == 2 && msg == 2) return new ResultVo(0, "设置成功");

        List<TNotice> noticeList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        Map s = msSubjectInvigilateService.getSubjectByMsId(msid);
        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 (MsSubjectInvigilate 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
     * @Description 监考设置列表
     * @Param [subject]
     * @Author auto
     * @Date 2024-10-15
     **/
    @PostMapping("/list")
    public ResultVo queryList(@RequestBody MsSubject subject) {
        Integer examid = subject.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = msSubjectInvigilateService.listSubject(examid);
        return new ResultVo(0, "查询成功", map);
    }

    /**
     * @Description 监考查看
     * @Date 2024/10/18 16:02
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody MsSubject subject) {
        Integer msid = subject.getMsid();
        N_Utils.validation(new Object[]{msid, "考试科目id", 1});
        Map list = msSubjectInvigilateService.listByMsId(msid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * @Description 删除单科考试监考人
     * @Date 2024/10/18 16:11
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/del")
    public ResultVo delete(@RequestBody MsSubject subject) {
        Integer msid = subject.getMsid();
        N_Utils.validation(new Object[]{msid, "考试科目id", 1});
        msSubjectInvigilateService.delByMsId(msid);
        return new ResultVo(0, "删除成功");
    }

    /**
     * @Description 教师监考-监控列表（兼容集团校）
     * @Date 2024/10/18 16:48
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_jkjt")
    public ResultVo listJtJkSubject(@RequestBody MsSubject subject) {
        Integer teacherid = subject.getTeacherid();
        Integer msstate = subject.getMsstate();
        Integer schoolid = subject.getSchoolid();
        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 = msSubjectInvigilateService.listJtJkSubject(0, teacherid, msstate, schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "查询成功", pageResult);
    }

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

    /**
     * @Description 管理员监考-监控列表（兼容集团校）
     * @Date 2024/10/18 16:56
     * @Author YWX
     * @Param [subject]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_ejkjt")
    public ResultVo listExamJtJkSubject(@RequestBody MsSubject subject) {
        Integer examid = subject.getExamid();
        Integer msstate = subject.getMsstate();
        Integer schoolid = subject.getSchoolid();
        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 = msSubjectInvigilateService.listJtJkSubject(examid, 0, msstate, schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "查询成功", pageResult);
    }
}
