package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.LiveSaleOutDto;
import com.yonge.cooleshow.biz.dal.dto.search.LiveCourseGroupSearch;
import com.yonge.cooleshow.biz.dal.dto.search.LiveCourseGroupStudentCourseSearch;
import com.yonge.cooleshow.biz.dal.dto.search.LiveCourseGroupStudentSearch;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.service.CourseGroupService;
import com.yonge.toolset.mybatis.support.PageUtil;
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.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

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

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "老师直播课列表", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/live/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<LiveCourseGroupVo>> teacherList(@Validated(value = LiveCourseGroupSearch.TeacherGroup.class)
                                                                           @RequestBody LiveCourseGroupSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        query.setCourseType(CourseScheduleEnum.LIVE);

        IPage<LiveCourseGroupVo> liveCourseGroupVoIPage = courseGroupService
                .selectAdminLivePage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(liveCourseGroupVoIPage));
    }


    @ApiOperation(value = "学生直播课列表", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/live/list/student", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<LiveCourseGroupVo>> studentList(@Validated(value = LiveCourseGroupSearch.StudentGroup.class)
                                                                           @RequestBody LiveCourseGroupSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        query.setCourseType(CourseScheduleEnum.LIVE);

        IPage<LiveCourseGroupVo> liveCourseGroupVoIPage = courseGroupService
                .selectAdminLivePage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(liveCourseGroupVoIPage));
    }

    @ApiOperation(value = "直播课详情")
    @PostMapping(value="/detail/{courseGroupId}")
    public HttpResponseResult<LiveCourseGroupVo> detail(@ApiParam(value = "课程组编号ID", required = true)
                                                                   @PathVariable("courseGroupId") Long courseGroupId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (courseGroupId == null) {
            return failed("课程组id不能为空");
        }

        LiveCourseGroupVo liveCourseGroupVo = courseGroupService.detail(courseGroupId);
        if (liveCourseGroupVo == null) {
            return failed("没找到课程组信息");
        }
        return succeed(liveCourseGroupVo);
    }


    @ApiOperation(value = "直播课下架")
    @PostMapping(value="/live/saleOut")
    public HttpResponseResult<Boolean> liveSaleOut(@RequestBody @Valid LiveSaleOutDto dto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (dto.getCourseGroupId() == null) {
            return failed("课程组id不能为空");
        }

        return succeed(courseGroupService.liveSaleOut(dto));
    }



    @ApiOperation(value = "老师详情-直播课购买学员信息", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/live/student", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<LiveCourseGroupStudentVo>> student(@Valid @RequestBody LiveCourseGroupStudentSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        IPage<LiveCourseGroupStudentVo> liveCourseGroupVoIPage = courseGroupService
                .selectAdminLiveStudentPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(liveCourseGroupVoIPage));
    }


    @ApiOperation(value = "老师详情-教学计划")
    @PostMapping(value="/live/{courseGroupId}")
    public HttpResponseResult<List<LiveCourseGroupPlanVo>> student(@ApiParam(value = "课程组编号ID", required = true)
                                                                              @PathVariable("courseGroupId") Long courseGroupId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (courseGroupId == null) {
            return failed("课程组id不能为空");
        }

        return succeed(courseGroupService.selectAdminLivePlan(courseGroupId));
    }


    @ApiOperation(value = "学生详情-直播课详情-学生上课状态", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/live/student/course", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<LiveCourseGroupStudentCourseVo>> studentCourse(@Valid @RequestBody LiveCourseGroupStudentCourseSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        IPage<LiveCourseGroupStudentCourseVo> liveCourseGroupStudentCourseVoIPage = courseGroupService
                .selectAdminLiveStudentCoursePage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(liveCourseGroupStudentCourseVoIPage));
    }

}

