1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cn.kk.spring_simple_operation;
import cn.kk.spring_simple_operation.entity.SyncProductPublish;
import cn.kk.spring_simple_operation.mapper.SyncProductAuditMapper;
import cn.kk.spring_simple_operation.mapper.SyncProductPublishMapper;
import cn.kk.spring_simple_operation.mapper.VideoProductMapper;
import cn.kk.spring_simple_operation.model.SkuVideoVO;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@SpringBootTest
class SpringSimpleOperationApplicationTests2 {
private static final BigDecimal TWENTY = new BigDecimal("20");
private static final BigDecimal THIRTY = new BigDecimal("30");
private static final BigDecimal FORTY = new BigDecimal("40");
private static final BigDecimal SIXTY = new BigDecimal("60");
@Resource
private SyncProductPublishMapper syncProductPublishMapper;
@Resource
private VideoProductMapper videoProductMapper;
@Resource
private SyncProductAuditMapper syncProductAuditMapper;
@Test
void contextLoads() {
}
private Map<String, Integer> initSalesMap() {
Map<String, Integer> salesMap = new HashMap<>();
salesMap.put("0~20", 0);
salesMap.put("20~30", 0);
salesMap.put("30~40", 0);
salesMap.put("40~60", 0);
salesMap.put(">60", 0);
return salesMap;
}
@Test
void singleSkuSolarTerm_1() {
//List<String> shortUrlSkuList = videoProductMapper.selectHaveTask();
Map<String, Integer> salesMap = initSalesMap();
List<SkuVideoVO> publishList = syncProductPublishMapper.selectChildSku();
List<SkuVideoVO> publishList_2 = new LinkedList<>();
publishList.forEach(item -> {
ArrayList<SkuVideoVO> list = new ArrayList<>();
list.add(item);
String s = checkPriceAndSales(list, item);
if (StringUtils.isBlank(s)) {
return;
}
salesMap.put(s, salesMap.get(s) + 1);
publishList_2.add(item);
});
printMap(salesMap);
exportSkuList(publishList_2, "常规产品--单个sku");
}
@Test
void groupSkuSolarTerm_1() {
//List<String> shortUrlSkuList = videoProductMapper.selectHaveTask();
Map<String, Integer> salesMap = initSalesMap();
List<String> motherList = syncProductAuditMapper.groupSkuSolarTerm_1();
List<SkuVideoVO> publishList_2 = new LinkedList<>();
motherList.forEach(sku -> {
List<SkuVideoVO> skuVideoVOS = syncProductPublishMapper.groupSkuSolarTerm_1(sku);
Optional<SkuVideoVO> first = skuVideoVOS.stream().filter(t -> t.getSku().equals(sku)).findFirst();
if (!first.isPresent()) {
return;
}
List<String> shortUrlSkuList = videoProductMapper.selectHaveTask(skuVideoVOS.stream().map(SkuVideoVO::getSku).collect(Collectors.toList()));
Iterator<SkuVideoVO> iterator = skuVideoVOS.iterator();
while (iterator.hasNext()) {
SkuVideoVO skuVideoVO = iterator.next();
if (shortUrlSkuList.contains(skuVideoVO.getSku())) {
iterator.remove();
}
}
if (CollectionUtils.isEmpty(skuVideoVOS)) {
return;
}
String s = checkPriceAndSales(skuVideoVOS, first.get());
if (StringUtils.isBlank(s)) {
return;
}
salesMap.put(s, salesMap.get(s) + 1);
publishList_2.add(first.get());
});
printMap(salesMap);
exportSkuList(publishList_2, "常规产品--组合产品母体sku");
}
private String checkPriceAndSales(List<SkuVideoVO> subList, SkuVideoVO item) {
BigDecimal totalPrice = subList.stream().map(SkuVideoVO::getLowestPrice).reduce(BigDecimal::add).get();
BigDecimal lowestPrice = totalPrice.divide(new BigDecimal(subList.size()), 2, BigDecimal.ROUND_HALF_UP);
if (CollectionUtils.isEmpty(subList) || lowestPrice.compareTo(BigDecimal.ZERO) < 0) {
return null;
}
int sum = subList.stream().map(SkuVideoVO::getThirtyDaySales).reduce(Integer::sum).get();
item.setThirtyDaySales(sum);
item.setLowestPrice(lowestPrice);
if (lowestPrice.compareTo(TWENTY) <= 0 && sum >= 25) {
return "0~20";
}
else if (lowestPrice.compareTo(TWENTY) > 0 && lowestPrice.compareTo(THIRTY) <= 0 && sum >= 15) {
return "20~30";
}
else if (lowestPrice.compareTo(THIRTY) > 0 && lowestPrice.compareTo(FORTY) <= 0 && sum >= 10) {
return "30~40";
}
else if (lowestPrice.compareTo(FORTY) > 0 && lowestPrice.compareTo(SIXTY) <= 0 && sum >= 8) {
return "40~60";
}
else if (lowestPrice.compareTo(SIXTY) > 0 && sum >= 5) {
return ">60";
}
return null;
}
private void printSkuList(List<SyncProductPublish> list) {
list.forEach(item -> {
String content = String.format("sku: %s, 30天销量: %d, price: %s, relationKey: %s, parentAsin: %s", item.getSku(), item.getThirtyDaySales(), item.getLowestPrice(), item.getRelationKey(), item.getParentAsin());
System.out.println(content);
});
}
private void exportSkuList(List<SkuVideoVO> list, String originName) {
list.sort(Comparator.comparing(SkuVideoVO::getLowestPrice));
List<List<Object>> dataList = ListUtils.newArrayList();
for (SkuVideoVO syncProductPublish : list) {
List<Object> data = ListUtils.newArrayList();
data.add(syncProductPublish.getSku());
data.add(syncProductPublish.getLowestPrice());
data.add(syncProductPublish.getThirtyDaySales());
data.add(syncProductPublish.getSaleStatus());
data.add(syncProductPublish.getUsUploadInfo());
dataList.add(data);
}
// 写法1
String fileName = "D:\\" + originName + "_" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(head()).sheet(originName).doWrite(dataList);
}
private void printMap(Map<String, Integer> salesMap) {
System.out.println("0~20: " + salesMap.get("0~20"));
System.out.println("20~30: " + salesMap.get("20~30"));
System.out.println("30~40: " + salesMap.get("30~40"));
System.out.println("40~60: " + salesMap.get("40~60"));
System.out.println(">60: " + salesMap.get(">60"));
}
private List<List<String>> head() {
List<List<String>> list = ListUtils.newArrayList();
List<String> head0 = ListUtils.newArrayList();
head0.add("sku");
List<String> head1 = ListUtils.newArrayList();
head1.add("单价");
List<String> head2 = ListUtils.newArrayList();
head2.add("月销");
List<String> head3 = ListUtils.newArrayList();
head3.add("销售状态");
List<String> head4 = ListUtils.newArrayList();
head4.add("是否拍摄");
list.add(head0);
list.add(head1);
list.add(head2);
list.add(head3);
list.add(head4);
return list;
}
@Test
void singleSkuSolarTerm_Q1Q2() {
Map<String, Integer> salesMap = initSalesMap();
List<SkuVideoVO> publishList = syncProductPublishMapper.selectChildSku_2();
List<SkuVideoVO> publishList_2 = new LinkedList<>();
publishList.forEach(item -> {
ArrayList<SkuVideoVO> list = new ArrayList<>();
list.add(item);
String s = checkPriceAndSales(list, item);
if (StringUtils.isBlank(s)) {
return;
}
salesMap.put(s, salesMap.get(s) + 1);
publishList_2.add(item);
});
printMap(salesMap);
exportSkuList(publishList_2, "节气产品--单个sku");
}
@Test
void groupSkuSolarTerm_Q1Q2() {
//List<String> shortUrlSkuList = videoProductMapper.selectHaveTask();
Map<String, Integer> salesMap = initSalesMap();
List<String> motherList = syncProductAuditMapper.groupSkuSolarTerm_Q1Q2();
List<SkuVideoVO> publishList_2 = new LinkedList<>();
motherList.forEach(sku -> {
List<SkuVideoVO> skuVideoVOS = syncProductPublishMapper.groupSkuSolarTerm_Q1Q2(sku);
Optional<SkuVideoVO> first = skuVideoVOS.stream().filter(t -> t.getSku().equals(sku)).findFirst();
if (!first.isPresent()) {
return;
}
List<String> shortUrlSkuList = videoProductMapper.selectHaveTask(skuVideoVOS.stream().map(SkuVideoVO::getSku).collect(Collectors.toList()));
skuVideoVOS.removeIf(skuVideoVO -> shortUrlSkuList.contains(skuVideoVO.getSku()));
if (CollectionUtils.isEmpty(skuVideoVOS)) {
return;
}
String s = checkPriceAndSales(skuVideoVOS, first.get());
if (StringUtils.isBlank(s)) {
return;
}
salesMap.put(s, salesMap.get(s) + 1);
publishList_2.add(first.get());
});
printMap(salesMap);
exportSkuList(publishList_2, "节气产品--组合产品母体sku");
}
}