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

import com.red.circle.common.business.core.AppBusinessUtils;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.cmd.user.user.AccountQryCmd;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Objects;

import com.red.circle.wallet.inner.endpoint.freight.FreightGoldClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 用户账号搜索.
 *
 * @author pengliang on 2021/4/14
 */
@Component
@RequiredArgsConstructor
public class UserProfileUseSearchQryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final FreightGoldClient freightGoldClient;

  public UserProfileDTO execute(AccountQryCmd cmd) {
    UserProfile userProfile = userProfileGateway.getByAccount(cmd.requireReqSysOrigin(),
        cmd.getAccount());
    ResponseAssert.notNull(UserErrorCode.USER_INFO_NOT_FOUND, userProfile);

    if (!AppBusinessUtils.eqSysOrigin(cmd.requireReqSysOrigin())
        || Objects.equals(userProfile.getDel(), Boolean.TRUE)) {
      return null;
    }

    UserProfileDTO userProfileDTO = userProfileAppConvertor.toUserProfileDTO(userProfile);
    userProfileDTO.setIsFreightAgent(ResponseAssert.requiredSuccess(freightGoldClient.checkFreightAgent(userProfile.getId())));
    userProfileDTO.setIsSuperFreightAgent(ResponseAssert.requiredSuccess(freightGoldClient.checkSuperFreightAgent(userProfile.getId())));
    return userProfileDTO;
  }

}
