package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.ClassGroupStudentMapperDao;
import com.ym.mec.biz.dal.dao.StudentDao;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.entity.ClassGroup;
import com.ym.mec.biz.dal.entity.ExtracurricularExercises;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.page.ExtraExercilseQueryInfo;
import com.ym.mec.biz.dal.page.TeacherServeHomeworkQueryInfo;
import com.ym.mec.biz.dal.page.TeacherServeQueryInfo;
import com.ym.mec.biz.service.ExtracurricularExercisesReplyService;
import com.ym.mec.biz.service.ExtracurricularExercisesService;
import com.ym.mec.biz.service.StudentExtracurricularExercisesSituationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2020/3/26
 */
@Api(tags = "课外训练服务")
@RequestMapping("extracurricularExercises")
@RestController
public class ExtracurricularExercisesController extends BaseController {

    @Autowired
    private ExtracurricularExercisesService extracurricularExercisesService;
    @Autowired
    private ExtracurricularExercisesReplyService extracurricularExercisesReplyService;
    @Autowired
    private StudentExtracurricularExercisesSituationService studentExtracurricularExercisesSituationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private ClassGroupStudentMapperDao classGroupStudentMapperDao;
    @Autowired
    private StudentDao studentDao;

    @ApiOperation(value = "获取教师有服务指标的课程列表")
    @GetMapping("/queryTeacherServeHomeworkDetail")
    public HttpResponseResult queryTeacherServeHomeworkDetail(TeacherServeHomeworkQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(studentExtracurricularExercisesSituationService.queryTeacherServeHomeworkDetail1(queryInfo));
    }


    @ApiOperation(value = "获取老师训练统计")
    @GetMapping("/getStatistics")
    public HttpResponseResult getStatistics(ExtraExercilseQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(studentExtracurricularExercisesSituationService.getStatistics(queryInfo));
    }

    @ApiOperation(value = "统计老师当前时间能布置作业的课程数量")
    @GetMapping("/countWaitCreateHomeworkNum")
    public HttpResponseResult countWaitCreateHomeworkNum() {
        return succeed(studentExtracurricularExercisesSituationService.countWaitCreateHomeworkNum());
    }

    @ApiOperation(value = "创建课外训练")
    @PostMapping("/createExtraExercises")
    @AuditLogAnnotation(operateName = "创建课外训练")
    private HttpResponseResult createExtraExercises(ExtracurricularExercises exercises){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        exercises.setTeacherId(sysUser.getId());
        extracurricularExercisesService.createExtraExercises(exercises);
        return succeed();
    }

    @ApiOperation(value = "创建课外训练")
    @PostMapping("/createExtraExercisesForJson")
    @AuditLogAnnotation(operateName = "创建课外训练")
    private HttpResponseResult createExtraExercisesForJson(@RequestBody ExtracurricularExercises exercises){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        if(null == exercises.getExpireDate()){
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.WEEK_OF_MONTH,1);
            exercises.setExpireDate(instance.getTime());
        }

        exercises.setTeacherId(sysUser.getId());
        extracurricularExercisesService.createExtraExercises(exercises);
        return succeed();
    }

    @ApiOperation(value = "获取课外训练列表")
    @GetMapping("/findExtraExercilses")
    private HttpResponseResult findExtraExercilses(ExtraExercilseQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(extracurricularExercisesService.findExtraExercilses(queryInfo));
    }

    @ApiOperation(value = "获取已布置训练列表（包含课后，课外）")
    @GetMapping("/findExtraExercilsesHomeworks")
    private HttpResponseResult findExtraExercilsesHomeworks(ExtraExercilseQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        queryInfo.setTeacherId(sysUser.getId());
        return succeed(extracurricularExercisesService.findExtraExercilsesHomeworks(queryInfo));
    }

    @ApiOperation(value = "获取课外训练详情")
    @GetMapping("/findStudentExtraExerciseDetail")
    private HttpResponseResult findStudentExtraExerciseDetail(Long studentExerciseId){
        if(Objects.isNull(studentExerciseId)){
            return failed("请选择课外训练");
        }
        return succeed(extracurricularExercisesReplyService.findStudentExtraExerciseDetail(studentExerciseId));
    }

    @ApiOperation(value = "获取待布置学员列表")
    @GetMapping("/getDontServeStudents")
    public HttpResponseResult getDontServeStudents(String search, String musicGroupId, Long classGroupId, Integer subjectId,Integer hasMember,String studentIds){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(extracurricularExercisesService.getDontServeStudents(sysUser.getId(), search, musicGroupId, classGroupId, subjectId,hasMember,studentIds));
    }

    @ApiOperation(value = "获取待布置学员乐团列表")
    @GetMapping("/getDontServeStudentMusicGroups")
    public HttpResponseResult getDontServeStudentMusicGroups(String search){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        List<BasicUserDto> dontServeStudents = extracurricularExercisesService.getDontServeStudents(sysUser.getId(), search, null, null,null,null,null);
        if(CollectionUtils.isEmpty(dontServeStudents)){
            return succeed(Collections.emptyList());
        }
        Set<Integer> studentIds = dontServeStudents.stream().map(BasicUserDto::getUserId).collect(Collectors.toSet());
        List<MusicGroup> studentNormalMusicGroups = studentRegistrationDao.getStudentNormalMusicGroups(studentIds);
        return succeed(studentNormalMusicGroups);
    }

    @ApiOperation(value = "获取服务学员查询条件")
    @GetMapping("/getDontServeStudentQueryCondition")
    public HttpResponseResult getDontServeStudentQueryCondition(String musicGroupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Map<String, Object> result = new HashMap<>();
        List<BasicUserDto> dontServeStudents = extracurricularExercisesService.getDontServeStudents(sysUser.getId(), null, musicGroupId, null, null,null,null);
        if(CollectionUtils.isEmpty(dontServeStudents)){
            result.put("musicGroups", Collections.emptyList());
            result.put("classGroups", Collections.emptyList());
            result.put("subjects", Collections.emptyList());
            return succeed(result);
        }
        Set<Integer> studentIds = dontServeStudents.stream().map(BasicUserDto::getUserId).collect(Collectors.toSet());
        List<MusicGroup> studentNormalMusicGroups = studentRegistrationDao.getStudentNormalMusicGroups(studentIds);
        result.put("musicGroups", studentNormalMusicGroups);

        List<ClassGroup> classGroups = classGroupStudentMapperDao.getStudentClassGroupWithTeacher(studentIds, sysUser.getId(), musicGroupId);
        result.put("classGroups", classGroups);

        List<Subject> studentSubjects = studentDao.getStudentSubjects(studentIds);
        result.put("subjects", studentSubjects);
        return succeed(result);
    }
}
