Authored by hugufei

活动店铺推荐接口优化

@@ -54,7 +54,15 @@ public class ActivityService { @@ -54,7 +54,15 @@ public class ActivityService {
54 } 54 }
55 55
56 private List<Integer> buildShopIds(List<ActivityShopBrand> shopBrandList, UserPersonalFactorRspNew userPersonalFactorRspNew) { 56 private List<Integer> buildShopIds(List<ActivityShopBrand> shopBrandList, UserPersonalFactorRspNew userPersonalFactorRspNew) {
57 - //1、计算店铺得分 57 + //1、获取品牌-店铺的对应关系
  58 + Map<Integer, Integer> brandId2ShopIdMap = new HashMap<>();
  59 + for (ActivityShopBrand activityShopBrand : shopBrandList) {
  60 + brandId2ShopIdMap.put(activityShopBrand.getBrandId(), activityShopBrand.getShopId());
  61 + }
  62 + //2、获取实时推荐的品牌
  63 + List<Integer> recBrandIds = this.getRecBrandIds(userPersonalFactorRspNew);
  64 +
  65 + //3、计算店铺得分并排序
58 List<Double> brandVector = userPersonalFactorRspNew.getBrandVector(); 66 List<Double> brandVector = userPersonalFactorRspNew.getBrandVector();
59 List<Double> brandVectorW2v = userPersonalFactorRspNew.getBrandVectorW2v(); 67 List<Double> brandVectorW2v = userPersonalFactorRspNew.getBrandVectorW2v();
60 if (brandVectorW2v != null && !brandVectorW2v.isEmpty()) { 68 if (brandVectorW2v != null && !brandVectorW2v.isEmpty()) {
@@ -62,23 +70,28 @@ public class ActivityService { @@ -62,23 +70,28 @@ public class ActivityService {
62 } else { 70 } else {
63 this.calScore(shopBrandList, brandVectorW2v, true); 71 this.calScore(shopBrandList, brandVectorW2v, true);
64 } 72 }
65 - //2、实时推荐店铺加分  
66 - List<Integer> recBrandIds = this.getRecBrandIds(userPersonalFactorRspNew);  
67 - for (ActivityShopBrand shopBrand : shopBrandList) {  
68 - if (recBrandIds.contains(shopBrand.getBrandId())) {  
69 - shopBrand.setScore(shopBrand.getScore()+100);  
70 - }  
71 - }  
72 - //3、按得分排序  
73 Collections.sort(shopBrandList, (o1, o2) -> (o2.getScore()).compareTo(o1.getScore())); 73 Collections.sort(shopBrandList, (o1, o2) -> (o2.getScore()).compareTo(o1.getScore()));
  74 +
74 //4、截取shopId 75 //4、截取shopId
75 List<Integer> shopIds = new ArrayList<>(); 76 List<Integer> shopIds = new ArrayList<>();
  77 +
  78 + //4.1、先获取推荐品牌对应的店铺
  79 + for (Integer brandId : recBrandIds) {
  80 + Integer shopId = brandId2ShopIdMap.get(brandId);
  81 + if (shopId == null || shopIds.contains(shopId)) {
  82 + continue;
  83 + }
  84 + shopIds.add(shopId);
  85 + }
  86 + //4.2、再按向量得分排序补满50个
76 for (ActivityShopBrand shopBrand : shopBrandList) { 87 for (ActivityShopBrand shopBrand : shopBrandList) {
77 if (shopIds.size() >= 50) { 88 if (shopIds.size() >= 50) {
78 break; 89 break;
79 } 90 }
  91 + if (!shopIds.contains(shopBrand.getShopId())) {
80 shopIds.add(shopBrand.getShopId()); 92 shopIds.add(shopBrand.getShopId());
81 } 93 }
  94 + }
82 return shopIds; 95 return shopIds;
83 } 96 }
84 97
@@ -101,7 +114,7 @@ public class ActivityService { @@ -101,7 +114,7 @@ public class ActivityService {
101 * 按向量计算品牌得分 114 * 按向量计算品牌得分
102 */ 115 */
103 private void calScore(List<ActivityShopBrand> shopBrandList, List<Double> userBrandVector, boolean isRnn) { 116 private void calScore(List<ActivityShopBrand> shopBrandList, List<Double> userBrandVector, boolean isRnn) {
104 - if(userBrandVector==null || userBrandVector.isEmpty()){ 117 + if (userBrandVector == null || userBrandVector.isEmpty()) {
105 return; 118 return;
106 } 119 }
107 double userBrandVectorNorm = Word2VectorCalculator.getVectorListNorm(userBrandVector); 120 double userBrandVectorNorm = Word2VectorCalculator.getVectorListNorm(userBrandVector);