package com.ym.mec.student.controller;

import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.CourseViewTypeEnum;
import com.ym.mec.biz.dal.enums.MusicGroupStatusEnum;
import com.ym.mec.biz.dal.enums.PaymentStatusEnum;
import com.ym.mec.biz.dal.enums.StudentMusicGroupStatusEnum;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.biz.service.MusicGroupSubjectPlanService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("register")
@Api(tags = "乐团注册")
@RestController
public class MusicGroupRegisterController extends BaseController {

    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupSubjectPlanService musicGroupSubjectPlanService;
    @Autowired
    private MusicGroupService musicGroupService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;


    @ApiOperation(value = "新增学生报名信息")
    @PostMapping("/add")
    public HttpResponseResult add(@RequestBody StudentRegistration studentRegistration) throws Exception {
        Integer userId = sysUserService.getUserId();
        Date date = new Date();
        MusicGroup musicGroup = musicGroupService.get(studentRegistration.getMusicGroupId());
        if (musicGroup == null) {
            return failed("乐团信息不存在");
        }
        if(musicGroup.getCourseViewType() == CourseViewTypeEnum.MEMBER_FEE){
            boolean hasMemberGroup = studentRegistrationDao.checkHasMemberGroup(musicGroup.getId(), userId);
            if(hasMemberGroup){
                throw new BizException("您已在其他会员团中，不可报名该乐团，请联系乐团主管");
            }
        }

        if (date.compareTo(DateUtil.getLastTimeWithDay(musicGroup.getApplyExpireDate())) > 0 && studentRegistration.getId() == null) {
            return failed("乐团已截止报名");
        }

        studentRegistration.setOrganId(musicGroup.getOrganId());
        if (studentRegistration.getId() != null) {
            return succeed(studentRegistrationService.updateStudent(studentRegistration));
        }

        StudentRegistration hasReg = studentRegistrationDao.getByUserIdAndMusicGroupId(studentRegistration.getMusicGroupId(),userId);
        if (hasReg != null && hasReg.getMusicGroupStatus() != StudentMusicGroupStatusEnum.QUIT) {
            return succeed(hasReg);
        }
        if (hasReg != null && hasReg.getMusicGroupStatus() == StudentMusicGroupStatusEnum.QUIT) {
            return failed( "无法重复报名，请联系教务老师");
        }
        String err = studentRegistrationService.checkRegOrPayStatus(musicGroup.getId());
        if (err != null) {
            return failed(err + "不可报名，请联系教务老师");
        }

        if (musicGroup.getStatus().equals(MusicGroupStatusEnum.PAY) ||
                musicGroup.getStatus().equals(MusicGroupStatusEnum.PREPARE) ||
                musicGroup.getStatus().equals(MusicGroupStatusEnum.PROGRESS)
        ) {
            studentRegistration.setPaymentStatus(PaymentStatusEnum.OPEN);
        } else {
            studentRegistration.setPaymentStatus(PaymentStatusEnum.NO);
        }

        if (studentRegistration.getUserId() == null) {
            studentRegistration.setUserId(userId);
        }

        return succeed(studentRegistrationService.addStudent(studentRegistration));
    }

    @ApiOperation(value = "获取乐团报名所需信息")
    @GetMapping("/getMusicGroupRegInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult getMusicGroupRegInfo(String musicGroupId) {
        return succeed(musicGroupSubjectPlanService.getMusicGroupRegInfo(musicGroupId));
    }

}
