2024-04-13 08:00:31 +00:00
|
|
|
package com.example.catchTheLetters.service.impl;
|
|
|
|
|
|
|
|
import com.example.catchTheLetters.entity.Version;
|
2024-06-09 06:17:54 +00:00
|
|
|
import com.example.catchTheLetters.entity.VersionDownload;
|
2024-04-13 08:00:31 +00:00
|
|
|
import com.example.catchTheLetters.service.VersionService;
|
2024-06-09 06:17:54 +00:00
|
|
|
import com.example.catchTheLetters.utils.R;
|
2024-04-13 08:00:31 +00:00
|
|
|
import jakarta.annotation.Resource;
|
2024-06-09 06:17:54 +00:00
|
|
|
import org.springframework.data.domain.Sort;
|
2024-04-13 08:00:31 +00:00
|
|
|
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;
|
|
|
|
|
2024-06-09 06:17:54 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2024-04-13 08:00:31 +00:00
|
|
|
@Service
|
|
|
|
public class VersionServiceImpl implements VersionService {
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Long getVersion(String tableName) {
|
|
|
|
var res = mongoTemplate.findOne(new Query(Criteria.where("table").is(tableName)), Version.class);
|
|
|
|
return res == null ? null : res.getVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setVersion(String tableName, Long version) {
|
|
|
|
var res = mongoTemplate.findOne(new Query(Criteria.where("table").is(tableName)), Version.class);
|
|
|
|
if (res == null) {
|
|
|
|
mongoTemplate.save(new Version(tableName, version));
|
|
|
|
} else {
|
|
|
|
res.setVersion(version);
|
|
|
|
mongoTemplate.save(res);
|
|
|
|
}
|
|
|
|
}
|
2024-06-09 06:17:54 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public R<String> addVersionDownload(Long time, String version, String url) {
|
|
|
|
VersionDownload versionDownload = new VersionDownload();
|
|
|
|
versionDownload.setUrl(url);
|
|
|
|
versionDownload.setTime(time);
|
|
|
|
versionDownload.setVersion(version);
|
|
|
|
VersionDownload insert = mongoTemplate.insert(versionDownload);
|
|
|
|
if (insert.getId() == null){
|
|
|
|
return R.fail("添加数据库失败");
|
|
|
|
}
|
|
|
|
//TODO
|
|
|
|
return R.ok(insert.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public R<List<VersionDownload>> getNewDownloads() {
|
|
|
|
Query query = new Query()
|
|
|
|
.with(Sort.by(Sort.Direction.DESC,"time"))
|
|
|
|
.limit(5);
|
|
|
|
List<VersionDownload> list = mongoTemplate.find(query, VersionDownload.class);
|
|
|
|
return R.ok(list);
|
|
|
|
}
|
2024-04-13 08:00:31 +00:00
|
|
|
}
|