package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.VideoGroupSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroupDetail;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.valid.AddGroup;
import com.yonge.cooleshow.biz.dal.valid.SelectGroup;
import com.yonge.cooleshow.biz.dal.vo.*;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @Author: cy
 * @Date: 2022/4/2
 */
@RestController
@RequestMapping("/videoLesson")
@Api(tags = "视频课")
@Validated
public class VideoLessonController extends BaseController {
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VideoLessonGroupService lessonGroupService;
    @Autowired
    private VideoLessonGroupDetailService videoLessonGroupDetailService;

    @ApiOperation(value = "根据视频课组id查详情")
    @GetMapping(value = "/selectGroupById")
    public HttpResponseResult<VideoLessonGroup> selectGroupById(@NotNull Long groupId) {
        return succeed(lessonGroupService.selectGroup(groupId));
    }

    /**
     * @Description: 学员详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/2
     */
    @ApiOperation(value = "学员详情-查询视频课列表")
    @PostMapping(value = "/student")
    public HttpResponseResult<PageInfo<VideoLessonStudentDetailVo>> selectStudentPage(@Validated(AddGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectStudentPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-查询视频课列表")
    @PostMapping(value = "/teacher")
    public HttpResponseResult<PageInfo<VideoLessonTeacherVo>> selectTeacherPage(@Validated(AddGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectTeacherPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-视频课详情-查询购买学员列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-视频课详情-查询购买学员列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/teacher/purchaseStudent", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonPurchaseVo>> selectPurchaseStudent(@Validated(SelectGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectPurchaseStudent(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-视频课详情-教学计划
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-视频课详情-教学计划", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/teacher/plan", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonGroupDetailVo>> page(@Validated(SelectGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(videoLessonGroupDetailService.selectPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 审核-视频课组列表
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-视频课组列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/queryGroupList", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonAuthGroup>> queryGroupList(@RequestBody VideoGroupSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.queryGroupList(PageUtil.getPage(search), search)));
    }

    @ApiOperation(value = "审核-视频课组历史记录列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/queryGroupHistoryList", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonAuthGroup>> queryGroupHistoryList(@RequestBody VideoGroupSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.queryGroupHistoryList(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 审核-根据视频课组id查视频课详情
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-根据视频课组id查视频课详情")
    @GetMapping("/authGroupDetil")
    public HttpResponseResult<VideoLessonAuthGroup> authGroupDetil(
            @ApiParam(value = "当前视频组id", required = true) @RequestParam(value = "videoGroupId") Long videoGroupId) {
        return succeed(lessonGroupService.authGroupDetil(videoGroupId));
    }

    /**
     * @Description: 审核-根据视频课组id查视频课详情
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-根据视频课组id查视频课详情")
    @GetMapping("/queryLessonInfo")
    public HttpResponseResult<List<VideoLessonGroupDetail>> queryLessonInfo(Integer groupId) {
        QueryWrapper<VideoLessonGroupDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_lesson_group_id_", groupId);
        return succeed(videoLessonGroupDetailService.list(queryWrapper));
    }



    /**
     * @Description: 审核-修改视频课组审核状态
     * @Author: cy
     * @Date: 2022/4/25
     */
    @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 VideoLessonExamineVo examineVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        examineVo.setAuditId(user.getId());
        examineVo.setAuditName(user.getUsername());
        lessonGroupService.updateGroup(examineVo);
        return succeed();
    }

    /**
     * @Description: 更新上架状态
     * @Author: cy
     * @Date: 2022/4/25
     */
    @ApiOperation(value = "更新上架状态", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/updateShelves", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> updateShelves(@Validated @RequestBody VideoLessonShelvesVo shelvesVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (shelvesVo.getShelvesFlag() == 0 && StringUtil.isEmpty(shelvesVo.getShelvesReason())) {
            return failed( "下架必须要有下架原因");
        }

        shelvesVo.setShelvesId(user.getId());
        shelvesVo.setShelvesTime(new Date());
        shelvesVo.setClientType(ClientEnum.SYSTEM);
        lessonGroupService.updateShelves(shelvesVo);
        return succeed();
    }

    @ApiOperation(value = "首页-视频课统计")
    @GetMapping("/countVideoGroup")
    public HttpResponseResult<CountVideoGroupVo> countVideoGroup() {
        return succeed(lessonGroupService.countVideoGroup());
    }
}
