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

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

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.MusicGroupPaymentCalenderDao;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.date.DateUtil;

@Service
public class MusicGroupPaymentCalenderServiceImpl extends BaseServiceImpl<Long, MusicGroupPaymentCalender> implements MusicGroupPaymentCalenderService {

	@Autowired
	private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;

	@Override
	public BaseDAO<Long, MusicGroupPaymentCalender> getDAO() {
		return musicGroupPaymentCalenderDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchInsert(List<MusicGroupPaymentCalender> musicGroupPaymentCalenders) {
		if (musicGroupPaymentCalenders != null && musicGroupPaymentCalenders.size() > 0) {
			musicGroupPaymentCalenderDao.delByGroupId(musicGroupPaymentCalenders.get(0).getMusicGroupId());
			musicGroupPaymentCalenders.forEach(e -> {
				musicGroupPaymentCalenderDao.insert(e);
			});
		}
	}

	@Override
	public Date getNextPaymentDate(String musicGroupId, Date latestPaidDate, MusicGroupStudentFee fee) {
		List<Integer> months = null;
		if(fee != null){
			String paymentPeriodList = fee.getPaymentPeriodList();
			if(StringUtils.isEmpty(paymentPeriodList)){
				return null;
			} else {
				months = Arrays.asList(paymentPeriodList.split(",")).stream().map(x -> Integer.parseInt(x)).collect(Collectors.toList());
				Collections.sort(months);
			}
		}else {
			List<MusicGroupPaymentCalender> musicGroupPaymentCalenderList = musicGroupPaymentCalenderDao.findByMusicGroupId(musicGroupId);
			if (musicGroupPaymentCalenderList != null && musicGroupPaymentCalenderList.size() > 0) {
				months = musicGroupPaymentCalenderList.stream().map(e -> e.getPaymentMonth()).sorted().collect(Collectors.toList());
			}
		}
		if (months != null && months.size() > 0) {
			Date date = latestPaidDate;
			if (date == null) {
				date = new Date();
			}
			// 获取当前月份
			int currentMonth = Integer.parseInt(DateUtil.getMonth(date));
			int nextMonth = months.get(0);
			for (int i = 0; i < months.size(); i++) {
				if (currentMonth < months.get(i)) {
					nextMonth = months.get(i);
					break;
				}
			}
			// 修改学员付费周期
			Date nextPaymentDate = null;
			if (nextMonth > currentMonth) {
				nextPaymentDate = DateUtil.addMonths(date, nextMonth - currentMonth);
			} else if (nextMonth < currentMonth) {
				nextPaymentDate = DateUtil.addMonths(date, 12 - currentMonth + nextMonth);
			} else {
				nextPaymentDate = DateUtil.addMonths(date, 12);
			}
			return DateUtil.getFirstDayOfMonth(nextPaymentDate);
		}
		return null;
	}
}