package com.red.circle.other.infra.database.cache.service.user.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.infra.convertor.user.FriendshipCardConfigConvertor;
import com.red.circle.other.infra.database.cache.entity.user.UserFriendshipCardConfigCache;
import com.red.circle.other.infra.database.cache.key.user.UserKey;
import com.red.circle.other.infra.database.cache.service.user.UserFriendshipCardConfigCacheService;
import com.red.circle.other.infra.database.rds.entity.user.user.FriendshipCardConfig;
import com.red.circle.other.infra.database.rds.service.user.user.FriendshipCardConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户友谊卡配置 - 缓存.
 * </p>
 *
 * @author pengshigang
 * @since 2022-6-9
 */
@Service
@RequiredArgsConstructor
public class UserFriendshipCardConfigCacheServiceImpl implements
    UserFriendshipCardConfigCacheService {

  private final RedisService redisService;
  private final FriendshipCardConfigService friendshipCardConfigService;
  private final FriendshipCardConfigConvertor friendshipCardConfigConvertor;

  @Override
  public List<FriendshipCardConfig> listSysOriginConfig(String sysOrigin) {
    return friendshipCardConfigConvertor.toListFriendshipCardConfig(
        listConfigCacheBySysOrigin(sysOrigin, (origin) -> {
          List<FriendshipCardConfig> configs = configs(origin);
          if (CollectionUtils.isEmpty(configs)) {
            return Lists.newArrayList();
          }
          return friendshipCardConfigConvertor.toListUserFriendshipCardConfigCache(configs);
        })
    );
  }

  @Override
  public FriendshipCardConfig getSysOriginConfig(String sysOrigin, String type) {
    List<FriendshipCardConfig> configCaches = listSysOriginConfig(sysOrigin);
    return configCaches.stream().filter(v -> Objects.equals(v.getCardType(), type)).findAny()
        .orElse(null);
  }


  @Override
  public void remove() {
    redisService.delete(UserKey.FRIENDSHIP_CARD_CONF.getKey());
  }


  private List<UserFriendshipCardConfigCache> listConfigCacheBySysOrigin(String sysOrigin,
      Function<String, List<UserFriendshipCardConfigCache>> orElsePut) {

    return Optional
        .ofNullable(redisService.hashGet(UserKey.FRIENDSHIP_CARD_CONF.getKey(), sysOrigin,
            new TypeReference<List<UserFriendshipCardConfigCache>>() {
            }))
        .orElseGet(() -> {
          List<UserFriendshipCardConfigCache> bannerCaches = orElsePut.apply(sysOrigin);
          if (CollectionUtils.isNotEmpty(bannerCaches)) {
            redisService.hashPut(UserKey.FRIENDSHIP_CARD_CONF.getKey(), sysOrigin, bannerCaches);
          }
          return bannerCaches;
        });
  }

  private List<FriendshipCardConfig> configs(String sysOrigin) {
    return friendshipCardConfigService.query()
        .eq(FriendshipCardConfig::getSysOrigin, sysOrigin)
        .eq(FriendshipCardConfig::getShowcase, Boolean.TRUE)
        .last(PageConstant.formatLimit(100))
        .list();
  }
}
