package com.yonge.cooleshow.website.controller;


import com.yonge.cooleshow.biz.dal.dto.CheckCourseTimeDto;
import com.yonge.cooleshow.biz.dal.dto.LiveCourseGroupDto;
import com.yonge.cooleshow.biz.dal.entity.CourseTimeEntity;
import com.yonge.cooleshow.biz.dal.service.CourseGroupService;
import com.yonge.cooleshow.biz.dal.vo.CourseGroupVo;
import com.yonge.cooleshow.biz.dal.vo.LiveCourseInfoVo;
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.*;
import org.springframework.web.bind.annotation.*;

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

/**
 * 课程组表(CourseGroup)表控制层
 *
 * @author hgw
 * @since 2022-03-18 15:29:10
 */
@Api(tags = "课程组表")
@RestController
@RequestMapping("/courseGroup")
public class CourseGroupController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseGroupService courseGroupService;

    @ApiOperation("直播课详情")
    @GetMapping("/queryLiveCourseInfo")
    public HttpResponseResult<LiveCourseInfoVo> queryLiveCourseInfo(@ApiParam(value = "课程组id", required = true) @RequestParam(value = "groupId") Long groupId) {
        return succeed(courseGroupService.queryLiveCourseInfo(groupId));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", dataType = "Long", value = "老师id"),
            @ApiImplicitParam(name = "search", dataType = "String", value = "关键字"),
            @ApiImplicitParam(name = "groupStatus", dataType = "String", value = "课程组状态  ING(进行中)  NOT_SALE(未开售,未上架) APPLY(报名中,销售中) COMPLETE(已完成)"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("分页查询直播课课程组列表")
    @PostMapping("/queryPageCourseGroup")
    public HttpResponseResult<PageInfo<CourseGroupVo>> queryPageLiveCourseGroup(@RequestBody Map<String, Object> param) {
        return succeed(courseGroupService.queryPageLiveCourseGroup(param));
    }

    @ApiOperation("创建直播课程组-新增课程组")
    @PostMapping("/addLiveCourse")
    public HttpResponseResult<Object> addLiveCourse(@RequestBody LiveCourseGroupDto dto) {
        if (dto.getCoursePrice().compareTo(BigDecimal.ZERO) <= 0) {
            return failed("课程价格不能为0元");
        }
        courseGroupService.addLiveCourse(dto);
        return succeed();
    }

    @ApiOperation("创建直播课程组-锁定课程时间-将课时写到缓存当作锁定的时间")
    @PostMapping("/lockCourseToCache")
    public HttpResponseResult<List<CourseTimeEntity>> lockCourseToCache(@RequestBody CheckCourseTimeDto dto) {
        return succeed(courseGroupService.lockCourseToCache(dto));
    }

    @ApiOperation("创建直播课程组-解除锁定课程时间-删除写到缓存当作锁定的课时")
    @GetMapping("/unlockCourseToCache")
    public HttpResponseResult<Object> unlockCourseToCache(@RequestParam("teacherId") Long teacherId) {
        courseGroupService.unlockCourseToCache(teacherId);
        return succeed();
    }

    @ApiOperation("取消课程组-下架课程组")
    @GetMapping("/cancelCourseGroup")
    public HttpResponseResult<Object> cancelCourseGroup(@RequestParam("groupId") Long groupId) {
        courseGroupService.cancelCourseGroup(groupId);
        return succeed();
    }

    @ApiOperation("获取老师排直播课时锁定的课时")
    @GetMapping("/getLockCache")
    public HttpResponseResult<Object> getLockCache(@RequestParam("teacherId") Long teacherId) {
        return succeed(courseGroupService.getLiveLockTimeCache(teacherId).get(teacherId));
    }


    @ApiOperation("修改直播课程组")
    @PostMapping("/updateLiveCourse")
    public HttpResponseResult<Object> updateLiveCourse(@RequestBody LiveCourseGroupDto dto) {
        courseGroupService.updateLiveCourse(dto);
        return succeed();
    }
}

