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

import com.ym.mec.biz.dal.dao.MemberFeeSettingDao;
import com.ym.mec.biz.dal.entity.MemberFeeSetting;
import com.ym.mec.biz.dal.enums.PeriodEnum;
import com.ym.mec.biz.service.MemberFeeSettingService;
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.Objects;
import java.util.function.BiFunction;

@Service
public class MemberFeeSettingServiceImpl extends BaseServiceImpl<Integer, MemberFeeSetting> implements MemberFeeSettingService {

    @Autowired
    private MemberFeeSettingDao memberFeeSettingDao;

    @Override
    public BaseDAO<Integer, MemberFeeSetting> getDAO() {
        return memberFeeSettingDao;
    }

    @Override
    public MemberFeeSetting findByRankIdAndOrganId(Integer organId, Integer rankId) {
        return memberFeeSettingDao.findByRankIdAndOrganId(organId, rankId);
    }

    @Override
    public BigDecimal getAmount(Integer organId, Integer memberRankSettingId, PeriodEnum period, BigDecimal num,
                                BiFunction<PeriodEnum, MemberFeeSetting, BigDecimal> func) {
        MemberFeeSetting memberFee = memberFeeSettingDao.findByRankIdAndOrganId(organId, memberRankSettingId);
        if (memberFee == null) {
            throw new BizException("操作失败：请配置当前分部会员收费标准");
        }
        //获取总价（原价或者现价）
        return func.apply(period, memberFee).multiply(num).setScale(0, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal getGroupPurchaseFee(PeriodEnum periodEnum, MemberFeeSetting memberFee) {
        BigDecimal amount;
        switch (periodEnum) {
            case DAY:
                amount = memberFee.getGroupPurchaseDayFee();
                break;
            case MONTH:
                amount = memberFee.getGroupPurchaseMonthFee();
                break;
            case QUARTERLY:
                amount = memberFee.getGroupPurchaseQuarterlyFee();
                break;
            case YEAR_HALF:
                amount = memberFee.getGroupPurchaseHalfYearFee();
                break;
            case YEAR:
                amount = memberFee.getGroupPurchaseYearFee();
                break;
            default:
                throw new BizException("请选择正确的会员有效期");
        }
        if (Objects.isNull(amount)) {
            throw new BizException("未设置云教练的价格");
        }
        return amount;
    }

    public static BigDecimal getOriginalFee(PeriodEnum periodEnum, MemberFeeSetting memberFee) {
        BigDecimal amount;
        switch (periodEnum) {
            case DAY:
                amount = memberFee.getOriginalDayFee();
                break;
            case MONTH:
                amount = memberFee.getOriginalMonthFee();
                break;
            case QUARTERLY:
                amount = memberFee.getOriginalQuarterlyFee();
                break;
            case YEAR_HALF:
                amount = memberFee.getOriginalHalfYearFee();
                break;
            case YEAR:
                amount = memberFee.getOriginalYearFee();
                break;
            default:
                throw new BizException("请选择正确的会员有效期");
        }
        if (Objects.isNull(amount)) {
            throw new BizException("未设置会员有效期的价格");
        }
        return amount;
    }

    public static BigDecimal getCurrentFee (PeriodEnum periodEnum, MemberFeeSetting memberFee) {
        BigDecimal amount;
        switch (periodEnum) {
            case DAY:
                amount = memberFee.getCurrentDayFee();
                break;
            case MONTH:
                amount = memberFee.getCurrentMonthFee();
                break;
            case QUARTERLY:
                amount = memberFee.getCurrentQuarterlyFee();
                break;
            case YEAR_HALF:
                amount = memberFee.getCurrentHalfYearFee();
                break;
            case YEAR:
                amount = memberFee.getCurrentYearFee();
                break;
            default:
                throw new BizException("请选择正确的会员有效期");
        }
        if (Objects.isNull(amount)) {
            throw new BizException("未设置会员有效期的价格");
        }
        return amount;
    }

}