package com.yonge.cooleshow.teacher.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonEvaluateSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonEvaluate;
import com.yonge.cooleshow.biz.dal.service.VideoLessonEvaluateService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.valid.impl.ValidList;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonEvaluateVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupDetailUpVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupDetailVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonPurchaseStudent;
import com.yonge.toolset.mybatis.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.VideoLessonGroupDetail;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;

/**
 * 视频课明细表 web 控制层
 *
 * @author yzp
 * @version v1.0
 * @date 2022-03-26 00:21:46
 **/
@RestController
@RequestMapping("/videoLesson")
@Api(tags = "视频课")
public class VideoLessonGroupDetailController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VideoLessonEvaluateService evaluateService;

    @Autowired
    private VideoLessonGroupDetailService videoLessonGroupDetailService;

    /**
     * @Description: 查询视频课
     * @Author: cy
     * @Date: 2022/3/31
     */
    @ApiOperation(value = "查询视频课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/page", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonGroupDetailVo>> page(@RequestBody VideoLessonSearch query) {
        IPage<VideoLessonGroupDetailVo> pages = videoLessonGroupDetailService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * @Description: 根据组id查询购买用户
     * @Author: cy
     * @Date: 2022/4/1
     */
    @ApiOperation(value = "根据组id查询购买用户", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/pageStudent", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonPurchaseStudent>> pageStudent(@RequestBody VideoLessonSearch query) {
        IPage<VideoLessonPurchaseStudent> pages = videoLessonGroupDetailService.selectPageStudent(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 ValidList<VideoLessonGroupDetail> videoLessonList) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        videoLessonGroupDetailService.insertVideoLesson(videoLessonList, sysUser);
        return succeed();
    }

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

        videoLessonGroupDetailService.updateDetail(detailUpVo, sysUser);
        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) {
        videoLessonGroupDetailService.deleteDetail(id);
        return succeed();
    }

    /**
     * @Description: 视频课评价
     * @Author: cy
     * @Date: 2022/4/8
     */
    @ApiOperation(value = "视频课评价", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/evaluate", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> evaluate(@Validated @RequestBody VideoLessonEvaluate evaluate) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        evaluateService.evaluate(evaluate, sysUser);
        return succeed();
    }

    /**
     * @Description: 根据视频id查评论
     * @Author: cy
     * @Date: 2022/4/11
     */
    @ApiOperation(value = "根据视频id查评论")
    @PostMapping(value = "/page")
    public HttpResponseResult<PageInfo<VideoLessonEvaluateVo>> selectEvaluate(@RequestBody VideoLessonEvaluateSearch query) {
        IPage<VideoLessonEvaluateVo> pages = evaluateService.selectEvaluate(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }
}
