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.TeacherDao;
import com.ym.mec.biz.dal.dto.VipGroupApplyDto;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/21
 */
@Api(tags = "vip课")
@RequestMapping("eduVipGroupManage")
@RestController
public class EduVipGroupManageController extends BaseController {

    @Autowired
    private VipGroupService vipGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TeacherDao teacherDao;

    @ApiOperation(value = "创建六一活动vip课")
    @PostMapping("/createActivityVipGroup")
    public Object createActivityVipGroup(@RequestBody VipGroupApplyDto vipGroupApplyDto){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Integer userId = vipGroupApplyDto.getVipGroupApplyBaseInfo().getUserId();
        Teacher teacher = teacherDao.get(userId);
        if(Objects.isNull(teacher)){
            return failed("请指定指导老师!");
        }
        if(Objects.isNull(vipGroupApplyDto.getVipGroupApplyBaseInfo().getOrganId())){
            Integer firstStudentId = vipGroupApplyDto.getVipGroupApplyBaseInfo().getFirstStudentId();
            SysUser student = teacherDao.getUser(firstStudentId);
            if(Objects.isNull(student)||Objects.isNull(student.getOrganId())){
                return failed("学员信息异常");
            }
            vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganId(student.getOrganId());
        }
        for (CourseSchedule courseSchedule : vipGroupApplyDto.getCourseSchedules()) {
            courseSchedule.setEndClassTime(DateUtil.addMinutes(courseSchedule.getStartClassTime(), vipGroupApplyDto.getVipGroupApplyBaseInfo().getSingleClassMinutes()));
        }
        vipGroupApplyDto.getVipGroupApplyBaseInfo().setEducationalTeacherId(sysUser.getId());
        return vipGroupService.createActivityVipGroup(vipGroupApplyDto);
    }

    @PostMapping("/exportActivityVipGroup")
    public HttpResponseResult importActivityVipGroup(String data){
        vipGroupService.importActivityVipGroup(data);
        return succeed();
    }

}
