package com.ym.mec.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dto.StudentCourseInfoDto;
import com.ym.mec.biz.dal.dto.UserMusicGroupQueryInfo;
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.common.controller.BaseController;

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

    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ImGroupNoticeService imGroupNoticeService;
    
    @Autowired
    private CourseScheduleDao courseScheduleDao;

    @ApiOperation("获取学生所购买课程列表")
    @GetMapping(value = "/queryUserMusicGroups")
    public Object queryCoursePage(UserMusicGroupQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(classGroupService.queryCoursePage(queryInfo,sysUser.getId()));
    }

    @ApiOperation("获取学生所购买课程列表")
    @GetMapping(value = "/queryRemainCourseNum")
    public Object queryRemainCourseNum(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        List<StudentCourseInfoDto> list = courseScheduleDao.findUserSurplusCourseInfoByGroup(null, null, sysUser.getId());
        if(list == null){
        	return succeed(0);
        }
        return succeed(list.size());
    }

    @ApiOperation("获取学员所有聊天群组")
    @GetMapping(value = "/queryUserGroups")
    public Object queryUserGroups(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("获取用户信息失败");
        }
        return succeed(classGroupService.queryUserGroups(sysUser.getId(),search));
    }

    @ApiOperation(value = "获取最新一条群公告")
    @RequestMapping("/getNewNotice")
    public Object getNewNotice(Long 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){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null || sysUser.getId() == null){
            return failed("获取用户失败");
        }
        studentPayLogQueryInfo.setUserId(sysUser.getId());
        return succeed(classGroupService.queryStudentPayLog(studentPayLogQueryInfo));
    }
}
