package com.red.circle.order.infra.database.rds.service.order.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.order.infra.database.rds.dao.order.OrderGoogleVoidedPurchaseDAO;
import com.red.circle.order.infra.database.rds.entity.order.OrderGoogleVoidedPurchase;
import com.red.circle.order.infra.database.rds.service.order.OrderGoogleVoidedPurchaseService;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;

/**
 * <p>
 * google 购买退款订单 服务实现类.
 * </p>
 *
 * @author pengliang
 * @since 2021-05-24
 */
@Service
public class OrderGoogleVoidedPurchaseServiceImpl extends
    BaseServiceImpl<OrderGoogleVoidedPurchaseDAO, OrderGoogleVoidedPurchase> implements
    OrderGoogleVoidedPurchaseService {

  @Override
  public boolean existsOrder(String orderId) {
    return Optional.ofNullable(
            query()
                .select(OrderGoogleVoidedPurchase::getId)
                .eq(OrderGoogleVoidedPurchase::getOrderId, orderId)
                .last(PageConstant.LIMIT_ONE)
                .getOne()
        ).map(orderGoogleVoidedPurchase -> Objects.nonNull(orderGoogleVoidedPurchase.getId()))
        .orElse(Boolean.FALSE);
  }

  @Override
  public boolean existsOrder(Long userId, Integer withinDays) {
    return Optional.ofNullable(
            query()
                .select(OrderGoogleVoidedPurchase::getId)
                .eq(OrderGoogleVoidedPurchase::getUserId, userId)
                .ge(OrderGoogleVoidedPurchase::getCreateTime,
                    TimestampUtils.nowMinusDays(withinDays))
                .last(PageConstant.LIMIT_ONE)
                .getOne()
        ).map(orderGoogleVoidedPurchase -> Objects.nonNull(orderGoogleVoidedPurchase.getId()))
        .orElse(Boolean.FALSE);
  }
}
