package com.red.circle.wallet.app.command.freight.query;

import com.red.circle.common.business.dto.cmd.app.AppFlowCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.wallet.app.dto.clientobject.FreightRunningWaterCO;
import com.red.circle.wallet.infra.database.rds.entity.freight.FreightBalanceRunningWater;
import com.red.circle.wallet.infra.database.rds.entity.freight.FreightSeller;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightBalanceRunningWaterService;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightSellerService;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取货运流水
 *
 * @author pengliang on 2021/7/6
 */
@Component
@RequiredArgsConstructor
public class FreightRunningWaterFlowQryExe {

  private final UserProfileClient userProfileClient;
  private final FreightSellerService freightSellerService;
  private final FreightBalanceRunningWaterService freightBalanceRunningWaterService;

  public List<FreightRunningWaterCO> execute(AppFlowCmd cmd) {
    List<FreightBalanceRunningWater> freightBalanceRunningWaters;
    FreightSeller freightSeller = freightSellerService.getFreightSellerByUserId(
            cmd.requiredReqUserId());

    if (Objects.nonNull(freightSeller)) {
      freightBalanceRunningWaters = freightBalanceRunningWaterService.flowBySellerId(
              freightSeller.getId(), cmd.getLastId());
    } else {
      freightBalanceRunningWaters = freightBalanceRunningWaterService.flowByUserIdAndDate(
              cmd.requiredReqUserId(), cmd.getLastId(),cmd.getCurrentDate());
    }

    if (CollectionUtils.isEmpty(freightBalanceRunningWaters)) {
      return CollectionUtils.newArrayList();
    }

    Set<Long> acceptUserIds = freightBalanceRunningWaters.stream()
        .map(FreightBalanceRunningWater::getAcceptUserId).collect(
            Collectors.toSet());

    Map<Long, UserProfileDTO> userProfileMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(acceptUserIds)
    );
    return freightBalanceRunningWaters.stream().map(freightBalanceRunningWater -> {
      UserProfileDTO userProfile = userProfileMap.get(freightBalanceRunningWater.getAcceptUserId());
      userProfile.setAccount(userProfile.actualAccount());
      return new FreightRunningWaterCO()
          .setId(freightBalanceRunningWater.getId())
          .setType(freightBalanceRunningWater.getType())
          .setQuantity(freightBalanceRunningWater.getQuantity())
          .setCreateTime(freightBalanceRunningWater.getCreateTime())
          .setUserProfile(userProfile);
    }).collect(Collectors.toList());

  }
}
