package com.dayou.brokerage;

import com.dayou.bo.BrokerageDetailBO;
import com.dayou.brokerage.factory.BrokerageCalculateFactory;
import com.dayou.brokerage.constants.BrokerageRule;
import com.dayou.brokerage.handler.LandMarketerBrokerageHandler;
import com.dayou.brokerage.handler.LandNonMarketerBrokerageHandler;
import com.dayou.brokerage.validator.BrokerageValidator;
import com.dayou.enums.GlobalConfigEnum;
import com.dayou.utils.DateUtils;
import com.google.common.cache.Cache;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import static com.dayou.brokerage.constants.BrokerageMsg.MONTH_OF_YEAR;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/3/3
 * created with IntelliJ IDEA.
 */
@Slf4j
@Component
public class BrokerageCalculateSupport implements BrokerageCalculateFactory,BrokerageCalculator{

    @Autowired
    private BrokerageValidator brokerageValidator;

    @Autowired
    @Qualifier("landMarketerBrokerageHandler")
    private LandMarketerBrokerageHandler landMarketerBrokerageHandler;

    @Autowired
    @Qualifier("landNonMarketerBrokerageHandler")
    private LandNonMarketerBrokerageHandler landNonMarketerBrokerageHandler;


    @Override
    public BrokerageCalculator getCalculator(BrokerageRule rule){
        switch (rule){
            case LAND_MARKETER_RULE:
                return landMarketerBrokerageHandler;
            case LAND_OTHER_RULE:
                return landNonMarketerBrokerageHandler;
        }
        return null;
    }
    /**
     * 校验项目提成状态 (预计提成)
     * @param brokerageDetailBo
     * @return
     */
    protected void checkItemBrokerageStatus(BrokerageDetailBO brokerageDetailBo){
        brokerageValidator.checkItemBrokerageStatus(brokerageDetailBo);
    }


    /**
     * 校验项目提成状态 (参与人实际提成预算)
     * @param brokerageStatus
     * @return
     */
    public void checkItemBrokerageStatusOnParticipantPredict(String brokerageStatus){
        brokerageValidator.checkItemBrokerageStatusOnParticipantPredict(brokerageStatus);
    }

    /**
     * 校验项目提成状态 (市场人员实际提成预算)
     * @param brokerageStatus
     * @return
     */
    public void checkItemBrokerageStatusOnMarketerPredict(String brokerageStatus){
        brokerageValidator.checkItemBrokerageStatusOnMarketerPredict(brokerageStatus);
    }
    /**
     * 校验项目提成状态 (参与人实际提成结算)
     * @param brokerageStatus
     * @return
     */
    public void checkItemBrokerageStatusOnParticipantSettle(String brokerageStatus){
        brokerageValidator.checkItemBrokerageStatusOnParticipantSettle(brokerageStatus);
    }

    /**
     * 校验项目提成状态 (客户经理实际提成结算)
     * @param brokerageStatus
     * @return
     */
    public void checkItemBrokerageStatusOnMarketerSettle(String brokerageStatus){
        brokerageValidator.checkItemBrokerageStatusOnMarketerSettle(brokerageStatus);
    }
    /**
     * 市场人员预提校验
     * @param brokerageDetailBo
     * @return
     */
    protected Boolean checkMarketerPredictBrokerage(BrokerageDetailBO brokerageDetailBo){
        @NotNull
        BigDecimal paymentTarget = brokerageDetailBo.getPaymentTarget();
        if (paymentTarget.compareTo(BigDecimal.ZERO)!=0){
            BigDecimal currentPayment = brokerageDetailBo.getCurrentPayment();
            BigDecimal avgMonthTarget = paymentTarget.divide(MONTH_OF_YEAR, 2, RoundingMode.HALF_UP);
            int month = DateUtils.getMonth(new Date());
            //当月应完成回款金额
            BigDecimal brokerageTarget = avgMonthTarget.multiply(new BigDecimal(month));
            return brokerageValidator.checkMarketerPredictBrokerage(currentPayment,brokerageTarget);
        }
        return Boolean.FALSE;
    }

    /**
     * 团队月度回款校验
     * @param teamCurrentYearPayment
     * @param teamPaymentTarget
     * @return
     */
    public Boolean checkTeamMonthlyPayment(BigDecimal teamCurrentYearPayment,BigDecimal teamPaymentTarget){
        if (teamPaymentTarget.compareTo(BigDecimal.ZERO)!=0){
            BigDecimal avgMonthTarget = teamPaymentTarget.divide(MONTH_OF_YEAR, 2, RoundingMode.HALF_UP);
            int month = DateUtils.getMonth(new Date());
            //当月应完成回款金额
            BigDecimal brokerageTarget = avgMonthTarget.multiply(new BigDecimal(month));
            return brokerageValidator.checkMarketerPredictBrokerage(teamCurrentYearPayment,brokerageTarget);
        }
        return Boolean.FALSE;
    }
    /**
     * 市场人员预提校验
     * @param paymentDone
     * @param paymentTarget
     * @return
     */
    public Boolean checkMarketerPredictBrokerage(BigDecimal paymentDone,BigDecimal paymentTarget){
        if (paymentTarget.compareTo(BigDecimal.ZERO)!=0){
            BigDecimal avgMonthTarget = paymentTarget.divide(MONTH_OF_YEAR, 2, RoundingMode.HALF_UP);
            int month = DateUtils.getMonth(new Date());
            BigDecimal brokerageTarget = avgMonthTarget.multiply(new BigDecimal(month));
            return brokerageValidator.checkMarketerPredictBrokerage(paymentDone,brokerageTarget);
        }
        return Boolean.FALSE;
    }

    /**
     * 未结算金额 = 实际提成金额 - 已结算金额
     * @param actualAmount
     * @param advanceAmount
     * @return
     */
    @Override
    public BigDecimal residueAmount(BigDecimal actualAmount,BigDecimal advanceAmount) {
        return actualAmount.subtract(advanceAmount);
    }


    @Override
    public BigDecimal advanceAmount(BigDecimal actualAmount) {
        //提成结算金额 == 实际提成金额
        return actualAmount;
    }

}
