VppUnPayloadHelper.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.usky.vpp.util;
  2. import com.usky.vpp.constant.VppDrEventStatus;
  3. import org.springframework.util.StringUtils;
  4. import java.math.BigDecimal;
  5. import java.time.Instant;
  6. import java.time.LocalDateTime;
  7. import java.time.ZoneId;
  8. import java.time.format.DateTimeFormatter;
  9. import java.time.format.DateTimeParseException;
  10. import java.util.Map;
  11. /**
  12. * 运管平台 UN 报文解析辅助(兼容多种字段命名与嵌套结构)
  13. */
  14. public final class VppUnPayloadHelper {
  15. private static final DateTimeFormatter[] DATE_TIME_FORMATTERS = {
  16. DateTimeFormatter.ISO_LOCAL_DATE_TIME,
  17. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
  18. DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"),
  19. DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")
  20. };
  21. private VppUnPayloadHelper() {
  22. }
  23. public static Map<String, Object> unwrapInvitationPayload(Map<String, Object> body) {
  24. return unwrap(body, "event", "eventInfo", "eventData", "invitation", "data");
  25. }
  26. public static Map<String, Object> unwrapOptPayload(Map<String, Object> body) {
  27. Map<String, Object> nested = unwrap(body, "opt", "optInfo", "optRequest", "event", "eventInfo", "data");
  28. return nested != null ? nested : body;
  29. }
  30. public static Map<String, Object> unwrapClearingPayload(Map<String, Object> body) {
  31. return unwrap(body, "cq", "cqInfo", "cqRequest", "clearing", "clearingInfo", "event", "data");
  32. }
  33. @SuppressWarnings("unchecked")
  34. private static Map<String, Object> unwrap(Map<String, Object> body, String... nestedKeys) {
  35. if (body == null || body.isEmpty()) {
  36. return body;
  37. }
  38. for (String key : nestedKeys) {
  39. Object nested = body.get(key);
  40. if (nested instanceof Map) {
  41. return (Map<String, Object>) nested;
  42. }
  43. }
  44. return body;
  45. }
  46. public static boolean hasInvitationFields(Map<String, Object> payload) {
  47. if (payload == null) {
  48. return false;
  49. }
  50. if (payload.containsKey("events") || payload.containsKey("descriptor")) {
  51. return true;
  52. }
  53. return getDateTime(payload, "startTime", "start_time", "dtstart", "beginTime") != null
  54. || getDecimal(payload, "targetCapacityKw", "target_capacity_kw", "targetKw", "capacity", "targetCapacity") != null;
  55. }
  56. public static Boolean getBoolean(Map<String, Object> map, String... keys) {
  57. Object value = getValue(map, keys);
  58. if (value == null) {
  59. return null;
  60. }
  61. if (value instanceof Boolean) {
  62. return (Boolean) value;
  63. }
  64. String text = String.valueOf(value).trim().toLowerCase();
  65. if ("true".equals(text) || "1".equals(text)) {
  66. return true;
  67. }
  68. if ("false".equals(text) || "0".equals(text)) {
  69. return false;
  70. }
  71. return null;
  72. }
  73. public static String getString(Map<String, Object> map, String... keys) {
  74. Object value = getValue(map, keys);
  75. if (value == null) {
  76. return null;
  77. }
  78. String text = String.valueOf(value).trim();
  79. return StringUtils.hasText(text) ? text : null;
  80. }
  81. public static Integer getInteger(Map<String, Object> map, String... keys) {
  82. Object value = getValue(map, keys);
  83. if (value == null) {
  84. return null;
  85. }
  86. if (value instanceof Number) {
  87. return ((Number) value).intValue();
  88. }
  89. String text = String.valueOf(value).trim();
  90. if (!StringUtils.hasText(text)) {
  91. return null;
  92. }
  93. try {
  94. return Integer.parseInt(text);
  95. } catch (NumberFormatException ex) {
  96. return null;
  97. }
  98. }
  99. public static Object getRaw(Map<String, Object> map, String... keys) {
  100. return getValue(map, keys);
  101. }
  102. public static BigDecimal getDecimal(Map<String, Object> map, String... keys) {
  103. Object value = getValue(map, keys);
  104. if (value == null) {
  105. return null;
  106. }
  107. if (value instanceof BigDecimal) {
  108. return (BigDecimal) value;
  109. }
  110. if (value instanceof Number) {
  111. return BigDecimal.valueOf(((Number) value).doubleValue());
  112. }
  113. String text = String.valueOf(value).trim();
  114. if (!StringUtils.hasText(text)) {
  115. return null;
  116. }
  117. try {
  118. return new BigDecimal(text);
  119. } catch (NumberFormatException ex) {
  120. return null;
  121. }
  122. }
  123. public static LocalDateTime getDateTime(Map<String, Object> map, String... keys) {
  124. Object value = getValue(map, keys);
  125. if (value == null) {
  126. return null;
  127. }
  128. if (value instanceof LocalDateTime) {
  129. return (LocalDateTime) value;
  130. }
  131. if (value instanceof Number) {
  132. long epoch = ((Number) value).longValue();
  133. if (String.valueOf(epoch).length() <= 10) {
  134. epoch *= 1000;
  135. }
  136. return LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.systemDefault());
  137. }
  138. String text = String.valueOf(value).trim();
  139. if (!StringUtils.hasText(text)) {
  140. return null;
  141. }
  142. for (DateTimeFormatter formatter : DATE_TIME_FORMATTERS) {
  143. try {
  144. return LocalDateTime.parse(text, formatter);
  145. } catch (DateTimeParseException ignored) {
  146. // try next pattern
  147. }
  148. }
  149. return null;
  150. }
  151. public static Integer parseResponseType(Object raw) {
  152. if (raw == null) {
  153. return null;
  154. }
  155. if (raw instanceof Number) {
  156. int value = ((Number) raw).intValue();
  157. return value >= 1 && value <= 3 ? value : null;
  158. }
  159. String text = String.valueOf(raw).trim().toUpperCase();
  160. if (text.contains("日前") || "DAY_AHEAD".equals(text) || "1".equals(text)) {
  161. return 1;
  162. }
  163. if (text.contains("日内") || "INTRADAY".equals(text) || "2".equals(text)) {
  164. return 2;
  165. }
  166. if (text.contains("秒") || "REALTIME".equals(text) || "3".equals(text)) {
  167. return 3;
  168. }
  169. return null;
  170. }
  171. public static Integer parseEventType(Object raw) {
  172. if (raw == null) {
  173. return null;
  174. }
  175. if (raw instanceof Number) {
  176. int value = ((Number) raw).intValue();
  177. return value >= 1 && value <= 2 ? value : null;
  178. }
  179. String text = String.valueOf(raw).trim().toUpperCase();
  180. if (text.contains("削峰") || "PEAK".equals(text) || "1".equals(text)) {
  181. return 1;
  182. }
  183. if (text.contains("填谷") || "VALLEY".equals(text) || "FILLVALLEY".equals(text) || "2".equals(text)) {
  184. return 2;
  185. }
  186. return null;
  187. }
  188. public static boolean isCancelled(Map<String, Object> payload) {
  189. Integer status = getInteger(payload, "eventStatus", "event_status", "status");
  190. if (status != null && (status == 4 || status == VppDrEventStatus.CANCELLED)) {
  191. return true;
  192. }
  193. String text = getString(payload, "eventStatus", "event_status", "status", "eventState");
  194. if (!StringUtils.hasText(text)) {
  195. return false;
  196. }
  197. text = text.trim().toUpperCase();
  198. return text.contains("取消") || "CANCELLED".equals(text) || "CANCELED".equals(text)
  199. || "4".equals(text) || "5".equals(text);
  200. }
  201. private static Object getValue(Map<String, Object> map, String... keys) {
  202. if (map == null) {
  203. return null;
  204. }
  205. for (String key : keys) {
  206. if (map.containsKey(key) && map.get(key) != null) {
  207. return map.get(key);
  208. }
  209. }
  210. return null;
  211. }
  212. }