package com.ym.mec.student.controller;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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.ClassGroupDao;
import com.ym.mec.biz.dal.dao.ClassGroupStudentMapperDao;
import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.dto.HighClassGroupDto;
import com.ym.mec.biz.dal.entity.ClassGroup;
import com.ym.mec.biz.dal.entity.ClassGroupStudentMapper;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.ClassGroupTypeEnum;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;

@RequestMapping("classGroup")
@Api(tags = "班级服务")
@RestController
public class ClassGroupController extends BaseController {

    @Resource
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private ClassGroupStudentMapperDao classGroupStudentMapperDao;
    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private ClassGroupDao classGroupDao;

    @ApiOperation(value = "基础提高班报名")
    @PostMapping("/highReg")
    @ApiImplicitParams({@ApiImplicitParam(name = "classGroupId", value = "班级id", required = true, dataType = "int")})
    public HttpResponseResult highReg(Integer classGroupId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        ClassGroup classGroup = classGroupDao.lockClassGroup(classGroupId);
        if (classGroup == null || (!classGroup.getType().equals(ClassGroupTypeEnum.HIGH) && !classGroup.getType().equals(ClassGroupTypeEnum.HIGH_ONLINE)) || classGroup.getDelFlag().equals(1)) {
            return failed("报名的班级不存在");
        }
        if (classGroup.getStudentNum().compareTo(classGroup.getExpectStudentNum()) >= 0) {
            return failed("班级人数已满");
        }
        ClassGroupStudentMapper classGroupStudentMapper = classGroupStudentMapperDao.query(classGroupId, sysUser.getId());
        if (classGroupStudentMapper != null) {
            return succeed(classGroup);
        }
        classGroupService.addStudent(classGroup, sysUser.getId(), GroupType.MUSIC);
        return succeed("报名成功");
    }

    @ApiOperation(value = "用户声部小班课列表")
    @GetMapping("/highClassGroups")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "classGroupIds", value = "班级ids", required = true, dataType = "String")}
    )
    public HttpResponseResult getHighClassGroup(String musicGroupId,String classGroupIds) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer userId = sysUser.getId();
        StudentRegistration studentRegistration = studentRegistrationService.queryByUserIdAndMusicGroupId(userId, musicGroupId);
        if (studentRegistration == null || studentRegistration.getActualSubjectId() == null) {
            return failed("学生不在该乐团");
        }
        MusicGroup musicGroup = musicGroupDao.get(musicGroupId);
        Map<String, Object> classGroupAndStatusMap = new HashMap<>();
        List<HighClassGroupDto> highClassGroups = new ArrayList<>();
        if (classGroupIds != null && !classGroupIds.isEmpty()) {
            highClassGroups = classGroupDao.findHighClassGroupByTypeAndSubjectId(musicGroupId, studentRegistration.getActualSubjectId(), "HIGH",classGroupIds);
            List<HighClassGroupDto> highOnlineClassGroup = classGroupDao.findHighClassGroupByTypeAndSubjectId(musicGroupId, studentRegistration.getActualSubjectId(), "HIGH_ONLINE",classGroupIds);
            highClassGroups.addAll(highOnlineClassGroup);
        } else {
            highClassGroups = classGroupService.findClassGroupByMusicGroupIdAndSubjectId(musicGroupId, studentRegistration.getActualSubjectId());
        }

        List<ClassGroupStudentMapper> ClassGroupStudentMapper = classGroupStudentMapperDao.findHighClassGroupHasUser(musicGroupId, userId, ClassGroupTypeEnum.HIGH);
        List<ClassGroupStudentMapper> highOnlineClassGroupStudentMapper = classGroupStudentMapperDao.findHighClassGroupHasUser(musicGroupId, userId, ClassGroupTypeEnum.HIGH_ONLINE);
        classGroupAndStatusMap.put("highClassGroups", highClassGroups);
        classGroupAndStatusMap.put("hasReg", false);
        classGroupAndStatusMap.put("onlineHasReg", false);
        if (ClassGroupStudentMapper.size() > 0) {
            classGroupAndStatusMap.put("hasReg", true);
        }
        if (highOnlineClassGroupStudentMapper.size() > 0) {
            classGroupAndStatusMap.put("onlineHasReg", true);
        }
        return succeed(classGroupAndStatusMap);
    }
}
