|
@@ -1,10 +1,12 @@
|
|
|
package com.tzld.piaoquan.api.service.strategy.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.tzld.piaoquan.api.common.enums.ReplyStrategyServiceEnum;
|
|
|
import com.tzld.piaoquan.api.component.TouLiuHttpClient;
|
|
|
import com.tzld.piaoquan.api.dao.mapper.AlgGhAutoreplyVideoRankDataMapper;
|
|
|
+import com.tzld.piaoquan.growth.common.common.enums.StrategyStatusEnum;
|
|
|
import com.tzld.piaoquan.growth.common.dao.mapper.CgiReplyBucketDataMapper;
|
|
|
import com.tzld.piaoquan.growth.common.dao.mapper.GhDetailMapper;
|
|
|
import com.tzld.piaoquan.api.model.bo.*;
|
|
@@ -43,6 +45,8 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
@Value("${bucketStrategyConfig:{}}")
|
|
|
private String bucketStrategyConfigV2;
|
|
|
|
|
|
+ private static final String MANUAL = "manual";
|
|
|
+
|
|
|
/**
|
|
|
* 自动回复使用小程序Id
|
|
|
*/
|
|
@@ -65,7 +69,7 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
@Override
|
|
|
public ReplyBucketData getResult(BucketDataParam bucketDataParam) {
|
|
|
// 0 获取策略key
|
|
|
- JSONObject bucketStrategyConfigJsonObject = getStrategyConfig(bucketDataParam.getGhId());
|
|
|
+ JSONObject bucketStrategyConfigJsonObject = getStrategyConfig(bucketDataParam);
|
|
|
Set<String> keyedSet = bucketStrategyConfigJsonObject.keySet();
|
|
|
// 1 处理文章--算法引擎--排序文章数据
|
|
|
// getWenzhangData();
|
|
@@ -75,12 +79,13 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
smallDataCgiReplyList = setSmallPageUrl(smallDataCgiReplyList);
|
|
|
log.info(JSON.toJSONString(smallDataCgiReplyList));
|
|
|
// 3 入库读表
|
|
|
- insertSmallData(smallDataCgiReplyList, keyedSet);
|
|
|
+ insertSmallData(smallDataCgiReplyList, keyedSet, bucketDataParam);
|
|
|
// 4 组装分桶数据
|
|
|
- return getReplyBucketData(bucketStrategyConfigJsonObject, keyedSet, bucketDataParam.getGhId());
|
|
|
+ return getReplyBucketData(bucketStrategyConfigJsonObject, keyedSet, bucketDataParam);
|
|
|
}
|
|
|
|
|
|
- private JSONObject getStrategyConfig(String ghId) {
|
|
|
+ private JSONObject getStrategyConfig(BucketDataParam bucketDataParam) {
|
|
|
+ String ghId = bucketDataParam.getGhId();
|
|
|
JSONObject allStrategyConfigs = JSON.parseObject(bucketStrategyConfigV2);
|
|
|
JSONObject currentGhIdStrategyConfig = null;
|
|
|
if (allStrategyConfigs.containsKey(ghId)) {
|
|
@@ -95,10 +100,34 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
if (!currentGhIdStrategyConfig.containsKey("base")) {
|
|
|
throw new RuntimeException("Strategy config does not have manual base");
|
|
|
}
|
|
|
+ if (Objects.equals(StrategyStatusEnum.MIXED_STRATEGY.status, bucketDataParam.getStrategyStatus())) {
|
|
|
+ String rate = bucketDataParam.getRate();
|
|
|
+ if (StringUtils.isNotEmpty(rate) && JSONArray.isValidArray(rate)) {
|
|
|
+ JSONArray jsonArray = JSONArray.parseArray(rate);
|
|
|
+ currentGhIdStrategyConfig = filterJson(currentGhIdStrategyConfig, jsonArray);
|
|
|
+ currentGhIdStrategyConfig.put(MANUAL, jsonArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
return currentGhIdStrategyConfig;
|
|
|
}
|
|
|
|
|
|
- private ReplyBucketData getReplyBucketData(JSONObject bucketStrategyConfigJsonObject, Set<String> keyedSet, String ghId) {
|
|
|
+
|
|
|
+ //过滤json中 保底组的策略
|
|
|
+ public JSONObject filterJson(JSONObject originalJson, JSONArray excludeArray) {
|
|
|
+ JSONObject newJSONObject = new JSONObject();
|
|
|
+ Set<String> keySet = originalJson.keySet();
|
|
|
+ for (String key : keySet) {
|
|
|
+ JSONArray jsonArray = originalJson.getJSONArray(key);
|
|
|
+ List<Object> collect = jsonArray.stream().filter(e -> !excludeArray.contains(e)).collect(Collectors.toList());
|
|
|
+ if (!CollectionUtils.isEmpty(collect)) {
|
|
|
+ newJSONObject.put(key, collect);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return newJSONObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ReplyBucketData getReplyBucketData(JSONObject bucketStrategyConfigJsonObject, Set<String> keyedSet, BucketDataParam bucketDataParam) {
|
|
|
+ String ghId = bucketDataParam.getGhId();
|
|
|
// 策略小程序数据
|
|
|
ReplyBucketData replyBucketData = new ReplyBucketData();
|
|
|
List<GroupData> groupDataList = new ArrayList<>();
|
|
@@ -138,41 +167,56 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
groupDataList.add(groupData);
|
|
|
}
|
|
|
}
|
|
|
- // 获取人工实验数据
|
|
|
- List<GroupData> groupDataBaseList = touLiuHttpClient.sendPenGongBaseRequest(ghId);
|
|
|
- if (!CollectionUtils.isEmpty(groupDataBaseList)) {
|
|
|
- int baseBucketNum = bucketStrategyConfigJsonObject.getJSONArray("base").size();
|
|
|
- if (groupDataBaseList.size() > baseBucketNum) {
|
|
|
- groupDataBaseList = groupDataBaseList.subList(0, baseBucketNum);
|
|
|
- }
|
|
|
- if (!CollectionUtils.isEmpty(groupDataBaseList)) {
|
|
|
- GroupData groupData = groupDataBaseList.get(0);
|
|
|
- List<MsgData> msgDataList = groupData.getMsgDataList();
|
|
|
- List<MsgData> changwenBase = msgDataList.stream().filter(x -> x.getMsgType().equals(2)).collect(Collectors.toList());
|
|
|
- if (CollectionUtils.isEmpty(changwenBase)) {
|
|
|
- log.error("get base changwenBase is null,data:" + JSON.toJSONString(msgDataList));
|
|
|
+ if (StringUtils.isNotEmpty(bucketDataParam.getContentDetail()) && JSONObject.isValidObject(bucketDataParam.getContentDetail())) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(bucketDataParam.getContentDetail());
|
|
|
+ Integer index = jsonObject.getInteger("index");
|
|
|
+ MsgData msgData = JSONObject.parseObject(bucketDataParam.getContentDetail(), MsgData.class);
|
|
|
+ msgData.setMsgType(2);
|
|
|
+ for (GroupData data : groupDataList) {
|
|
|
+ List<MsgData> msgDataList1 = data.getMsgDataList();
|
|
|
+ if (index != null) {
|
|
|
+ msgDataList1.add(index, msgData);
|
|
|
} else {
|
|
|
- // 策略拼接base数据
|
|
|
- for (GroupData data : groupDataList) {
|
|
|
- List<MsgData> msgDataList1 = data.getMsgDataList();
|
|
|
- MsgData msgData = changwenBase.get(0);
|
|
|
-// msgData.setSort(3);
|
|
|
- msgDataList1.add(msgData);
|
|
|
- }
|
|
|
+ msgDataList1.add(msgData);
|
|
|
}
|
|
|
- // 补充人工数据
|
|
|
- groupDataList.addAll(groupDataBaseList);
|
|
|
- } else {
|
|
|
- log.error("get base data is null,ghId:" + ghId);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ // 获取人工实验数据
|
|
|
+// List<GroupData> groupDataBaseList = touLiuHttpClient.sendPenGongBaseRequest(ghId);
|
|
|
+// if (!CollectionUtils.isEmpty(groupDataBaseList)) {
|
|
|
+// int baseBucketNum = bucketStrategyConfigJsonObject.getJSONArray("base").size();
|
|
|
+// if (groupDataBaseList.size() > baseBucketNum) {
|
|
|
+// groupDataBaseList = groupDataBaseList.subList(0, baseBucketNum);
|
|
|
+// }
|
|
|
+// if (!CollectionUtils.isEmpty(groupDataBaseList)) {
|
|
|
+// GroupData groupData = groupDataBaseList.get(0);
|
|
|
+// List<MsgData> msgDataList = groupData.getMsgDataList();
|
|
|
+// List<MsgData> changwenBase = msgDataList.stream().filter(x -> x.getMsgType().equals(2)).collect(Collectors.toList());
|
|
|
+// if (CollectionUtils.isEmpty(changwenBase)) {
|
|
|
+// log.error("get base changwenBase is null,data:" + JSON.toJSONString(msgDataList));
|
|
|
+// } else {
|
|
|
+// // 策略拼接base数据
|
|
|
+// for (GroupData data : groupDataList) {
|
|
|
+// List<MsgData> msgDataList1 = data.getMsgDataList();
|
|
|
+// MsgData msgData = changwenBase.get(0);
|
|
|
+//// msgData.setSort(3);
|
|
|
+// msgDataList1.add(msgData);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// // 补充人工数据
|
|
|
+// groupDataList.addAll(groupDataBaseList);
|
|
|
+// } else {
|
|
|
+// log.error("get base data is null,ghId:" + ghId);
|
|
|
+// }
|
|
|
+// }
|
|
|
// groupDataList排序
|
|
|
replyBucketData.setGroupList(groupDataList.stream().sorted(Comparator.comparingInt(GroupData::getGroupIndex)).collect(Collectors.toList()));
|
|
|
return replyBucketData;
|
|
|
}
|
|
|
|
|
|
|
|
|
- private void insertSmallData(List<CgiReplyBucketData> smallDataCgiReplyList, Set<String> keyedSet) {
|
|
|
+ private void insertSmallData(List<CgiReplyBucketData> smallDataCgiReplyList, Set<String> keyedSet, BucketDataParam bucketDataParam) {
|
|
|
if (CollectionUtils.isEmpty(smallDataCgiReplyList)) {
|
|
|
return;
|
|
|
}
|
|
@@ -180,14 +224,17 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
if ("base".equals(key)) {
|
|
|
continue;
|
|
|
}
|
|
|
- List<CgiReplyBucketData> collect = smallDataCgiReplyList.stream().filter(x -> x.getStrategy().equals(key)).collect(Collectors.toList());
|
|
|
+ List<CgiReplyBucketData> collect = smallDataCgiReplyList.stream()
|
|
|
+ .filter(x -> x.getStrategy().equals(key))
|
|
|
+ .filter(x -> x.getGhId().equals(bucketDataParam.getGhId()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(collect)) {
|
|
|
log.error("insertSmallData 算法排序数据异常,data:" + JSON.toJSONString(smallDataCgiReplyList));
|
|
|
continue;
|
|
|
}
|
|
|
// 清上个版本的策略数据
|
|
|
CgiReplyBucketDataExample cgiReplyBucketDataExample = new CgiReplyBucketDataExample();
|
|
|
- cgiReplyBucketDataExample.createCriteria().andIsDeleteEqualTo(0).andMsgTypeEqualTo(1).andStrategyEqualTo(key);
|
|
|
+ cgiReplyBucketDataExample.createCriteria().andIsDeleteEqualTo(0).andMsgTypeEqualTo(1).andStrategyEqualTo(key).andGhIdEqualTo(bucketDataParam.getGhId());
|
|
|
List<CgiReplyBucketData> cgiReplyBucketData1 = cgiReplyBucketDataMapper.selectByExample(cgiReplyBucketDataExample);
|
|
|
for (CgiReplyBucketData cgiReplyBucketData : cgiReplyBucketData1) {
|
|
|
cgiReplyBucketData.setIsDelete(1);
|
|
@@ -255,48 +302,122 @@ public class BuckStrategyV1 implements ReplyStrategyService {
|
|
|
|
|
|
private List<CgiReplyBucketData> readStrategyOrderSmallData(Set<String> keyedSet, BucketDataParam bucketDataParam) {
|
|
|
List<CgiReplyBucketData> result = new ArrayList<>();
|
|
|
- for (String key : keyedSet) {
|
|
|
- if ("base".equals(key)) {
|
|
|
- // base作为人工控制
|
|
|
- continue;
|
|
|
+ if (Objects.equals(StrategyStatusEnum.DEFAULT.status, bucketDataParam.getStrategyStatus())) {
|
|
|
+ for (String key : keyedSet) {
|
|
|
+ List<CgiReplyBucketData> defaultData = getDefaultData(bucketDataParam, key);
|
|
|
+ if (!CollectionUtils.isEmpty(defaultData)) {
|
|
|
+ result.addAll(defaultData);
|
|
|
+ }
|
|
|
}
|
|
|
- // 获取最新dt的策略
|
|
|
- String dtVersion = algGhAutoreplyVideoRankDataMapper.selectLatestDtVersionByStrategyKey(key);
|
|
|
- // 判断当前的dtVersion是否已经处理过了
|
|
|
- CgiReplyBucketDataExample cgiReplyBucketDataExample = new CgiReplyBucketDataExample();
|
|
|
- cgiReplyBucketDataExample.createCriteria().andIsDeleteEqualTo(0).andStrategyDtEqualTo(dtVersion).andStrategyEqualTo(key);
|
|
|
- long count = cgiReplyBucketDataMapper.countByExample(cgiReplyBucketDataExample);
|
|
|
- if (count != 0) {
|
|
|
- // 说明已处理过该dtVersion数据
|
|
|
- continue;
|
|
|
+ } else if (Objects.equals(StrategyStatusEnum.STRATEGY.status, bucketDataParam.getStrategyStatus())) {
|
|
|
+ for (String key : keyedSet) {
|
|
|
+ List<CgiReplyBucketData> strategyData = getStrategyData(key);
|
|
|
+ if (!CollectionUtils.isEmpty(strategyData)) {
|
|
|
+ result.addAll(strategyData);
|
|
|
+ }
|
|
|
}
|
|
|
- // 获取最新dt数据
|
|
|
- List<AlgGhAutoreplyVideoRankData> dtVersionStrategyData = getDtVersionStrategyData(key, dtVersion);
|
|
|
- List<Long> videoIds = dtVersionStrategyData.stream().map(AlgGhAutoreplyVideoRankData::getVideoId).collect(Collectors.toList());
|
|
|
- Map<Long, VideoDetail> videoDetailMap = touLiuHttpClient.getVideoDetailRequest(videoIds);
|
|
|
- result.addAll(dtVersionStrategyData.stream().map(x -> {
|
|
|
- CgiReplyBucketData cgiReplyBucketData = new CgiReplyBucketData();
|
|
|
- cgiReplyBucketData.setStrategy(key);
|
|
|
- cgiReplyBucketData.setSort(x.getSort());
|
|
|
- cgiReplyBucketData.setStrategyDt(x.getDtVersion());
|
|
|
- cgiReplyBucketData.setGhId(x.getGhId());
|
|
|
- cgiReplyBucketData.setMsgType(1);
|
|
|
- cgiReplyBucketData.setTitle(x.getTitle());
|
|
|
- VideoDetail videoDetail = videoDetailMap.get(x.getVideoId());
|
|
|
- if (videoDetail != null && StringUtils.isNotEmpty(videoDetail.getCover())) {
|
|
|
- cgiReplyBucketData.setCoverUrl(videoDetail.getCover());
|
|
|
+ } else if (Objects.equals(StrategyStatusEnum.MIXED_STRATEGY.status, bucketDataParam.getStrategyStatus())) {
|
|
|
+ for (String key : keyedSet) {
|
|
|
+ if (key.equals(MANUAL)) {
|
|
|
+ List<CgiReplyBucketData> defaultData = getDefaultData(bucketDataParam, key);
|
|
|
+ if (!CollectionUtils.isEmpty(defaultData)) {
|
|
|
+ result.addAll(defaultData);
|
|
|
+ }
|
|
|
} else {
|
|
|
- cgiReplyBucketData.setCoverUrl(CDN_URL + x.getCoverUrl());
|
|
|
+ List<CgiReplyBucketData> strategyData = getStrategyData(key);
|
|
|
+ if (!CollectionUtils.isEmpty(strategyData)) {
|
|
|
+ result.addAll(strategyData);
|
|
|
+ }
|
|
|
}
|
|
|
- cgiReplyBucketData.setMiniAppId(SMALL_APP_Id);
|
|
|
- cgiReplyBucketData.setMiniVideoId(x.getVideoId());
|
|
|
- return cgiReplyBucketData;
|
|
|
- }).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
}
|
|
|
// 获取最新数据版本
|
|
|
return CollectionUtils.isEmpty(result) ? null : result;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private List<CgiReplyBucketData> getDefaultData(BucketDataParam bucketDataParam, String key) {
|
|
|
+ List<CgiReplyBucketData> result = new ArrayList<>();
|
|
|
+ if (CollectionUtils.isEmpty(bucketDataParam.getVideos())) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ int existNum = 0;
|
|
|
+ for (int i = 0; i < bucketDataParam.getVideos().size(); i++) {
|
|
|
+ int sort = i + 1;
|
|
|
+ Long videoId = bucketDataParam.getVideos().get(i);
|
|
|
+ CgiReplyBucketDataExample cgiReplyBucketDataExample = new CgiReplyBucketDataExample();
|
|
|
+ cgiReplyBucketDataExample.createCriteria().andIsDeleteEqualTo(0).andStrategyEqualTo(key)
|
|
|
+ .andGhIdEqualTo(bucketDataParam.getGhId()).andMiniVideoIdEqualTo(videoId).andSortEqualTo(sort);
|
|
|
+ long count = cgiReplyBucketDataMapper.countByExample(cgiReplyBucketDataExample);
|
|
|
+ if (count > 0) {
|
|
|
+ existNum++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (existNum == bucketDataParam.getVideos().size()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < bucketDataParam.getVideos().size(); i++) {
|
|
|
+ int sort = i + 1;
|
|
|
+ Long videoId = bucketDataParam.getVideos().get(i);
|
|
|
+ Map<Long, VideoDetail> videoDetailMap = touLiuHttpClient.getVideoDetailRequest(bucketDataParam.getVideos());
|
|
|
+ CgiReplyBucketData cgiReplyBucketData = new CgiReplyBucketData();
|
|
|
+ cgiReplyBucketData.setStrategy(key);
|
|
|
+ cgiReplyBucketData.setSort(sort);
|
|
|
+ cgiReplyBucketData.setGhId(bucketDataParam.getGhId());
|
|
|
+ cgiReplyBucketData.setMsgType(1);
|
|
|
+ VideoDetail videoDetail = videoDetailMap.get(videoId);
|
|
|
+ if (videoDetail != null && StringUtils.isNotEmpty(videoDetail.getCover())) {
|
|
|
+ cgiReplyBucketData.setCoverUrl(videoDetail.getCover());
|
|
|
+ }
|
|
|
+ if (videoDetail != null && StringUtils.isNotEmpty(videoDetail.getTitle())) {
|
|
|
+ cgiReplyBucketData.setTitle(videoDetail.getTitle());
|
|
|
+ }
|
|
|
+ cgiReplyBucketData.setMiniAppId(SMALL_APP_Id);
|
|
|
+ cgiReplyBucketData.setMiniVideoId(videoId);
|
|
|
+ result.add(cgiReplyBucketData);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<CgiReplyBucketData> getStrategyData(String key) {
|
|
|
+ List<CgiReplyBucketData> result = new ArrayList<>();
|
|
|
+ // 获取最新dt的策略
|
|
|
+ String dtVersion = algGhAutoreplyVideoRankDataMapper.selectLatestDtVersionByStrategyKey(key);
|
|
|
+ // 判断当前的dtVersion是否已经处理过了
|
|
|
+ CgiReplyBucketDataExample cgiReplyBucketDataExample = new CgiReplyBucketDataExample();
|
|
|
+ cgiReplyBucketDataExample.createCriteria().andIsDeleteEqualTo(0).andStrategyDtEqualTo(dtVersion).andStrategyEqualTo(key);
|
|
|
+ long count = cgiReplyBucketDataMapper.countByExample(cgiReplyBucketDataExample);
|
|
|
+ if (count != 0) {
|
|
|
+ // 说明已处理过该dtVersion数据
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ // 获取最新dt数据
|
|
|
+ List<AlgGhAutoreplyVideoRankData> dtVersionStrategyData = getDtVersionStrategyData(key, dtVersion);
|
|
|
+ List<Long> videoIds = dtVersionStrategyData.stream().map(AlgGhAutoreplyVideoRankData::getVideoId).collect(Collectors.toList());
|
|
|
+ Map<Long, VideoDetail> videoDetailMap = touLiuHttpClient.getVideoDetailRequest(videoIds);
|
|
|
+ result.addAll(dtVersionStrategyData.stream().map(x -> {
|
|
|
+ CgiReplyBucketData cgiReplyBucketData = new CgiReplyBucketData();
|
|
|
+ cgiReplyBucketData.setStrategy(key);
|
|
|
+ cgiReplyBucketData.setSort(x.getSort());
|
|
|
+ cgiReplyBucketData.setStrategyDt(x.getDtVersion());
|
|
|
+ cgiReplyBucketData.setGhId(x.getGhId());
|
|
|
+ cgiReplyBucketData.setMsgType(1);
|
|
|
+ cgiReplyBucketData.setTitle(x.getTitle());
|
|
|
+ VideoDetail videoDetail = videoDetailMap.get(x.getVideoId());
|
|
|
+ if (videoDetail != null && StringUtils.isNotEmpty(videoDetail.getCover())) {
|
|
|
+ cgiReplyBucketData.setCoverUrl(videoDetail.getCover());
|
|
|
+ } else {
|
|
|
+ cgiReplyBucketData.setCoverUrl(CDN_URL + x.getCoverUrl());
|
|
|
+ }
|
|
|
+ cgiReplyBucketData.setMiniAppId(SMALL_APP_Id);
|
|
|
+ cgiReplyBucketData.setMiniVideoId(x.getVideoId());
|
|
|
+ return cgiReplyBucketData;
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
private List<AlgGhAutoreplyVideoRankData> getDtVersionStrategyData(String key, String dtVersion) {
|
|
|
AlgGhAutoreplyVideoRankDataExample algGhAutoreplyVideoRankDataExample = new AlgGhAutoreplyVideoRankDataExample();
|
|
|
algGhAutoreplyVideoRankDataExample.createCriteria().andIsDeleteEqualTo(0).andDtVersionEqualTo(dtVersion).andStrategyKeyEqualTo(key);
|