package com.bonus.core.marketing.v2.rule.price.handler.limitmaxamount.extension.impl;

import com.alibaba.fastjson.JSONObject;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.marketing.v2.rule.price.dto.RulePriceResultOrderDTO;
import com.bonus.core.marketing.v2.rule.price.handler.limitmaxamount.dto.PriceLimitMaxAmountDTO;
import com.bonus.core.marketing.v2.rule.price.handler.limitmaxamount.extension.PriceLimitMaxAmountHandlerExtension;
import com.bonus.core.marketing.vo.MarketRuleVO;
import com.bonus.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

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

   public void handle(RulePriceResultOrderDTO order, MarketRuleVO rule, List<RulePriceResultOrderDTO> orderResults) {
      PriceLimitMaxAmountDTO limitMaxAmountDTO = (PriceLimitMaxAmountDTO)JSONObject.parseObject(rule.getRuleInfo(), PriceLimitMaxAmountDTO.class);
      BigDecimal maxAmount = limitMaxAmountDTO.getMaxAmount();
      BigDecimal realAmount = order.getRealAmount();
      if (maxAmount.compareTo(realAmount) < 0) {
         throw new ServiceException(I18n.getMessage("mkt_rule_over_amount", new Object[0]));
      }
   }
}
