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

import com.red.circle.console.infra.database.mongo.count.entity.CountGoldOriginDailyDetails;
import com.red.circle.console.infra.database.mongo.count.service.CountGoldOriginDailyDetailsService;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2022/9/21
 */
@Service
@AllArgsConstructor
public class CountGoldOriginDailyDetailsServiceImpl implements CountGoldOriginDailyDetailsService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void addBatch(List<CountGoldOriginDailyDetails> details) {
    mongoTemplate.insert(details, CountGoldOriginDailyDetails.class);
  }

  @Override
  public List<CountGoldOriginDailyDetails> listLatestDays(String origin, Integer size) {
    return mongoTemplate.find(Query.query(Criteria.where("origin").is(origin))
            .with(Sort.by(Sort.Order.desc("dateNumber")))
            .limit(size)
        , CountGoldOriginDailyDetails.class);
  }

  @Override
  public List<CountGoldOriginDailyDetails> listTopsSize(Integer dateNumber) {
    return mongoTemplate.find(Query.query(Criteria.where("dateNumber").is(dateNumber))
            .with(Sort.by(Sort.Order.desc("quantity")))
        , CountGoldOriginDailyDetails.class);
  }

}
