package com.red.circle.console.infra.database.rds.service.count.impl;

import com.red.circle.console.infra.database.rds.dao.count.StatisticsDailyCurrencyCountPlatformDAO;
import com.red.circle.console.infra.database.rds.entity.count.StatisticsDailyCurrencyCountPlatform;
import com.red.circle.console.infra.database.rds.service.count.StatisticsDailyCurrencyCountPlatformService;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 每日货币流通统计 服务实现类
 * </p>
 *
 * @author pengliang
 * @since 2021-07-30
 */
@Service
@RequiredArgsConstructor
public class StatisticsDailyCurrencyCountPlatformServiceImpl extends
    BaseServiceImpl<StatisticsDailyCurrencyCountPlatformDAO, StatisticsDailyCurrencyCountPlatform> implements
    StatisticsDailyCurrencyCountPlatformService {

  @Override
  public Map<String, List<StatisticsDailyCurrencyCountPlatform>> mapSysOriginLatestGroup(
      int latestDays) {
    return Optional.ofNullable(query()
            .gt(StatisticsDailyCurrencyCountPlatform::getCreateTime,
                LocalDateTime.now().minusDays(latestDays))
            .orderByAsc(StatisticsDailyCurrencyCountPlatform::getCreateTime)
            .list())
        .map(platforms -> platforms.stream()
            .collect(Collectors.groupingBy(StatisticsDailyCurrencyCountPlatform::getSysOrigin)))
        .orElseGet(CollectionUtils::newHashMap);
  }

  @Override
  public void countIntegral(Integer countTimeNumber, LocalDate countTime, LocalDate endCountTime) {
    baseDAO.countIntegral(countTimeNumber, countTime, endCountTime);
  }

}
