package com.xhkjedu.slive.controller.liveplay;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.slive.model.gradeclass.TClass;
import com.xhkjedu.slive.model.liveplay.LCourseSection;
import com.xhkjedu.slive.model.liveplay.LCourseSectionAskteacher;
import com.xhkjedu.slive.model.liveplay.LCourseSectionStudent;
import com.xhkjedu.slive.service.liveplay.CourseSectionService;
import com.xhkjedu.slive.vo.liveplay.CSVStudentVo;
import com.xhkjedu.slive.vo.liveplay.CourseParams;
import com.xhkjedu.slive.vo.liveplay.CourseSectionVo;
import com.xhkjedu.slive.vo.liveplay.CourseVo;
import com.xhkjedu.utils.DatesUtil;
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.List;
import java.util.Map;

/**
* @author ywx
* @classname LCourseSectionController
* @description
* @date 2021/01/15 12:25
**/
@RestController
@RequestMapping("/section")
public class CourseSectionController {
    @Resource
    private CourseSectionService courseSectionService;

    @PostMapping("/add")
    @Action("直播课节-增加")
    public ResultVo add(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getCourseid(),"课程id",1,section.getTeacherid(),"教师id",1});
        if (DatesUtil.dateNan(section.getSectiondate())) return new ResultVo(1, "日期格式不正确");
        return courseSectionService.save(section);
    }

    @PostMapping("/batch_add")
    @Action("直播课节-批量增加")
    public ResultVo batchSave(@RequestBody List<LCourseSection> sections) {
        return courseSectionService.batchSave(sections);
    }

    @PostMapping("/delete")
    @Action("直播课节-删除")
    public ResultVo delete(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1,section.getCourseid(),"课程id",1
        ,section.getCreateid(),"用户id",1});
        return courseSectionService.deleteById(section.getSectionid(),section.getCourseid(),section.getCreateid());
    }

    @PostMapping("/update")
    @Action("直播课节-修改")
    public ResultVo update(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1,section.getCreateid(),"用户id",1});
        if (DatesUtil.dateNan(section.getSectiondate())) return new ResultVo(1, "日期格式不正确");
        return courseSectionService.update(section);
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1});
        LCourseSection lCourseSection = courseSectionService.findById(section.getSectionid());
        return new ResultVo(0,"获取课节详情成功",lCourseSection);
    }

    @PostMapping("/xkdetail")
    public ResultVo xkDetail(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1});
        Map map = courseSectionService.xkDetail(section.getSectionid());
        return new ResultVo(0,"获取巡课详情成功",map);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getCourseid(),"课程id",1});
        List<CourseSectionVo> list = courseSectionService.listByCourseId(section.getCourseid(),section.getTeacherid());
        return new ResultVo(0,"获取课程课节列表成功",list);
    }

    /**
     * @Description 开始直播
     * @Param [section]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/update_state")
    public ResultVo updateState(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1});
        section.setSectionstate(2);
        return courseSectionService.updateState(section);
    }

    @PostMapping("/analyze")
    public ResultVo analyze(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1,section.getClassid(),"状态",1});
        Map<String,Integer> map = courseSectionService.analyze(section.getSectionid(), section.getClassid());
        return new ResultVo(0,"获取课节统计成功",map);
    }

    /*@PostMapping("/update_playback")
    public ResultVo updatePlayback(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1
                ,section.getMp4code(),"视频编码格式",2,section.getPlayback(),"回放地址",2});
        courseSectionService.updatePlayback(section);
        return new ResultVo(0,"修改课节回放信息成功");
    }*/

    //教师直播端--课节列表
    @PostMapping("/list_date")
    public ResultVo listByDate(@RequestBody LCourseSection section) {
        String sectiondate = section.getSectiondate();
        N_Utils.validation(new Object[]{sectiondate,"课节日期",2});
        List<CourseSectionVo> list = courseSectionService.listByDate(section);
        return new ResultVo(0,"根据日期获取教师课节列表成功",list);
    }

    //直播巡课--课节列表
    @PostMapping("/list_xkdate2")
    public ResultVo listXkByDate2(@RequestBody CourseParams params) {
        if (params.getUsertype() == null) return new ResultVo(1, "用户类型不能为空");
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getPage(), "显示页码", 1, params.getPageSize(), "显示条数", 1
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1});
        courseSectionService.setClassIds(params);//设置教师任教行政和提高班id
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<CourseSectionVo> list = courseSectionService.listXkByDate(params);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "根据日期获取教师课节列表成功", pageResult);
    }

    /**
     * 全校巡课-课节列表
     *
     * @param [params]
     * @return com.xhkjedu.vo.ResultVo
     * @author ywx
     * @date 2022/4/28 16:14
     */
    @PostMapping("/list_qxxkdate")
    public ResultVo listQxXkByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getPage(), "显示页码", 1, params.getPageSize(), "显示条数", 1
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1});
        params.setUsertype(0);
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<CourseSectionVo> list = courseSectionService.listXkByDate(params);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "根据日期获取教师课节列表成功", pageResult);
    }

    /**
     * 教师听课-课节列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [params]
     * @Author ywx
     * @Date 2022/6/28 9:10
     **/
    @PostMapping("/list_jstkdate")
    public ResultVo listJsTkByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getPage(), "显示页码", 1, params.getPageSize(), "显示条数", 1
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1
                , params.getGradeid(), "年级id", 1, params.getSubjectid(), "科目id", 2});
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<CourseSectionVo> list = courseSectionService.listXkByDate(params);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "根据日期获取教师课节列表成功", pageResult);
    }

    /*
     * 直播管理-月份下日期课节数量
     * @Param [params]
     * @Author ywx
     * @Date 2022/12/6 15:11
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/kj_zbgldate")
    public ResultVo listDateForZbGl(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2, params.getSchoolid(), "学校id", 1});
        List<Map> list = courseSectionService.listDateForZbGl(params);
        return new ResultVo(0, "获取月份下日期课节数量成功", list);
    }

    /*
     * 直播管理-课节列表
     * @Param [params]
     * @Author ywx
     * @Date 2022/12/6 13:45
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_zbgldate")
    public ResultVo listZbGlByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2, params.getSchoolid(), "学校id", 1
                , params.getPage(), "显示页码", 1, params.getPageSize(), "显示条数", 1});
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<Map> list = courseSectionService.listGlByDate(params);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "根据日期获取教师课节列表成功", pageResult);
    }

    /*
     * 直播管理-课节详情
     * @Param [section]
     * @Author ywx
     * @Date 2022/12/6 14:46
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_zbkj")
    public ResultVo listZbKj(@RequestBody LCourseSection section) {
        Integer courseid = section.getCourseid();
        Integer page = section.getPage();
        Integer pageSize = section.getPageSize();
        N_Utils.validation(new Object[]{courseid, "课程id", 1, page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = courseSectionService.listZbKj(courseid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取课节列表成功", pageResult);
    }

    /*
     * 直播管理-学生列表
     * @Param [section]
     * @Author ywx
     * @Date 2022/12/6 15:26
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_zbxs")
    public ResultVo listZbXs(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getClassid(), "班级id", 1});
        TClass c = courseSectionService.getClassById(params.getClassid());
        params.setYear(c.getYear());
        Integer gradeid = c.getGradeid();
        if (N_Utils.isEmptyInteger(params.getPage())) {
            List<Map> list = courseSectionService.listZbXs(params);
            for (Map map : list) {
                map.put("gradeid", gradeid);
            }
            return new ResultVo(0, "获取学生列表成功", list);
        } else {
            PageHelper.startPage(params.getPage(), params.getPageSize());
            List<Map> list = courseSectionService.listZbXs(params);
            for (Map map : list) {
                map.put("gradeid", gradeid);
            }
            PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
            return new ResultVo(0, "获取学生列表成功", pageResult);
        }
    }

    /**
     * 教师巡课-学生列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [params]
     * @Author ywx
     * @Date 2022/12/9 9:06
     **/
    @PostMapping("/list_xkxs")
    public ResultVo listXkXs(@RequestBody CourseParams params) {
        Integer classid = params.getCourseclassid();
        Integer page = params.getPage();
        Integer pageSize = params.getPageSize();
        N_Utils.validation(new Object[]{classid, "直播班级id", 1, params.getSectionid(), "课节id", 1
                , page, "显示页码", 1, pageSize, "显示条数", 1});
        if (N_Utils.isEmptyInteger(params.getClassid())) {
            CourseParams p = courseSectionService.getCourseParamByCid(classid);//获取课程年份和班级ids
            params.setYear(p.getYear());
            params.setClassids(p.getClassids());
        }
        PageHelper.startPage(page, pageSize);
        List<Map> list = courseSectionService.listXkXs(params);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取学生列表成功", pageResult);
    }

    @PostMapping("/update_time")
    public ResultVo updateTime(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(), "课节id", 1
                , section.getTeacherid(), "教师id", 1, section.getTotaltime(), "时长", 1});
        courseSectionService.updateTime(section);
        return new ResultVo(0, "修改成功");
    }

    @PostMapping("/report")
    public ResultVo report(@RequestBody LCourseSection section) {
        Integer sectionid = section.getSectionid();
        N_Utils.validation(new Object[]{sectionid,"课节id",1});
        Map map = courseSectionService.getReport(sectionid);
        return new ResultVo(0,"获取课节报告成功",map);
    }

    /*
     * 教师app课节报告
     * @Param [section]
     * @Author ywx
     * @Date 2022/12/13 17:42
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/report2")
    public ResultVo report2(@RequestBody LCourseSection section) {
        Integer sectionid = section.getSectionid();
        N_Utils.validation(new Object[]{sectionid,"课节id",1});
        Map map = courseSectionService.getReport2(sectionid);
        return new ResultVo(0,"获取课节报告成功",map);
    }

    @PostMapping("/list_class")
    public ResultVo listClass(@RequestBody LCourseSection section) {
        Integer courseid = section.getCourseid();
        N_Utils.validation(new Object[]{courseid,"课程id",1});
        List<Map> list = courseSectionService.listClass(courseid);
        return new ResultVo(0,"获取课程班级列表成功",list);
    }

    @PostMapping("/list_view2")
    public ResultVo listView2(@RequestBody LCourseSectionStudent section) {
        Integer sectionid = section.getSectionid();
        Integer page = section.getPage();
        Integer pageSize = section.getPageSize();
        N_Utils.validation(new Object[]{sectionid, "课节id", 1, page, "显示页码", 1, pageSize, "显示条数", 1});
        CourseVo course = courseSectionService.getCourseVo(section);
        section.setCourseid(course.getCourseid());
        section.setGradeid(course.getGradeid());
        section.setClassids(course.getClassids());
        section.setTotaltime(course.getDuration());
        PageHelper.startPage(page, pageSize);
        List<Map> list = courseSectionService.listView2(section, course.getCoursetype());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取课节观看列表成功", pageResult);
    }

    @PostMapping("/list_stuview")
    public ResultVo listStuView(@RequestBody LCourseSectionStudent section) {
        Integer sectionid = section.getSectionid();
        Integer studentid = section.getStudentid();
        N_Utils.validation(new Object[]{sectionid,"课节id",1,studentid,"学生id",1});
        List<CSVStudentVo> list = courseSectionService.listStuView(section);
        return new ResultVo(0,"获取学生观看详情列表成功",list);
    }

    @PostMapping("/list_ask")
    public ResultVo listAsk(@RequestBody LCourseSection section) {
        Integer sectionid = section.getSectionid();
        N_Utils.validation(new Object[]{sectionid,"课节id",1});
        List<Map> list = courseSectionService.listAsk(sectionid);
        return new ResultVo(0,"获取课节提问列表成功",list);
    }

    @PostMapping("/list_ask_stu")
    public ResultVo listAskStu(@RequestBody LCourseSectionAskteacher ask) {
        String satid = ask.getSatid();
        N_Utils.validation(new Object[]{satid,"课节互动id",2});
        List<Map> list = courseSectionService.listAskStu(satid);
        return new ResultVo(0,"获取课节提问详情成功",list);
    }

    @PostMapping("/list_ask_stu2")
    public ResultVo listAskStu2(@RequestBody LCourseSectionAskteacher ask) {
        String satid = ask.getSatid();
        N_Utils.validation(new Object[]{satid,"课节互动id",2});
        Map list = courseSectionService.listAskStu2(satid);
        return new ResultVo(0,"获取课节提问详情成功",list);
    }

    @PostMapping("/kj_mydate")
    public ResultVo listMySectionDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(),"月份",2,params.getTeacherid(),"用户id",1});
        List<Map> list = courseSectionService.listMySectionDate(params);
        return new ResultVo(0,"获取月份下课节数量成功",list);
    }

    @PostMapping("/list_mydate")
    public ResultVo listMyByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(),"日期",2,params.getTeacherid(),"用户id",1});
        List<Map> list = courseSectionService.listMySection(params);
        return new ResultVo(0,"获取我的课节列表成功",list);
    }

    @PostMapping("/myupdate")
    public ResultVo myUpdate(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(), "课节id", 1, section.getCreateid(), "用户id", 1
                , section.getSectionname(), "课节名称", 2, section.getPlanbegintime(), "计划开始时间", 2, section.getPlanendtime(),
                "计划结束时间", 2});
        if (DatesUtil.dateNan(section.getSectiondate())) return new ResultVo(1, " 日期格式不正确 ");
        return courseSectionService.myUpdate(section);
    }

    @PostMapping("/delete_course")
    @Action("直播课节-删除")
    public ResultVo deleteCourse(@RequestBody LCourseSection section) {
        N_Utils.validation(new Object[]{section.getSectionid(),"课节id",1,section.getCreateid(),"用户id",1});
        return courseSectionService.deleteCourse(section);
    }

    @PostMapping("/list_stu")
    public ResultVo listStudent(@RequestBody CourseParams student) {
        Integer classid = student.getClassid();
        N_Utils.validation(new Object[]{classid, "班级id", 1});
        Integer page = student.getPage();
        String studentname = student.getStudentname();
        CourseParams p = courseSectionService.getCourseParamByCid(classid);//获取课程年份和班级ids
        student.setYear(p.getYear());
        student.setClassids(p.getClassids());
        student.setCourseschool(p.getCourseschool());
        if (N_Utils.isEmptyInteger(page)) {
            List<Map> list = courseSectionService.listStudent(student);
            return new ResultVo(0, "获取课程学生列表成功", list);
        } else {
            Integer pageSize = student.getPageSize();
            N_Utils.validation(new Object[]{pageSize, "显示条数", 1});
            PageHelper.startPage(page, pageSize);
            List<Map> list = courseSectionService.listStudent(student);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
            return new ResultVo(0, "获取课程学生列表成功", pageResult);
        }
    }

    //学生课节日历
    @PostMapping("/kj_studate")
    public ResultVo listStuSectionDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(),"月份",2,params.getStudentid(),"用户id",1});
        List<Map> list = courseSectionService.listStuSectionDate(params);
        return new ResultVo(0,"获取月份下课节数量成功",list);
    }

    //学生课节列表
    @PostMapping("/list_studate")
    public ResultVo listStuByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(),"日期",2,params.getStudentid(),"用户id",1});
        List<Map> list = courseSectionService.listStuByDate(params);
        return new ResultVo(0,"获取课节列表成功",list);
    }

    /**
     * 获取课节主讲人
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [params]
     * @Author ywx
     * @Date 2023/1/3 16:49
     **/
    @PostMapping("/get_zjr")
    public ResultVo getZjr(@RequestBody CourseParams params) {
        Integer sectionid = params.getSectionid();
        N_Utils.validation(new Object[]{sectionid, "课节id", 1});
        List<Map> list = courseSectionService.getZjr(sectionid);
        return new ResultVo(0, "获取成功", list);
    }

    /**
     * 课节是否可以修改
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [params]
     * @Author ywx
     * @Date 2023/1/13 16:51
     **/
    @PostMapping("/is_update")
    public ResultVo isUpdate(@RequestBody CourseParams params) {
        Integer sectionid = params.getSectionid();
        N_Utils.validation(new Object[]{sectionid, "课节id", 1});
        return courseSectionService.isUpdate(sectionid);
    }
}
