package com.rj.util; import com.fasterxml.jackson.databind.JsonNode; import com.rj.entity.LbBuyerShopping; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.List; /** * 将 hxrd {@code /api/order/list} 响应中的订单节点解析为 {@link LbBuyerShopping}。 */ public final class LbBuyerShoppingOrderParser { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private LbBuyerShoppingOrderParser() { } public static List parseOrderList(JsonNode dataNode) { JsonNode listNode = extractListNode(dataNode); if (listNode == null || !listNode.isArray() || listNode.isEmpty()) { return List.of(); } if (listNode.get(0).isTextual() || listNode.get(0).isNumber()) { return List.of(); } List orders = new ArrayList<>(listNode.size()); for (JsonNode item : listNode) { LbBuyerShopping entity = fromOrderNode(item); if (entity != null) { orders.add(entity); } } return orders; } public static boolean resolveHasMore(JsonNode dataNode, int rowCount, int pageLimit) { if (dataNode != null && dataNode.isObject()) { if (dataNode.has("hasmore")) { return dataNode.path("hasmore").asBoolean(false); } } return rowCount >= pageLimit; } public static int resolveLastPage(JsonNode dataNode, int safePage, boolean hasMore) { if (dataNode != null && dataNode.isObject()) { int lastPage = dataNode.path("last_page").asInt(-1); if (lastPage >= 1) { return lastPage; } } return hasMore ? Integer.MAX_VALUE : safePage; } public static LbBuyerShopping fromOrderNode(JsonNode node) { if (node == null || node.isNull() || !node.isObject()) { return null; } Long id = longValue(node.get("id")); if (id == null) { return null; } LbBuyerShopping entity = new LbBuyerShopping(); entity.setId(id); entity.setSellerId(longValue(node.get("seller_id"))); entity.setMerchandiseId(longValue(node.get("merchandise_id"))); entity.setBuyerId(longValue(node.get("buyer_id"))); entity.setOrderSn(textValue(node.get("order_sn"))); entity.setTotalMoney(parseMoney(textValue(node.get("total_money")))); entity.setNewTotal(parseMoney(textValue(node.get("new_total")))); entity.setCreatedAt(parseDateTime(textValue(node.get("created_at")))); entity.setBuyTime(parseDateTime(textValue(node.get("buy_time")))); entity.setConfirmTime(parseDateTime(textValue(node.get("confirm_time")))); JsonNode seller = node.get("seller"); if (seller != null && seller.isObject()) { entity.setSellerNickname(textValue(seller.get("nickname"))); entity.setSellerMobile(textValue(seller.get("mobile"))); } else { entity.setSellerNickname(textValue(node.get("seller_name"))); entity.setSellerMobile(textValue(node.get("seller_phone"))); } JsonNode buyer = node.get("buyer"); if (buyer != null && buyer.isObject()) { entity.setBuyerNickname(textValue(buyer.get("nickname"))); entity.setBuyerMobile(textValue(buyer.get("mobile"))); } else { entity.setBuyerNickname(textValue(node.get("buyer_name"))); entity.setBuyerMobile(textValue(node.get("buyer_phone"))); } return entity; } private static JsonNode extractListNode(JsonNode dataNode) { if (dataNode == null || dataNode.isNull()) { return null; } if (dataNode.isArray()) { return dataNode; } if (dataNode.isObject()) { return dataNode.get("list"); } return null; } private static Long longValue(JsonNode node) { if (node == null || node.isNull()) { return null; } if (node.isNumber()) { return node.longValue(); } String text = node.asText(null); if (text == null || text.isBlank()) { return null; } try { return Long.parseLong(text.trim()); } catch (NumberFormatException e) { return null; } } private static String textValue(JsonNode node) { if (node == null || node.isNull()) { return null; } String text = node.asText(null); if (text == null) { return null; } text = text.trim(); return text.isEmpty() ? null : text; } private static BigDecimal parseMoney(String money) { if (money == null || money.isBlank()) { return null; } try { return new BigDecimal(money.trim()); } catch (NumberFormatException e) { return null; } } private static LocalDateTime parseDateTime(String text) { if (text == null || text.isBlank()) { return null; } String trimmed = text.trim(); try { return LocalDateTime.parse(trimmed, DATE_TIME_FORMATTER); } catch (DateTimeParseException ignored) { // continue } try { return LocalDateTime.parse(trimmed, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { return null; } } }