package com.xhkjedu.sexam.controller.exam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sexam.model.exam.EBase;
import com.xhkjedu.sexam.model.exam.EClassStudent;
import com.xhkjedu.sexam.model.exam.ESubject;
import com.xhkjedu.sexam.model.notice.TMsg;
import com.xhkjedu.sexam.model.notice.TNotice;
import com.xhkjedu.sexam.model.notice.TNoticeClass;
import com.xhkjedu.sexam.model.notice.TNoticeTeacher;
import com.xhkjedu.sexam.service.exam.EBaseService;
import com.xhkjedu.sexam.service.notice.NoticeService;
import com.xhkjedu.sexam.service.report.EReportGenerateService;
import com.xhkjedu.sexam.vo.exam.EBaseVo;
import com.xhkjedu.sexam.vo.exam.ExamVo;
import com.xhkjedu.sexam.vo.system.UserVo;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname EBaseController
 * @description 考试基础信息
 * @date 2022/7/18 9:26
 **/
@RestController
@RequestMapping("/eb")
public class EBaseController {
    @Resource
    private EBaseService eBaseService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private EReportGenerateService eReportGenerateService;

    /**
     * 发起考试
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 10:15
     **/
    @PostMapping("/add")
    public ResultVo add(@RequestBody EBase eBase) {
        if (N_Utils.isListEmpty(eBase.getClassids())) return new ResultVo(1, "考试班级不能为空");
        if (N_Utils.isListEmpty(eBase.getSubjects())) return new ResultVo(1, "考试科目不能为空");
        return eBaseService.save(eBase);
    }

