package com.xhkjedu.slive.controller.xz;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.slive.model.xz.LXz;
import com.xhkjedu.slive.service.xz.XzService;
import com.xhkjedu.slive.vo.xz.XzDateKjTeaVo;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *@Description
 *@Author WN
 *Date 2022/3/30 12:09
 **/
@RestController
@RequestMapping("/xz")
public class XzController {

    @Resource
    private XzService xzService;

    /** 行政直播列表
     * @Description
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/3/30 13:55
     **/
    @PostMapping("/list")
    public ResultVo list(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getSchoolid(),"学校id",1,xz.getGradeid(),"年级",1});
        PageHelper.startPage(xz.getPage(), xz.getPageSize());
        List<Map> list = xzService.listXzs(xz);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取行政直播列表成功",pageResult);
    }


   /**
    * @Description 保存行政直播
    * @Param xz
    * @Return com.xhkjedu.vo.ResultVo
    * @Author wn
    * @Date 2022/3/30 13:51
    **/
    @PostMapping("/save")
    @Action("行政直播-增加")
    public ResultVo save(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzname(),"名称",2,xz.getGradeid(),"年级",1,xz.getDaynum(),"课节数量",1,
        xz.getBegindate(),"开始日期",2,xz.getEnddate(),"结束日期",2,xz.getSchoolid(),"学校id",1});
        if (DatesUtil.dateNan(xz.getBegindate()) || DatesUtil.dateNan(xz.getEnddate())) {
            return new ResultVo(1, "日期格式不正确");
        }
        ResultVo vo = xzService.saveXz(xz);
        if (vo.getCode() == 0) {
            xzService.checkClass(xz.getSchoolid(), xz.getGradeid());
        }
        return vo;
    }

    /**
     * @Description 修改行政直播
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/3/30 17:05
     **/
    @PostMapping("/update")
    @Action("行政直播-修改")
    public ResultVo update(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1,xz.getXzname(),"名称",2});
        xzService.updateXz(xz);
        return new ResultVo(0, "修改成功");
    }

    /**
     * @Description 校验班级
     * @Param [xz]
     * @Author ywx
     * @Date 2023/6/1 16:15
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/cc")
    public ResultVo checkClass(@RequestBody LXz xz) {
        Integer schoolid = xz.getSchoolid();
        Integer gradeid = xz.getGradeid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, gradeid, "年级id", 1});
        xzService.checkClass(schoolid, gradeid);
        return new ResultVo(0, "校验班级成功");
    }

    /**
     * @Description 行政直播详细信息
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/2 11:23
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1});
        Map rtnobj = xzService.getXzByXzid(xz.getXzid());
        return new ResultVo(0, "成功获取行政直播信息",rtnobj);
    }

    /**
     * @Description  行政直播下进行中和已结束课数量
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/3/30 15:23
     **/
    @PostMapping("/snum")
    public ResultVo getXzSectionKsNum(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1});
        Integer num = xzService.getXzSectionKsNum(xz.getXzid());
        return new ResultVo(0, "成功获取直播下进行中和结束课数量",num);
    }

    /**
     * @Description 删除行政直播
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/3/30 14:29
     **/
    @PostMapping("/del")
    @Action("行政直播-删除")
    public ResultVo delete(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1});
        xzService.deleteXz(xz.getXzid());
        return new ResultVo(0, "删除成功");
    }

    /**
     * @Description 获取行政直播课程表
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/1 13:57
     **/
    @PostMapping("/xzkb")
    public ResultVo getXzKjDetail(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1,xz.getBegindate(),"开始时间",2});
        Map rtnmap = xzService.getXzKjDetail(xz);
        return new ResultVo(0, "成功获取行政直播课程表",rtnmap);
    }


    /**
     * @Description  保存行政直播课程表(整个)
     * @Param [xz]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/1 9:41
     **/
    @PostMapping("/kcbs")
    public ResultVo saveXzCourse(@RequestBody LXz xz){
        N_Utils.validation(new Object[]{xz.getXzid(),"行政直播id",1});
        return xzService.saveXzCourse(xz);
    }

    /**
     * @Description 删除课程表单个单元格数据
     * @Param [kjTeaVo]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/1 17:52
     **/
    @PostMapping("/kcbd")
    @Action("行政直播-删除课节")
    public ResultVo delXzkjDyg(@RequestBody XzDateKjTeaVo kjTeaVo){
        N_Utils.validation(new Object[]{kjTeaVo.getCourseid(), "课程id", 1, kjTeaVo.getClassid(), "班级id", 1
                , kjTeaVo.getXzid(), "行政直播id", 1});
        return xzService.delXzkjDyg(kjTeaVo);
    }

    /**
     * @Description 保存课程表单个单元格数据（单元格）
     * @Param [kjTeaVo]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/4/2 17:11
     **/
    @PostMapping("/kcbsone")
    @Action("行政直播-增加课节")
    public ResultVo saveOneXzkjDyg(@RequestBody XzDateKjTeaVo kjTeaVo) {
        N_Utils.validation(new Object[]{kjTeaVo.getKjid(), "课节id", 1, kjTeaVo.getClassid(), "班级id", 1, kjTeaVo.getCreateid(), "操作人id", 1,
                kjTeaVo.getTeacherid(), "教师id", 1, kjTeaVo.getSeldate(), "时间", 2});
        return xzService.saveOneXzKjDyg(kjTeaVo);
    }

    @PostMapping("/import")
    @Action("行政直播-导入")
    public ResultVo importKb(LXz xz,MultipartFile file) {
        Integer xzid = xz.getXzid();
        Integer createid = xz.getCreateid();
        N_Utils.validation(new Object[]{xzid, "行政直播id", 1, createid, "创建人id", 1});
        return xzService.importKb(file, xzid, createid);
    }

    /**
     * 获取保存状态
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [xz]
     * @Author ywx
     * @Date 2023/1/28 15:43
     **/
    @PostMapping("/get_savestate")
    public ResultVo getSaveState(@RequestBody LXz xz) {
        Integer xzid = xz.getXzid();
        N_Utils.validation(new Object[]{xzid, "行政直播id", 1});
        Integer savestate = xzService.getSaveStateById(xzid);
        if (savestate == 1) {
            return new ResultVo(1, "有课表在保存中禁止操作", savestate);
        } else {
            return new ResultVo(0, "获取保存状态成功", savestate);
        }
    }

}
