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.dao.MusicGroupDao;
import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderCourseSettingsDao;
import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderDetailDao;
import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderStudentDetailDao;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderCourseSettings;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderDetail;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee.PaymentStatus;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderService;
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.common.exception.BizException;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("musicGroupPaymentCalender")
@Api(tags = "乐团缴费项目服务")
@RestController
public class MusicGroupPaymentCalenderController extends BaseController {

    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private MusicGroupPaymentCalenderService musicGroupPaymentCalenderService;
    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private MusicGroupPaymentCalenderDetailDao musicGroupPaymentCalenderDetailDao;
    @Autowired
    private MusicGroupPaymentCalenderStudentDetailDao musicGroupPaymentCalenderStudentDetailDao;
    @Autowired
    private MusicGroupPaymentCalenderCourseSettingsDao musicGroupPaymentCalenderCourseSettingsDao;


    @ApiOperation(value = "查询学员缴费详情")
    @GetMapping("/getCalenderInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "calenderId", value = "缴费项目编号", required = true, dataType = "Long")})
    public HttpResponseResult getCalenderInfo(Long calenderId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer userId = sysUser.getId();
        MusicGroupPaymentCalender calender = musicGroupPaymentCalenderService.get(calenderId);
        if (calender == null) {
            return failed("缴费项目不存在");
        }
        String musicGroupId = calender.getMusicGroupId();
        MusicGroupPaymentCalenderDetail calenderDetail = musicGroupPaymentCalenderDetailDao.findByCalenderIdAndUserId(calenderId, userId);
        if (calenderDetail == null) {
            throw new BizException("缴费项不存在该学员，请联系教务老师");
        }
        if(calenderDetail.getPaymentStatus() == PaymentStatus.PAID_COMPLETED){
            throw new BizException("您已缴费，请勿重复提交");
        }else if(calenderDetail.getPaymentStatus() == PaymentStatus.PROCESSING){
            throw new BizException("存在待处理的订单，请稍候尝试");
        }
        if(calender.getStatus() == PaymentCalenderStatusEnum.OPEN || calenderDetail.getOpen() == 1){
            StudentRegistration studentRegistration = studentRegistrationService.queryByUserIdAndMusicGroupId(userId,musicGroupId);
            if (studentRegistration == null) {
                return failed("乐团报名信息不存在");
            }
            List<MusicGroupPaymentCalenderCourseSettings> calenderCourseSettings = musicGroupPaymentCalenderCourseSettingsDao.queryCalenderCourseSettings(calenderId);
            ModelMap model = new ModelMap();
            musicGroupPaymentCalenderStudentDetailDao.findByBatchNoAndUserId(calender.getBatchNo(),userId);
            if(calenderCourseSettings.size() == 0){
                model.put("musicGroupPaymentCalenderStudentDetails",musicGroupPaymentCalenderStudentDetailDao.findByBatchNoAndUserId(calender.getBatchNo(),userId));
            }
            model.put("musicGroup",musicGroupDao.get(musicGroupId));
            model.put("amount", calenderDetail.getExpectAmount());
            model.put("calenderCourseSettings", calenderCourseSettings);
            model.put("balance",studentRegistration.getBalance());
            return succeed(model);
        }else if(calender.getStatus() == PaymentCalenderStatusEnum.OVER){
            throw new BizException("缴费已截止,如有问题请联系指导老师");
        }else {
            throw new BizException("缴费暂未开始,如有问题请联系指导老师");
        }
    }
}
