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

import com.red.circle.console.infra.database.rds.dao.count.StatisticsDailyPurchaseAmountDAO;
import com.red.circle.console.infra.database.rds.entity.count.StatisticsDailyPurchaseAmount;
import com.red.circle.console.infra.database.rds.service.count.StatisticsDailyPurchaseAmountService;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 每日内购量 服务实现类
 * </p>
 *
 * @author pengliang
 * @since 2020-11-12
 */
@Service
public class StatisticsDailyPurchaseAmountServiceImpl extends
    BaseServiceImpl<StatisticsDailyPurchaseAmountDAO, StatisticsDailyPurchaseAmount> implements
    StatisticsDailyPurchaseAmountService {

  @Override
  public List<StatisticsDailyPurchaseAmount> listLatest(int latestDays, String areaCode) {
    return query()
        .eq(StatisticsDailyPurchaseAmount::getAreaCode, areaCode)
        .gt(StatisticsDailyPurchaseAmount::getStatisticsTime,
            LocalDateTime.now().minusDays(latestDays))
        .orderByAsc(StatisticsDailyPurchaseAmount::getStatisticsTime)
        .list();
  }

  @Override
  public void inrOfflineQuantity(Integer dateNumber, BigDecimal quantity) {
    update()
        .setSql("purchase=IF(purchase IS NULL, 0, purchase)+" + quantity)
        .eq(StatisticsDailyPurchaseAmount::getPlatform, "Offline")
        .apply("date_format(statistics_time,'%Y%m%d') = {0}", dateNumber)
        .last(PageConstant.LIMIT_ONE)
        .execute();
  }

  @Override
  public void countInAppPurchaseOrRefund(LocalDate countTime,
      LocalDate endCountTime) {
    baseDAO.countInAppPurchaseOrRefund(countTime, endCountTime);
  }

  @Override
  public void countInAppPurchaseOrRefundRegion(LocalDate countTime,
      LocalDate endCountTime) {
    baseDAO.countInAppPurchaseOrRefundRegion(countTime, endCountTime);
  }

  @Override
  public void countInAppPurchaseOrRefundOther(LocalDate countTime, LocalDate endCountTime) {
    baseDAO.countInAppPurchaseOrRefundOther(countTime, endCountTime);
  }

  @Override
  public void countInAppPurchaseOrRefundOffline(LocalDate countTime,
      LocalDate endCountTime) {
    baseDAO.countInAppPurchaseOrRefundOffline(countTime, endCountTime);
  }
}
