package com.red.circle.other.app.dto.clientobject.gift;

import com.red.circle.framework.dto.ClientObject;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 礼物信息-分组.
 * </p>
 *
 * @author pengliang
 * @since 2020-06-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class GiftConfigGroupByTabCO extends ClientObject {

  /**
   * 礼物tab.
   */
  private String giftTab;

  /**
   * 礼物信息.
   */
  private List<GiftConfigCO> giftConfigs;

  /**
   * GiftConfigCO 转 GiftConfigGroupByTabCO.
   */
  public static List<GiftConfigGroupByTabCO> toGiftConfigGroupByTab(
      List<GiftConfigCO> giftConfigCos, Predicate<GiftConfigCO> filter) {
    return Optional.ofNullable(giftConfigCos)
        .map(configs -> configs.stream()
            .filter(filter)
            .collect(Collectors.groupingBy(GiftConfigCO::getGiftTab))
            .entrySet().stream()
            .map(entry -> new GiftConfigGroupByTabCO()
                .setGiftTab(entry.getKey())
                .setGiftConfigs(entry.getValue())
            ).collect(Collectors.toList()))
        .orElse(null);
  }

}
