Bladeren bron

发起对话回传 方式修改

chenbiao 2 jaren geleden
bovenliggende
commit
ca9303bd56

+ 7 - 0
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -809,6 +809,13 @@ public class RedisService {
         USER_DISTRIBUTE_ADD_KEY("user_distribute_add_key:%s", "分销用户新增key", 30l),
 
         CORP_CUSTOMER_ACQUISTION_CLICKID("corp:customer:post:%s","巨量投放,获客链接 跳转存储点击id",60*60*24),
+
+        CORP_CUSTOMER_ACQUISTION_START_CHAT("corp:customer:post:start_chat","获客链接 发起回话存储点击id",60*60*24),
+
+        CORP_CUSTOMER_ACQUISTION_START_CHAT_COUNT("corp:customer:post:start_chat:count:%s","获客链接 发起回话回传计数",60*60*24),
+
+        CORP_CUSTOMER_ACQUISTION_START_CHAT_SET("corp:customer:post:start_chat_set","获客链接 发起回话待回传set",60*60*24),
+
         //获客链接 到企业微信内将平台标识与unionId 对应
         CORP_CUSTOMER_ACQUISTION_UNIONID_CLICKID("corp:customer:unionId:%s","unionid 对应的clickId",60*60*24),
 

+ 9 - 56
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpEventActionServiceImpl.java

@@ -575,68 +575,15 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 		//外部联系人id
 		String externalUserId = message.getExternalUserId();
 		log.info("监听微信客户:{}发起会话事件", externalUserId);
-		try {
-			//等待3s
-			Thread.sleep(3000);
-		} catch (InterruptedException e) {
-		}
+
 		//获客链接id
 		String linkId = message.getLinkId();
-		CorpUserAuth corpUserAuth = corpUserAuthMapper.selectOne(Wrappers.lambdaQuery(CorpUserAuth.class)
-				.eq(CorpUserAuth::getExternalUserid, externalUserId).last("limit 1"));
-		String unionId = null;
-		if (corpUserAuth != null) {
-			CorpUser corpUser = corpUserMapper.selectById(corpUserAuth.getCorpUserId());
-			if (corpUser != null) {
-				unionId = corpUser.getUnionid();
-			}
-		}
-		if (StrUtil.isEmpty(unionId)) {
-			return null;
-		}
+		//记录发起对话事件
 		CorpCustomerAcquisitionLinkPopularize corpCustomerAcquisitionLinkPopularize = corpCustomerAcquisitionLinkPopularizeMapper.selectOne(Wrappers.lambdaQuery(CorpCustomerAcquisitionLinkPopularize.class)
 				.eq(CorpCustomerAcquisitionLinkPopularize::getIsChat, 1)
 				.eq(CorpCustomerAcquisitionLinkPopularize::getCustomerAcquistionLinkId, linkId).last("limit 1"));
 		if (corpCustomerAcquisitionLinkPopularize != null) {
-			//callback
-			String callback = null;
-			String params = redisService.getStr(String.format(RedisService.key.CORP_CUSTOMER_ACQUISTION_UNIONID_CLICKID.getName(), unionId));
-			if (StringUtils.isNotBlank(params)) {
-				JSONObject paramsObj = JSONUtil.parseObj(params);
-				callback = paramsObj.getStr("callback");
-			}
-			//如果需要延后主动回传的做持久化.
-			if (StrUtil.isNotBlank(callback)) {
-				log.info("开始回传客户:{}发起会话事件", externalUserId);
-				//如果为加粉则直接回传
-				if (corpCustomerAcquisitionLinkPopularize.getType() == 1) {
-					//如果需要延后主动回传的做持久化.
-					if (!corpCustomerAcquisitionLinkPopularize.getDelayedPost()) {
-						postData(callback, corpCustomerAcquisitionLinkPopularize.getEvent());
-					}
-				} else {
-					//深度转化 前置加粉事件回传.
-					if (StringUtils.isNotBlank(corpCustomerAcquisitionLinkPopularize.getAddEvent())) {
-						postData(callback, corpCustomerAcquisitionLinkPopularize.getAddEvent());
-					}
-				}
-				// 是否延后回传都记录,只看是否直接回传.
-				CorpCustomerAcquistionCallback corpCustomerAcquistionCallback =
-						corpCustomerAcquistionCallbackMapper.selectOne(Wrappers.lambdaQuery(CorpCustomerAcquistionCallback.class).eq(CorpCustomerAcquistionCallback::getCallback, callback).eq(CorpCustomerAcquistionCallback::getUnionId, unionId));
-				if (corpCustomerAcquistionCallback == null) {
-					corpCustomerAcquistionCallback = new CorpCustomerAcquistionCallback();
-				}
-				corpCustomerAcquistionCallback.setCallback(callback);
-				corpCustomerAcquistionCallback.setEvent(corpCustomerAcquisitionLinkPopularize.getEvent());
-				corpCustomerAcquistionCallback.setUnionId(unionId);
-				corpCustomerAcquistionCallback.setStatus(!corpCustomerAcquisitionLinkPopularize.getDelayedPost());
-				if (corpCustomerAcquistionCallback.getId() != null) {
-					corpCustomerAcquistionCallbackMapper.updateById(corpCustomerAcquistionCallback);
-				} else {
-					corpCustomerAcquistionCallbackMapper.insert(corpCustomerAcquistionCallback);
-				}
-
-			}
+			redisService.sSet(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT_SET.getEnvName(), externalUserId);
 		}
 		return null;
 	}