    /**
     * 考试设置
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [subjects]
     * @Author ywx
     * @Date 2022/7/18 10:52
     **/
    @PostMapping("/set_exam")
    public ResultVo setExam(@RequestBody ExamVo map) {
        Integer notice = map.getNotice();//是否发送通知1发送2不发送
        Integer msg = map.getMsg();//是否发送短信1发送2不发送
        List<ESubject> subjects = map.getSubjects();
        Integer schoolid = map.getSchoolid();
        Integer createid = map.getHandleid();
        Integer examid = map.getExamid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, createid, "操作人id", 1, examid, "考试id", 1});
        Integer examstate = eBaseService.getExamState(examid);
        if (2 == examstate) return new ResultVo(1, "考试已结束禁止操作");
        List<TNotice> noticeList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        String examname = eBaseService.getNameByExamId(examid);
        String noticetitle = examname + "-考试设置";
        for (ESubject s : subjects) {
            try {
                N_Utils.validation(new Object[]{s.getSubjectid(), "科目id", 2});
                s.setExamid(examid);
                Integer teacherid = s.getTeacherid();
                if (N_Utils.isEmptyInteger(teacherid)) continue;
                String noticecontent = "【" + s.getSubjectname() + "】" + s.getSdate() + " "
                        + s.getBegintime() + "-" + s.getEndtime() + "\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);
                }
            } catch (Exception e) {
                return new ResultVo(1, e.getMessage());
            }
        }
        eBaseService.setExam(subjects, createid);
        
        noticeService.batchSave(noticeList, msgList, schoolid, createid, 2);
        return new ResultVo(0, "设置成功");
    }

    /**
     * 考试删除
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 11:30
     **/
    @PostMapping("/delete")
    public ResultVo delete(@RequestBody EBase eBase) {
        N_Utils.validation(new Object[]{eBase.getExamid(), "考试id", 1});
        eBaseService.deleteById(eBase.getExamid());
        return new ResultVo(0, "删除成功");
    }

    /**
     * 考试修改
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 11:30
     **/
    @PostMapping("/update")
    public ResultVo update(@RequestBody EBase eBase) {
        N_Utils.validation(new Object[]{eBase.getExamid(), "考试id", 1});
        return eBaseService.update(eBase);
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody EBase eBase) {
        Map base = eBaseService.findById(eBase.getExamid());
        return new ResultVo(0, "获取详情成功", base);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody EBase eBase) {
        N_Utils.validation(new Object[]{eBase.getSchoolid(), "学校id"
                , 1, eBase.getPage(), "页码", 1, eBase.getPageSize(), "显示条数", 1});
        PageHelper.startPage(eBase.getPage(), eBase.getPageSize());
        List<Map> list = eBaseService.listAll(eBase);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取列表成功", pageResult);
    }

    /**
     * 发布考试
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 15:21
     **/
    @PostMapping("/fb")
    public ResultVo postExam(@RequestBody ExamVo vo) {
        Integer notice = vo.getNotice();//是否发送通知1发送2不发送
        Integer msg = vo.getMsg();//是否发送短信1发送2不发送
        String examcomm = vo.getExamcomm();
        Integer createid = vo.getHandleid();
        Integer examid = vo.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1, examcomm, "考试公告", 2, createid, "操作人id", 1});
        EBaseVo exam = eBaseService.getExamStateById(examid);//根据考试id获取考试状态
        String examname = exam.getExamname();
        Integer schoolid = exam.getSchoolid();
        if (exam.getWwcnum() != 0) return new ResultVo(1, "还有考试试卷未设置完成");
        if (exam.getAnswered() == 0) return new ResultVo(1, "还有考试试卷未设置正确答案");
        if (exam.getExamstate() != 0) return new ResultVo(0, "发布成功");

        List<UserVo> students = eBaseService.listStuByExamId(examid);//获取考生
        eBaseService.updateExamStateStart(examid, examcomm, students,exam.getExammode());//发布考试

        if (notice == 2 && msg == 2) return new ResultVo(0, "发布成功");

        List<TNotice> noticeList = new ArrayList<>();
        List<TNoticeClass> classList = new ArrayList<>();
        List<TNoticeTeacher> teacherList = new ArrayList<>();
        List<TMsg> msgList = new ArrayList<>();
        String noticetitle = examname + "-考试公告";
        List<Map> classes = eBaseService.listClass(examid);
        String classids = classes.stream().map(c -> c.get("classid").toString()).collect(Collectors.joining(","));
        List<UserVo> teachers = eBaseService.listTeacherByClassIds(classids);//获取考试班级任教老师
        if (notice == 1) {
            for (Map s : classes) {
                TNoticeClass nt = new TNoticeClass();
                nt.setClassid((Integer) s.get("classid"));
                classList.add(nt);
            }
            TNotice n = new TNotice();
            n.setNoticetitle(noticetitle);
            n.setNoticecontent(examcomm);
            n.setNoticetype(1);
            n.setClasses(classList);
            noticeList.add(n);

            for (UserVo t : teachers) {
                TNoticeTeacher nt = new TNoticeTeacher();
                nt.setTeacherid(t.getUserid());
                teacherList.add(nt);
            }
            TNotice n2 = new TNotice();
            n2.setNoticetitle(noticetitle);
            n2.setNoticecontent(examcomm);
            n2.setNoticetype(2);
            n2.setTeachers(teacherList);
            noticeList.add(n2);
        }
        if (msg == 1) {
            String msgtxt = noticetitle + "\n" + examcomm;
            List<String> userphones = students.stream().filter(s -> N_Utils.isNotEmpty(s.getUserphone()))
                    .map(s -> s.getUserphone()).collect(Collectors.toList());
            for (String userphone : userphones) {
                TMsg m = new TMsg();
                m.setMsgtxt(msgtxt);
                m.setUserphone(userphone);
                msgList.add(m);
            }
            userphones = teachers.stream().filter(s -> N_Utils.isNotEmpty(s.getUserphone()))
                    .map(s -> s.getUserphone()).collect(Collectors.toList());
            for (String userphone : userphones) {
                TMsg m = new TMsg();
                m.setMsgtxt(msgtxt);
                m.setUserphone(userphone);
                msgList.add(m);
            }
        }
        noticeService.batchSave2(noticeList, msgList, schoolid, createid);
        return new ResultVo(0, "发布成功");
    }

    /*
     * 保存发布信息
     * @Param [vo]
     * @Author ywx
     * @Date 9:09 2022/8/2
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/save_fb")
    public ResultVo savePostExam(@RequestBody ExamVo vo) {
        String examcomm = vo.getExamcomm();
        Integer examid = vo.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1, examcomm, "考试公告", 2});
        Integer examstate = eBaseService.getExamState(examid);
        if (2 == examstate) return new ResultVo(1, "考试已结束禁止操作");
        eBaseService.savePostExam(examid, examcomm);
        return new ResultVo(0, "保存成功");
    }

    /**
     * 结束考试
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 15:28
     **/
    @PostMapping("/js")
    public ResultVo endExam(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        Integer createid = eBase.getCreateid();
        N_Utils.validation(new Object[]{examid, "考试id", 1, createid, "操作人id", 1});
        EBaseVo vo = eBaseService.getExamStateAnseredById(examid);//获取考试状态和是否设置答案
        //非进行中的考试结束不处理数据，直接返回结束成功
        Integer exammode = vo.getExammode();
        if (2 == exammode && 1 != vo.getAnswered()) return new ResultVo(1, "还有考试试卷未设置正确答案");
        if (1 == exammode && 2 != vo.getAnswered()) return new ResultVo(1, "还有考试试卷未结束");
        if (1 != vo.getExamstate()) return new ResultVo(0, "结束成功");
        try {
            eBaseService.updateExamStateEnd(eBase);
            return new ResultVo(0, "结束成功");
        } catch (Exception e) {
            return new ResultVo(1, "结束失败");
        }
    }

    /**
     * 获取考试班级
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 16:12
     **/
    @PostMapping("/list_class")
    public ResultVo listClass(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        List<Map> list = eBaseService.listClass(examid);
        return new ResultVo(0, "获取考试班级成功", list);
    }

    /**
     * 考生管理--获取考试和班级信息
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/19 10:56
     **/
    @PostMapping("/get_eclass")
    public ResultVo getExamAndClass(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eBaseService.getExamAndClass(examid);
        return new ResultVo(0, "获取考试班级成功", map);
    }

    /**
     * 考生管理--获取考试学生
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/18 16:57
     **/
    @PostMapping("/list_stu")
    public ResultVo listStu(@RequestBody EClassStudent cs) {
        Integer examid = cs.getExamid();
        Integer page = cs.getPage();
        Integer pageSize = cs.getPageSize();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        if (N_Utils.isEmptyInteger(page)) {
            List<Map> list = eBaseService.listStu(cs);
            return new ResultVo(0, "获取考试学生成功", list);
        } else {
            PageHelper.startPage(page, pageSize);
            List<Map> list = eBaseService.listStu(cs);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
            return new ResultVo(0, "获取考试学生成功", pageResult);
        }
    }

    /**
     * 考生管理--移除学生
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [student]
     * @Author ywx
     * @Date 2022/7/19 11:34
     **/
    @PostMapping("/del_stu")
    public ResultVo delStu(@RequestBody EClassStudent student) {
        Integer ecsid = student.getEcsid();
        N_Utils.validation(new Object[]{ecsid, "考试id", 1});
        return eBaseService.delStu(ecsid);
    }

    /**
     * 考试科目列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/19 9:03
     **/
    @PostMapping("/list_subject")
    public ResultVo listSubject(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eBaseService.listSubject(examid);
        return new ResultVo(0, "获取考试科目列表成功", map);
    }

    /**
     * 设置出卷人
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [subject]
     * @Author ywx
     * @Date 2022/7/27 17:28
     **/
    @PostMapping("/set_st")
    public ResultVo setSubjectTeacher(@RequestBody ESubject subject) {
        Integer esid = subject.getEsid();
        Integer teacherid = subject.getTeacherid();
        N_Utils.validation(new Object[]{esid, "考试科目id", 1, teacherid, "出卷人", 1});
        Integer examstate = eBaseService.getExamStateByEsId(esid);
        if (2 == examstate) return new ResultVo(1, "考试已结束禁止修改");
        eBaseService.setSubjectTeacher(subject);
        return new ResultVo(0, "设置成功");
    }

    /**
     * 考试设置状态
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/7/28 9:37
     **/
    @PostMapping("/get_set")
    public ResultVo getSetState(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eBaseService.getSetState(examid);
        return new ResultVo(0, "获取考试设置状态成功", map);
    }

    /**
     * 获取mac编号
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [mac]
     * @Author ywx
     * @Date 2022/8/10 17:40
     **/
    @PostMapping("/get_mn")
    public ResultVo getMacNumber(@RequestBody String mac) {
        N_Utils.validation(new Object[]{mac, "硬件地址", 2});
        String macNumber = eBaseService.getMacNumber(mac);
        return new ResultVo(0, "获取mac编号成功", macNumber);
    }

    /**
     * 考试科目选择列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/9/7 14:23
     **/
    @PostMapping("/list_selsub")
    public ResultVo listSubjectForSel(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        List<Map> list = eBaseService.listSubjectForSel(examid);
        return new ResultVo(0, "获取考试科目列表成功", list);
    }

    /**
     * 生成考试报告
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/9/8 9:56
     **/
    @PostMapping("/scbg")
    public ResultVo generateExamReport(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        Integer createid = eBase.getCreateid();
        N_Utils.validation(new Object[]{examid, "考试id", 1, createid, "操作人", 1});
        try {
            eBaseService.generateExamReport(examid, createid);
            return new ResultVo(0, "生成成功");
        } catch (Exception e) {
            return new ResultVo(1, "生成失败");
        }
    }

    /**
     * 班级选择列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/9/15 11:35
     **/
    @PostMapping("/list_bj")
    public ResultVo listBj(@RequestBody EBase eBase) {
        Integer schoolid = eBase.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        List<Map> list = eBaseService.listBj(schoolid, eBase.getCreateid());
        return new ResultVo(0, "获取班级列表成功", list);
    }

    /**
     * 获取考试状态
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [eBase]
     * @Author ywx
     * @Date 2022/9/30 14:28
     **/
    @PostMapping("/get_zt")
    public ResultVo getExamState(@RequestBody EBase eBase) {
        Integer examid = eBase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Integer examstate = eBaseService.getExamState(examid);
        return new ResultVo(0, "获取考试状态成功", examstate);
    }

}
