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

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.marketing.v2.rule.price.constants.RulePriceEnum;
import com.bonus.core.marketing.v2.rule.price.handler.RulePriceHandler;
import com.bonus.core.marketing.v2.rule.price.handler.deductionfull.constants.PriceTypeEnum;
import com.bonus.core.marketing.v2.rule.price.handler.deductionfull.dto.PriceDeductionFullDTO;
import com.bonus.core.marketing.v2.rule.price.handler.deductionfull.dto.PriceDeductionFullItemDTO;

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

public interface PriceDeductionFullHandlerExtension extends RulePriceHandler {
   default Integer getRuleType() {
      return RulePriceEnum.RULE_DEDUCTION_FULL.getKey();
   }

   default String checkRuleInfo(String ruleInfo) {
      PriceDeductionFullDTO rule = (PriceDeductionFullDTO)JSONObject.parseObject(ruleInfo, PriceDeductionFullDTO.class);
      List<PriceDeductionFullItemDTO> items = rule.getItems();
      if (CollUtil.isEmpty(items)) {
         throw new ServiceException("规则配置有误");
      } else {
         List<PriceDeductionFullItemDTO> errorDoorAmount = items.stream().filter((e) -> {
            return PriceTypeEnum.PRICE_TYPE_CYCLE.getKey().equals(rule.getPriceType()) && e.getDoorAmount().compareTo(BigDecimal.ZERO) <= 0;
         }).collect(Collectors.toList());
         if (CollUtil.isNotEmpty(errorDoorAmount)) {
            throw new ServiceException("门槛金额设置有误");
         } else {
            return rule.toString();
         }
      }
   }
}
