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

import com.ym.mec.biz.dal.dao.MemberRankSettingDao;
import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderMemberDao;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentBaseCalender;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.CalenderBaseServiceEnum;
import com.ym.mec.biz.dal.enums.PeriodEnum;
import com.ym.mec.biz.service.MemberFeeSettingService;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderBaseService;
import com.ym.mec.biz.service.MusicGroupPaymentCalenderMemberService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.function.BiFunction;

import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum.AUDITING;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum.NO;

@Service
public class MusicGroupPaymentCalenderMemberServiceImpl extends BaseServiceImpl<Long, MusicGroupPaymentCalenderMember>
        implements MusicGroupPaymentCalenderBaseService<MusicGroupPaymentCalenderMember>, MusicGroupPaymentCalenderMemberService {

    @Autowired
    private MusicGroupPaymentCalenderMemberDao musicGroupPaymentCalenderMemberDao;

    @Override
    public BaseDAO<Long, MusicGroupPaymentCalenderMember> getDAO() {
        return musicGroupPaymentCalenderMemberDao;
    }

    @Autowired
    private MemberFeeSettingService memberFeeSettingService;
    @Autowired
    private MemberRankSettingDao memberRankSettingDao;

    public MusicGroupPaymentCalenderMemberServiceImpl() {
        MusicGroupPaymentCalenderBaseService.calenderBaseServiceMap.put(CalenderBaseServiceEnum.MEMBER,this);
    }

    @Override
    public MusicGroupPaymentCalender.PaymentCalenderStatusEnum checkComponentAmount(MusicGroupPaymentBaseCalender baseCalender) {
        if(getAmount(baseCalender, MemberFeeSettingServiceImpl::getGroupPurchaseFee).
                compareTo(baseCalender.getCalenderMember().getActualAmount()) != 0){
            return AUDITING;
        }
        return NO;
    }

    @Override
    public BigDecimal getActualAmount(MusicGroupPaymentBaseCalender baseCalender) {
        MusicGroupPaymentCalenderMember calenderMember = baseCalender.getCalenderMember();
        if(calenderMember != null){
            return calenderMember.getActualAmount();
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal getOriginalAmount(MusicGroupPaymentBaseCalender baseCalender) {
        return getAmount(baseCalender, MemberFeeSettingServiceImpl::getOriginalFee);
    }

    @Override
    public BigDecimal getCurrentAmount(MusicGroupPaymentBaseCalender calenderMember) {
        return getAmount(calenderMember, MemberFeeSettingServiceImpl::getGroupPurchaseFee);
    }

    @Override
    public <E> E initBean(E bean) {
        if(bean != null){
            MusicGroupPaymentCalenderMember calenderMember = (MusicGroupPaymentCalenderMember)bean;
            MemberRankSetting memberRankSetting = memberRankSettingDao.get(calenderMember.getMemberRankSettingId());
            if(memberRankSetting == null){
                throw new BizException("会员信息不存在");
            }
            calenderMember.setName(memberRankSetting.getName());
        }
        return bean;
    }

    private BigDecimal getAmount(MusicGroupPaymentBaseCalender baseCalender, BiFunction<PeriodEnum,MemberFeeSetting,BigDecimal> func){
        MusicGroupPaymentCalenderMember calenderMember = baseCalender.getCalenderMember();
        if(calenderMember != null){
            MusicGroup musicGroup = baseCalender.getMusicGroup();
            //会员价格是否变动
            return memberFeeSettingService.getAmount(musicGroup.getOrganId(),calenderMember.getMemberRankSettingId(),
                    calenderMember.getPeriod(),calenderMember.getNum(),func);
        }
        return BigDecimal.ZERO;
    }

    @Override
    public void deleteByCalenderId(Long calenderId) {
        musicGroupPaymentCalenderMemberDao.deleteByCalenderId(calenderId);
    }

    @Override
    public MusicGroupPaymentCalenderMember findByCalenderId(Long calenderId) {
        return musicGroupPaymentCalenderMemberDao.findByCalenderId(calenderId);
    }
}