@@ -1229,6 +1176,12 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
 							corpCustomerAcquistionCallbackMapper.insert(corpCustomerAcquistionCallback);
 						}
 					}
+
+					//是否为对话 回传
+					if (corpCustomerAcquisitionLinkPopularize.getIsChat()) {
+						jsonObject.putOpt("event", corpCustomerAcquisitionLinkPopularize.getAddEvent());
+						redisService.hset(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT.getEnvName(), unionId, jsonObject.toString(), RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT.getTimeout());
+					}
 				}
 			}
 		}

+ 80 - 0
netflix-service/src/main/java/com/cyksj/task/CorpChatScheduler.java

@@ -0,0 +1,80 @@
+package com.cyksj.task;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.util.StringUtil;
+import com.cyksj.mapper.corp.CorpUserAuthMapper;
+import com.cyksj.mapper.corp.CorpUserMapper;
+import com.cyksj.model.entity.CorpUser;
+import com.cyksj.model.entity.CorpUserAuth;
+import com.cyksj.model.entity.OrderDonPost;
+import com.cyksj.redis.RedisService;
+import com.cyksj.service.corp.WxCorpOps;
+import com.cyksj.service.corp.msg.CorpEventActionService;
+import com.cyksj.service.order.OrderDonPostService;
+import com.ejlchina.searcher.BeanSearcher;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * @author chan
+ * @date 2024-04-29 12:14
+ */
+@Component
+@Slf4j
+@RequiredArgsConstructor
+public class CorpChatScheduler {
+
+	private final CorpUserMapper corpUserMapper;
+
+	private final CorpUserAuthMapper corpUserAuthMapper;
+
+	private final RedisService redisService;
+
+	private final OrderDonPostService orderDonPostService;
+
+	/**
+	 * 客服业绩数据统计
+	 */
+	@Scheduled(cron = "5 0/2 * * * ? *")
+	public void chatPost() {
+		Set<Object> chatRecord = redisService.sGet(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT_SET.getEnvName());
+		log.info("发起对话回传. size: {}", chatRecord.size());
+		chatRecord.forEach((key) -> {
+			CorpUserAuth corpUserAuth = corpUserAuthMapper.selectOne(Wrappers.lambdaQuery(CorpUserAuth.class)
+					.eq(CorpUserAuth::getExternalUserid, key).last("limit 1"));
+			String unionId = null;
+			if (corpUserAuth != null) {
+				CorpUser corpUser = corpUserMapper.selectById(corpUserAuth.getCorpUserId());
+				if (corpUser != null) {
+					unionId = corpUser.getUnionid();
+				}
+			}
+			if (StrUtil.isNotBlank(unionId)) {
+				String json = StringUtil.getString(redisService.hget(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT.getEnvName(), StringUtil.getString(key)));
+				if (StringUtil.isNotBlank(json)) {
+					JSONObject jsonObject = JSONUtil.parseObj(json);
+					String callback = jsonObject.getStr("callback");
+					try {
+						orderDonPostService.post(OrderDonPost.Type.OE.getType(), callback, Optional.ofNullable(jsonObject.getStr("event")).orElse("customer_effective"));
+						redisService.setRemove(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT_SET.getEnvName(), key);
+					} catch (Exception e) {
+						log.error("发起对话回传失败. {}", e.getMessage());
+					}
+				}
+			}else {
+				Long count = redisService.incr(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT_COUNT.getNameFormat(key), 1L);
+				if(count > 3){
+					redisService.setRemove(RedisService.key.CORP_CUSTOMER_ACQUISTION_START_CHAT_SET.getEnvName(), key);
+				}
+			}
+		});
+	}
+}