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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.wallet.infra.database.rds.entity.freight.FreightSeller;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightBalanceService;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightSellerService;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 检查是否是货运代理.
 *
 * @author pengliang on 2021/7/19
 */
@Service
@RequiredArgsConstructor
public class CheckFreightQryExe {

  private final FreightSellerService freightSellerService;
  private final FreightBalanceService freightBalanceService;

  public Boolean execute(AppExtCommand cmd) {

    if (Boolean.TRUE.equals(freightBalanceService.existsBalance(cmd.getReqUserId()))) {
      return Boolean.TRUE;
    }

    FreightSeller freightSeller = freightSellerService.getFreightSellerByUserId(
        cmd.requiredReqUserId());
    if (Objects.isNull(freightSeller)) {
      return false;
    }
    return freightBalanceService.checkDealerById(freightSeller.getFreightId());
  }
}
