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

import com.google.common.collect.Lists;
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.OrderPurchaseFriendshipRecordDAO;
import com.red.circle.order.infra.database.rds.entity.order.OrderPurchaseFriendshipRecord;
import com.red.circle.order.infra.database.rds.service.order.OrderPurchaseFriendshipRecordService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 购买订单朋友关系记录 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-01-20
 */
@Service
public class OrderPurchaseFriendshipRecordServiceImpl extends
    BaseServiceImpl<OrderPurchaseFriendshipRecordDAO, OrderPurchaseFriendshipRecord> implements
    OrderPurchaseFriendshipRecordService {

  @Override
  public Long getFriendId(Long orderId) {
    return Optional.ofNullable(query()
            .select(OrderPurchaseFriendshipRecord::getFriendId)
            .eq(OrderPurchaseFriendshipRecord::getOrderId, orderId)
            .last(PageConstant.LIMIT_ONE).getOne())
        .map(OrderPurchaseFriendshipRecord::getFriendId)
        .orElse(null);
  }

  @Override
  public List<OrderPurchaseFriendshipRecord> listByOrderIds(Set<Long> orderIds) {
    if (CollectionUtils.isEmpty(orderIds)) {
      return CollectionUtils.newArrayList();
    }
    return Optional.ofNullable(query()
            .in(OrderPurchaseFriendshipRecord::getOrderId, orderIds)
            .list())
        .orElse(Lists.newArrayList());
  }
}
