package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CourseScheduleStudentPaymentDao;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.OnlineMusicGroupCourseDto;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.enums.MusicGroupStatusEnum;
import com.ym.mec.biz.dal.page.MusicGroupQueryInfo;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderDetailService;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.biz.service.OnlineMusicGroupService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RequestMapping("eduOnlineMusic")
@Api(tags = "教务端乐团网管课")
@RestController
public class EduOnlineMusicGroupController extends BaseController {

    @Autowired
    private OnlineMusicGroupService onlineMusicGroupService;
    @Autowired
    private MusicGroupService musicGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupPaymentCalenderDetailService musicGroupPaymentCalenderDetailService;
    @Autowired
    private CourseScheduleStudentPaymentDao courseScheduleStudentPaymentDao;

    @ApiOperation(value = "分页查询乐团列表")
    @GetMapping("/findMusicGroups")
    public HttpResponseResult findMusicGroups(MusicGroupQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setEducationalTeacherId(sysUser.getId());
        queryInfo.setStatus(MusicGroupStatusEnum.PROGRESS);
        return succeed(musicGroupService.findEduTeacherMusicGroups(queryInfo));
    }

    @ApiOperation(value = "获取乐团下所有学员身上的声部列表")
    @GetMapping("/findMusicGroupAllStudentSubjects")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/findMusicGroupAllStudentSubjects')")
    public HttpResponseResult findMusicGroupAllStudentSubjects(String musicGroupId){
        return succeed(studentRegistrationService.findMusicGroupAllStudentSubjects(musicGroupId));
    }

    @ApiOperation(value = "根据声部获取乐团下完成缴费的学员")
    @GetMapping("/findMusicGroupStudentWithSubject")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/findMusicGroupStudentWithSubject')")
    public HttpResponseResult findMusicGroupStudentWithSubject(String musicGroupId,Integer subjectId){
        if(Objects.isNull(subjectId)){
            return failed("请指定声部");
        }
        return succeed(musicGroupPaymentCalenderDetailService.findMusicGroupStudentWithSubject(musicGroupId, subjectId));
    }

    @ApiOperation(value = "获取可选择的教师")
    @GetMapping("/getEnableApplyTeachers")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/getEnableApplyTeachers')")
    public HttpResponseResult getEnableApplyTeachers(Integer studentId, Integer subjectId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return succeed(onlineMusicGroupService.getEnableApplyTeachers(studentId, subjectId));
    }

    @ApiOperation(value = "获取教师空闲时间")
    @GetMapping("/getTeacherFreeTimes")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/getTeacherFreeTimes')")
    public HttpResponseResult getTeacherFreeTimes(Integer studentId, Integer teacherId, boolean skipHoliday) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return succeed(onlineMusicGroupService.getTeacherFreeTimes(studentId,teacherId,skipHoliday));
    }

    @ApiOperation(value = "检测学生是否存在进行中的乐团网管课")
    @GetMapping("/checkStudentExistMusicNetwork")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/checkStudentExistMusicNetwork')")
    public HttpResponseResult checkStudentExistMusicNetwork(Integer studentId){
        int i = courseScheduleStudentPaymentDao.countStudentNotStartCourseNumWithCourseType(studentId, CourseSchedule.CourseScheduleType.MUSIC_NETWORK);
        return succeed(i>0?1:0);
    }

    @ApiOperation(value = "创建乐团网管课")
    @PostMapping("/createOnlineMusicClassGroup")
//    @PreAuthorize("@pcs.hasPermissions('eduOnlineMusic/createOnlineMusicClassGroup')")
    public HttpResponseResult createOnlineMusicClassGroup(OnlineMusicGroupCourseDto onlineMusicGroupCourseInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(onlineMusicGroupService.createOnlineMusicClassGroup(onlineMusicGroupCourseInfo));
    }

}
