package com.red.circle.other.infra.database.rds.service.dynamic.impl;


import com.google.common.collect.Maps;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.other.infra.database.cache.service.other.DynamicCacheService;
import com.red.circle.other.infra.database.rds.dao.dynamic.DynamicLikeDAO;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicLike;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicLikeService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 动态-朋友圈点赞 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-11
 */
@Service
@RequiredArgsConstructor
public class DynamicLikeServiceImpl extends BaseServiceImpl<DynamicLikeDAO, DynamicLike> implements
    DynamicLikeService {

  private final DynamicLikeDAO dynamicLikeDAO;
  private final DynamicCacheService dynamicCacheService;

  @Override
  public Map<Long, Boolean> mapByContentIdsByUserId(Collection<Long> contentIds, Long userId) {

    if (CollectionUtils.isEmpty(contentIds)) {
      return CollectionUtils.newHashMap();
    }

    List<DynamicLike> likes = query()
        .eq(DynamicLike::getCreateUser, userId)
        .in(DynamicLike::getDynamicContentId, contentIds)
        .last(PageConstant.formatLimit(contentIds.size()))
        .list();

    if (CollectionUtils.isEmpty(likes)) {
      return CollectionUtils.newHashMap();
    }

    return likes.stream()
        .collect(Collectors.toMap(
                DynamicLike::getDynamicContentId,
                Function.identity(),
                (v0, v1) -> v0
            )
        ).values().stream().collect(Collectors.toMap(
            DynamicLike::getDynamicContentId,
            v -> Objects.nonNull(v.getId())
        ));
  }

  private Long getLikeQuantity(Long contentId) {
    return Optional.ofNullable(dynamicLikeDAO.getLikeQuantity(contentId))
        .orElse(0L);
  }

  @Override
  public Boolean exist(Long contentId, Long createUser) {

    return Optional.ofNullable(
        query()
            .eq(DynamicLike::getDynamicContentId, contentId)
            .eq(DynamicLike::getCreateUser, createUser)
            .last(PageConstant.LIMIT_ONE)
            .getOne()
    ).map(obj -> Boolean.TRUE).orElse(Boolean.FALSE);
  }

  @Override
  public void delete(Long contentId, Long createUser) {

    delete()
        .eq(DynamicLike::getDynamicContentId, contentId)
        .eq(DynamicLike::getCreateUser, createUser)
        .last(PageConstant.LIMIT_ONE)
        .execute();

  }

  @Override
  public Map<Long, Long> mapLikeQuantity(Collection<Long> contentIds) {
    if (CollectionUtils.isEmpty(contentIds)) {
      return Maps.newHashMap();
    }

    Map<Long, Long> result = Maps.newHashMap();

    for (Long contentId : contentIds) {
      Long quantity = dynamicCacheService.getLikeQuantity(contentId);
      if (Objects.isNull(quantity)) {
        quantity = getLikeQuantity(contentId);
        dynamicCacheService.initLikeQuantity(contentId, quantity);
      }
      result.put(contentId, quantity);
    }

    return result;
  }
}
