package com.bonus.canteen.core.marketing.v2.rule.price.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

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

@ApiModel("计价规则出参")
public class RulePriceResultDTO {
   @ApiModelProperty("订单列表")
   private List<RulePriceResultOrderDTO> orderResults;
   @ApiModelProperty("订单实付总金额")
   private BigDecimal realAmount;
   @ApiModelProperty("订单总优惠金额")
   private BigDecimal discountsAmount;
   @ApiModelProperty("钱包实付总金额详情，目前计价规则里面涉及固定补贴")
   private List<RulePriceWalletDTO> walletDetails;
   @ApiModelProperty("预留字段")
   private JsonNode extInfo;

   public RulePriceResultDTO(List<RulePriceResultOrderDTO> orderResults) {
      this.realAmount = (BigDecimal)orderResults.stream().map(RulePriceResultOrderDTO::getRealAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
      this.discountsAmount = (BigDecimal)orderResults.stream().map(RulePriceResultOrderDTO::getDiscountsAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
      this.orderResults = orderResults;
      List<RulePriceWalletDTO> temp = new ArrayList();
      orderResults.forEach((orderResult) -> {
         CollUtil.addAll(temp, orderResult.getWalletDetails());
      });
      Map<String, BigDecimal> map = (Map)temp.stream().collect(Collectors.groupingBy((a) -> {
         Integer var10000 = a.getWalletId();
         return "" + var10000 + "_" + a.getTradeType();
      }, Collectors.mapping(RulePriceWalletDTO::getAmount, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
      this.walletDetails = new ArrayList();
      map.forEach((key, value) -> {
         List<String> split = CharSequenceUtil.split(key, "_");
         this.walletDetails.add(new RulePriceWalletDTO(Integer.valueOf((String)split.get(0)), value, Integer.valueOf((String)split.get(1)), (JsonNode)null));
      });
   }

   public List<RulePriceResultOrderDTO> getOrderResults() {
      return this.orderResults;
   }

   public BigDecimal getRealAmount() {
      return this.realAmount;
   }

   public BigDecimal getDiscountsAmount() {
      return this.discountsAmount;
   }

   public List<RulePriceWalletDTO> getWalletDetails() {
      return this.walletDetails;
   }

   public JsonNode getExtInfo() {
      return this.extInfo;
   }

   public RulePriceResultDTO setOrderResults(final List<RulePriceResultOrderDTO> orderResults) {
      this.orderResults = orderResults;
      return this;
   }

   public RulePriceResultDTO setRealAmount(final BigDecimal realAmount) {
      this.realAmount = realAmount;
      return this;
   }

   public RulePriceResultDTO setDiscountsAmount(final BigDecimal discountsAmount) {
      this.discountsAmount = discountsAmount;
      return this;
   }

   public RulePriceResultDTO setWalletDetails(final List<RulePriceWalletDTO> walletDetails) {
      this.walletDetails = walletDetails;
      return this;
   }

   public RulePriceResultDTO setExtInfo(final JsonNode extInfo) {
      this.extInfo = extInfo;
      return this;
   }

   public RulePriceResultDTO(final List<RulePriceResultOrderDTO> orderResults, final BigDecimal realAmount, final BigDecimal discountsAmount, final List<RulePriceWalletDTO> walletDetails, final JsonNode extInfo) {
      this.orderResults = orderResults;
      this.realAmount = realAmount;
      this.discountsAmount = discountsAmount;
      this.walletDetails = walletDetails;
      this.extInfo = extInfo;
   }

   public RulePriceResultDTO() {
   }
}
