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.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.MusicGroupStatusEnum;
import com.ym.mec.biz.dal.enums.PaymentStatusEnum;
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.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.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.io.IOException;
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 SysUserFeignService sysUserFeignService;


    @ApiOperation(value = "新增学生报名信息")
    @PostMapping("/add")
    public HttpResponseResult add(@RequestBody StudentRegistration studentRegistration) throws Exception {
    	
    	SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        
        Date date = new Date();
        MusicGroup musicGroup = musicGroupService.get(studentRegistration.getMusicGroupId());
        if (musicGroup == null) {
            return failed("乐团信息不存在");
        }
        
        if (DateUtil.daysBetween(musicGroup.getApplyExpireDate(),date) > 1) {
            return failed("乐团已截止报名");
        }
        
        if(studentRegistration.getId() != null){
            return  succeed(studentRegistrationService.updateStudent(studentRegistration));
        }

        if (musicGroup.getStatus().equals(MusicGroupStatusEnum.PAY)) {
            studentRegistration.setPaymentStatus(PaymentStatusEnum.OPEN);
        }else{
            studentRegistration.setPaymentStatus(PaymentStatusEnum.NO);
        }
        
        if(studentRegistration.getUserId() == null){
        	studentRegistration.setUserId(sysUser.getId());
        }
        
        studentRegistration.setOrganId(musicGroup.getOrganId());
        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));
    }

}
