Browse Source

电视剧,导入代码优化

chenjz 3 years ago
parent
commit
958d7934bf
2 changed files with 25 additions and 12 deletions
  1. 13 0
      sql/update.sql
  2. 12 12
      src/main/java/com/ylcm/sys/controller/AdTheatreController.java

+ 13 - 0
sql/update.sql

@@ -0,0 +1,13 @@
1
+-- 电视剧导入,统计sql处理,保留最新数据
2
+DELETE t1 FROM ad_theatre_tv_stat t1,
3
+(
4
+	select name,`media_name`,`theatre_time`,`show_date`,
5
+	max(id) id FROM ad_theatre_tv_stat group by
6
+	name,`media_name`,`theatre_time`,`show_date`
7
+	having count(*) >1
8
+) t2 WHERE t1.name = t2.name and
9
+ t1.media_name = t2.media_name and t1.theatre_time = t2.theatre_time and t1.show_date = t2.show_date
10
+ AND t1.id < t2.id
11
+
12
+
13
+

+ 12 - 12
src/main/java/com/ylcm/sys/controller/AdTheatreController.java

@@ -35,7 +35,9 @@ import javax.servlet.http.HttpServletResponse;
35 35
 import java.util.ArrayList;
36 36
 import java.util.Comparator;
37 37
 import java.util.List;
38
+import java.util.Map;
38 39
 import java.util.concurrent.CompletableFuture;
40
+import java.util.stream.Collectors;
39 41
 
40 42
 /**
41 43
  * 剧场管理
@@ -262,11 +264,18 @@ public class AdTheatreController extends BaseController{
262 264
 			}
263 265
 			// 入库
264 266
 			List<AdTheatre> adTheatres = adTheatreService.batchAdd(list, tvList);
265
-
267
+			//电视剧分组
268
+			Map<String,List<AdTheatre>> map = list.stream().collect(Collectors.groupingBy(b -> b.getName()));
269
+			for (Map.Entry<String, List<AdTheatre>> entry : map.entrySet()) {
270
+				if(CollectionUtils.isNotEmpty(entry.getValue())){
271
+					CompletableFuture[] completableFutures = entry.getValue().stream().map(x -> CompletableFuture.runAsync(() -> {
272
+						adTheatreTvStatService.saveStatByTheatreTv(x);
273
+					}, taskExecutor)).toArray(CompletableFuture[]::new);
274
+					CompletableFuture.allOf(completableFutures).join();
275
+				}
276
+			}
266 277
 			// 判断执行结果
267 278
 			if (adTheatres != null) {
268
-				//异步统计数据
269
-				asyncBatchSaveStatByTheatreTv(adTheatres);
270 279
 				request.setAttribute("repeat", 0);
271 280
 				request.setAttribute("code", Constants.CODE_SUCCESS);
272 281
 				request.setAttribute("msg", msg);
@@ -283,13 +292,4 @@ public class AdTheatreController extends BaseController{
283 292
 		request.setAttribute("msg", msg);
284 293
 		return returnHtml;
285 294
 	}
286
-
287
-	// 异步统计数据
288
-	private void asyncBatchSaveStatByTheatreTv(List<AdTheatre> adTheatres) {
289
-		CompletableFuture.runAsync(() -> {
290
-			for (AdTheatre adTheatre:adTheatres) {
291
-				adTheatreTvStatService.saveStatByTheatreTv(adTheatre);
292
-			}
293
-		}, taskExecutor);
294
-	}
295 295
 }