商品多规格json数据迭代方法记录

    /**
     * <一句话功能简述> 递归遍历所有规格的规格值,并生成微盟所需的saleStandard和skuList
     * <功能详细描述>
     * author: zhanggw
     * 创建时间:  2021/12/15
     * @param allStandardPriceArray json格式的所有规格
     * [{
     * 	"children": [{
     * 		"children": [{
     * 			"imgUrl": "",
     * 			"isShow": true,
     * 			"name": "中级",
     * 			"price": 180,
     * 			"specifications": "等级"
     *                }],
     * 		"imgUrl": "",
     * 		"isShow": true,
     * 		"name": "S",
     * 		"specifications": "尺码"    * 	}],
     * 	"imgUrl": "",
     * 	"isShow": true,
     * 	"name": "01白色",
     * 	"specifications": "颜色"
     * }]
     * @param weiMengStandardJson 微盟规格和规格值映射{"treeStandard":{"颜色":{"attributeId":"53000","attr":{"01白色":"148597579149"}}}}
     * @param retMap 用于迭代保存数据和传入特定数据
     */
    private void processWmStandardRecursion(JSONArray allStandardPriceArray, JSONObject weiMengStandardJson, Map retMap) {
        try{
            Integer layerNum = retMap.get("layerNum") == null ? 1 : Integer.parseInt(retMap.get("layerNum").toString()) + 1;
            retMap.put("layerNum", layerNum);

            // 保存递归层数路径,方便最后一层按顺序取到前面规格数据
            List routeList = retMap.get("route") == null ? new ArrayList<>() : (ArrayList)retMap.get("route");
            routeList.add(layerNum);
            retMap.put("route", routeList);

            // 遍历当前规格的所有规格值,注意当存在多种规格时,会通过链表的方式关联下一种规格,children为下一种规格的所有信息
            for(int i=0; i < allStandardPriceArray.size(); i++){
                JSONObject childStandardJson = allStandardPriceArray.getJSONObject(i);
                String mainStandardName = childStandardJson.getString("specifications"); // 规格名称 颜色
                String mainStandardValue = childStandardJson.getString("name"); // 规格值 01白色
                JSONObject mainStandard = weiMengStandardJson.getJSONObject("treeStandard").getJSONObject(mainStandardName); // 规格对象 {"attributeId":"53000","attr":{"01白色":"148597579149"}}
                String attributeId = mainStandard.getString("attributeId"); // 规格名称(颜色)对应微盟id
                String mainStandardValueId = mainStandard.getJSONObject("attr").getString(mainStandardValue); // 规格值(01白色)对应微盟id
                logger.debug("开始递归循环规格,递归层数:{},for次序:{},规格名称:{},规格值:{}", layerNum, i, mainStandardName, mainStandardValue);

                // 业务:设置迭代每层规格对象 "53000":{"name":"颜色","value":"01白色","key":148597579149}
                Map subSkuAttrMap = new HashMap<>();
                JSONObject skuAttr = new JSONObject();
                skuAttr.put("name", mainStandardName);
                skuAttr.put("value", mainStandardValue);
                skuAttr.put("key", mainStandardValueId);
                subSkuAttrMap.put(attributeId, skuAttr);
                retMap.put("layerSku_"+layerNum, subSkuAttrMap);

                // 业务:添加销售规格
                Object saleStandardObj = retMap.get("saleStandard");
                JSONObject saleStandard;
                if(saleStandardObj == null){
                    saleStandard = new JSONObject();
                }else{
                    saleStandard = (JSONObject)saleStandardObj;
                }
                JSONObject selectedSaleAttr = saleStandard.getJSONObject(mainStandardName);
                if(selectedSaleAttr == null){
                    selectedSaleAttr = new JSONObject();
                    selectedSaleAttr.put("attributeId", attributeId);
                }

                // 业务:销售规格中添加规格值
                JSONArray attrValueArray = selectedSaleAttr.getJSONArray("attrValueArray"); // 与attrValueList不同
                if(attrValueArray == null){
                    attrValueArray = new JSONArray();
                }
                if(!attrValueArray.contains(mainStandardValueId)){
                    attrValueArray.add(mainStandardValueId);
                }
                selectedSaleAttr.put("attrValueArray", attrValueArray);
                saleStandard.put(mainStandardName, selectedSaleAttr);
                retMap.put("saleStandard", saleStandard);
                logger.debug("retMap:{}", retMap);

                JSONArray childrenArray = childStandardJson.getJSONArray("children");
                if(childrenArray != null){ // 还有其它规格未遍历,递归遍历其它规格
                    processWmStandardRecursion(childrenArray, weiMengStandardJson, retMap);
                }else{ // 没有其它规格,已关联上所有规格
                    BigDecimal price = childStandardJson.getBigDecimal("price"); // 规格价格,只有最底层有
                    if(price != null){ // 具有价格才有意义
                        boolean isShow = childStandardJson.getBooleanValue("isShow"); // 是否展示,true:显示,默认库存量50 false:不显示,库存量0

                        // 开始生成skuMap
                        JSONObject skuAdditional = weiMengStandardJson.getJSONObject("skuAdditional");
                        Map skuMap = new HashMap<>();
                        skuMap.put("b2cSku", skuAdditional.get("b2cSku"));
                        skuMap.put("salePrice", skuAdditional.getBigDecimal("salePrice"));
                        skuMap.put("costPrice", skuAdditional.getBigDecimal("costPrice"));
                        skuMap.put("outerSkuCode", skuAdditional.get("productCode"));
                        if(isShow){
                            skuMap.put("editStockNum", 50);
                        }else{
                            skuMap.put("editStockNum", 0);
                        }
                        Map skuAttrMap = new HashMap<>();
                        for(Integer routeNum:routeList){
                            skuAttrMap.putAll((Map)retMap.get("layerSku_"+routeNum));
                        }
                        skuMap.put("skuAttrMap", skuAttrMap);

                        // 将skuMap保存到skuList
                        List> skuList = retMap.get("skuList") == null ? new ArrayList<>() : (List>)retMap.get("skuList");
                        skuList.add(skuMap);
                        retMap.put("skuList", skuList);
                    }
                }
            }

            // 返回上一层或结束
            retMap.put("layerNum", layerNum-1); // 层级上升
            // 更新递归层数路径,去掉当前这一层(也就是最后)
            routeList.remove(routeList.size()-1);
            retMap.put("route", routeList);
        }catch (Exception e){
            logger.error("递归生成微盟商品规格信息异常!", e);
        }
    }

运行结果:

商品多规格json数据迭代方法记录_第1张图片

 

你可能感兴趣的:(java,多规格,递归法)