package com.zy.asrs.controller;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.R;
|
import com.zy.common.utils.RedisUtil;
|
import com.zy.core.enums.RedisKeyType;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.*;
|
|
@RestController
|
@RequestMapping("/watch/stationColor")
|
public class WatchStationColorController {
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
@GetMapping("/config/auth")
|
@ManagerAuth
|
public R getConfig() {
|
return R.ok(buildResponseData(loadStoredColorMap()));
|
}
|
|
@PostMapping("/config/save/auth")
|
@ManagerAuth
|
public R saveConfig(@RequestBody Map<String, Object> payload) {
|
Map<String, String> storedMap = loadStoredColorMap();
|
Map<String, String> mergedMap = new LinkedHashMap<>(storedMap);
|
|
Object itemsObj = payload == null ? null : payload.get("items");
|
if (!(itemsObj instanceof List)) {
|
return R.error("请传入颜色配置列表");
|
}
|
|
List<?> items = (List<?>) itemsObj;
|
Set<String> allowedStatuses = getDefaultColorMap().keySet();
|
for (Object obj : items) {
|
if (!(obj instanceof Map)) {
|
continue;
|
}
|
Map<?, ?> item = (Map<?, ?>) obj;
|
String status = item.get("status") == null ? null : String.valueOf(item.get("status")).trim();
|
String color = item.get("color") == null ? null : String.valueOf(item.get("color")).trim();
|
if (status == null || status.isEmpty() || !allowedStatuses.contains(status)) {
|
continue;
|
}
|
mergedMap.put(status, normalizeColor(color, getDefaultColorMap().get(status)));
|
}
|
|
redisUtil.set(RedisKeyType.WATCH_STATION_COLOR_CONFIG.key, JSON.toJSONString(mergedMap));
|
return R.ok(buildResponseData(mergedMap));
|
}
|
|
private Map<String, Object> buildResponseData(Map<String, String> storedMap) {
|
Map<String, String> defaults = getDefaultColorMap();
|
List<Map<String, String>> items = new ArrayList<>();
|
for (Map<String, String> meta : getColorMetaList()) {
|
String status = meta.get("status");
|
Map<String, String> item = new LinkedHashMap<>(meta);
|
item.put("defaultColor", defaults.get(status));
|
item.put("color", normalizeColor(storedMap.get(status), defaults.get(status)));
|
items.add(item);
|
}
|
Map<String, Object> data = new LinkedHashMap<>();
|
data.put("items", items);
|
data.put("defaults", defaults);
|
return data;
|
}
|
|
private Map<String, String> loadStoredColorMap() {
|
Map<String, String> result = new LinkedHashMap<>(getDefaultColorMap());
|
Object object = redisUtil.get(RedisKeyType.WATCH_STATION_COLOR_CONFIG.key);
|
if (object == null) {
|
return result;
|
}
|
try {
|
JSONObject jsonObject;
|
if (object instanceof JSONObject) {
|
jsonObject = (JSONObject) object;
|
} else {
|
jsonObject = JSON.parseObject(String.valueOf(object));
|
}
|
if (jsonObject == null) {
|
return result;
|
}
|
for (String status : getDefaultColorMap().keySet()) {
|
String color = jsonObject.getString(status);
|
if (color != null) {
|
result.put(status, normalizeColor(color, getDefaultColorMap().get(status)));
|
}
|
}
|
} catch (Exception ignore) {
|
}
|
return result;
|
}
|
|
private Map<String, String> getDefaultColorMap() {
|
Map<String, String> defaults = new LinkedHashMap<>();
|
defaults.put("site-auto", "#78FF81");
|
defaults.put("site-auto-run", "#FA51F6");
|
defaults.put("site-auto-id", "#C4C400");
|
defaults.put("site-auto-run-id", "#30BFFC");
|
defaults.put("site-enable-in", "#A81DEE");
|
defaults.put("site-unauto", "#B8B8B8");
|
defaults.put("machine-pakin", "#30BFFC");
|
defaults.put("machine-pakout", "#97B400");
|
defaults.put("site-run-block", "#E69138");
|
return defaults;
|
}
|
|
private List<Map<String, String>> getColorMetaList() {
|
List<Map<String, String>> list = new ArrayList<>();
|
list.add(buildMeta("site-auto", "自动", "站点自动待命时的颜色。"));
|
list.add(buildMeta("site-auto-run", "自动 + 有物", "站点自动运行且有物,但还没有工作号时的颜色。"));
|
list.add(buildMeta("site-auto-id", "自动 + 工作号", "站点自动且存在工作号,但当前无物时的颜色。"));
|
list.add(buildMeta("site-auto-run-id", "自动 + 有物 + 工作号", "普通运行中的站点颜色,未命中入库/出库范围时使用。"));
|
list.add(buildMeta("site-enable-in", "启动入库", "工作号为 9998 或站点带启动入库标记时的颜色。"));
|
list.add(buildMeta("machine-pakin", "入库任务", "工作号命中入库范围时的颜色。"));
|
list.add(buildMeta("machine-pakout", "出库任务", "工作号命中出库范围时的颜色。"));
|
list.add(buildMeta("site-run-block", "运行堵塞", "站点处于运行堵塞时的颜色。"));
|
list.add(buildMeta("site-unauto", "非自动", "站点非自动时的颜色。"));
|
return list;
|
}
|
|
private Map<String, String> buildMeta(String status, String name, String desc) {
|
Map<String, String> map = new LinkedHashMap<>();
|
map.put("status", status);
|
map.put("name", name);
|
map.put("desc", desc);
|
return map;
|
}
|
|
private String normalizeColor(String color, String fallback) {
|
if (color == null) {
|
return fallback;
|
}
|
String value = color.trim();
|
if (value.matches("^#[0-9a-fA-F]{6}$")) {
|
return value.toUpperCase();
|
}
|
if (value.matches("^#[0-9a-fA-F]{3}$")) {
|
return ("#" + value.charAt(1) + value.charAt(1) + value.charAt(2) + value.charAt(2) + value.charAt(3) + value.charAt(3)).toUpperCase();
|
}
|
if (value.matches("^0x[0-9a-fA-F]{6}$")) {
|
return ("#" + value.substring(2)).toUpperCase();
|
}
|
return fallback;
|
}
|
}
|