package com.red.circle.console.app.service.app.user;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.user.user.UserLogoutLogClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.cmd.user.UserLogoutLogQryCmd;
import com.red.circle.other.inner.model.dto.user.UserLogoutLogDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户注销记录
 *
 * @author zongpubin on 2024/2/26
 */
@RequiredArgsConstructor
@Service
public class UserLogoutLogServiceImpl implements UserLogoutLogService {

  private final UserProfileClient userProfileClient;
  private final UserLogoutLogClient userLogoutLogClient;

  @Override
  public PageResult<UserLogoutLogDTO> pageUserLogoutLog(UserLogoutLogQryCmd query) {
    PageResult<UserLogoutLogDTO> pageResult = ResponseAssert.requiredSuccess(
        userLogoutLogClient.pageUserLogoutLog(query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult;
    }

    Map<Long, UserProfileDTO> userBaseInfoMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(
            pageResult.getRecords().stream()
                .map(UserLogoutLogDTO::getUserId).collect(Collectors.toSet())));

    return pageResult.convert(userLogoutLog -> {
      userLogoutLog.setUserProfile(userBaseInfoMap.get(userLogoutLog.getUserId()));
      return userLogoutLog;
    });
  }
}
