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

import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMember;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.infra.database.rds.service.team.BusinessDevelopmentTeamService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 团队关联bd信息.
 *
 * @author pengliang on 2023/2/21
 */
@Component
@RequiredArgsConstructor
public class TeamBdProfileQryExe {

  private final TeamMemberService teamMemberService;
  private final TeamProfileService teamProfileService;
  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final BusinessDevelopmentTeamService businessDevelopmentTeamService;

  public UserProfileDTO execute(CommonCommand cmd) {
    TeamMember teamMember = teamMemberService.getByMemberId(cmd.getReqUserId());
    if (Objects.isNull(teamMember)
        || !teamProfileService.checkStatusAvailable(teamMember.getTeamId())) {
      return null;
    }

    Long bdUserId = businessDevelopmentTeamService.getBdUserIdByTeamId(teamMember.getTeamId());

    if (Objects.isNull(bdUserId)) {
      return null;
    }

    return userProfileAppConvertor.toUserProfileDTO(userProfileGateway.getByUserId(bdUserId));
  }
}
