From f2e37334553cb5e4e55595e6b0670caec945ba36 Mon Sep 17 00:00:00 2001 From: wangchaoxu Date: Fri, 15 Sep 2023 12:14:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E8=8D=90=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ms/api/biz/DsCatService.java | 52 +++++++++++++++++++ .../move/GetRecommendProductListService.java | 44 ++++++++++------ 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/ms-biz/src/main/java/com/ms/api/biz/DsCatService.java b/ms-biz/src/main/java/com/ms/api/biz/DsCatService.java index f30aa9fc..72d4d467 100644 --- a/ms-biz/src/main/java/com/ms/api/biz/DsCatService.java +++ b/ms-biz/src/main/java/com/ms/api/biz/DsCatService.java @@ -74,6 +74,7 @@ public class DsCatService { JSONArray treeData = getDSProductCatTree(); Queue queue = new ArrayDeque<>(); +// treeData = treeData.getJSONArray(0); // 结构兼容 for (int i = 0; i < treeData.size(); i++) { JSONObject rootObj = treeData.getJSONObject(i); queue.add(rootObj); @@ -101,5 +102,56 @@ public class DsCatService { return leafArray; } + /** + * 查询非叶子节点数组 + * @return + */ + public JSONArray getDSProductCatTreeNotLeafArray() { + JSONArray nodeArray = new JSONArray(); + JSONArray treeData = getDSProductCatTree(); + Queue queue = new ArrayDeque<>(); + +// treeData = treeData.getJSONArray(0); // 结构兼容 + for (int i = 0; i < treeData.size(); i++) { + JSONObject rootObj = treeData.getJSONObject(i); + queue.add(rootObj); + } + while (!queue.isEmpty()) { + JSONObject frontObj = queue.poll(); + if (frontObj == null) { + break; + } + JSONObject childsObj = frontObj.getJSONObject("childs"); + if (childsObj != null) { + JSONObject node = new JSONObject(); + node.put("name", frontObj.getString("name")); + node.put("cid", frontObj.getString("cid")); + nodeArray.add(node); + for (Object child : childsObj.values()) { + JSONObject jsonObject = (JSONObject) child; + queue.add(jsonObject); + } + } + } + + return nodeArray; + } + + /** + * 查询非叶子节点数组 + * @return + */ + public JSONArray getDSProductTopCatArray() { + JSONArray nodeArray = new JSONArray(); + JSONArray treeData = getDSProductCatTree(); + +// treeData = treeData.getJSONArray(0); // 结构兼容 + for (int i = 0; i < treeData.size(); i++) { + JSONObject rootObj = treeData.getJSONObject(i); + nodeArray.add(rootObj); + } + + return nodeArray; + } } diff --git a/ms-biz/src/main/java/com/ms/api/spi/move/GetRecommendProductListService.java b/ms-biz/src/main/java/com/ms/api/spi/move/GetRecommendProductListService.java index 41caab58..05aa786e 100644 --- a/ms-biz/src/main/java/com/ms/api/spi/move/GetRecommendProductListService.java +++ b/ms-biz/src/main/java/com/ms/api/spi/move/GetRecommendProductListService.java @@ -41,11 +41,16 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex getAuthCode(); GetRecommendProductListRequestDTO requestDTO = req.getData(); + Integer pageSize = requestDTO.getPageSize(); + if (pageSize == null) { + pageSize = 0; + } + JSONArray productArray = new JSONArray(); try { - // 1688类目树 - JSONArray dsCateLeafArray = dsCatService.getDSProductCatTreeLeafArray(); + // 1688类目列表 + JSONArray dsCateLeafArray = dsCatService.getDSProductTopCatArray(); // 获取商家抖店类目列表 List categoryList = categoryService.getAllLeafProductCatListFromTos(shopId.intValue(), false); @@ -53,12 +58,12 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex if (ObjectUtil.isNotEmpty(dsCateLeafArray) && ObjectUtil.isNotEmpty(dsCateLeafArray)) { final int eachCateProductChooseCount = 3; - int productCnt = 0; int categoryIndex = 0; - while (productCnt < requestDTO.getPageSize() && categoryIndex < categoryList.size()) { + HashSet sourceItemIdSet = new HashSet<>(); + while (productArray.size() < pageSize && categoryIndex < categoryList.size()) { Category category = categoryList.get(categoryIndex); String categoryName = category.getCategoryName(); - +// categoryName="女装"; double maxPercent = 0; String maxPercentName = ""; String maxPercentCid = ""; @@ -67,16 +72,19 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex String dsCateName = dsCateLeaf.getString("name"); String dsCateCid = dsCateLeaf.getString("cid"); double percent = StringUtils.getJaroWinklerDistance(categoryName, dsCateName); - maxPercent = Math.max(maxPercent, percent); - maxPercentName = dsCateName; - maxPercentCid = dsCateCid; + if (percent > maxPercent) { + maxPercent = percent; + maxPercentName = dsCateName; + maxPercentCid = dsCateCid; + } + log.info(dsCateName + " " + percent); } // 匹配到的最终类目,每个查询N个 HashMap params = new HashMap<>(); // params.put("keyword",fields.getKeyword()); -// params.put("topCid",fields.getTopCid()); - params.put("cid", maxPercentCid); + params.put("topCid", maxPercentCid); +// params.put("cid", maxPercentCid); params.put("sort", "saleCntDesc"); // params.put("ruleIds", fields.getRuleIds()); params.put("isHideMoved", 1); @@ -86,15 +94,21 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex log.info(params.toString()); JSONObject resObj = null; String res = null; - List products = new LinkedList<>(); - try { res = dsJsonRequestTemplate.execute("/move/search_choiceness_source_item", params); resObj = JSON.parseObject(res); if (resObj.get("result").equals("success")) { JSONArray sourceItems = resObj.getJSONArray("sourceItems"); - productArray.addAll(sourceItems); - productCnt += sourceItems.size(); + if (ObjectUtil.isNotEmpty(sourceItems)) { + for (int i = 0; i < sourceItems.size(); ++i) { + JSONObject sourceItem = sourceItems.getJSONObject(i); + String sourceItemId = sourceItem.getString("sourceItemId"); + if (!sourceItemIdSet.contains(sourceItemId)) { + productArray.add(sourceItem); + sourceItemIdSet.add(sourceItemId); + } + } + } } } catch (Exception e) { log.error("/move/search_choiceness_source_item request error", e); @@ -110,7 +124,7 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex } Map retMap = new HashMap(); - retMap.put("products", productArray); + retMap.put("products", productArray.subList(0, Math.min(pageSize, productArray.size()))); return R.ok(Ret.success(retMap)); } }