package com.ym.mec.student.controller;

import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dto.CourseScheduleStudentDto;
import com.ym.mec.biz.dal.dto.StudentCourseInfoDto;
import com.ym.mec.biz.dal.dto.UserMusicGroupQueryInfo;
import com.ym.mec.biz.dal.page.CourseDetailQueryInfo;
import com.ym.mec.biz.dal.page.ImGroupNoticeQueryInfo;
import com.ym.mec.biz.dal.page.StudentPayLogQueryInfo;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.ImGroupNoticeService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("course")
@Api(tags = "课程服务")
@RestController
public class CourseController extends BaseController {

    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ImGroupNoticeService imGroupNoticeService;
    
    @Autowired
    private CourseScheduleDao courseScheduleDao;

    @ApiOperation("获取学生所购买课程列表")
    @GetMapping(value = "/queryUserMusicGroups")
    public Object queryCoursePage(UserMusicGroupQueryInfo queryInfo){
        return succeed(classGroupService.queryCoursePage(queryInfo,sysUserService.getUserId()));
    }

    @ApiOperation("获取学生所购买课程列表")
    @GetMapping(value = "/queryRemainCourseNum")
    public Object queryRemainCourseNum(){
        List<StudentCourseInfoDto> list = courseScheduleDao.findUserSurplusCourseInfoByGroup(null, null, sysUserService.getUserId());
        if(list == null){
        	return succeed(0);
        }
        return succeed(list.size());
    }

    @ApiOperation("获取学员所有聊天群组")
    @GetMapping(value = "/queryUserGroups")
    public Object queryUserGroups(String search){
        return succeed(classGroupService.queryUserGroups(sysUserService.getUserId(),search));
    }

    @ApiOperation(value = "获取最新一条群公告")
    @RequestMapping("/getNewNotice")
    public Object getNewNotice(String groupId) {
        return succeed(imGroupNoticeService.queryLatestNotice(groupId));
    }

    @ApiOperation(value = "获取群公告列表")
    @RequestMapping("/queryNoticePage")
    public Object queryPage(ImGroupNoticeQueryInfo queryInfo) {
        return succeed(imGroupNoticeService.queryPage(queryInfo));
    }

    @ApiOperation(value = "根据群编号，获取群组基本信息")
    @GetMapping("/findGroupById")
    public Object findGroupById(Integer groupId){
        if(null == groupId){
            return failed("参数校验错误");
        }
        return succeed(classGroupService.findGroupById(groupId));
    }

    @ApiOperation(value = "分页获取学员购买记录")
    @PostMapping("/queryStudentPayLog")
    public Object queryStudentPayLog(StudentPayLogQueryInfo studentPayLogQueryInfo){
        studentPayLogQueryInfo.setUserId(sysUserService.getUserId());
        return succeed(classGroupService.queryStudentPayLog(studentPayLogQueryInfo));
    }

    @ApiOperation(value = "根据课程组编号，获取课程列表")
    @PostMapping("/queryDetailList")
    public HttpResponseResult<List<CourseScheduleStudentDto>> queryDetailList(@RequestBody CourseDetailQueryInfo courseDetailQueryInfo){
        courseDetailQueryInfo.setUserId(sysUserService.getUserId());
        return succeed(courseScheduleDao.queryDetailList(courseDetailQueryInfo));
    }
}
