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.EmployeeDao;
import com.ym.mec.biz.dal.dao.PracticeGroupDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.PracticeGroupBuyDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.PracticeGroup;
import com.ym.mec.biz.dal.page.StudentQueryInfo;
import com.ym.mec.biz.service.EduPracticeGroupService;
import com.ym.mec.biz.service.PracticeGroupService;
import com.ym.mec.biz.service.StudentService;
import com.ym.mec.biz.service.TeacherCourseRewardService;
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.web.bind.annotation.*;

import java.util.*;

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

    @Autowired
    private PracticeGroupService practiceGroupService;
    @Autowired
    private EduPracticeGroupService eduPracticeGroupService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private PracticeGroupDao practiceGroupDao;

    List<Integer> excludeOrganIds=new ArrayList<>(Arrays.asList(new Integer[]{36}));

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

    @ApiOperation("获取可以预约的教师--付费")
    @GetMapping(value = "/getEnableApplyTeachersWithPay")
    public Object getEnableApplyTeachersWithPay(Integer studentId,Integer subjectId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return succeed(practiceGroupService.getEnableApplyTeachersWithPay(studentId, subjectId));
    }

    @ApiOperation("获取陪练课预约参数——付费")
    @GetMapping(value = "/getPayPracticeApplyParams")
    public Object getPayPracticeApplyParams(Integer studentId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        SysUser student = teacherDao.getUser(studentId);
        if(Objects.isNull(student)){
            throw new BizException("指定的学生不存在");
        }
        return succeed(practiceGroupService.getPayPracticeApplyParams(studentId, student.getOrganId()));
    }

    @ApiOperation("获取指定教师的空闲时间——付费")
    @GetMapping(value = "/getPayPracticeTeacherFreeTimes")
    public Object getPayPracticeTeacherFreeTimes(Integer teacherId,Integer studentId,Integer buyMonths, boolean renew, Long groupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return succeed(practiceGroupService.getPayPracticeTeacherFreeTimes(studentId,teacherId, buyMonths,renew,groupId));
    }

    @ApiOperation("获取学生上一次的购买信息--付费")
    @GetMapping(value = "/findUserLatestPracticeGroup")
    public HttpResponseResult findUserLatestPracticeGroup(Long groupId,Integer studentId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return succeed(practiceGroupService.findUserLatestPracticeGroup(studentId,groupId));
    }

    @ApiOperation("检测学生是否是新用户")
    @GetMapping(value = "/isNewStudent")
    public HttpResponseResult isNewStudent(Integer studentId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        int studentExitChargePractices = practiceGroupDao.checkStudentExitChargePractice(studentId);
        Map<String, Integer> result=new HashMap<>();
        result.put("isNewStudent", studentExitChargePractices<=0?0:1);
        return succeed(result);
    }

    @ApiOperation("陪练课购买")
    @PostMapping(value = "/buyPracticeGroup")
    public HttpResponseResult buyPracticeGroup(@RequestBody PracticeGroupBuyDto practiceGroupBuyParams){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(excludeOrganIds.contains(sysUser.getOrganId())){
            throw new BizException("请联系老师确认您的所属分部");
        }
        if(Objects.isNull(practiceGroupBuyParams.getStudentId())){
            throw new BizException("请指定学生");
        }
        practiceGroupBuyParams.setEducationalTeacherId(sysUser.getId());
        return eduPracticeGroupService.buyPracticeGroup(practiceGroupBuyParams, sysUser.getRealName()+"("+sysUser.getId()+")");
    }

    @ApiOperation("获取用户历史购买的陪练课")
    @GetMapping(value = "/findUserHistoryBuyPracticeGroups")
    public HttpResponseResult findUserHistoryBuyPracticeGroups(Integer studentId,Long groupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(practiceGroupService.findUserHistoryBuyPracticeGroups(studentId, groupId));
    }

    @ApiOperation("查询订单信息")
    @GetMapping(value = "/queryOrderInfo")
    public HttpResponseResult queryOrderInfo(String orderNo){
        return succeed(practiceGroupService.queryOrderInfo(orderNo));
    }

    @ApiOperation("获取学生列表")
    @GetMapping(value = "/findStudents")
    public HttpResponseResult findStudents(StudentQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }
            queryInfo.setOrganIdList(Arrays.asList(employee.getOrganIdList().split(",")));
        }
        return succeed(studentService.findStudentVipGroupList(queryInfo));
    }

    @ApiOperation("取消待支付的订单")
    @PostMapping(value = "/cancelWaitPayOrder")
    public HttpResponseResult cancelWaitPayOrder(Integer studentId,Integer groupId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        return practiceGroupService.cancelWaitPayOrder(studentId, groupId);
    }

    @ApiOperation("获取陪练课预约参数——试听")
    @GetMapping(value = "/getTrialPracticeApplyParams")
    public Object getTrialPracticeApplyParams(Integer studentId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            throw new BizException("请指定学生");
        }
        SysUser student = teacherDao.getUser(studentId);
        if(Objects.isNull(student)){
            throw new BizException("指定的学生不存在");
        }
        return succeed(eduPracticeGroupService.getTrialPracticeApplyParams(studentId));
    }

    @ApiOperation("获取转化失败的学员列表")
    @GetMapping(value = "/findConvertDefeatStudents")
    public HttpResponseResult findConvertDefeatStudents(StudentQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }
            queryInfo.setOrganIdList(Arrays.asList(employee.getOrganIdList().split(",")));
        }
        return succeed(practiceGroupService.findConvertDefeatStudents(queryInfo));
    }

    @ApiOperation("获取可以指派试听课的老师列表")
    @GetMapping(value = "/findEnableAssignTeachers")
    public HttpResponseResult findEnableAssignTeachers(Integer subjectId,Integer studentId){
        return succeed(eduPracticeGroupService.findEnableAssignTeachers(subjectId,studentId));
    }

    @ApiOperation("获取指定教师的空闲时间——试听")
    @GetMapping(value = "/getTrialPracticeTeacherFreeTimes")
    public HttpResponseResult getTrialPracticeTeacherFreeTimes(Integer studentId, Integer teacherId, String monday){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(studentId)){
            return failed("请指定学生");
        }
        if(Objects.isNull(teacherId)){
            return failed("请指定教师");
        }
        return succeed(eduPracticeGroupService.getTrialPracticeTeacherFreeTimes(studentId, teacherId, monday));
    }

    @ApiOperation("创建网管试听课")
    @PostMapping(value = "/createTrialPracticeGroup")
    public HttpResponseResult createTrialPracticeGroup(@RequestBody PracticeGroupBuyDto practiceGroupBuyParams){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if(Objects.isNull(practiceGroupBuyParams.getStudentId())){
            throw new BizException("请指定学生");
        }
        practiceGroupBuyParams.setEducationalTeacherId(sysUser.getId());
        return eduPracticeGroupService.createTrialPracticeGroup(practiceGroupBuyParams, sysUser.getRealName()+"("+sysUser.getId()+")");
    }

    @ApiOperation("获取学生的试听课")
    @GetMapping(value = "/getStudentTrialPractices")
    public HttpResponseResult getStudentTrialPractices(Integer studentId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(eduPracticeGroupService.getStudentTrialPractices(studentId));
    }

    @ApiOperation("更新课程组信息")
    @GetMapping(value = "/updatePractice")
    public HttpResponseResult updatePractice(PracticeGroup practiceGroup){
        practiceGroupService.update(practiceGroup);
        return succeed();
    }
}
