package com.yonge.cooleshow.website.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonGroupSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
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.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 视频课基本信息表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("${app-config.url.website:}/videoLessonGroup")
@Api(tags = "视频课组")
public class VideoGroupController extends BaseController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private VideoLessonGroupService videoLessonGroupService;

    @ApiOperation(value = "查询视频课组")
    @PostMapping(value = "/page")
    public HttpResponseResult<PageInfo<VideoLessonGroupVo>> page(@RequestBody VideoLessonGroupSearch query) {
        query.setTeacherId(sysUserService.getUserId());
        IPage<VideoLessonGroupVo> pages = videoLessonGroupService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @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) {
        videoLessonGroup.setTeacherId(sysUserService.getUserId());
        videoLessonGroupService.save(videoLessonGroup);
        return succeed();
    }

    @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();
    }

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

    @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 = sysUserService.getUser();
        lessonVo.setUserBy(sysUser.getId());
        if ( CollectionUtils.isEmpty(lessonVo.getLessonList())) {
            return failed("课程不能为空");
        }
        videoLessonGroupService.add(lessonVo,sysUser);
        return succeed();
    }

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

    @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 = sysUserService.getUser();
        lessonVo.setUserBy(sysUser.getId());
        if ( CollectionUtils.isEmpty(lessonVo.getLessonList())) {
            return failed("课程不能为空");
        }
        return succeed(videoLessonGroupService.update(lessonVo,sysUser));
    }
}
