From 46133d7d164a2a916e98eeeca6bcca6992bccd95 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Mon, 19 Aug 2024 14:35:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=9F=A5=E8=AF=A2=E5=95=86=E5=93=81=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E8=A7=84=E6=A0=BC=E4=B8=8A=E4=B8=8B=E6=9E=B6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductController.java | 10 ++ .../entity/TbProductWithBLOBs.java | 32 +--- .../entity/dto/QuerySpecDTO.java | 12 ++ .../service/ProductService.java | 141 ++++++++++-------- .../resources/mapper/TbShopPayTypeMapper.xml | 22 +-- 5 files changed, 117 insertions(+), 100 deletions(-) create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/QuerySpecDTO.java diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java index a2590cd..098406f 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java @@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.controller; import com.chaozhanggui.system.cashierservice.entity.dto.ProductStatusDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ProductStockDTO; +import com.chaozhanggui.system.cashierservice.entity.dto.QuerySpecDTO; import com.chaozhanggui.system.cashierservice.service.ProductService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; @@ -58,6 +59,15 @@ public class ProductController { return productService.queryCommodityInfo(shopId,categoryId,commdityName,page,pageSize,masterId); } + @GetMapping("querySpec") + public Result querySpec( + @RequestParam Integer shopId, + @RequestParam Integer productId + ) { + return Result.success(CodeEnum.SUCCESS, productService.querySpec(shopId, productId)); + } + + @GetMapping(value = "queryNewCommodityInfo") public Result queryNewCommodityInfo( @RequestHeader("token") String token, diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbProductWithBLOBs.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbProductWithBLOBs.java index 4933c00..3302de2 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbProductWithBLOBs.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbProductWithBLOBs.java @@ -1,9 +1,13 @@ package com.chaozhanggui.system.cashierservice.entity; +import lombok.Getter; + import java.io.Serializable; import java.util.List; +@Getter public class TbProductWithBLOBs extends TbProduct implements Serializable { + private Object groundingSpecInfo; private String images; private String video; @@ -20,58 +24,34 @@ public class TbProductWithBLOBs extends TbProduct implements Serializable { private static final long serialVersionUID = 1L; - public List getSkuList() { - return skuList; + public void setGroundingSpecInfo(Object groundingSpecInfo) { + this.groundingSpecInfo = groundingSpecInfo; } public void setSkuList(List skuList) { this.skuList = skuList; } - public String getImages() { - return images; - } - public void setImages(String images) { this.images = images == null ? null : images.trim(); } - public String getVideo() { - return video; - } - public void setVideo(String video) { this.video = video == null ? null : video.trim(); } - public String getNotice() { - return notice; - } - public void setNotice(String notice) { this.notice = notice == null ? null : notice.trim(); } - public String getGroupSnap() { - return groupSnap; - } - public void setGroupSnap(String groupSnap) { this.groupSnap = groupSnap == null ? null : groupSnap.trim(); } - public String getSpecInfo() { - return specInfo; - } - public void setSpecInfo(String specInfo) { this.specInfo = specInfo == null ? null : specInfo.trim(); } - public String getSelectSpec() { - return selectSpec; - } - public void setSelectSpec(String selectSpec) { this.selectSpec = selectSpec == null ? null : selectSpec.trim(); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/QuerySpecDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/QuerySpecDTO.java new file mode 100644 index 0000000..d5a779b --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/QuerySpecDTO.java @@ -0,0 +1,12 @@ +package com.chaozhanggui.system.cashierservice.entity.dto; + +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class QuerySpecDTO { + @NotNull + private Integer shopId; + private Integer productId; +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 584a49d..f3c3719 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -9,6 +9,7 @@ import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.entity.*; import com.chaozhanggui.system.cashierservice.entity.dto.ProductStatusDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ProductStockDTO; +import com.chaozhanggui.system.cashierservice.entity.dto.QuerySpecDTO; import com.chaozhanggui.system.cashierservice.entity.vo.ShopCategoryVo; import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.interceptor.LimitSubmitAspect; @@ -134,69 +135,7 @@ public class ProductService { it.setTbProductSpec(tbProductSpec); TbProductSkuResult skuResult=tbProductSkuResultMapper.selectByPrimaryKey(it.getId()); - - - // 上下架对应的sku - String selectSpec = it.getSelectSpec(); - - List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(it.getId()); - HashSet specSet = new HashSet<>(); - tbProductSkus.forEach(item -> { - String specSnap = item.getSpecSnap(); - if (specSnap != null) { - specSet.addAll(Arrays.asList(specSnap.split(","))); - } - }); - String tagSnap = skuResult != null ? skuResult.getTagSnap() : null; - if (tagSnap != null) { - JSONArray tagSnaps = JSONObject.parseArray(tagSnap); - JSONObject snapJSON; - - JSONArray finalSnap = new JSONArray(); - - HashMap snapMap = new HashMap<>(); - for (Object snap : tagSnaps) { - StringBuilder finalValues = new StringBuilder(); - snapJSON = (JSONObject) snap; - String values = snapJSON.getString("value"); - if (StrUtil.isNotBlank(values)) { - String[] valueList = values.split(","); - for (String value : valueList) { - if (specSet.contains(value)) { - finalValues.append(value).append(","); - } - } - - if (StrUtil.isNotBlank(finalValues.toString())) { - finalValues = new StringBuilder(StrUtil.removeSuffix(finalValues.toString(), ",")); - snapJSON.put("value", finalValues.toString()); - finalSnap.add(snapJSON); - snapMap.put(snapJSON.getString("name"), snapJSON.getString("value")); - } - - } - } - - if (selectSpec != null) { - JSONArray selectSpecJSON = JSONObject.parseArray(selectSpec); - for (Object selectSpecInfo : selectSpecJSON) { - JSONObject specInfo = (JSONObject) selectSpecInfo; - String name = snapMap.get(specInfo.getString("name")); - if (name != null) { - specInfo.put("value", name.split(",")); - specInfo.put("selectSpecResult", name.split(",")); - } - } - - it.setSelectSpec(selectSpecJSON.toJSONString()); - } - - skuResult.setTagSnap(finalSnap.toJSONString()); - } it.setProductSkuResult(skuResult); - - - // 查询sku信息 List skuWithBLOBs = tbProductSkuMapper.selectByProductCheckGrounding(it.getId()); List skus = tbProductSkuMapper.selectByProductId(it.getId()); @@ -209,7 +148,7 @@ public class ProductService { } it.setSkuList(skuWithBLOBs); - + it.setGroundingSpecInfo(querySpec(Integer.valueOf(shopId), it.getId())); }); } PageInfo pageInfo=new PageInfo(tbProductWithBLOBs); @@ -280,4 +219,80 @@ public class ProductService { producer.sendStockRecordMsg(data); } } + + private static void generateCombinations(List> valuesList, int index, List current, List result) { + if (index == valuesList.size()) { + result.add(String.join(",", current)); + return; + } + + for (String value : valuesList.get(index)) { + current.add(value); + generateCombinations(valuesList, index + 1, current, result); + current.remove(current.size() - 1); // 回溯 + } + } + + public Object querySpec(Integer shopId, Integer productId) { + TbProduct tbProduct = tbProductMapper.selectByPrimaryKey(productId); + if (tbProduct == null) { + throw new MsgException("商品不存在"); + } + TbProductSkuResult skuResult = tbProductSkuResultMapper.selectByPrimaryKey(productId); + + // 重组有效规格数据 + String tagSnap = skuResult != null ? skuResult.getTagSnap() : null; + List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(productId); + + JSONArray finalSnap = new JSONArray(); + + List result = new ArrayList<>(); + if (tagSnap != null) { + JSONArray tagSnaps = JSONObject.parseArray(tagSnap); + List> valuesList = new ArrayList<>(); + + // 提取所有 value 的列表 + for (int i = 0; i < tagSnaps.size(); i++) { + JSONObject jsonObject = tagSnaps.getJSONObject(i); + if (jsonObject.containsKey("value")) { + String[] values = jsonObject.getString("value").split(","); + valuesList.add(Arrays.asList(values)); + } + } + // 生成所有可能的排列组合 + generateCombinations(valuesList, 0, new ArrayList<>(), result); + + } + + ArrayList> specList = new ArrayList<>(); + tbProductSkus.forEach(item -> { + HashMap itemMap = new HashMap<>(); + itemMap.put("specSnap", item.getSpecSnap()); + itemMap.put("skuId", item.getId()); + specList.add(itemMap); + }); + + + ArrayList> otherVal = new ArrayList<>(); + for (String res : result) { + boolean found = false; + for (HashMap spec : specList) { + if (res.equals(spec.get("specSnap").toString())) { + spec.put("isGrounding", true); + found = true; + break; + } + } + if (!found) { + HashMap itemMap = new HashMap<>(); + itemMap.put("specSnap", res); + itemMap.put("skuId", null); + itemMap.put("isGrounding", false); + otherVal.add(itemMap); + } + } + specList.addAll(otherVal); + return specList; + } + } diff --git a/src/main/resources/mapper/TbShopPayTypeMapper.xml b/src/main/resources/mapper/TbShopPayTypeMapper.xml index 843d921..8d402ce 100644 --- a/src/main/resources/mapper/TbShopPayTypeMapper.xml +++ b/src/main/resources/mapper/TbShopPayTypeMapper.xml @@ -18,11 +18,11 @@ - id, pay_type, pay_name, is_show_shortcut, shop_id, is_refundable, is_open_cash_drawer, + id, pay_type, pay_name, is_show_shortcut, shop_id, is_refundable, is_open_cash_drawer, is_system, is_ideal, is_display, sorts, created_at, updated_at, icon - \ No newline at end of file +