private JSONObject skuToJson( ProductSku productSku, Map<SkuOption, SkuOptionValue> skuOptionAndValues) { ConfigUtil configUtil = ConfigUtil.getInstance(); JSONObject jsonProductSku = new JSONObject(); jsonProductSku.put("productSkuId", productSku.getProductSkuId()); jsonProductSku.put("productSkuCode", productSku.getProductSkuCode()); jsonProductSku.put("image", productSku.getImage()); jsonProductSku.put( "weight", productSku.getWeight() == null ? null : productSku.getWeight().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); // 相应的价格等 jsonProductSku.put("priceViewType", productSku.getPriceViewType()); jsonProductSku.put( "price", productSku.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); jsonProductSku.put( "salePrice", productSku.getSalePrice() == null ? null : productSku.getSalePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); jsonProductSku.put( "discountPrice", productSku.getDiscountPrice() == null ? null : productSku.getDiscountPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); jsonProductSku.put( "listPrice", productSku.getListPrice() == null ? null : productSku.getListPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); jsonProductSku.put("discountPercent", productSku.getDiscountPercent()); // 获取批发价 Set<WholesalePrice> wholesalePrices = productSku.getWholesalePrices(); List<JSONArray> jsonWholesalePrices = new ArrayList<JSONArray>(); for (WholesalePrice wholesalePrice : wholesalePrices) { JSONArray jsonWholesalePrice = new JSONArray(); jsonWholesalePrice.add(wholesalePrice.getMinQuantity()); jsonWholesalePrice.add( wholesalePrice.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString()); jsonWholesalePrices.add(jsonWholesalePrice); } jsonProductSku.put("wholesalePrices", jsonWholesalePrices); // 获取产品媒体productMedia Set<ProductMedia> productMedias = productSku.getProductMedias(); JSONArray jsonProductMedias = new JSONArray(); for (ProductMedia media : productMedias) { jsonProductMedias.add(media.getMediaUrlLarge()); } jsonProductSku.put("productMedias", jsonProductMedias); // 获取库存信息 if (productSku.getProduct().getAvailabilityRule().intValue() != CatalogConstants.PRODUCT_AVAILABILITY_ALWAYS_SELL.intValue() && productSku.getProduct().getAvailabilityRule().intValue() != CatalogConstants.PRODUCT_AVAILABILITY_NOT_IN_STOCK_SELL) { Inventory inventory = productSku.getInventory(); // TODO 没有库存的全设置为0 if (inventory == null) { inventory = new Inventory(); inventory.setProductSku(productSku); inventory.setQuantityOnHand(0); inventory.setAllocatedQuantity(0); inventory.setReservedQuantity(0); inventory.setPreOrBackOrderedQty(0); } Map<String, Object> jsonInventory = new HashMap<String, Object>(); jsonInventory.put("productSkuId", inventory.getProductSkuId()); jsonInventory.put("quantityOnHand", inventory.getQuantityOnHand()); jsonInventory.put("allocatedQuantity", inventory.getAllocatedQuantity()); jsonInventory.put("availableQuantity", inventory.getAvailableQuantity()); jsonInventory.put( "expectedRestockDate", inventory.getExpectedRestockDate() == null ? "" : DateUtil.convertDateToString(inventory.getExpectedRestockDate())); jsonInventory.put("preOrBackOrderedQty", inventory.getPreOrBackOrderedQty()); jsonProductSku.put("inventory", jsonInventory); } if (skuOptionAndValues != null && skuOptionAndValues.size() > 0) { JSONArray jsonSkuOptions = new JSONArray(); Set<SkuOption> skuOptions = skuOptionAndValues.keySet(); for (SkuOption skuOption : skuOptions) { JSONObject jsonSkuOption = new JSONObject(); jsonSkuOption.put("id", skuOption.getId()); jsonSkuOption.put("vid", skuOptionAndValues.get(skuOption).getId()); jsonSkuOptions.add(jsonSkuOption); } jsonProductSku.put("skuOptions", jsonSkuOptions); } return jsonProductSku; }