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

import com.google.common.collect.Lists;
import com.red.circle.framework.dto.PageQuery;
import com.red.circle.framework.dto.PageResult;
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.DynamicTimelineDAO;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicTimeline;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicTimelineService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 动态-时间轴表 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-11
 */
@Service
public class DynamicTimelineServiceImpl extends
    BaseServiceImpl<DynamicTimelineDAO, DynamicTimeline> implements DynamicTimelineService {


  @Override
  public void deleteByDynamicContentId(Long dynamicContentId) {

    if (Objects.isNull(dynamicContentId)) {
      return;
    }
    delete().eq(DynamicTimeline::getDynamicContentId, dynamicContentId).execute();
  }

  @Override
  public void deleteByDynamicContentIds(Set<Long> dynamicIds) {

    if (CollectionUtils.isEmpty(dynamicIds)) {
      return;
    }

    delete()
        .in(DynamicTimeline::getDynamicContentId, dynamicIds)
        .last(PageConstant.formatLimit(dynamicIds.size()))
        .execute();

  }

  @Override
  public List<DynamicTimeline> pageByTimeDesc(String region, Integer pageNumber, Long createUser,
      Boolean isOwn) {

    if (Objects.isNull(pageNumber) || pageNumber < 1) {
      pageNumber = 1;
    }

    return query()
        .eq(DynamicTimeline::getCreateUser, createUser)
        .eq(DynamicTimeline::getOwn, isOwn)
        .in(DynamicTimeline::getRegion, region, "")
        .orderByDesc(DynamicTimeline::getCreateTime)
        .last(getPageSql(pageNumber))
        .list();
  }


  private String getPageSql(Integer pageNumber) {

    return String.format("LIMIT %s, %s", (pageNumber - 1) * PageConstant.DEFAULT_LIMIT_SIZE,
        PageConstant.DEFAULT_LIMIT_SIZE);
  }

  @Override
  public PageResult<DynamicTimeline> pageByTimeDesc(List<Long> ids, String region, PageQuery pageQuery,
                                                    Boolean isOwn) {
    return query()
        .in(DynamicTimeline::getCreateUser, ids)
        .eq(DynamicTimeline::getOwn, isOwn)
        .in(region != null, DynamicTimeline::getRegion, region, "")
        .orderByDesc(DynamicTimeline::getCreateTime)
        .page(pageQuery);
  }

  @Override
  public PageResult<DynamicTimeline> pageByTimeDesc(List<Long> ids, String region, PageQuery pageQuery,
                                                    Boolean isOwn, List<Long> pendingDynamicIds) {
    return query()
        .in(DynamicTimeline::getCreateUser, ids)
        .eq(DynamicTimeline::getOwn, isOwn)
        .in(region != null, DynamicTimeline::getRegion, region, "")
        .notIn(CollectionUtils.isNotEmpty(pendingDynamicIds), DynamicTimeline::getDynamicContentId, pendingDynamicIds)
        .orderByDesc(DynamicTimeline::getCreateTime)
        .page(pageQuery);
  }

  @Override
  public List<DynamicTimeline> ownListByUserId(Long userId) {

    return Optional.ofNullable(query()
        .eq(DynamicTimeline::getOwn, Boolean.TRUE)
        .eq(DynamicTimeline::getCreateUser, userId)
        .list()).orElse(Lists.newArrayList());
  }
}
