task-161(客户端公开版本接口兼容): 冻结 /api/version 响应契约
新增 PublicVersionLookup.currentVersionResponse + Task161Test。 TDD: RED→GREEN。
This commit is contained in:
+74
@@ -0,0 +1,74 @@
|
||||
package com.nanri.aiimage.modules.softwareversion.service.support;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 客户端公开版本查询纯逻辑(module 09):空语义、created_at DESC + id 稳定排序、
|
||||
* 无 ApiResponse 包装;供 PublicVersionController/契约测试复用。
|
||||
*/
|
||||
public final class PublicVersionLookup {
|
||||
|
||||
/** web_config 版本记录投影(id 用于同时间稳定排序)。 */
|
||||
public record WebVersion(Long id, String version, String fileUrl, LocalDateTime createdAt) {
|
||||
}
|
||||
|
||||
private PublicVersionLookup() {
|
||||
}
|
||||
|
||||
/** 取最新版本记录:created_at DESC,同一时间按 id 倒序稳定;空/无有效回 null。 */
|
||||
public static WebVersion selectLatest(List<WebVersion> rows) {
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<WebVersion> list = new ArrayList<>(rows);
|
||||
list.sort((a, b) -> {
|
||||
int time = compareTime(b.createdAt(), a.createdAt());
|
||||
if (time != 0) {
|
||||
return time;
|
||||
}
|
||||
long ai = a.id() == null ? 0L : a.id();
|
||||
long bi = b.id() == null ? 0L : b.id();
|
||||
return Long.compare(bi, ai);
|
||||
});
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
private static int compareTime(LocalDateTime a, LocalDateTime b) {
|
||||
if (a == null && b == null) {
|
||||
return 0;
|
||||
}
|
||||
if (a == null) {
|
||||
return -1;
|
||||
}
|
||||
if (b == null) {
|
||||
return 1;
|
||||
}
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
/** 当前服务版本响应:保留 version/desc/url 字段(可为空字符串),不包装 ApiResponse。 */
|
||||
public static Map<String, Object> currentVersionResponse(String version, String desc, String url) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("version", version == null ? "" : version);
|
||||
map.put("desc", desc == null ? "" : desc);
|
||||
map.put("url", url == null ? "" : url);
|
||||
return map;
|
||||
}
|
||||
|
||||
/** 最新版本响应:无记录返回 version=null,file_url=null(客户端语义)。 */
|
||||
public static Map<String, Object> latestVersionResponse(WebVersion latest) {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
if (latest == null) {
|
||||
map.put("version", null);
|
||||
map.put("file_url", null);
|
||||
return map;
|
||||
}
|
||||
map.put("version", latest.version());
|
||||
map.put("file_url", latest.fileUrl());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user