提交 30027566 authored 作者: wangqiang's avatar wangqiang

客户端优化

上级 2f7cf16c
......@@ -109,53 +109,57 @@ public class ProgramController {
@GetMapping(value = "{type}/list")
public R<Page<Map<String, String>>> list(@PathVariable @Pattern(regexp = "install|upgrade") String type,
@RequestParam(required = false, defaultValue = "1") Integer current,
@RequestParam(required = false, defaultValue = "10") Integer size)
throws IOException {
String mac = MacAddrUtil.get();
if (StrUtil.isBlank(mac)) return R.error("未获取到MAC地址");
String url = gateway + StrUtil.format(type.equals("install") ? installUrl : upgradeUrl, mac, current, size);
JSONObject rsp = JSONUtil.parseObj(HttpUtil.get(url));
if (ObjectUtil.notEqual(rsp.getInt("code"), 0)) return R.error(rsp.getStr("msg"));
Page<Map<String, String>> page = new Page<>();
page.setCurrent(current).setSize(size).setTotal(rsp.getLong("total"));
JSONArray rows = rsp.getJSONArray("rows");
if (CollUtil.isEmpty(rows)) return R.success(page.setRecords(Collections.emptyList()));
List<Map<String, String>> programs = installedProgramCache.getPrograms();
List<Map<String, String>> list = rows.stream().map(JSONObject.class::cast)
.map(o -> {
Map<String, String> map = new HashMap<>();
map.put("id", o.getStr(type.equals("install") ? "installId" : "upgradeId"));
map.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true));
map.put("size", o.getStr("fileSize"));
map.put("version", o.getStr("version"));
map.put("localVersion", null);
map.put("url", o.getStr("url"));
Long id = Convert.toLong(map.get("id"));
String key = type + "-" + id;
map.put("status", SET.contains(key) ? "1" : MAP.containsKey(key) ? "2" : "0");
return map;
})
/*.peek(m -> */
.peek(m -> {
if (type.equals("install")) {
programs.stream()
.filter(p -> p.get("name").equalsIgnoreCase(m.get("name")))
.findFirst()
.ifPresent(p -> m.put("localVersion", p.get("version")));
} else {
programs.stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), m.get("name").toLowerCase()),
p -> p.getOrDefault("version", ""), (l, r) -> r))
.entrySet()
.stream()
.max(Map.Entry.comparingByKey())
.filter(e -> e.getKey() >= MIN_MATCH)
.filter(e -> StrUtil.isNotBlank(e.getValue()))
.ifPresent(e -> m.put("localVersion", e.getValue()));
}
})
.collect(Collectors.toList());
return R.success(page.setRecords(list));
@RequestParam(required = false, defaultValue = "10") Integer size) {
try {
String mac = MacAddrUtil.get();
if (StrUtil.isBlank(mac)) return R.error("未获取到MAC地址");
String url = gateway + StrUtil.format(type.equals("install") ? installUrl : upgradeUrl, mac, current, size);
JSONObject rsp = JSONUtil.parseObj(HttpUtil.get(url));
if (ObjectUtil.notEqual(rsp.getInt("code"), 0)) return R.error(rsp.getStr("msg"));
Page<Map<String, String>> page = new Page<>();
page.setCurrent(current).setSize(size).setTotal(rsp.getLong("total"));
JSONArray rows = rsp.getJSONArray("rows");
if (CollUtil.isEmpty(rows)) return R.success(page.setRecords(Collections.emptyList()));
List<Map<String, String>> programs = installedProgramCache.getPrograms();
List<Map<String, String>> list = rows.stream().map(JSONObject.class::cast)
.map(o -> {
Map<String, String> map = new HashMap<>();
map.put("id", o.getStr(type.equals("install") ? "installId" : "upgradeId"));
map.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true));
map.put("size", o.getStr("fileSize"));
map.put("version", o.getStr("version"));
map.put("localVersion", null);
map.put("url", o.getStr("url"));
Long id = Convert.toLong(map.get("id"));
String key = type + "-" + id;
map.put("status", SET.contains(key) ? "1" : MAP.containsKey(key) ? "2" : "0");
return map;
})
/*.peek(m -> */
.peek(m -> {
if (type.equals("install")) {
programs.stream()
.filter(p -> p.get("name").equalsIgnoreCase(m.get("name")))
.findFirst()
.ifPresent(p -> m.put("localVersion", p.get("version")));
} else {
programs.stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), m.get("name").toLowerCase()),
p -> p.getOrDefault("version", ""), (l, r) -> r))
.entrySet()
.stream()
.max(Map.Entry.comparingByKey())
.filter(e -> e.getKey() >= MIN_MATCH)
.filter(e -> StrUtil.isNotBlank(e.getValue()))
.ifPresent(e -> m.put("localVersion", e.getValue()));
}
})
.collect(Collectors.toList());
return R.success(page.setRecords(list));
} catch (IOException e) {
log.error("客户端系统异常", e);
return R.error("客户端正在初始化,请稍后再试!");
}
}
private static final Set<String> SET = new ConcurrentHashSet<>();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论