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

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.MusicGroup;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee.PaymentStatus;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.service.MusicGroupStudentFeeService;
import com.ym.mec.biz.service.SysConfigService;
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;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext.MessageSender;
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 java.math.BigDecimal;
import java.util.*;

@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() {

		int days = Integer.parseInt(sysConfigDao.findConfigValue(SysConfigService.REFRESH_PAYMENT_STATUS_EARLY_DAYS));

		Date date = new Date();

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

		List<MusicGroupStudentFee> musicGroupStudentFeeList = musicGroupStudentFeeDao.queryWillRenewList(days);
		for (MusicGroupStudentFee musicGroupStudentFee : musicGroupStudentFeeList) {
			//课程费用为空，或者课程费用为0，只更新下次缴费时间
			if (musicGroupStudentFee.getPaymentStatus() == PaymentStatus.PAID_COMPLETED &&
					(musicGroupStudentFee.getCourseFee() != null && musicGroupStudentFee.getCourseFee().doubleValue() > 0)) {
				musicGroupStudentFee.setPaymentStatus(PaymentStatus.NON_PAYMENT);
				musicGroupStudentFee.setUpdateTime(date);
				updateList.add(musicGroupStudentFee);
			}
		}

		if (updateList.size() > 0) {
			musicGroupStudentFeeDao.batchUpdate(updateList);
			MusicGroup musicGroup = musicGroupDao.get(updateList.get(0).getMusicGroupId());
			Map<Integer, String> push = new HashMap<>();
			for (MusicGroupStudentFee sf : updateList) {
				push.put(sf.getUserId(), sf.getUserId() + "");
			}
			String baseUrl = sysConfigDao.findConfigValue(SysConfigService.BASE_API_URL);
			String memo = "4?" + baseUrl + "/#/renew?musicGroupId=" + musicGroup.getId();
			// 发送续费通知
			sysMessageService.batchSendMessage(MessageSender.JIGUANG, MessageTypeEnum.STUDENT_PUSH_WAIT_RENEW_MESSAGE, push, null, 0, memo,"STUDENT",musicGroup.getName());
		}

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

		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);
	}

}