/
* 解析属性,得到属性格式 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> id * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jsonStr * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ResponseVO getAttrFormat(Integer id, String jsonStr) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (id == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> ResponseVO.error("请选择商品"<span style="color: rgba(0, 0, 0, 1)">); } Product product </span>=<span style="color: rgba(0, 0, 0, 1)"> productMapper.selectByPrimaryKey(id); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (product == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> ResponseVO.error("商品不存在"<span style="color: rgba(0, 0, 0, 1)">); } AttrDetailDto detailDTO </span>=<span style="color: rgba(0, 0, 0, 1)"> analysisAttr(jsonStr); List</span><ProductSkuDto> newList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map<String, Map<String,String>><span style="color: rgba(0, 0, 0, 1)"> map : detailDTO.getRes()) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 封装结果</span> ProductSkuDto productSkuDto = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ProductSkuDto(); productSkuDto.setDetail(map.get(</span>"detail"<span style="color: rgba(0, 0, 0, 1)">)); productSkuDto.setCost(product.getCost()); productSkuDto.setPrice(product.getPrice()); productSkuDto.setSales(product.getSales()); productSkuDto.setLogo(product.getLogo()); newList.add(productSkuDto); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ResponseVO.success(newList); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 解析属性规则算法 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jsonStr * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> AttrDetailDto analysisAttr(String jsonStr){ JSONObject jsonObject </span>=<span style="color: rgba(0, 0, 0, 1)"> JSON.parseObject(jsonStr); List</span><ProductAttrDto> productAttrList = JSONArray.parseArray(jsonObject.get("attrs"<span style="color: rgba(0, 0, 0, 1)">).toString(), ProductAttrDto.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 声明当前属性所拥有规格模板</span> List<String> currentAttrSpecTemp = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); List</span><Map<String,Map<String,String>>> res =<span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(productAttrList.size() > 1<span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果大于1,说明增加了多个属性</span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i<productAttrList.size()-1; i++){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历属性</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(i == 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果是第一个属性 获取第一个属性的元素规格列表,给模板temp0进行遍历操作,如果不是,说明已经拼接过一轮,继续拼接</span> currentAttrSpecTemp =<span style="color: rgba(0, 0, 0, 1)"> productAttrList.get(i).getDetail(); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空初始化模板集合,用于存储拼接后的规格信息,注意,只能在这个位置初始化,如果拿到上一个for循环之上,下面给将skuTemp实例的引用赋值之后(currentAttrSpecTemp = skuTemp),currentAttrSpecTemp就会因为skuTemp.add(skuDetail);而实时增加数据,导致出现ConcurrentModificationException异常</span> List<String> skuTemp = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历元素规格列表</span> <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String skuName : currentAttrSpecTemp) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前元素拼接下一个元素 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将下一个属性的 元素依次遍历</span> List<String> nextAttrSpecTemp = productAttrList.get(i + 1<span style="color: rgba(0, 0, 0, 1)">).getDetail(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String nextSpecName : nextAttrSpecTemp) { String skuDetail </span>= ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span>(i == 0<span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果为0,就使用第一属性的名称去拼接第一个规格,用下一个属性名称拼接下一个属性的当前规格 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 颜色_白色-体积_小</span> skuDetail = productAttrList.get(i).getAttrName() + "_" + skuName + "-" + productAttrList.get(i+1).getAttrName() + "_" +<span style="color: rgba(0, 0, 0, 1)"> nextSpecName; }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果不为0,表示不是第一个属性,那么就使用之前拼接得到的结果,继续拼接 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 颜色_白色-体积_小-温度_冰</span> skuDetail = skuName + "-" + productAttrList.get(i+1).getAttrName() + "_" +<span style="color: rgba(0, 0, 0, 1)"> nextSpecName; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将解析拼接后的规格存入</span>
skuTemp.add(skuDetail);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果是数组中的倒数第二个元素,因为会向后拼接一个元素(productAttrList.get(i+1)),所以此处表示已经拼接结束</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(i == productAttrList.size() - 2<span style="color: rgba(0, 0, 0, 1)">){ Map</span><String,Map<String,String>> skuDetailTemp = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashMap<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将一组结果,拆分成键值对的方式存储</span> Map<String,String> skuDetailTempKv = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashMap<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 根据-分成数组,得到[颜色_白色,体积_小,温度_冰]</span> List<String> attr_sku_arr = Arrays.asList(skuDetail.split("-"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String h : attr_sku_arr) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> _分成数组,得到[颜色,白色]</span> List<String> attrBySpecArr = Arrays.asList(h.split("_"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果大于1,说明属性有对应的 规格值</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(attrBySpecArr.size() > 1<span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 按照属性名:规格值的结构存入临时模板列表</span> skuDetailTempKv.put(attrBySpecArr.get(0), attrBySpecArr.get(1<span style="color: rgba(0, 0, 0, 1)">)); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 未获取到属性名,按空字符串存储</span> skuDetailTempKv.put(attrBySpecArr.get(0),""<span style="color: rgba(0, 0, 0, 1)">); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 得到[颜色:白色,体积:小,温度:冰]</span> skuDetailTemp.put("detail"<span style="color: rgba(0, 0, 0, 1)">,skuDetailTempKv); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将这一组结果存入到最终将要返回的数据中</span>
res.add(skuDetailTemp);
} } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 走到这里,表示第一个元素和第二个元素已经全部生成完毕,将得到的结果列表交给temp0去执行下一趟</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">skuTemp.isEmpty()){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这里只能将自己的参数给这个值,不能将引用也给他,否则在内循环的时候,就会照成java.util.ConcurrentModificationException</span> currentAttrSpecTemp =<span style="color: rgba(0, 0, 0, 1)"> skuTemp; } } }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 只有一种属性</span> List<String> temp0Arr = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空初始化模板集合,用于存储拼接后的规格信息</span> List<String> skuTemp = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedList<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (ProductAttrDto productAttr : productAttrList) { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (String str : productAttr.getDetail()) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这这种属性和其中的规格遍历得到属性规格键值对,不需要拼接下一个属性</span> Map<String,Map<String,String>> skuDetailTemp = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashMap<><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">List<Map<String,String>> list1 = new ArrayList<>();</span> temp0Arr.add(productAttr.getAttrName()+"_"+<span style="color: rgba(0, 0, 0, 1)">str); Map</span><String,String> skuDetailTempKv = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashMap<><span style="color: rgba(0, 0, 0, 1)">(); skuDetailTempKv.put(productAttr.getAttrName(),str); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">list1.add(map1);</span> skuDetailTemp.put("detail"<span style="color: rgba(0, 0, 0, 1)">,skuDetailTempKv); res.add(skuDetailTemp); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将解析拼接后的规格存入</span> skuTemp.add(productAttr.getAttrName() + "-" +<span style="color: rgba(0, 0, 0, 1)"> str); } currentAttrSpecTemp </span>=<span style="color: rgba(0, 0, 0, 1)"> skuTemp; } }</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 此时已得到每一组的匹配详情,(所有可能产生的匹配结果列表),将其已文本,和键值对的方式返回</span> AttrDetailDto detailDTO = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AttrDetailDto(); detailDTO.setData(currentAttrSpecTemp); detailDTO.setRes(res); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> detailDTO; } @Transactional(rollbackFor </span>= Exception.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ResponseVO productAttrAdd(Integer productId, String jsonStr) { JSONObject jsonObject </span>=<span style="color: rgba(0, 0, 0, 1)"> JSON.parseObject(jsonStr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 传入属性列表[{"attrName":"颜色","defaulValue":"","isHidden":1,"detail":["白色","黑色"]},{"attrName":"体积","defaulValue":"","isHidden":1,"detail":["小","中","大"]},{"attrName":"温度","defaulValue":"","isHidden":1,"detail":["冰","常温"]}]</span> List<ProductAttrDto> attrDtoList =<span style="color: rgba(0, 0, 0, 1)"> JSON.parseArray( jsonObject.get(</span>"attrs"<span style="color: rgba(0, 0, 0, 1)">).toString(), ProductAttrDto.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); List</span><ProductSkuDto> valueDtoList = JSON.parseArray(<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 传入解析后的规格详情列表[{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"小","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"小","温度":"常温"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"中","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"中","温度":"常温"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"大","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"白色","体积":"大","温度":"常温"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"小","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"小","温度":"常温"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"中","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"中","温度":"常温"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"大","温度":"冰"},"check":false},{"price":120,"cost":0.2,"sales":52,"logo":"</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">https://image.dayouqiantu.cn/5ca011a1cd487.jpg</span><span style="color: rgba(0, 128, 0, 1)">","detail":{"颜色":"黑色","体积":"大","温度":"常温"},"check":false}]</span> jsonObject.get("sku"<span style="color: rgba(0, 0, 0, 1)">).toString(), ProductSkuDto.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); Product product </span>=<span style="color: rgba(0, 0, 0, 1)"> productMapper.selectByPrimaryKey(productId); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取最小价格</span> Double minPrice =<span style="color: rgba(0, 0, 0, 1)"> product.getPrice(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算库存</span> Integer stock =<span style="color: rgba(0, 0, 0, 1)"> product.getStock(); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(attrDtoList.isEmpty() ||<span style="color: rgba(0, 0, 0, 1)"> valueDtoList.isEmpty()){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> ResponseVO.error("请设置至少一个属性!"<span style="color: rgba(0, 0, 0, 1)">); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">插入之前清空</span>
productAttrClear(product.getId());
} </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (ProductAttrDto attrDto : attrDtoList) { ProductAttr attr </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ProductAttr(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 记录商品属性</span>
attr.setProductId(productId);
attr.setAttrName(attrDto.getAttrName()); attr.setDefaulValue(attrDto.getDefaulValue()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据,号拼接商品属性下的规格 "大,中,小"</span> String attrValues = ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String valueName : attrDto.getDetail()){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果未指定默认值,那么就使用第一个</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (attr.getDefaulValue() == <span style="color: rgba(0, 0, 255, 1)">null</span> || attr.getDefaulValue().length() == 0<span style="color: rgba(0, 0, 0, 1)">){ attr.setDefaulValue(valueName); } attrValues </span>+= attrValues.length() > 0 ? "," +<span style="color: rgba(0, 0, 0, 1)"> valueName : valueName; } attr.setAttrValues(attrValues); productAttrMapper.insertSelective(attr); } </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (ProductSkuDto attrValuesDTO : valueDtoList) { ProductSku attrValues </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ProductSku();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 记录商品属性规格详情信息</span>
attrValues.setProductId(productId);
List</span><String> stringList =<span style="color: rgba(0, 0, 0, 1)"> attrValuesDTO.getDetail().values() .stream().collect(Collectors.toList()); Collections.sort(stringList); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将属性对应的每一种规格通过,号拼接,存入表中,同一产品唯一 "冰,小,白色"</span> String suk = ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String sukName : stringList){ suk </span>+= suk.length() > 0 ? "," +<span style="color: rgba(0, 0, 0, 1)"> sukName : sukName; } attrValues.setSuk(suk); attrValues.setPrice(attrValuesDTO.getPrice()); attrValues.setCost(attrValuesDTO.getCost()); attrValues.setStock(attrValuesDTO.getSales()); attrValues.setLogo(attrValuesDTO.getLogo()); productSkuMapper.insertSelective(attrValues); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算价格</span> minPrice = minPrice > attrValues.getPrice() ?<span style="color: rgba(0, 0, 0, 1)"> attrValues.getPrice() : minPrice; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 计算库存</span> stock +=<span style="color: rgba(0, 0, 0, 1)"> attrValues.getStock(); } Map</span><String,Object> map = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedHashMap<><span style="color: rgba(0, 0, 0, 1)">(); map.put(</span>"attr",jsonObject.get("attr"<span style="color: rgba(0, 0, 0, 1)">)); map.put(</span>"sku",jsonObject.get("sku"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置库存及价格</span>
product.setPrice(minPrice);
product.setStock(stock); product.setAttrResult(JSON.toJSONString(map)); </span><span style="color: rgba(0, 0, 255, 1)">int</span> result =<span style="color: rgba(0, 0, 0, 1)"> productMapper.updateByPrimaryKeySelective(product); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result != 1<span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">return</span> ResponseVO.error("请设置至少一个属性!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ResponseVO.success(); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 清空属性及sku * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> productId * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> productAttrClear(Integer productId) { productAttrMapper.deleteByProductId(productId); productSkuMapper.deleteByProductId(productId); }</span></pre>
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/14574.html