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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.MusicGroupStudentFeeDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee.PaymentStatus;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.service.MusicGroupStudentFeeService;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.common.dal.BaseDAO;
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;

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

	@Override
	public boolean refreshPaymentFeeStatus() {

		int days = 7;

		SysConfig sysConfig = sysConfigDao.findByParamName(SysConfigService.REFRESH_PAYMENT_STATUS_EARLY_DAYS);
		if (sysConfig != null) {
			days = Integer.parseInt(sysConfig.getParanValue());
		}

		Date date = new Date();

		List<MusicGroupStudentFee> updateList = new ArrayList<MusicGroupStudentFee>();

		List<MusicGroupStudentFee> musicGroupStudentFeeList = musicGroupStudentFeeDao.queryWillRenewList(days);

		for (MusicGroupStudentFee musicGroupStudentFee : musicGroupStudentFeeList) {
			if (musicGroupStudentFee.getPaymentStatus() == PaymentStatus.PAID_COMPLETED) {
				musicGroupStudentFee.setPaymentStatus(PaymentStatus.NON_PAYMENT);
				musicGroupStudentFee.setUpdateTime(date);
				updateList.add(musicGroupStudentFee);
			}
		}

		if (updateList.size() > 0) {
			musicGroupStudentFeeDao.batchUpdate(updateList);
		}

		// int i = DateUtil.daysBetween(new Date(), musicGroupStudentFee.getNextPaymentDate());
		// e.setRenewStatus(i < 8 ? 0 : 1);

		return true;
	}

}