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

import com.red.circle.common.business.dto.cmd.app.AppTypeCmd;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.AgentActivityOwnCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.activity.AgentActivityCount;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.service.activity.AgentActivityCountService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.num.NumUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 代理活动 - 自己数据.
 * </p>
 *
 * @author pengshigang
 * @since 2023-05-29
 */
@Component
@RequiredArgsConstructor
public class AgentActivityOwnQueryExe {

  private final TeamProfileService teamProfileService;
  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final AgentActivityCountService agentActivityCountService;

  public AgentActivityOwnCO execute(AppTypeCmd cmd) {

    TeamProfile teamProfile = teamProfileService.getAvailableByOwnUserId(cmd.getReqUserId());
    if (Objects.isNull(teamProfile)) {
      return null;
    }

    AgentActivityCount count = agentActivityCountService.own(teamProfile.getId(), cmd.getType());
    if (Objects.isNull(count)) {
      return null;
    }

    UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO(
        userProfileGateway.getByUserId(cmd.getReqUserId()));
    if (Objects.isNull(userProfile)) {
      return null;
    }

    return new AgentActivityOwnCO()
        .setUserProfile(userProfile)
        .setQuantity(count.getTarget() > 0 ? NumUtils.formatLong(count.getTarget()) : "0");

  }

}
