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.liveplay.LCourse;
import com.xhkjedu.slive.service.liveplay.CourseService;
import com.xhkjedu.slive.vo.liveplay.*;
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 LCourseController
* @description
* @date 2021/01/15 12:25
**/
@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;

    @PostMapping("/add")
    @Action("直播管理-增加")
    public ResultVo add(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getSubjectid(),"科目id",2});
        if (DatesUtil.dateNan(course.getBegindate()) || DatesUtil.dateNan(course.getEnddate())) {
            return new ResultVo(1, "日期格式不正确");
        }
        return courseService.save(course);
    }

    @PostMapping("/delete")
    @Action("直播管理-删除")
    public ResultVo delete(@RequestBody LCourse course) {
        Integer createid = course.getCreateid();
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,createid,"用户id",1});
        return courseService.deleteById(course.getCourseid(),createid);
    }

    @PostMapping("/update")
    @Action("直播管理-修改")
    public ResultVo update(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,course.getCreateid(),"用户id",1});
        return courseService.update(course);
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1});
        CourseVo lCourse = courseService.findById(course);
        return new ResultVo(0,"获取课程详情成功",lCourse);
    }

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

    @PostMapping("/selection")
    public ResultVo selection(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,course.getCreateid(),"学生id",1});
        return courseService.selection(course);
    }

    @PostMapping("/cancel_selection")
    public ResultVo cancelSelection(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,course.getCreateid(),"学生id",1});
        return courseService.cancelSelection(course);
    }

    @PostMapping("/list_stu")
    public ResultVo listStu(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getPage(),"显示页码",1
                ,course.getPageSize(),"显示条数",1,course.getCreateid(),"学生id",1});
        PageHelper.startPage(course.getPage(), course.getPageSize());
        List<CourseVo> list = courseService.listStu(course);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取学生课程列表成功",pageResult);
    }

    @PostMapping("/on_section")
    public ResultVo findOnSection(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1});
        Integer cisectionid = courseService.findOnSection(course.getCourseid());
        return new ResultVo(0,"获取进行中的课节id成功",cisectionid);
    }

    @PostMapping("/update_state")
    @Action("直播管理-结束课程")
    public ResultVo updateState(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1
                ,course.getTeacherid(),"用户id",1});
        course.setCoursestate(3);//结束课程
        return courseService.updateState(course);
    }

    //添加学生
    @PostMapping("/add_stu")
    @Action("直播管理-添加学生")
    public ResultVo addStu(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,course.getCreatename(),"学生账号",2
                ,course.getTeacherid(),"用户id",1});
        return courseService.addStu(course);
    }

    //给课节未结束的课程添加巡课学生 (帮忙处理数据时用的，系统未调用)
    @PostMapping("/add_xkstu")
    public ResultVo addXkStu(@RequestBody Map user) {
        String loginname = user.get("loginname").toString();
        N_Utils.validation(new Object[]{loginname,"学生账号",2});
        return courseService.addXkStu(loginname);
    }

    //给今天以后行政和提高班的课程添加学生(帮忙处理数据时用的，系统未调用)
    @PostMapping("/add_xzstu")
    public ResultVo adXzStu(@RequestBody Map user) {
        String loginname = user.get("loginname").toString();
        N_Utils.validation(new Object[]{loginname,"学生账号",2});
        return courseService.addXzStu(loginname);
    }

    //删除学生
    @PostMapping("/del_stu")
    @Action("直播管理-删除学生")
    public ResultVo delStu(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getCourseid(),"课程id",1,course.getCreateid(),"学生id",1
                ,course.getTeacherid(),"用户id",1});
        return courseService.delStu(course);
    }
    //删除学生所有直播课 (帮忙处理数据时用的，系统未调用)
    @PostMapping("/del_xzstu")
    public ResultVo delXzStu(@RequestBody Map user) {
        String loginname = user.get("loginname").toString();
        N_Utils.validation(new Object[]{loginname,"学生账号",2});
        return courseService.delXzStu(loginname);
    }

    //删除学生指定班级直播课 (帮忙处理数据时用的，系统未调用)
    @PostMapping("/del_cstu")
    public ResultVo delClassStu(@RequestBody Map user) {
        String loginname = user.get("loginname").toString();
        Integer classid = (Integer) user.get("classid");
        N_Utils.validation(new Object[]{loginname,"学生账号",2,classid,"班级id",1});
        return courseService.delClassStu(loginname, classid);
    }

    //添加学生指定班级直播课 (帮忙处理数据时用的，系统未调用)
    @PostMapping("/add_cstu")
    public ResultVo adClassStu(@RequestBody Map user) {
        String loginname = user.get("loginname").toString();
        Integer classid = (Integer) user.get("classid");
        N_Utils.validation(new Object[]{loginname,"学生账号",2,classid,"班级id",1});
        return courseService.addClassStu(loginname, classid);
    }

    //删除指定班级直播课
    @PostMapping("/del_class")
    @Action("直播管理-删除班级直播课")
    public ResultVo delClass(@RequestBody CourseParams params) {
        Integer courseid = N_Utils.str2Int(params.getSectionname());
        Integer classid = params.getClassid();
        String classids = params.getClassids();
        N_Utils.validation(new Object[]{courseid,"课程id",1,classid,"班级id",1,classids,"班级ids",2});
        return courseService.delClass(courseid, classid, classids);
    }

    //删除指定班级直播课（课程班级里没有班级信息）
    @PostMapping("/del_class2")
    @Action("直播管理-删除班级直播课")
    public ResultVo delClass2(@RequestBody CourseParams params) {
        Integer courseid = N_Utils.str2Int(params.getSectionname());
        Integer classid = params.getClassid();
        String classids = params.getClassids();
        N_Utils.validation(new Object[]{courseid,"课程id",1,classid,"班级id",1,classids,"班级ids",2});
        return courseService.delClass2(courseid, classid, classids);
    }

    @PostMapping("/listforstu")
    public ResultVo listForStu(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getSchoolid(),"学校id",1,course.getCreateid(),"学生id",1});
        List<CourseTypeVo> lst = courseService.listAllForStu(course);
        return new ResultVo(0,"获取课程列表成功",lst);
    }

    @PostMapping("/list_type")
    public ResultVo listType(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getSchoolid(),"学校id",1,course.getCreateid(),"学生id",1});
        List<CTypeVo> lst = courseService.listType(course);
        return new ResultVo(0,"获取课程分类列表成功",lst);
    }

    @PostMapping("/listforstu2")
    public ResultVo listForStu2(@RequestBody LCourse course) {
        N_Utils.validation(new Object[]{course.getSchoolid(),"学校id",1,course.getCreateid(),"学生id",1
        ,course.getCtypeid(),"课程分类",1});
        List<CourseVo> lst = courseService.listAllForStu2(course);
        return new ResultVo(0,"获取课程列表成功",lst);
    }

    //学生直播端--指定月份课程日期数量
    @PostMapping("/kj_date")
    public ResultVo listSectionDateForStu(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getStudentid(),"学生id",1,params.getTimestr(),"月份",2});
        List<SectionDateVo> lst = courseService.listSectionDateForStu(params);
        return new ResultVo(0,"获取月份下日期课节数量成功",lst);
    }

    //学生直播端--指定日期的课节列表
    @PostMapping("/kj_list")
    public ResultVo listSectionsByDate(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getStudentid(),"学生id",1,params.getTimestr(),"日期",2});
        List<CourseSectionVo> lst = courseService.listSectionsByDate(params);
        return new ResultVo(0,"获取日期下课节集合成功",lst);
    }

    //教师直播端--指定月份课程日期数量
    @PostMapping("/kj_tdate")
    public ResultVo listSectionDateForTeacher(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(),"月份",2});
        List<SectionDateVo> lst = courseService.listSectionDateForTeacher(params);
        return new ResultVo(0,"获取月份下日期课节数量成功",lst);
    }

    //直播巡课--指定月份课程日期数量
    @PostMapping("/kj_xkdate2")
    public ResultVo listSectionDateForXk2(@RequestBody CourseParams params) {
        if (params.getUsertype() == null) return new ResultVo(1, "用户类型不能为空");
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1});
        List<Map> lst = courseService.listSectionDateForXk2(params);
        return new ResultVo(0, "获取月份下日期课节数量成功", lst);
    }

    /**
     * 全校巡课-课节数量
     *
     * @param [params]
     * @return com.xhkjedu.vo.ResultVo
     * @author ywx
     * @date 2022/4/28 16:12
     */
    @PostMapping("/kj_qxxkdate")
    public ResultVo listSectionDateForQxXk(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1});
        params.setUsertype(0);
        List<Map> lst = courseService.listSectionDateForXk2(params);
        return new ResultVo(0, "获取月份下日期课节数量成功", lst);
    }

    /**
     * 教师听课-课节数量
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [params]
     * @Author ywx
     * @Date 2022/6/28 9:20
     **/
    @PostMapping("/kj_jstkdate")
    public ResultVo listSectionDateForJsTk(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getTimestr(), "月份", 2
                , params.getTeacherid(), "用户id", 1, params.getSchoolid(), "学校id", 1
                , params.getGradeid(), "年级id", 1, params.getSubjectid(), "科目id", 2});
        params.setUsertype(0);
        List<Map> list = courseService.listSectionDateForXk2(params);
        return new ResultVo(0, "获取月份下日期课节数量成功", list);
    }

    //处理直播学生 (帮忙处理数据时用的，系统未调用)
    @PostMapping("/handle_stu")
    public ResultVo handleStu(@RequestBody CourseParams params) {
        N_Utils.validation(new Object[]{params.getSchoolid(), "学校id", 1});
        courseService.handleStu(params);
        return new ResultVo(0, "处理直播学生成功");
    }

    /**
     * @Description 修改课程名称
     * @Param [c]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/1 14:41
     **/
    @PostMapping("/upname")
    @Action("直播管理-修改课程名称")
    public ResultVo updateCoursename(@RequestBody LCourse c){
        N_Utils.validation(new Object[]{c.getCourseid(),"课程id",1,c.getCoursename(),"课程名称",2});
        courseService.updateCoursename(c);
        return new ResultVo(0, "成功修改课程名称");
    }

    /**
     * @Description 修改课程简介
     * @Param [c]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/1 14:41
     **/
    @PostMapping("/upcomm")
    @Action("直播管理-修改课程简介")
    public ResultVo updateCoursecomm(@RequestBody LCourse c){
        N_Utils.validation(new Object[]{c.getCourseid(),"课程id",1});
        courseService.updateCoursecomm(c);
        return new ResultVo(0, "成功修改课程简介");
    }
}
