package com.bonus.canteen.core.marketing.v2.rule.recharge.handler.maxamount.extension.impl;

import com.alibaba.fastjson.JSONObject;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.marketing.v2.rule.recharge.dto.RuleRechargeResultDTO;
import com.bonus.canteen.core.marketing.v2.rule.recharge.handler.maxamount.dto.RechargeMaxAmountDTO;
import com.bonus.canteen.core.marketing.v2.rule.recharge.handler.maxamount.extension.RechargeMaxAmountHandlerExtension;
import com.bonus.canteen.core.marketing.vo.MarketRuleVO;
import com.bonus.common.houqin.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

@Service
public class DefaultRechargeMaxAmountHandlerImpl implements RechargeMaxAmountHandlerExtension {
   private static final Logger log = LoggerFactory.getLogger(DefaultRechargeMaxAmountHandlerImpl.class);

   @Override
   public void handle(RuleRechargeResultDTO resultDTO, MarketRuleVO ruleInfo) {
      RechargeMaxAmountDTO rule = (RechargeMaxAmountDTO)JSONObject.parseObject(ruleInfo.getRuleInfo(), RechargeMaxAmountDTO.class);
      if (!Objects.isNull(rule) && !Objects.isNull(rule.getMaxAmount())) {
         BigDecimal maxAmount = rule.getMaxAmount();
         if (maxAmount.compareTo(resultDTO.getAmount()) < 0) {
            throw new ServiceException("market_recharge_over_max_amount");
         }
      }
   }
}
