package com.red.circle.console.infra.database.rds.service.admin.impl;

import com.red.circle.console.infra.database.rds.dao.admin.UserDAO;
import com.red.circle.console.infra.database.rds.entity.admin.User;
import com.red.circle.console.infra.database.rds.service.admin.UserRoleService;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.parse.DataTypeUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户表 服务实现类.
 * </p>
 *
 * @author pengliang
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends BaseServiceImpl<UserDAO, User> implements UserService {

  private final UserRoleService userRoleService;

  @Override
  public Map<Long, User> mapBackUser(Collection<Long> userIds) {
    if (CollectionUtils.isEmpty(userIds)) {
      return CollectionUtils.newHashMap();
    }
    return Optional.ofNullable(
            query().in(User::getId,
                userIds instanceof Set ? userIds
                    : userIds.stream().filter(Objects::nonNull).collect(Collectors.toSet())).list()
        ).map(users -> users.stream()
            .collect(Collectors.toMap(k -> DataTypeUtils.toLong(k.getId()), v -> v)))
        .orElse(CollectionUtils.newHashMap());
  }

  @Override
  public Map<Long, String> mapBackUserNickname(Collection<Long> userIds) {
    if (CollectionUtils.isEmpty(userIds)) {
      return CollectionUtils.newHashMap();
    }
    return Optional.ofNullable(
            query().select(User::getId, User::getNickname)
                .in(User::getId, userIds)
                .list()
        ).map(users -> users.stream()
            .collect(Collectors.toMap(k -> DataTypeUtils.toLong(k.getId()), User::getNickname)))
        .orElse(CollectionUtils.newHashMap());
  }

}
