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

import com.red.circle.common.business.dto.cmd.app.AppAccountCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.region.UserRegionClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.wallet.app.dto.clientobject.FreightDealerUserSearchResultCO;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightBalanceService;
import com.red.circle.wallet.inner.error.FreightErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取经销商搜索货运代理结果.
 *
 * @author zongpubin on 2023/5/24
 */
@Component
@RequiredArgsConstructor
public class DealerFreightUserSearchQryExe {

  private final UserRegionClient userRegionClient;
  private final UserProfileClient userProfileClient;
  private final FreightBalanceService freightBalanceService;

  public FreightDealerUserSearchResultCO execute(AppAccountCmd cmd) {

    UserProfileDTO userProfile = ResponseAssert.requiredSuccess(
        userProfileClient.getByAccount(cmd.requireReqSysOrigin(), cmd.getAccount())
    );

    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, userProfile);

    ResponseAssert.isTrue(FreightErrorCode.NOT_FREIGHT,
        freightBalanceService.existsBalance(userProfile.getId()));

    // 2023.6.2根据刘占洋需要货运代理收发区域必须精准相同才能交易
    ResponseAssert.isTrue(CommonErrorCode.REGION_NOT_SUPPORTED_ERROR,
        ResponseAssert.requiredSuccess(
            userRegionClient.checkEqRegion(cmd.requiredReqUserId(), userProfile.getId())));

    return new FreightDealerUserSearchResultCO()
        .setUserProfile(userProfile);
  }

}
