1
|
package com.yoho.search.consumer.service.logic;
|
1
|
package com.yoho.search.consumer.service.logic;
|
2
|
|
2
|
|
3
|
-import java.util.ArrayList;
|
|
|
4
|
-import java.util.HashMap;
|
|
|
5
|
-import java.util.LinkedHashMap;
|
|
|
6
|
-import java.util.List;
|
|
|
7
|
-import java.util.Map;
|
|
|
8
|
-import java.util.stream.Collectors;
|
|
|
9
|
-
|
3
|
+import com.yoho.search.base.utils.FileUtils;
|
|
|
4
|
+import com.yoho.search.consumer.service.base.BrandService;
|
|
|
5
|
+import com.yoho.search.consumer.service.base.ProductSortService;
|
|
|
6
|
+import com.yoho.search.dal.ForbiddenSortBrandMapper;
|
|
|
7
|
+import com.yoho.search.dal.model.Brand;
|
|
|
8
|
+import com.yoho.search.dal.model.ForbiddenSortBrand;
|
|
|
9
|
+import com.yoho.search.dal.model.ProductSort;
|
10
|
import org.apache.commons.collections.CollectionUtils;
|
10
|
import org.apache.commons.collections.CollectionUtils;
|
11
|
import org.apache.commons.lang3.StringUtils;
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.slf4j.Logger;
|
12
|
import org.slf4j.Logger;
|
|
@@ -14,11 +14,8 @@ import org.slf4j.LoggerFactory; |
|
@@ -14,11 +14,8 @@ import org.slf4j.LoggerFactory; |
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.stereotype.Service;
|
15
|
import org.springframework.stereotype.Service;
|
16
|
|
16
|
|
17
|
-import com.yoho.search.base.utils.FileUtils;
|
|
|
18
|
-import com.yoho.search.consumer.service.base.BrandService;
|
|
|
19
|
-import com.yoho.search.consumer.service.base.ProductSortService;
|
|
|
20
|
-import com.yoho.search.dal.model.Brand;
|
|
|
21
|
-import com.yoho.search.dal.model.ProductSort;
|
17
|
+import java.util.*;
|
|
|
18
|
+import java.util.stream.Collectors;
|
22
|
|
19
|
|
23
|
@Service
|
20
|
@Service
|
24
|
public class ForbidenSortBrandLogicService {
|
21
|
public class ForbidenSortBrandLogicService {
|
|
@@ -32,6 +29,8 @@ public class ForbidenSortBrandLogicService { |
|
@@ -32,6 +29,8 @@ public class ForbidenSortBrandLogicService { |
32
|
private ProductSortService productSortService;
|
29
|
private ProductSortService productSortService;
|
33
|
@Autowired
|
30
|
@Autowired
|
34
|
private BrandService brandService;
|
31
|
private BrandService brandService;
|
|
|
32
|
+ @Autowired
|
|
|
33
|
+ private ForbiddenSortBrandMapper forbiddenSortBrandMapper;
|
35
|
|
34
|
|
36
|
private final String forbiddenSortBrandFileName = "inner/forbidden_brands.txt";
|
35
|
private final String forbiddenSortBrandFileName = "inner/forbidden_brands.txt";
|
37
|
|
36
|
|
|
@@ -46,6 +45,31 @@ public class ForbidenSortBrandLogicService { |
|
@@ -46,6 +45,31 @@ public class ForbidenSortBrandLogicService { |
46
|
return;
|
45
|
return;
|
47
|
}
|
46
|
}
|
48
|
forbiddenSortBrandMap = genForbiddenSortBrandMap();
|
47
|
forbiddenSortBrandMap = genForbiddenSortBrandMap();
|
|
|
48
|
+ //insert to DB
|
|
|
49
|
+ List<ForbiddenSortBrand> forbiddenSortBrands = new ArrayList<>();
|
|
|
50
|
+ forbiddenSortBrandMap.forEach((maxSortId, map) -> {
|
|
|
51
|
+ map.forEach((middleSortId, list) -> {
|
|
|
52
|
+ for (Integer brandId : list) {
|
|
|
53
|
+ ForbiddenSortBrand forbiddenSortBrand = new ForbiddenSortBrand();
|
|
|
54
|
+ forbiddenSortBrand.setMaxSortId(maxSortId);
|
|
|
55
|
+ forbiddenSortBrand.setMiddleSortId(middleSortId);
|
|
|
56
|
+ forbiddenSortBrand.setBrandId(brandId);
|
|
|
57
|
+ long time = System.currentTimeMillis() / 1000L;
|
|
|
58
|
+ forbiddenSortBrand.setCreateTime((int) time);
|
|
|
59
|
+ forbiddenSortBrands.add(forbiddenSortBrand);
|
|
|
60
|
+ }
|
|
|
61
|
+ });
|
|
|
62
|
+ });
|
|
|
63
|
+ forbiddenSortBrandMapper.deleteAll();
|
|
|
64
|
+ long begin = System.currentTimeMillis();
|
|
|
65
|
+ logger.info("deleteAll forbiddenSortBrand success [cost={}]", (System.currentTimeMillis() - begin));
|
|
|
66
|
+ List<List<ForbiddenSortBrand>> result = createList(forbiddenSortBrands, 1000);
|
|
|
67
|
+ logger.info("batch insert forbiddenSortBrand begin");
|
|
|
68
|
+ for (int i = 0; i < result.size(); i++) {
|
|
|
69
|
+ forbiddenSortBrandMapper.insertBatch(result.get(i));
|
|
|
70
|
+ logger.info("batch insert forbiddenSortBrand [page={}]of[{}],[cost={}]", i, result.size(),(System.currentTimeMillis() - begin));
|
|
|
71
|
+ }
|
|
|
72
|
+ logger.info("batch insert forbiddenSortBrand success,[cost={}]",(System.currentTimeMillis() - begin));
|
49
|
}
|
73
|
}
|
50
|
}
|
74
|
}
|
51
|
|
75
|
|
|
@@ -78,16 +102,16 @@ public class ForbidenSortBrandLogicService { |
|
@@ -78,16 +102,16 @@ public class ForbidenSortBrandLogicService { |
78
|
List<ProductSort> productSortList = productSortService.getPageLists(0, Integer.MAX_VALUE);
|
102
|
List<ProductSort> productSortList = productSortService.getPageLists(0, Integer.MAX_VALUE);
|
79
|
|
103
|
|
80
|
//3、获取大分类Map
|
104
|
//3、获取大分类Map
|
81
|
- Map<String,Integer> maxSortMap = new HashMap<String, Integer>();
|
105
|
+ Map<String, Integer> maxSortMap = new HashMap<String, Integer>();
|
82
|
productSortList.stream().filter(sort -> PARENTID_OF_MAXSORT.equals(sort.getParentId())).collect(Collectors.toList())
|
106
|
productSortList.stream().filter(sort -> PARENTID_OF_MAXSORT.equals(sort.getParentId())).collect(Collectors.toList())
|
83
|
.forEach(sort -> maxSortMap.put(sort.getSortName(), sort.getId()));
|
107
|
.forEach(sort -> maxSortMap.put(sort.getSortName(), sort.getId()));
|
84
|
|
108
|
|
85
|
//4、构造大分类和中分类的对应关系
|
109
|
//4、构造大分类和中分类的对应关系
|
86
|
- Map<Integer,Map<String,Integer>> maxSortToMiddleSort = new HashMap<Integer, Map<String,Integer>>();
|
110
|
+ Map<Integer, Map<String, Integer>> maxSortToMiddleSort = new HashMap<Integer, Map<String, Integer>>();
|
87
|
for (Integer maxSortId : maxSortMap.values()) {
|
111
|
for (Integer maxSortId : maxSortMap.values()) {
|
88
|
- Map<String,Integer> middleSortMap = new HashMap<String, Integer>();
|
112
|
+ Map<String, Integer> middleSortMap = new HashMap<String, Integer>();
|
89
|
for (ProductSort productSort : productSortList) {
|
113
|
for (ProductSort productSort : productSortList) {
|
90
|
- if(productSort.getParentId().equals(maxSortId)){
|
114
|
+ if (productSort.getParentId().equals(maxSortId)) {
|
91
|
middleSortMap.put(productSort.getSortName(), productSort.getId());
|
115
|
middleSortMap.put(productSort.getSortName(), productSort.getId());
|
92
|
}
|
116
|
}
|
93
|
}
|
117
|
}
|
|
@@ -175,4 +199,20 @@ public class ForbidenSortBrandLogicService { |
|
@@ -175,4 +199,20 @@ public class ForbidenSortBrandLogicService { |
175
|
return brandNameToIdMap;
|
199
|
return brandNameToIdMap;
|
176
|
}
|
200
|
}
|
177
|
|
201
|
|
|
|
202
|
+ public static List<List<ForbiddenSortBrand>> createList(List<ForbiddenSortBrand> target, int size) {
|
|
|
203
|
+ List<List<ForbiddenSortBrand>> listArr = new ArrayList<List<ForbiddenSortBrand>>();
|
|
|
204
|
+ //获取被拆分的数组个数
|
|
|
205
|
+ int arrSize = target.size() % size == 0 ? target.size() / size : target.size() / size + 1;
|
|
|
206
|
+ for (int i = 0; i < arrSize; i++) {
|
|
|
207
|
+ List<ForbiddenSortBrand> sub = new ArrayList<ForbiddenSortBrand>();
|
|
|
208
|
+ //把指定索引数据放入到list中
|
|
|
209
|
+ for (int j = i * size; j <= size * (i + 1) - 1; j++) {
|
|
|
210
|
+ if (j <= target.size() - 1) {
|
|
|
211
|
+ sub.add(target.get(j));
|
|
|
212
|
+ }
|
|
|
213
|
+ }
|
|
|
214
|
+ listArr.add(sub);
|
|
|
215
|
+ }
|
|
|
216
|
+ return listArr;
|
|
|
217
|
+ }
|
178
|
} |
218
|
} |