com.github.shoothzj.sdk.distribute.impl.mongo.repo.ElectionIdRepoImpl Maven / Gradle / Ivy
package com.github.shoothzj.sdk.distribute.impl.mongo.repo;
import com.github.shoothzj.sdk.distribute.impl.mongo.dao.ElectionId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.Component;
/**
* @author hezhangjian
*/
@Slf4j
@Component
public class ElectionIdRepoImpl implements ElectionIdRepoCustom {
private MongoTemplate mongoTemplate;
public ElectionIdRepoImpl(@Autowired MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Override
public Long maxId(String scene) {
Query query = new Query(Criteria.where("scene").is(scene)).limit(1)
.with(Sort.by(Sort.Direction.DESC, "incrId"));
ElectionId template = mongoTemplate.findOne(query, ElectionId.class);
return template == null ? 0 : template.getIncrId();
}
}