package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.PracticeGroup;
import com.ym.mec.biz.service.PracticeGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
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.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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2020/1/31
 */
@RequestMapping("practiceGroup")
@Api(tags = "陪练课服务")
@RestController
public class PracticeGroupController extends BaseController {

    @Autowired
    private PracticeGroupService practiceGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    List<Integer> excludeOrganIds=new ArrayList<>(Arrays.asList(new Integer[]{7,36,37,38}));

    @ApiOperation("获取学生的陪练课")
    @GetMapping(value = "/findUserPracticeCourses")
    public Object findUserPracticeCourses(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.findUserPracticeCourses(sysUser.getId()));
    }

    @ApiOperation("获取陪练课预约参数")
    @GetMapping(value = "/getPracticeApplyParams")
    public Object getPracticeApplyParams(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.getPracticeApplyParams(sysUser.getId()));
    }

    @ApiOperation("获取可以预约的教师")
    @GetMapping(value = "/getEnableApplyTeachers")
    public Object getEnableApplyTeachers(Integer subjectId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.getEnableApplyTeachers(sysUser.getId(), subjectId));
    }

    @ApiOperation("获取指定学员所在分部下的教师空闲时间")
    @GetMapping(value = "/getTeacherFreeTimes")
    public Object getTeacherFreeTimes(Integer teacherId, Date firstClassTime){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.getTeacherFreeTimes(sysUser.getId(),teacherId,firstClassTime));
    }

    @ApiOperation("获取指定学员所在分部下的教师空闲时间")
    @GetMapping(value = "/getTeachersFreeTimes")
    public Object getTeachersFreeTimes(Integer subjectId, Date firstClassTime){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.getTeachersFreeTimes(sysUser.getId(),subjectId,firstClassTime));
    }

    @ApiOperation("陪练课预约")
    @PostMapping(value = "/practiceApply")
    public Object practiceApply(PracticeGroup practiceGroup){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(excludeOrganIds.contains(sysUser.getOrganId())){
            throw new BizException("请联系老师确认您的所属分部");
        }
        practiceGroup.setStudentId(sysUser.getId());
        return succeed(practiceGroupService.practiceApply2(practiceGroup));
    }

}
