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


import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.other.infra.database.rds.dao.dynamic.DynamicCommentLikeDAO;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicCommentLike;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicCommentLikeService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 动态-朋友圈评论点赞 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-11
 */
@Service
public class DynamicCommentLikeServiceImpl extends
    BaseServiceImpl<DynamicCommentLikeDAO, DynamicCommentLike> implements
    DynamicCommentLikeService {

  @Override
  public Map<Long, Boolean> mapByCommentIdsByUserId(Collection<Long> commentIds, Long userId) {

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

    return Optional.ofNullable(
            query()
                .eq(DynamicCommentLike::getCreateUser, userId)
                .in(DynamicCommentLike::getDynamicCommentId, commentIds)
                .last(PageConstant.formatLimit(commentIds.size()))
                .list()
        ).map(
            likes -> likes.stream()
                .collect(Collectors
                    .toMap(DynamicCommentLike::getDynamicCommentId,
                        Function.identity(),
                        (v0, v1) -> v0)).values().stream()
                .collect(Collectors.toMap(DynamicCommentLike::getDynamicCommentId,
                    v -> Objects.nonNull(v.getId())))
        )
        .orElse(CollectionUtils.newHashMap());
  }

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

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

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

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

  }

  @Override
  public void deleteByRootCommentId(Long rootCommentId) {
    if (Objects.isNull(rootCommentId)) {
      return;
    }

    delete().in(DynamicCommentLike::getRootCommentId, rootCommentId).execute();

    delete().eq(DynamicCommentLike::getDynamicCommentId, rootCommentId).last(PageConstant.LIMIT_ONE)
        .execute();
  }

  @Override
  public Integer getNumberByRootCommentIdByNotUserId(Long rootCommentId, Long notUserId) {

    if (Objects.isNull(rootCommentId)) {
      return 0;
    }

    return Optional.ofNullable(query()
            .in(DynamicCommentLike::getRootCommentId, rootCommentId)
            .ne(DynamicCommentLike::getCreateUser, notUserId)
            .count())
        .map(Long::intValue)
        .orElse(0);
  }
}
