CommonBizService.java
49.4 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
package cn.fw.dalaran.service.biz;
import cn.fw.dalaran.common.exception.BusinessException;
import cn.fw.dalaran.common.utils.DateUtil;
import cn.fw.dalaran.common.utils.ImageUtils;
import cn.fw.dalaran.common.utils.StringUtils;
import cn.fw.dalaran.domain.db.*;
import cn.fw.dalaran.domain.db.config.ValidConfig;
import cn.fw.dalaran.domain.db.config.ValidConfigNew;
import cn.fw.dalaran.domain.dto.LivePoolDTO;
import cn.fw.dalaran.domain.dto.VideoPoolDTO;
import cn.fw.dalaran.domain.enums.*;
import cn.fw.dalaran.domain.vo.ValidConfigNewVo;
import cn.fw.dalaran.service.Common;
import cn.fw.dalaran.service.data.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author kurisu
* @date 2021-12-04 14:29
* @description 通用服务
*/
@Slf4j
@Service
@RequiredArgsConstructor
@SuppressWarnings("Duplicates")
public class CommonBizService {
@Value("${spring.profiles.active}")
private String env;// 获取系统当前环境
private final AccountService accountService;
private final VideoPoolService videoPoolService;
private final LivePoolService livePoolService;
private final ValidConfigService validConfigService;// 直播/视频有效性
private final ValidConfigNewService validConfigNewService;// 直播/视频有效性
private final ActivityThemeService activityThemeService;// 主题配置
private final GlobalConfigService globalConfigService;
private final ThemeFileService themeFileService;
private final Common common;
/**
* 储存短视频数据
*
* @param videoPoolDTO 自媒体服务器传过来的视频池
*/
public boolean saveVideoData(VideoPoolDTO videoPoolDTO) {
String account = videoPoolDTO.getAccount();
Integer type = videoPoolDTO.getType();
List<Account> accountList = queryAccList(account, type);
if (CollectionUtils.isEmpty(accountList)) {
log.info(String.format("不存在[%s]平台, 账号为: %s的账号, 视频保存失败", PlatformEnum.getNameByVale(type), account));
return false;
}
List<VideoPoolDTO.VideoDTO> videoList = videoPoolDTO.getVideoList();
if (CollectionUtils.isEmpty(videoList)) {
return true;
}
if (!this.acceptData()) {
log.info(String.format("%s 的视频数据提交时间已超当日期限, 数据将不保存到视频池", account));
return true;
}
for (Account db : accountList) {
saveVideoPoolList(db, videoList);
}
return true;
}
/**
* 储存直播数据
*
* @param livePoolDTO 自媒体服务器传过来的直播池
*/
public boolean saveLiveData(LivePoolDTO livePoolDTO) {
String account = livePoolDTO.getAccount();
Integer type = livePoolDTO.getType();
List<Account> accountList = queryAccList(account, type);
if (CollectionUtils.isEmpty(accountList)) {
log.info(String.format("不存在[%s]平台, 账号为: %s的账号, 直播保存失败", PlatformEnum.getNameByVale(type), account));
return false;
}
List<LivePoolDTO.LiveDTO> liveList = livePoolDTO.getLiveList();
if (CollectionUtils.isEmpty(liveList)) {
return true;
}
if (!this.acceptData()) {
log.info(String.format("%s 的直播数据提交时间已超当日期限, 数据将不保存到直播池", account));
return true;
}
for (Account db : accountList) {
saveLivePoolList(db, liveList);
}
return true;
}
/**
* 是否保存数据入池
*/
public boolean acceptData() {
/*if (env.contains("prd")) {
final long acceptDataMaxTime = DateUtil.getBeginInTime(new Date()).getTime() + 27 * 1800 * 1000L;
return System.currentTimeMillis() - acceptDataMaxTime <= 0;
}
return true;*/
return true;
}
/**
* 查询指定类型的账号列表
*
* @param account 账户号
* @param type 账户类型
* @return
*/
private List<Account> queryAccList(String account, Integer type) {
PlatformEnum platformEnum = PlatformEnum.ofValue(type);
if (Objects.isNull(platformEnum)) {
return new ArrayList<>();
}
List<Account> accountList = accountService.list(Wrappers.<Account>lambdaQuery()
.eq(Account::getAccount, account)
.eq(Account::getType, platformEnum)
.eq(Account::getYn, Boolean.TRUE)
);
return CollectionUtils.isEmpty(accountList) ? new ArrayList<>() : accountList;
}
/**
* 保存视频池列表
*
* @param account 账户
* @param videoList 该账户的视频信息
*/
private void saveVideoPoolList(Account account, List<VideoPoolDTO.VideoDTO> videoList) {
List<VideoPool> poolList = new ArrayList<>();
final LocalDate today = LocalDate.now();
final Long groupId = account.getGroupId();
final Long shopId = account.getShopId();
final String accountNo = account.getAccount();
final PlatformEnum platformEnum = account.getType();
final String platformName = platformEnum.getName();
final List<ActivityTheme> thisMonthThemeList = this.getGroupActivityTheme(groupId, shopId, false);
List<ActivityTheme> themeList = thisMonthThemeList;
Date themeStartTime;// 主题活动开始时间
Date themeEndTime;// 主题活动结束时间
final ValidConfigNew thisThemeFilterTopic = this.getIsFilterTagsConfig(themeList.get(0).getConfigGroupId());
ValidConfigNew config;
for (VideoPoolDTO.VideoDTO videoDTO : videoList) {
{// 让主题相关信息初始化到本月本天
themeList = thisMonthThemeList;
themeStartTime = themeList.get(0).getStartTime();// 获取最新的一个主题活动开始时间
themeEndTime = themeList.get(0).getEndTime();// 获取最新的一个主题活动结束时间
config = thisThemeFilterTopic;
}
final String videoId = videoDTO.getVideoId();// 获取视频id
final Date publishTime = videoDTO.getPublishTime();// 获取视频发布时间
final String videoCover = videoDTO.getPreview();
final String format = String.format("账户号为: %s, id为: %s, %s平台的视频", accountNo, videoId, platformName);
final long timeSub = System.currentTimeMillis() - themeStartTime.getTime();
if (2 * 24 * 3600 * 1000L < timeSub && timeSub < 3 * 24 * 3600 * 1000L)// 新主题开始第二天了
if (themeList.size() > 1) {// 目前不止一个主题
Date lastThemeEndTime = themeList.get(1).getEndTime();
final long timeSub1 = lastThemeEndTime.getTime() - publishTime.getTime();
if (0 < timeSub1 && timeSub1 < 12 * 3600 * 1000L) {// 说明是上一个主题结束时间的中午发布的
themeStartTime = themeList.get(1).getStartTime();// 获取上一个主题活动开始时间
themeEndTime = themeList.get(1).getEndTime();// 获取上一个主题活动结束时间
}
}
if (Objects.equals(today.getDayOfMonth(), 2)) {// 今天是这个月的第2天
final LocalDateTime publishTime1 = DateUtil.date2LocalDateTime(publishTime);
final LocalDateTime lastMonthLastDayAfternoon = today.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).atTime(12, 0, 0);
final LocalDateTime lastMonthLastDayEndTime = today.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).atTime(23, 59, 59);
if (publishTime1.compareTo(lastMonthLastDayAfternoon) >= 0 && publishTime1.compareTo(lastMonthLastDayEndTime) <= 0) {// 作品是上个主题中午12点后发布的
themeList = this.getGroupActivityTheme(groupId, shopId, true);
themeStartTime = themeList.get(0).getStartTime();// 获取最新的一个主题活动开始时间
themeEndTime = themeList.get(0).getEndTime();// 获取最新的一个主题活动结束时间
config = this.getIsFilterTagsConfig(themeList.get(0).getConfigGroupId());
}
}
final List<ActivityTheme> collect = themeList.stream()
.filter(item -> publishTime.compareTo(item.getStartTime()) >= 0 && publishTime.compareTo(item.getEndTime()) <= 0)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(collect)) {
log.info(String.format(" %s, 不是该月发布的视频, 视作无效视频", format));
continue;
}
VideoPool videoPool = new VideoPool();
if (publishTime.compareTo(themeStartTime) >= 0 && publishTime.compareTo(themeEndTime) <= 0) {
// 封面图验证
boolean validCover = false;
final ActivityTheme activityTheme = collect.get(0);
final Long configGroupId = activityTheme.getConfigGroupId();
BigDecimal rate = this.getGlobalConfigValue(configGroupId, ConfigEnum.COVER_SIMILARITY.getValue());
final List<ThemeFile> list = themeFileService.lambdaQuery()
.eq(ThemeFile::getThemeId, activityTheme.getId())
.eq(ThemeFile::getType, FileTypeEnum.THEME_COVER.getValue())
.list();
for (ThemeFile themeFile : list) {
try {
final BigDecimal coverSimilarity = this.validCover(activityTheme.getTheme(), themeFile.getThemeId(), themeFile.getFileId(), videoCover, accountNo, videoId, 1);
if (validCover = coverSimilarity.compareTo(rate) >= 0)
break;
} catch (Exception e) {
log.error(String.format("%s, 验证封面图出现异常", format), e);
}
}
if (!validCover) {
log.info(String.format("%s, 封面图不匹配, 视作无效视频", format));
continue;
}
// 话题及指标验证
Set<String> tagsSet = this.processTags(videoDTO.getTitle());
final JSONObject validTags = this.validTags(config, collect, tagsSet, groupId, shopId);
final Boolean containsTag = validTags.getBoolean("containsTag");
final boolean validVideo = this.newValidVideo(account, configGroupId, videoDTO);
if (!containsTag) {
log.info(String.format("%s, 不包含主题指定的话题, 未能成功进入视频池", format));
continue;
}
if (!validVideo) {
log.info(String.format("%s, 指标数据不满足集团标准, 视作无效视频", format));
}
if (validVideo) {
videoPool.setValidVideo(1);
} else {
videoPool.setValidVideo(0);
}
videoPool.setAccountId(account.getId());
videoPool.setPlatform(platformEnum);
videoPool.setVideoId(videoId);
videoPool.setPlayUrl(videoDTO.getPlayUrl());
videoPool.setTitle(videoDTO.getTitle());
videoPool.setPreview(videoCover);
videoPool.setUv(videoDTO.getUv());
videoPool.setFullUv(videoDTO.getFullUv());
videoPool.setLikeNum(videoDTO.getLikeNum());
videoPool.setShareNum(videoDTO.getShareNum());
videoPool.setCommentNum(videoDTO.getCommentNum());
videoPool.setNewFanNum(videoDTO.getNewFanNum());
videoPool.setDuration(videoDTO.getDuration());
videoPool.setThemeId(validTags.getLong("theme_id"));
videoPool.setTheme(validTags.getString("theme"));
videoPool.setTags(this.collection2String(tagsSet));
//videoPool.setBrandId();
//videoPool.setBrandName();
//videoPool.setSeriesId();
//videoPool.setSeriesName();
videoPool.setPublishTime(publishTime);
} else {
log.info(String.format("%s, 不在目前最新活动时间内, 尝试查询历史记录", format));
VideoPool latestVideo = videoPoolService.getLatestVideo(videoId);
if (Objects.isNull(latestVideo)) {
log.info(String.format("%s, 不在目前最新活动时间内, 且无历史记录, 不进入视频池", format));
continue;
}
videoPool = latestVideo;
}
poolList.add(videoPool);
}
videoPoolService.removeAccountDataByDate(account.getId(), today);
videoPoolService.saveBatch(poolList);
}
/**
* 校验该视频是否符合集团配置的标准
*
* @param groupId 集团id
* @param video 视频信息
* @return 该条视频是否符合集团配置的标准
*/
@Deprecated
private boolean validVideo(Long groupId, VideoPoolDTO.VideoDTO video) {
ValidConfig config = validConfigService.lambdaQuery()
.eq(ValidConfig::getGroupId, groupId)
.list().get(0);
if (config.getLikeCntEffectVideo() > 0) {
if (video.getLikeNum() < config.getLikeCntVideo()) {
return false;
}
}
if (config.getCommentCntEffectVideo() > 0) {
if (video.getCommentNum() < config.getCommentCntVideo()) {
return false;
}
}
if (config.getShareCntEffectVideo() > 0) {
if (video.getShareNum() < config.getShareCntVideo()) {
return false;
}
}
if (config.getNewFansCntEffectVideo() > 0) {
if (video.getNewFanNum() < config.getNewFansCntVideo()) {
return false;
}
}
if (config.getPlayCntEffect() > 0) {
if (video.getUv() < config.getPlayCnt()) {
return false;
}
}
if (config.getFullPlayCntEffect() > 0) {
if (video.getFullUv() < config.getFullPlayCnt()) {
return false;
}
}
return true;
}
/**
* 测试
*/
@Scheduled(fixedRate = 5000L)
public void test() {
}
/**
* new校验该视频是否符合集团配置的标准
*
* @param configGroupId 所属配置组id
* @param video 视频信息
* @return 该条视频是否符合集团配置的标准
*/
private boolean newValidVideo(Account account, Long configGroupId, VideoPoolDTO.VideoDTO video) {
final String accountNo = account.getAccount();
final PlatformEnum platformEnum = account.getType();
final String platformName = platformEnum.getName();
final String videoId = video.getVideoId();
final List<ValidConfigNew> configList = validConfigNewService
.queryList(configGroupId, BizTypeEnum.VIDEO.getValue())
.getDetails()
.stream()
.map(ValidConfigNewVo::toDB)
.sorted(Comparator.comparing(ValidConfigNew::getIndexType))
.skip(1)
.collect(Collectors.toList());// 获取配置组的短视频检验最低标准指标
final int indexCnt = ValidVideoEnum.values().length - 1;// 获取视频指标配置项数目
final LocalDate now = LocalDate.now();
final String format = String.format("%s 账户号为: %s, id为: %s, %s平台的视频", now, accountNo, videoId, platformName);
if (indexCnt > 0) {
final ValidConfigNew playCntConfig = configList.get(0);// 播放数
if (playCntConfig.getValid() > 0) {
final Integer playCnt = video.getUv();
if (Objects.isNull(playCnt)) {
log.error(String.format("%s[播放数]数据为空", format));
} else {
final Integer minValue = playCntConfig.getMinValue();
if (playCnt < minValue) {
log.info(String.format("%s[播放数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 1) {
final ValidConfigNew fullPlayCntConfig = configList.get(1);// 完整播放数
if (fullPlayCntConfig.getValid() > 0) {
final Integer fullPlayCnt = video.getFullUv();
if (Objects.isNull(fullPlayCnt)) {
log.error(String.format("%s[完整播放数]数据为空", format));
} else {
final Integer minValue = fullPlayCntConfig.getMinValue();
if (fullPlayCnt < minValue) {
log.info(String.format("%s[完整播放数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 2) {
final ValidConfigNew likeCntConfig = configList.get(2);// 点赞数
if (likeCntConfig.getValid() > 0) {
final Integer likeCnt = video.getLikeNum();
if (Objects.isNull(likeCnt)) {
log.error(String.format("%s[点赞数]数据为空", format));
} else {
final Integer minValue = likeCntConfig.getMinValue();
if (likeCnt < minValue) {
log.info(String.format("%s[点赞数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 3) {
final ValidConfigNew commentCntConfig = configList.get(3);// 评论数
if (commentCntConfig.getValid() > 0) {
final Integer commentCnt = video.getCommentNum();
if (Objects.isNull(commentCnt)) {
log.error(String.format("%s[评论数]数据为空", format));
} else {
final Integer minValue = commentCntConfig.getMinValue();
if (commentCnt < minValue) {
log.info(String.format("%s[评论数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 4) {
final ValidConfigNew shareCntConfig = configList.get(4);// 分享数
if (shareCntConfig.getValid() > 0) {
final Integer shareCnt = video.getShareNum();
if (Objects.isNull(shareCnt)) {
log.error(String.format("%s[分享数]数据为空", format));
} else {
final Integer minValue = shareCntConfig.getMinValue();
if (shareCnt < minValue) {
log.info(String.format("%s[分享数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 5) {
if (!Objects.equals(PlatformEnum.BILIBILI.getValue(), platformEnum.getValue())) {// bilibili视频[不过滤]增粉指标
final ValidConfigNew newFansCntConfig = configList.get(5);// 增粉数
if (newFansCntConfig.getValid() > 0) {
final Integer newFansCnt = video.getNewFanNum();
if (Objects.isNull(newFansCnt)) {
log.error(String.format("%s[增粉数]数据为空", format));
} else {
final Integer minValue = newFansCntConfig.getMinValue();
if (newFansCnt < minValue) {
log.info(String.format("%s[增粉数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
}
return true;
}
/**
* 保存直播池列表
*
* @param account 账户
* @param liveList 该账户的直播信息
*/
private void saveLivePoolList(Account account, List<LivePoolDTO.LiveDTO> liveList) {
List<LivePool> poolList = new ArrayList<>();
ValidConfigNew config = new ValidConfigNew();
final LocalDate today = LocalDate.now();
final Long groupId = account.getGroupId();
final Long shopId = account.getShopId();
final String accountNo = account.getAccount();
final PlatformEnum platformEnum = account.getType();
final String platformName = platformEnum.getName();
final List<ActivityTheme> themeList = this.getGroupActivityTheme(groupId, shopId, false);
for (LivePoolDTO.LiveDTO liveDTO : liveList) {
final String roomNo = liveDTO.getRoomNo();
final String liveCover = liveDTO.getCover();
final String format = String.format("账户号为: %s, id为: %s, %s平台的直播", accountNo, roomNo, platformName);
final Date liveStartTime = liveDTO.getLiveStartTime();
final String playbackUrl = liveDTO.getPlaybackUrl();
if (StringUtils.isEmpty(playbackUrl)) {
log.info(String.format("%s, 无直播回放, 视作无效直播, 未能成功进入直播池", format));
continue;
}
LivePool livePool = new LivePool();
Set<String> tagsSet = this.processTags(liveDTO.getTitle());// 直播标题包含话题
final List<ActivityTheme> collect = themeList.stream()
.filter(item -> liveStartTime.compareTo(item.getStartTime()) >= 0 && liveStartTime.compareTo(item.getEndTime()) <= 0)
.collect(Collectors.toList());// 作品发布时间和活动主题生效时间匹配的活动主题
// 封面图验证
boolean validCover = false;
final ActivityTheme activityTheme = collect.get(0);
final Long configGroupId = activityTheme.getConfigGroupId();// 获取主题所处配置组id
BigDecimal rate = this.getGlobalConfigValue(configGroupId, ConfigEnum.COVER_SIMILARITY.getValue());
final List<ThemeFile> list = themeFileService.lambdaQuery()
.eq(ThemeFile::getThemeId, activityTheme.getId())
.eq(ThemeFile::getType, FileTypeEnum.THEME_COVER.getValue())
.list();
for (ThemeFile themeFile : list) {
try {
final BigDecimal coverSimilarity = this.validCover(activityTheme.getTheme(), activityTheme.getId(), themeFile.getFileId(), liveCover, accountNo, roomNo, 2);
if (validCover = coverSimilarity.compareTo(rate) >= 0)
break;
} catch (Exception e) {
log.error(String.format("%s, 验证封面图出现异常", format), e);
}
}
if (!validCover) {
log.info(String.format("%s, 封面图不匹配, 视作无效直播", format));
continue;
}
// 话题及指标验证, 付费判定
if (Objects.isNull(config.getMinValue()))
config = this.getIsFilterTagsConfig(configGroupId);
final JSONObject validTags = this.validTags(config, collect, tagsSet, groupId, shopId);
final Boolean containsTag = validTags.getBoolean("containsTag");// 是否包含指定话题
final boolean validLive = this.newValidLive(account, configGroupId, liveDTO);// 是否满足指标
final boolean pay = this.validPay(liveDTO, configGroupId);
if (!containsTag) {
log.info(String.format("%s, 不包含主题指定的话题, 未能成功进入直播池", format));
continue;
}
if (!validLive) {
log.info(String.format("%s, 指标数据不满足集团标准, 视作无效直播", format));
livePool.setValidLive(0);
} else {
livePool.setValidLive(1);
if (pay) {
livePool.setValidLive(-1);
log.info(String.format("%s, 系统判定直播观看巅峰数据异常", format));
}
}
livePool.setAccountId(account.getId());
livePool.setPlatform(platformEnum);
livePool.setRoomNo(roomNo);
livePool.setPlaybackUrl(playbackUrl);
livePool.setUserNick(liveDTO.getUserNick());
livePool.setTitle(liveDTO.getTitle());
livePool.setCover(liveCover);
livePool.setUv(liveDTO.getUv());
livePool.setUvPeak(liveDTO.getUvPeak());
livePool.setLikeNum(liveDTO.getLikeNum());
livePool.setShareNum(liveDTO.getShareNum());
livePool.setCommentNum(liveDTO.getCommentNum());
livePool.setNewFanNum(liveDTO.getNewFanNum());
livePool.setReceiveNum(liveDTO.getReceiveNum());
livePool.setReceiveAmount(liveDTO.getReceiveAmount());
livePool.setLiveDuration(liveDTO.getLiveDuration());
livePool.setLiveStartTime(liveStartTime);
livePool.setLiveEndTime(liveDTO.getLiveEndTime());
livePool.setThemeId(validTags.getLong("theme_id"));
livePool.setTheme(validTags.getString("theme"));
livePool.setTags(this.collection2String(tagsSet));
//livePool.setBrandId();
//livePool.setBrandName();
//livePool.setSeriesId();
//livePool.setSeriesName();
poolList.add(livePool);
}
livePoolService.removeAccountDataByDate(account.getId(), today);
livePoolService.saveBatch(poolList);
}
/**
* 观看巅峰数据异常判定
*
* @param live 直播数据
* @param configGroupId 配置组id
* @return 数据是否异常
*/
private boolean validPay(LivePoolDTO.LiveDTO live, Long configGroupId) {
final ValidConfigNew config = validConfigNewService.getOne(Wrappers.<ValidConfigNew>lambdaQuery()
.eq(ValidConfigNew::getConfigGroupId, configGroupId)
.eq(ValidConfigNew::getType, BizTypeEnum.LIVE.getValue())
.eq(ValidConfigNew::getIndexType, ValidLiveEnum.PAY_LIVE_RATE.getValue()));
if (Objects.equals(config.getValid(), 0))
return false;
else
return live.getUvPeak() * config.getMinValue() >= live.getUv();
}
/**
* 校验该直播是否符合集团配置的标准
*
* @param groupId 集团id
* @param live 直播信息
* @return 该次直播是否符合集团配置的标准
*/
@Deprecated
private boolean validLive(Long groupId, LivePoolDTO.LiveDTO live) {
ValidConfig config = validConfigService.lambdaQuery()
.eq(ValidConfig::getGroupId, groupId)
.list().get(0);
if (config.getLikeCntEffectLive() > 0) {
if (live.getLikeNum() < config.getLikeCntLive()) {
return false;
}
}
if (config.getCommentCntEffectLive() > 0) {
if (live.getCommentNum() < config.getCommentCntLive()) {
return false;
}
}
if (config.getShareCntEffectLive() > 0) {
if (live.getShareNum() < config.getShareCntLive()) {
return false;
}
}
if (config.getNewFansCntEffectLive() > 0) {
if (live.getNewFanNum() < config.getNewFansCntLive()) {
return false;
}
}
if (config.getWatchUserEffect() > 0) {
if (live.getUv() < config.getWatchUser()) {
return false;
}
}
if (config.getWatchUserPeakEffect() > 0) {
if (live.getUvPeak() < config.getWatchUserPeak()) {
return false;
}
}
if (config.getDurationEffect() > 0) {
if (live.getLiveDuration() < config.getDuration()) {
return false;
}
}
return true;
}
/**
* new校验该直播是否符合集团配置的标准
*
* @param account 账户信息
* @param configGroupId 所属配置组id
* @param live 直播信息
* @return 该次直播是否符合集团配置的标准
*/
private boolean newValidLive(Account account, Long configGroupId, LivePoolDTO.LiveDTO live) {
final String accountNo = account.getAccount();
final PlatformEnum platformEnum = account.getType();
final String platformName = platformEnum.getName();
final String roomNo = live.getRoomNo();
List<ValidConfigNew> configList = validConfigNewService
.queryList(configGroupId, BizTypeEnum.LIVE.getValue())
.getDetails()
.stream()
.map(ValidConfigNewVo::toDB)
.sorted(Comparator.comparing(ValidConfigNew::getIndexType))
.skip(2)
.collect(Collectors.toList());// 获取配置组的直播检验最低标准指标
final int indexCnt = ValidLiveEnum.values().length - 2;// 获取直播指标配置项数目
final String format = String.format("账户号为: %s, id为: %s, %s平台的直播", accountNo, roomNo, platformName);
if (indexCnt > 0) {
if (!Objects.equals(PlatformEnum.BILIBILI.getValue(), platformEnum.getValue())) {// bilibili直播[不过滤]观看人数
final ValidConfigNew watchCntConfig = configList.get(0);// 观看人数
if (watchCntConfig.getValid() > 0) {
final Long watchCnt = live.getUv();
if (Objects.isNull(watchCnt)) {
log.error(String.format("%s[观看人数]数据为空", format));
} else {
final Integer minValue = watchCntConfig.getMinValue();
if (watchCnt < minValue) {
log.info(String.format("%s[观看人数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
}
if (indexCnt > 1) {
if (!Objects.equals(PlatformEnum.BILIBILI.getValue(), platformEnum.getValue())) {// bilibili直播[不过滤]观看人数巅峰
final ValidConfigNew watchPeakCntConfig = configList.get(1);// 观看人数峰值
if (watchPeakCntConfig.getValid() > 0) {
final Long watchCntPeak = live.getUvPeak();
if (Objects.isNull(watchCntPeak)) {
log.error(String.format("%s[观看人数巅峰]数据为空", format));
} else {
final Integer minValue = watchPeakCntConfig.getMinValue();
if (watchCntPeak < minValue) {
log.info(String.format("%s[观看人数峰值]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
}
if (indexCnt > 2) {
if (Objects.equals(PlatformEnum.KS.getValue(), platformEnum.getValue())) {// 快手直播[才过滤]点赞指标
final ValidConfigNew likeCntConfig = configList.get(2);// 点赞数
if (likeCntConfig.getValid() > 0) {
final Long likeCnt = live.getLikeNum();
if (Objects.isNull(likeCnt)) {
log.error(String.format("%s[点赞数]数据为空", format));
} else {
final Integer minValue = likeCntConfig.getMinValue();
if (likeCnt < minValue) {
log.info(String.format("%s[点赞数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
}
if (indexCnt > 3) {
final ValidConfigNew commentCntConfig = configList.get(3);// 评论数
if (commentCntConfig.getValid() > 0) {
final Long commentCnt = live.getCommentNum();
if (Objects.isNull(commentCnt)) {
log.error(String.format("%s[评论数]数据为空", format));
} else {
final Integer minValue = commentCntConfig.getMinValue();
if (commentCnt < minValue) {
log.info(String.format("%s[评论数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 4) {
if (Objects.equals(PlatformEnum.KS.getValue(), platformEnum.getValue())) {// 快手直播[才过滤]分享指标
final ValidConfigNew shareCntConfig = configList.get(4);// 分享数
if (shareCntConfig.getValid() > 0) {
final Long shareCnt = live.getShareNum();
if (Objects.isNull(shareCnt)) {
log.error(String.format("%s[分享数]数据为空", format));
} else {
final Integer minValue = shareCntConfig.getMinValue();
if (shareCnt < minValue) {
log.info(String.format("%s[分享数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
}
if (indexCnt > 5) {
final ValidConfigNew newFansCntConfig = configList.get(5);// 增粉数
if (newFansCntConfig.getValid() > 0) {
final Long newFanCnt = live.getNewFanNum();
if (Objects.isNull(newFanCnt)) {
log.error(String.format("%s[增粉数]数据为空", format));
} else {
final Integer minValue = newFansCntConfig.getMinValue();
if (newFanCnt < minValue) {
log.info(String.format("%s[增粉数]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
if (indexCnt > 6) {
final ValidConfigNew durationCntConfig = configList.get(6);// 直播时长
if (durationCntConfig.getValid() > 0) {
final Integer liveDuration = live.getLiveDuration();
if (Objects.isNull(liveDuration)) {
log.error(String.format("%s[直播时长]数据为空", format));
} else {
final Integer minValue = durationCntConfig.getMinValue();
if (liveDuration < minValue) {
log.info(String.format("%s[直播时长]数据不满足集团配置标准(最低: %s)", format, minValue));
return false;
}
}
}
}
return true;
}
/**
* 将标题中的话题拿出来
*
* @param title 标题
* @return 话题集合
*/
private static Set<String> processTags(String title) {
title = title
.replace("#", "#")
.replace(" ", "")
.replace("\n", "")
.replace("—", "一");
String tagsStr = String.join("", StringUtils.matchs(title, StringUtils.tagsPattern));
return Arrays.stream(tagsStr.split("#"))
.map(item -> item.replace("#", ""))
.filter(item -> item.length() > 0)
.collect(Collectors.toSet());
}
public static void main(String[] args) {
System.out.println(System.getProperty("java.io.tmpdir"));
String workTitle = "#现车加\\\"免\\\"开新过新年";
String setTopic = "#温暖回家路提现车过新年,#现车加\\免\\开新过新年,#温暖回家路春节不打烊,#开年有礼大展宏兔,#智电超能体验,#开新年夜FUN,#温暖回家路一路有你,#展厅实车带货UNI-K iDD".replace(",", "");
Set<String> strings1 = processTags(workTitle);
Set<String> strings2 = processTags(setTopic);
System.out.println(CollectionUtils.containsAny(strings1, strings2));
}
/**
* 集合转换成字符串
*
* @param collection 输入集合
* @return 字符串类型的集合
*/
private <Q> String collection2String(Collection<Q> collection) {
return JSON.toJSONString(collection)
.replace("[", "")
.replace("]", "")
.replace("\"", "");
}
/**
* 验证标题的话题
*
* @param config 集团是否过滤话题配置
* @param themeList 集团活动主题
* @param tags 话题集合
* @param groupId 集团id
* @param shopId 门店id
* @return 该视频/直播包含话题是否符合主题配置的话题
*/
private JSONObject validTags(ValidConfigNew config, List<ActivityTheme> themeList, Set<String> tags, Long groupId, Long shopId) {
final JSONObject result = new JSONObject();
if (config.getValid() <= 0) {// 配置不过滤话题
result.put("containsTag", true);
return result;
}
if (CollectionUtils.isEmpty(themeList)) {// 未配置活动主题
final String format = String.format("集团id: %s, 门店id: %s未配置活动主题, 所有'直播'/'视频'数据都将无法保存", groupId, shopId);
log.error(String.format("%s, 请联系管理员配置活动主题或者禁用话题过滤", format));
throw new BusinessException(String.format("%s, 请联系管理员配置活动主题或者禁用话题过滤", format));
}
HashSet<Long> shopIds = new HashSet<>();
HashSet<String> configTags = new HashSet<>();// 主题包含话题
themeList.forEach(item -> {
configTags.addAll(Arrays.stream(item.getTopic().replace(",", "").split("#")).skip(1).collect(Collectors.toSet()));
if (item.getAllShop() > 0) {
shopIds.add(-1L);
} else {
shopIds.addAll(Arrays.stream(item.getShopIds().split(",")).map(Long::valueOf).collect(Collectors.toSet()));
}
});// 找到所有活动主题的所有话题, 覆盖的所有门店
if (shopIds.contains(-1L) || shopIds.contains(shopId)) {
ActivityTheme activityTheme = themeList.get(0);
BigDecimal rate = this.getGlobalConfigValue(activityTheme.getConfigGroupId(), ConfigEnum.TOPIC_SIMILARITY.getValue());
boolean containsTag = false;
if (rate.compareTo(BigDecimal.valueOf(100)) == 0) {// 原方案
Set<String> resultSet = new HashSet<>(tags);
resultSet.retainAll(configTags);// 交集
//resultSet.removeAll(configTags);// 差集
//resultSet.addAll(configTags);// 并集
containsTag = !resultSet.isEmpty();
} else {// LCS计算话题匹配度
for (String configTag : configTags) {
for (String tag : tags) {
if (this.LCSCalcTopicSimilarity(rate, configTag, tag)) {
containsTag = true;
break;
}
}
if (containsTag)
break;
}
}
if (containsTag) {
result.put("containsTag", true);
result.put("theme_id", activityTheme.getId());// 设置所属主题id
result.put("theme", activityTheme.getTheme());// 设置所属主题
} else {
result.put("containsTag", false);
}
} else {
result.put("containsTag", false);
}
return result;
}
/**
* 获取集团是否过滤话题配置
*
* @param configGroupId 所属配置组id
* @return 集团是否过滤话题配置
*/
private ValidConfigNew getIsFilterTagsConfig(Long configGroupId) {
return validConfigNewService
.queryList(configGroupId, 3)
.getDetails()
.stream()
.map(ValidConfigNewVo::toDB)
.findFirst()
.orElse(null);
}
/**
* 获取集团活动主题
*
* @param groupId 集团id
* @param shopId 门店id
* @return 集团活动主题(全集团, 该门店)
*/
private List<ActivityTheme> getGroupActivityTheme(Long groupId, Long shopId, boolean queryLastMonth) {
Date yesterday, firstDayMin;
if (queryLastMonth) {
yesterday = DateUtil.localDateTime2Date(LocalDateTime.of(LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.of(12, 30, 0)));
firstDayMin = DateUtil.localDateTime2Date(LocalDateTime.of(LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN));
} else {
yesterday = new Date(System.currentTimeMillis() - 24 * 3600 * 1000L);
firstDayMin = DateUtil.localDateTime2Date(LocalDateTime.of(LocalDate.now().minusDays(1).withDayOfMonth(1), LocalTime.MIN));
}
List<ActivityTheme> list = activityThemeService.lambdaQuery()
.eq(ActivityTheme::getGroupId, groupId)
.and(wrapper -> wrapper
.le(ActivityTheme::getStartTime, firstDayMin)
.ge(ActivityTheme::getEndTime, firstDayMin)
.or()
.ge(ActivityTheme::getStartTime, firstDayMin)
.le(ActivityTheme::getEndTime, yesterday)
.or()
.le(ActivityTheme::getStartTime, yesterday)
.ge(ActivityTheme::getEndTime, yesterday)
.or()
.le(ActivityTheme::getStartTime, firstDayMin)
.ge(ActivityTheme::getEndTime, yesterday)
).list();
return list.stream()
.distinct()
.filter(item -> {
if (Objects.equals(item.getAllShop(), 1))
return true;
else
return Arrays.stream(item.getShopIds().split(","))
.map(Long::valueOf)
.collect(Collectors.toList())
.contains(shopId)
;
})
.sorted(Comparator.comparing(ActivityTheme::getStartTime).reversed())
.collect(Collectors.toList());
}
/**
* LCS计算话题匹配度
*
* @param setSimilarity 设置匹配度
* @param setTopic 主题设置话题
* @param userTopic 用户话题
* @return 话题匹配度是否合格
*/
private boolean LCSCalcTopicSimilarity(BigDecimal setSimilarity, String setTopic, String userTopic) {
final int length = setTopic.length();
final BigDecimal single = BigDecimal.valueOf(100).divide(BigDecimal.valueOf(length), 4, RoundingMode.HALF_UP);// 单个字占比
int m = setTopic.length();
int n = userTopic.length();
int[][] c = new int[m + 1][n + 1];
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (setTopic.charAt(i - 1) == userTopic.charAt(j - 1))
c[i][j] = c[i - 1][j - 1] + 1;
else
c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]);
}
}
return c[setTopic.length()][userTopic.length()] >= setSimilarity.divideAndRemainder(single)[0].intValue();
}
/**
* 验证封面匹配度
*
* @param theme 主题名
* @param themeId 主题id
* @param fileId 文件id(主题配置指定封面图文件)
* @param coverUrl 用户作品封面图地址
* @param account 账户号
* @param itemId 作品id
* @param type 类型(1:视频, 2:直播)
* @return 封面图匹配度
* @throws Exception
*/
public BigDecimal validCover(String theme, Long themeId, String fileId, String coverUrl, String account, String itemId, Integer type) throws Exception {
int[][] imagePixArr1;// 图片1的像素数组
int[][] imagePixArr2;// 图片2的像素数组
File imageFile1 = this.findLocalCacheImageTheme(/*theme,*/ themeId, fileId);
imagePixArr1 = ImageUtils.readImagePixel(imageFile1);
File imageFile2;
File localCacheImage = this.findLocalCacheImageUser(/*theme,*/ themeId, account, itemId, type);
if (Objects.nonNull(localCacheImage)) {
imageFile2 = localCacheImage;
} else {
imageFile2 = ImageUtils.convertFileByUrl(coverUrl, itemId);
String tempDir = common.getActivityThemeCoverDir();
File file = new File(ImageUtils.modifyResolution1(imageFile2.getPath(),
tempDir + "activityTheme" + File.separator + themeId /*+ "#" + theme*/ + File.separator + account + File.separator + (Objects.equals(type, 1) ? "video" : "live"),
"fw_theme_cover_" + itemId,
512, 512));
if (imageFile2.delete()) {
imageFile2 = file;
}
}
imagePixArr2 = ImageUtils.readImagePixel(imageFile2);
String msg = String.format("\n 主题id: %s \n 主题名: %s\n 设置封面文件id: %s\n 账户号: %s\n %s%s\n", themeId, theme, fileId, account, Objects.equals(type, 1) ? "短视频id: " : "直播间号: ", itemId);
return BigDecimal.valueOf(ImageUtils.calcSimilarity(ImageUtils.getFingerprint(imagePixArr1), ImageUtils.getFingerprint(imagePixArr2), msg));
}
/**
* 获取本地已下载的封面图(用户作品封面图)
*
* @param theme 主题名
* @param themeId 主题id
* @param account 账号
* @param itemId 视频id/直播间id
* @param type 1:视频, 2:直播
*/
private File findLocalCacheImageUser(/*String theme,*/ Long themeId, String account, String itemId, Integer type) {
String tempDir = common.getActivityThemeCoverDir();
File file = new File(tempDir + File.separator + "activityTheme" + File.separator + themeId /*+ "#" + theme*/ + File.separator + account + File.separator + (Objects.equals(type, 1) ? "video" : "live"));
if (!file.exists()) {
boolean mkdirs = file.mkdirs();
if (mkdirs)
return null;
}
File imageFile = null;
for (File item : Objects.requireNonNull(file.listFiles())) {
if (item.getName().startsWith("fw_theme_cover_" + itemId)) {
imageFile = item;
break;
}
}
return imageFile;
}
/**
* 获取本地已下载的主题背景图(主题预设封面图)
*
* @param theme 主题名
* @param themeId 主题id
* @param fileId 文件id(主题配置指定封面图文件)
*/
private File findLocalCacheImageTheme(/*String theme,*/ Long themeId, String fileId) {
String tempDir = common.getActivityThemeCoverDir();
File file = new File(tempDir + "activityTheme" + File.separator + themeId /*+ "#" + theme*/ + File.separator + "settingCover");
if (!file.exists()) {
boolean mkdirs = file.mkdirs();
if (mkdirs)
return null;
}
File imageFile = null;
for (File item : Objects.requireNonNull(file.listFiles())) {
if (item.getName().startsWith("fw_theme_cover_" + fileId)) {
imageFile = item;
break;
}
}
return imageFile;
}
/**
* 获取配置值
*
* @param configGroupId 所属配置组id
* @param type 类型
* @return
*/
private BigDecimal getGlobalConfigValue(Long configGroupId, Integer type) {
final Optional<GlobalConfig> first = Optional.ofNullable(globalConfigService.queryList(configGroupId))
.orElse(new ArrayList<>())
.stream()
.filter(item -> Objects.equals(item.getType(), type))
.findFirst();
BigDecimal rate = BigDecimal.ZERO;
if (first.isPresent())
rate = first.get().getRate();
return rate;
}
}