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

import com.alibaba.fastjson.JSONObject;
import com.bonus.core.account.v3.constants.AccTradeTypeEnum;
import com.bonus.core.account.v3.metadata.enums.AccWalletIdEnum;
import com.bonus.core.marketing.v2.rule.price.dto.RulePriceResultOrderDTO;
import com.bonus.core.marketing.v2.rule.price.dto.RulePriceWalletDTO;
import com.bonus.core.marketing.v2.rule.price.handler.subsidyfixed.dto.PriceSubsidyFixedDTO;
import com.bonus.core.marketing.v2.rule.price.handler.subsidyfixed.extension.PriceSubsidyFixedHandlerExtension;
import com.bonus.core.marketing.vo.MarketRuleVO;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

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

   public void handle(RulePriceResultOrderDTO order, MarketRuleVO rule, List<RulePriceResultOrderDTO> orderResults) {
      PriceSubsidyFixedDTO subsidyFixedDTO = (PriceSubsidyFixedDTO)JSONObject.parseObject(rule.getRuleInfo(), PriceSubsidyFixedDTO.class);
      if (subsidyFixedDTO.getSubsidyAmount().compareTo(BigDecimal.ZERO) > 0) {
         order.getWalletDetails().add(new RulePriceWalletDTO(AccWalletIdEnum.SUBSIDY.getKey(), subsidyFixedDTO.getSubsidyAmount(), AccTradeTypeEnum.RECHARGE_GIFT.getKey(), (JsonNode)null));
      }

      order.getWalletDetails().add(new RulePriceWalletDTO(-1, order.getRealAmount(), AccTradeTypeEnum.CONSUME.getKey(), (JsonNode)null));
   }
}
