package com.red.circle.other.app.command.material.query;

import com.red.circle.other.app.convertor.material.PropsMaterialAppConvertor;
import com.red.circle.other.app.dto.clientobject.material.PropsStoreCommodityInfoCO;
import com.red.circle.other.app.dto.cmd.material.PropsStoreTypeQryCmd;
import com.red.circle.other.domain.gateway.props.PropsStoreGateway;
import com.red.circle.other.inner.model.dto.material.props.PropsStoreCommodity;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 道具商店商品查询.
 *
 * @author pengliang on 2022/4/13
 */
@Component
@RequiredArgsConstructor
public class PropsStoreCommodityQryExe {

  private final PropsMaterialAppConvertor propsMaterialAppConvertor;
  private final PropsStoreGateway propsStoreGateway;

  public List<PropsStoreCommodityInfoCO> execute(PropsStoreTypeQryCmd cmd) {
    return Optional.ofNullable(
            propsStoreGateway.listReleaseStoreCommodity(cmd.requireReqSysOriginEnum(),
                cmd.getPropsType()))
        .map(toPropsDecorationCO(cmd))
        .orElseGet(CollectionUtils::newArrayList);
  }

  private Function<List<PropsStoreCommodity>, List<PropsStoreCommodityInfoCO>> toPropsDecorationCO(
      PropsStoreTypeQryCmd cmd) {
    return storeCommodities -> propsMaterialAppConvertor.toListPropsStoreCommodityInfoCO(
        storeCommodities.stream()
            .filter(commodity -> commodity.filterCurrency(cmd.getCurrencyType())).collect(
                Collectors.toList())
    );
  }

}
