package com.ym.mec.biz.service.impl;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.dao.MusicGroupStudentFeeDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.UpdateStudentFeeDto;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee;
import com.ym.mec.biz.service.MusicGroupStudentFeeService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class MusicGroupStudentFeeServiceImpl extends BaseServiceImpl<Long, MusicGroupStudentFee> implements MusicGroupStudentFeeService {

	@Autowired
	private MusicGroupStudentFeeDao musicGroupStudentFeeDao;

	@Autowired
	private SysConfigDao sysConfigDao;

	@Autowired
	private MusicGroupDao musicGroupDao;

	@Autowired
	private SysMessageService sysMessageService;

	@Override
	public BaseDAO<Long, MusicGroupStudentFee> getDAO() {
		return musicGroupStudentFeeDao;
	}

	@Override
	public boolean refreshPaymentFeeStatus() {
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateStudentFee(UpdateStudentFeeDto studentFeeDto) {
		//修改学员缴费周期和是否锁定z
		musicGroupStudentFeeDao.updateStudentFee(studentFeeDto);
		MusicGroupStudentFee fee = musicGroupStudentFeeDao.findByUser(studentFeeDto.getStudentId(), studentFeeDto.getMusicGroupId());
		if(fee == null){
			throw new BizException("学员缴费信息不存在");
		}
//		fee.setNextPaymentDate(musicGroupPaymentCalenderService.getNextPaymentDate(studentFeeDto.getMusicGroupId(),null,fee));
		musicGroupStudentFeeDao.update(fee);
	}

	@Override
	public MusicGroupStudentFee findByUser(Integer userId, String musicGroupId) {
		return musicGroupStudentFeeDao.findByUser(userId,musicGroupId);
	}

	@Override
	public void updateStudentFeeIsLock(UpdateStudentFeeDto studentFeeDto) {
		musicGroupStudentFeeDao.updateStudentFeeIsLock(studentFeeDto);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchUpdateCourseFee(String ids, BigDecimal courseFee) {
		if(StringUtils.isEmpty(ids) || courseFee == null){
			throw new BizException("参数校验失败");
		}
		musicGroupStudentFeeDao.batchUpdateCourseFee(ids,courseFee);
	}

}