package com.red.circle.other.app.inner.service.dynamic.impl;


import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.dynamic.DynamicInnerConvertor;
import com.red.circle.other.app.inner.service.dynamic.DynamicContentClientService;
import com.red.circle.other.infra.database.cache.service.other.DynamicCacheService;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicPicture;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicContentService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicMessageService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicPictureService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicTimelineService;
import com.red.circle.other.infra.database.rds.service.dynamic.TopDynamicService;
import com.red.circle.other.inner.model.cmd.dynamic.DelDynamicContentCmd;
import com.red.circle.other.inner.model.cmd.dynamic.DynamicContentCmd;
import com.red.circle.other.inner.model.cmd.dynamic.DynamicContentQryCmd;
import com.red.circle.other.inner.model.dto.dynamic.DynamicContentDTO;
import com.red.circle.other.inner.model.dto.dynamic.PageDynamicContentDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.LocalDateTimeUtils;
import java.util.List;
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 pengshigang
 * @since 2022-06-30
 */
@Service
@RequiredArgsConstructor
public class DynamicContentClientServiceImpl implements DynamicContentClientService {

  private final TopDynamicService topDynamicService;
  private final DynamicCacheService dynamicCacheService;
  private final DynamicContentService dynamicContentService;
  private final DynamicInnerConvertor dynamicInnerConvertor;
  private final DynamicPictureService dynamicPictureService;
  private final DynamicMessageService dynamicMessageService;
  private final DynamicTimelineService dynamicTimelineService;

  @Override
  public PageResult<PageDynamicContentDTO> page(DynamicContentQryCmd query) {
    if (query.getStartTime() != null && query.getEndTime() != null){
      query.setStartCreateDate(LocalDateTimeUtils.convert(query.getStartTime()));
      query.setEndCreateDate(LocalDateTimeUtils.convert(query.getEndTime()));
    }
    PageResult<PageDynamicContentDTO> pageResult = dynamicContentService.page(query);
    Set<Long> dynamicIds = getDynamicIds(pageResult);
    Map<Long, List<DynamicPicture>> pictureMap = dynamicPictureService.mapByDynamicIds(dynamicIds);

    return pageResult.convert(vo -> {
      List<DynamicPicture> pictures = pictureMap.get(vo.getId());
      if (CollectionUtils.isNotEmpty(pictures)) {
        vo.setPictures(pictures.stream().map(dynamicInnerConvertor::toDynamicPictureDTO)
            .collect(Collectors.toList()));
      }
      return vo;
    });
  }

  @Override
  public void deleteByIds(DelDynamicContentCmd cmd) {

    ResponseAssert.notEmpty(CommonErrorCode.NOT_FOUND_RECORD_INFO, cmd.getIds());

    dynamicContentService.deleteByIds(cmd);

    dynamicTimelineService.deleteByDynamicContentIds(cmd.getIds());

    topDynamicService.deleteByDynamicIds(cmd.getIds());
    //移除置顶缓存
    dynamicCacheService.removeTopDynamic();

  }

  @Override
  public Long countDynamicMessage(Long userId) {
    return Optional.ofNullable(dynamicMessageService.countDynamicMessage(userId)).orElse(0L);
  }


  private Set<Long> getDynamicIds(PageResult<PageDynamicContentDTO> iPage) {
    return iPage.getRecords().stream().map(PageDynamicContentDTO::getId)
        .collect(Collectors.toSet());
  }

  @Override
  public void save(DynamicContentCmd cmd) {
    DynamicContent dynamicContent = dynamicInnerConvertor.toDynamicContent(cmd);
    if (Objects.nonNull(dynamicContent)) {
      dynamicContentService.save(dynamicContent);
    }
  }
}

