package com.yonge.cooleshow.student.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.search.MyCourseSearch;
import com.yonge.cooleshow.biz.dal.dto.search.PracticeTeacherSearch;
import com.yonge.cooleshow.biz.dal.entity.CourseCalendarEntity;
import com.yonge.cooleshow.biz.dal.entity.CourseScheduleReplied;
import com.yonge.cooleshow.biz.dal.entity.TeacherSubjectPrice;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.service.CourseRepliedService;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
import com.yonge.cooleshow.biz.dal.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.mybatis.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: cy
 * @Date: 2022/4/13
 */
@Api(tags = "学生课表")
@RestController
@RequestMapping("/courseSchedule")
public class StudentCourseScheduleController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private CourseScheduleService courseScheduleService;
    @Autowired
    private CourseRepliedService repliedService;

    @ApiOperation("学生端-我的-我的课程-陪练课")
    @PostMapping("/queryStudentPracticeCourse")
    public HttpResponseResult<PageInfo<MyCourseVo>> queryStudentPracticeCourse(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        search.setStudentId(user.getId());
        IPage<MyCourseVo> pages = courseScheduleService.queryStudentPracticeCourse(PageUtil.getPage(search), search);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "学生端-我的-我的课程-课程详情-评价陪练课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/replied", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> replied(@Validated @RequestBody CourseScheduleReplied replied) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        replied.setCourseGroupType(CourseScheduleEnum.PRACTICE.getCode());
        repliedService.replied(replied, sysUser);
        return succeed();
    }

    @ApiOperation(value = "学生端-我的-我的课程-课程详情-查询陪练课评价")
    @PostMapping(value = "/selectReplied")
    public HttpResponseResult<CourseScheduleReplied> selectReplied(@Validated @RequestBody CourseScheduleReplied replied) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        replied.setStudentId(user.getId());
        replied.setCourseGroupType(CourseScheduleEnum.PRACTICE.getCode());
        return succeed(repliedService.selectReplied(replied));
    }

    @ApiOperation("学生端-课表-日历")
    @PostMapping("/queryCourseScheduleStudent")
    public HttpResponseResult<Set<String>> queryCourseScheduleStudent(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        search.setStudentId(user.getId());
        return succeed(courseScheduleService.queryCourseScheduleStudent(search));
    }

    @ApiOperation(value = "学生端-首页-陪练课老师列表", notes = "search:{\"subjectId\":null,\"search\":\"\",\"sort\":\"starGrade ASC,expTime DESC,subjectPrice DESC\"}")
    @PostMapping("/teacherList")
    public HttpResponseResult<PageInfo<PracticeTeacherVo>> teacherList(@RequestBody PracticeTeacherSearch search) {
        IPage<PracticeTeacherVo> pages = courseScheduleService.teacherList(PageUtil.getPage(search), search);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "学生端-课表-日历-用户", notes = "search:{\"classDate\":\"2022-03-27\"}")
    @PostMapping("/queryCourseTeacher")
    public HttpResponseResult<Map<String, Object>> queryCourseTeacher(@RequestBody MyCourseSearch search) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        search.setStudentId(user.getId());
        return succeed(courseScheduleService.queryCourseTeacher(search));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", dataType = "String", required = true, value = "学生id"),
            @ApiImplicitParam(name = "classDate", dataType = "String", required = true, value = "年月"),
            @ApiImplicitParam(name = "courseState", dataType = "String", value = "课程状态 NOT_START未开始 ING进行中 COMPLETE已完成 CANCEL已取消"),
            @ApiImplicitParam(name = "subjectId", dataType = "Long", value = "声部id"),
            @ApiImplicitParam(name = "page", dataType = "Integer", value = "页数"),
            @ApiImplicitParam(name = "rows", dataType = "Integer", value = "每页数量"),
    })
    @ApiOperation("学生-查询直播课")
    @PostMapping("/queryStudentLiveCourse")
    public HttpResponseResult<PageInfo<CourseStudent>> queryStudentLiveCourse(@RequestBody Map<String, Object> param) {
        return succeed(courseScheduleService.queryStudentLiveCourse(param));
    }

    @ApiOperation("学生-首页-直播课&视频课&最近课程")
    @GetMapping("/queryLiveAndVideo")
    public HttpResponseResult<StudentHomePage> queryLiveAndVideo() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleService.queryLiveAndVideo(user.getId()));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId", dataType = "Long", value = "老师id"),
            @ApiImplicitParam(name = "studentId", dataType = "Long", value = "学生id"),
            @ApiImplicitParam(name = "year", dataType = "Integer", value = "年"),
            @ApiImplicitParam(name = "month", dataType = "Integer", value = "月"),
            @ApiImplicitParam(name = "day", dataType = "Integer", value = "日"),
    })
    @ApiOperation("陪练课日历-用于学生购买指定老师陪练课")
    @PostMapping("/createPracticeCourseCalendar")
    public HttpResponseResult<List<CourseCalendarEntity>> generatePracticeCourseCalender(@RequestBody Map<String, Object> param) {
        return succeed(courseScheduleService.createPracticeCourseCalender(param));
    }

    @ApiOperation("查询老师陪练课配置")
    @GetMapping("/getTeacherSubjectPrice")
    public HttpResponseResult<List<TeacherSubjectPrice>> getTeacherSubjectPrice(@NotNull Long teacherId) {
        return succeed(courseScheduleService.teacherSubjectPrice(teacherId));
    }
}

