package com.yonge.cooleshow.teacher.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonGroupSearch;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.valid.AddGroup;
import com.yonge.cooleshow.biz.dal.valid.UpdateGroup;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupUpVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonStudentVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonVo;
import com.yonge.toolset.mybatis.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;

import javax.validation.constraints.NotNull;

/**
 * 视频课基本信息表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("/videoLessonGroup")
@Api(tags = "视频课组")
public class VideoLessonGroupController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private VideoLessonGroupService videoLessonGroupService;

    /**
    * @Description: 查询视频课组
    * @Author: cy
    * @Date: 2022/3/31
    */
    @ApiOperation(value = "查询视频课组")
    @PostMapping(value = "/page")
    public HttpResponseResult<PageInfo<VideoLessonGroupVo>> page(@RequestBody VideoLessonGroupSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        query.setTeacherId(sysUser.getId());
        IPage<VideoLessonGroupVo> pages = videoLessonGroupService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
    * @Description: 新增视频课组
    * @Author: cy
    * @Date: 2022/3/31
    */
    @ApiOperation(value = "新增视频课组", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/create", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> create(@Validated @RequestBody VideoLessonGroup videoLessonGroup) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        videoLessonGroup.setTeacherId(sysUser.getId());
        videoLessonGroupService.save(videoLessonGroup);
        return succeed();
    }

    /**
    * @Description: 修改视频课组
    * @Author: cy
    * @Date: 2022/3/31
    */
    @ApiOperation(value = "修改视频课组", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/updateGroup", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> update(@Validated @RequestBody VideoLessonGroupUpVo lessonGroupUpVo) {
        videoLessonGroupService.updateGroup(lessonGroupUpVo);
        return succeed();
    }

    /**
    * @Description: 删除视频课组
    * @Author: cy
    * @Date: 2022/3/31
    */
    @ApiOperation(value = "删除视频课组", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable Long id) {
        videoLessonGroupService.deleteGroup(id);
        return succeed();
    }

    /**
    * @Description: 新增视频课组&视频课
    * @Author: cy
    * @Date: 2022/4/1
    */
    @ApiOperation(value = "新增视频课组&视频课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/add", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> add(@Validated(AddGroup.class) @RequestBody VideoLessonVo lessonVo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        if (sysUser == null||sysUser.getId()==null) {
            return failed("用户信息获取失败");
        }
        if (lessonVo.getLessonList().isEmpty()) {
            return failed("课程不能为空");
        }
        videoLessonGroupService.add(lessonVo,sysUser);
        return succeed();
    }

    /**
     * @Description: 根据组id查询视频课&视频课组
     * @Author: cy
     * @Date: 2022/4/2
     */
    @ApiOperation(value = "根据组id查询视频课&视频课组")
    @GetMapping(value="/selectVideoLesson")
    public HttpResponseResult<VideoLessonStudentVo> selectVideoLesson(@NotNull(message = "视频组id不能为空") Long groupId) {
        return succeed(videoLessonGroupService.selectVideoLesson(groupId));
    }

    /**
     * @Description: 修改视频课组&视频课
     * @Author: cy
     * @Date: 2022/4/7
     */
    @ApiOperation(value = "修改视频课组&视频课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/update", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> update(@Validated(UpdateGroup.class) @RequestBody VideoLessonVo lessonVo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        if (sysUser == null||sysUser.getId()==null) {
            return failed("用户信息获取失败");
        }
        if (lessonVo.getLessonList().isEmpty()) {
            return failed("课程不能为空");
        }
        videoLessonGroupService.update(lessonVo,sysUser);
        return succeed();
    }
}
