推荐商品

20230922-ljl-fixBug
wangchaoxu 1 year ago
parent c4a5747cfa
commit b3261494d5

@ -4,6 +4,5 @@ import lombok.Data;
@Data
public class GetRecommendProductListRequestDTO {
private Integer pageNo;
private Integer pageSize;
}

@ -11,7 +11,10 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
@Component
@Slf4j
@ -62,4 +65,41 @@ public class DsCatService {
return arrayData;
}
/**
*
* @return
*/
public JSONArray getDSProductCatTreeLeafArray() {
JSONArray leafArray = new JSONArray();
JSONArray treeData = getDSProductCatTree();
Queue<JSONObject> queue = new ArrayDeque<>();
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 leaf = new JSONObject();
leaf.put("name", frontObj.getString("name"));
leaf.put("cid", frontObj.getString("cid"));
leafArray.add(leaf);
} else {
for (Object child : childsObj.values()) {
JSONObject jsonObject = (JSONObject) child;
queue.add(jsonObject);
}
}
}
return leafArray;
}
}

@ -17,6 +17,7 @@ import com.ms.api.service.CategoryService;
import com.ms.api.tool.DsJsonRequestTemplate;
import com.ms.dal.entity.Category;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
@ -40,60 +41,76 @@ public class GetRecommendProductListService extends SPIBaseService implements Ex
getAuthCode();
GetRecommendProductListRequestDTO requestDTO = req.getData();
// 1688类目树
JSONArray arrayData = dsCatService.getDSProductCatTree();
// 获取商家抖店类目列表
List<Category> categoryList = categoryService.getAllLeafProductCatListFromTos(shopId.intValue(), false);
Collections.shuffle(categoryList);
int productCnt = 0;
int categoryIndex = 0;
while (productCnt < requestDTO.getPageSize()) {
categoryIndex++;
JSONArray productArray = new JSONArray();
try {
// 1688类目树
JSONArray dsCateLeafArray = dsCatService.getDSProductCatTreeLeafArray();
// 获取商家抖店类目列表
List<Category> categoryList = categoryService.getAllLeafProductCatListFromTos(shopId.intValue(), false);
Collections.shuffle(categoryList);
if (ObjectUtil.isNotEmpty(dsCateLeafArray) && ObjectUtil.isNotEmpty(dsCateLeafArray)) {
final int eachCateProductChooseCount = 3;
int productCnt = 0;
int categoryIndex = 0;
while (productCnt < requestDTO.getPageSize() && categoryIndex < categoryList.size()) {
Category category = categoryList.get(categoryIndex);
String categoryName = category.getCategoryName();
double maxPercent = 0;
String maxPercentName = "";
String maxPercentCid = "";
for (int i = 0; i < dsCateLeafArray.size(); ++i) {
JSONObject dsCateLeaf = dsCateLeafArray.getJSONObject(i);
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;
}
// 匹配到的最终类目每个查询N个
HashMap<String, Object> params = new HashMap<>();
// params.put("keyword",fields.getKeyword());
// params.put("topCid",fields.getTopCid());
params.put("cid", maxPercentCid);
params.put("sort", "saleCntDesc");
// params.put("ruleIds", fields.getRuleIds());
params.put("isHideMoved", 1);
params.put("pageNo", 1);
params.put("pageSize", eachCateProductChooseCount);
params.put("authCode", authCode);
log.info(params.toString());
JSONObject resObj = null;
String res = null;
List<Object> 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();
}
} catch (Exception e) {
log.error("/move/search_choiceness_source_item request error", e);
}
categoryIndex++;
}
Collections.shuffle(productArray);
}
} catch (Exception e) {
log.error("getRecommendProductList error", e);
}
// HashMap<String, Object> params = new HashMap<>();
//// params.put("keyword",fields.getKeyword());
//// params.put("topCid",fields.getTopCid());
// params.put("cid",fields.getCid());
// params.put("sort", "saleCntDesc");
//// params.put("ruleIds", fields.getRuleIds());
// params.put("isHideMoved", 1);
// params.put("pageNo",requestDTO.getPageNo());
// params.put("pageSize",requestDTO.getPageSize());
// params.put("authCode",authCode);
// log.info(params.toString());
// JSONObject resObj = null;
// String res = null;
// List<Object> products = new LinkedList<>();
//
// try {
// res = dsJsonRequestTemplate.execute("/move/search_choiceness_source_item", params);
// resObj = JSON.parseObject(res);
// if (resObj.get("result").equals("success")) {
// log.info("resObj::"+resObj.toString());
// Map result = new HashMap();
// List<Object> sourceItems = new ArrayList<>();
// JSONArray sourceItemjson = resObj.getJSONArray("sourceItems");
// if (!ObjectUtil.isEmpty(sourceItemjson)) {
// sourceItems = sourceItemjson;
// }
// result.put("result", resObj.getString("result"));
// result.put("sourceItems", sourceItems);
// result.put("total", resObj.getInteger("total"));
// res = JSON.toJSONString(result);
// }
// } catch (Exception e) {
// e.printStackTrace();
// log.error(e.getMessage());
// }
return R.ok(Ret.success());
Map retMap = new HashMap();
retMap.put("products", productArray);
return R.ok(Ret.success(retMap));
}
}

Loading…
Cancel
Save