Commit 913d77fa by 章春雨

Merge branch 'guiq-yesno' into 'master'

小Ray 对话文案

See merge request rays/pcloud-book!341
parents 175df8c6 d79b8465
package com.pcloud.book.book.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class BookGuideDelayDto {
private String userWxId;
private Date expireTime;
private String keyword;
private String ip;
}
package com.pcloud.book.keywords.biz.impl; package com.pcloud.book.keywords.biz.impl;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.book.base.exception.BookBizException; import com.pcloud.book.base.exception.BookBizException;
import com.pcloud.book.book.constant.BookConstant; import com.pcloud.book.book.constant.BookConstant;
import com.pcloud.book.book.dto.BookGuideDelayDto;
import com.pcloud.book.consumer.channel.QrcodeSceneConsr; import com.pcloud.book.consumer.channel.QrcodeSceneConsr;
import com.pcloud.book.consumer.content.ResourceConsr; import com.pcloud.book.consumer.content.ResourceConsr;
import com.pcloud.book.consumer.resource.ProductConsr; import com.pcloud.book.consumer.resource.ProductConsr;
...@@ -41,6 +43,7 @@ import com.pcloud.book.weixinclock.dao.WeixinClockGroupClassifyDao; ...@@ -41,6 +43,7 @@ import com.pcloud.book.weixinclock.dao.WeixinClockGroupClassifyDao;
import com.pcloud.book.weixinclock.dto.WeixinClockDto; import com.pcloud.book.weixinclock.dto.WeixinClockDto;
import com.pcloud.channelcenter.wechat.dto.AccountSettingDto; import com.pcloud.channelcenter.wechat.dto.AccountSettingDto;
import com.pcloud.common.core.aspect.ParamLog; import com.pcloud.common.core.aspect.ParamLog;
import com.pcloud.common.utils.DateUtils;
import com.pcloud.common.utils.ListUtils; import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.cache.redis.JedisClusterUtils; import com.pcloud.common.utils.cache.redis.JedisClusterUtils;
import com.pcloud.common.utils.httpclient.UrlUtils; import com.pcloud.common.utils.httpclient.UrlUtils;
...@@ -64,6 +67,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -64,6 +67,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -158,63 +162,19 @@ public class BookGuideBizImpl implements BookGuideBiz { ...@@ -158,63 +162,19 @@ public class BookGuideBizImpl implements BookGuideBiz {
bookGroupCipherUserDao.insert(bookGroupCipherUser); bookGroupCipherUserDao.insert(bookGroupCipherUser);
//好友引导语 //好友引导语
sendFriendGuide(bookGroupId, agreeAddUserDTO); sendFriendGuide(bookGroupId, agreeAddUserDTO);
//回复关键词引导语
List<SelfRobotKeyword> selfRobotKeywords = selfRobotKeywordDao.getListByBookGroupId(bookGroupId); // TODO 引导语发送后 查询等待时间
if (!ListUtils.isEmpty(selfRobotKeywords)){ int time = 60;
ThreadPoolUtils.SEND_MESSAGE_THREAD_POOL.execute(()->{ // 向缓存中写入相关信息 延迟推送时间
for (SelfRobotKeyword selfRobotKeyword:selfRobotKeywords){ if (envStr.toUpperCase().equals("TEST") && agreeAddUserDTO.getRobotWxId().equals("wxid_zus32xb5ukjn22")
try { || envStr.toUpperCase().equals("UAT") && agreeAddUserDTO.getRobotWxId().equals("wxid_p7gywuxkk89112")
if ("长江作业本".equals(selfRobotKeyword.getKeyword())){ || envStr.toUpperCase().equals("PRO") && agreeAddUserDTO.getRobotWxId().equals("wxid_x8i897ryabo722")) {
Thread.sleep(1000*60*10); List<SelfRobotKeyword> selfRobotKeywords = selfRobotKeywordDao.getListByBookGroupId(bookGroupId);
} BookGuideDelayDto delayDto = new BookGuideDelayDto(agreeAddUserDTO.getUserWxId(), DateUtils.addSecond(new Date(), time), selfRobotKeywords.get(0).getKeyword(), agreeAddUserDTO.getIp());
if (!StringUtil.isEmpty(selfRobotKeyword.getGuide())){ log.info("[向缓存中写入相关信息 延迟推送时间]:{}",JSONObject.toJSONString(delayDto));
SendTextMessageVO vo = new SendTextMessageVO(); // 加好友 清除历史
vo.setContent(selfRobotKeyword.getGuide()); JedisClusterUtils.hdel(agreeAddUserDTO.getRobotWxId()+"_DELAY_PUSH", agreeAddUserDTO.getUserWxId());
vo.setCode(SendMessageTypeEnum.SELF.getCode()); JedisClusterUtils.hset(agreeAddUserDTO.getRobotWxId()+"_DELAY_PUSH", agreeAddUserDTO.getUserWxId(), JSONObject.toJSONString(delayDto));
vo.setWxId(agreeAddUserDTO.getRobotWxId());
vo.setAltId(agreeAddUserDTO.getRobotWxId());
vo.setWxGroupId(agreeAddUserDTO.getUserWxId());
vo.setIp(agreeAddUserDTO.getIp());
WxGroupSDK.sendTextMessage(vo);
log.info("临时demo发送个人号加好友信息"+vo.toString());
}
if ("学前".equals(selfRobotKeyword.getKeyword())
&&envStr.toUpperCase().equals("PRO")&&agreeAddUserDTO.getRobotWxId().equals("wxid_x8i897ryabo722")){
SendGroupInviteVO sendGroupInviteVO = new SendGroupInviteVO();
sendGroupInviteVO.setAltId(agreeAddUserDTO.getRobotWxId());
sendGroupInviteVO.setWxId(agreeAddUserDTO.getUserWxId());
sendGroupInviteVO.setWxGroupId("23084048866@chatroom");
sendGroupInviteVO.setIp(agreeAddUserDTO.getIp());
WxGroupSDK.sendGroupInvite(sendGroupInviteVO);
log.info("临时发送入群连接 sendGroupInviteVO:{}", sendGroupInviteVO);
}
if ("教辅书单".equals(selfRobotKeyword.getKeyword())){
List<SelfRobotKeywordReply> replyList = selfRobotKeywordReplyDao.getListByKeywordId(selfRobotKeyword.getId());
if (ListUtils.isEmpty(replyList)){
continue;
}
for (SelfRobotKeywordReply keywordReply : replyList){
if (ReplyTypeEnum.RESOURCE.value.equals(keywordReply.getType())){
SendFileVO sendFileVO = new SendFileVO();
Map<Long, ResourceDTO> map = resourceConsr.mapByIds(Arrays.asList(keywordReply.getResourceId()));
ResourceDTO resourceDTO = map.get(keywordReply.getResourceId());
if (resourceDTO != null) {
sendFileVO.setFileUrl(resourceDTO.getFileUrl());
sendFileVO.setFileName(resourceDTO.getResourceName());
}
sendFileVO.setIp(agreeAddUserDTO.getIp());
sendFileVO.setAltId(agreeAddUserDTO.getRobotWxId());
sendFileVO.setWxId(agreeAddUserDTO.getUserWxId());
WxGroupSDK.sendFile(sendFileVO);
}
}
}
Thread.sleep(1000*40);
} catch (InterruptedException e) {
log.error("发消息休眠5秒出错", e);
}
}
});
} }
return; return;
} }
......
...@@ -30,5 +30,7 @@ public interface SelfRobotKeywordDao extends BaseDao<SelfRobotKeyword> { ...@@ -30,5 +30,7 @@ public interface SelfRobotKeywordDao extends BaseDao<SelfRobotKeyword> {
List<String> getKeywords(Long bookGroupId); List<String> getKeywords(Long bookGroupId);
SelfRobotKeyword getNextKeyWord(Long bookGroupId, String keyword);
List<SelfRobotKeyword> getListByBookGroupIds(List<Long> bookGroupIds); List<SelfRobotKeyword> getListByBookGroupIds(List<Long> bookGroupIds);
} }
...@@ -79,6 +79,14 @@ public class SelfRobotKeywordDaoImpl extends BaseDaoImpl<SelfRobotKeyword> imple ...@@ -79,6 +79,14 @@ public class SelfRobotKeywordDaoImpl extends BaseDaoImpl<SelfRobotKeyword> imple
} }
@Override @Override
public SelfRobotKeyword getNextKeyWord(Long bookGroupId, String keyword) {
Map<String, Object> map = new HashMap<>();
map.put("bookGroupId", bookGroupId);
map.put("keyword", keyword);
return this.getSqlSession().selectOne(this.getStatement("getNextKeyWord"), map);
}
@Override
public List<SelfRobotKeyword> getListByBookGroupIds(List<Long> bookGroupIds) { public List<SelfRobotKeyword> getListByBookGroupIds(List<Long> bookGroupIds) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("bookGroupIds", bookGroupIds); map.put("bookGroupIds", bookGroupIds);
......
package com.pcloud.book.keywords.runner;
import com.alibaba.fastjson.JSONObject;
import com.pcloud.book.book.dto.BookGuideDelayDto;
import com.pcloud.book.consumer.content.ResourceConsr;
import com.pcloud.book.keywords.biz.BookKeywordBiz;
import com.pcloud.book.keywords.dao.SelfRobotKeywordDao;
import com.pcloud.book.keywords.dao.SelfRobotKeywordReplyDao;
import com.pcloud.book.keywords.entity.SelfRobotKeyword;
import com.pcloud.book.keywords.entity.SelfRobotKeywordReply;
import com.pcloud.book.keywords.enums.ReplyTypeEnum;
import com.pcloud.common.utils.DateUtils;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.cache.redis.JedisClusterUtils;
import com.pcloud.contentcenter.resource.dto.ResourceDTO;
import com.sdk.wxgroup.SendFileVO;
import com.sdk.wxgroup.SendGroupInviteVO;
import com.sdk.wxgroup.SendMessageTypeEnum;
import com.sdk.wxgroup.SendTextMessageVO;
import com.sdk.wxgroup.WxGroupSDK;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 项目启动制行的任务
*
* @author Gui_q
* @since 2020年2月3日 12点14分
*/
@Component
@Slf4j
public class KeyWordDelayRunner implements ApplicationRunner {
@Value("${system.env}")
private String envStr;
@Autowired
private SelfRobotKeywordDao selfRobotKeywordDao;
@Autowired
private BookKeywordBiz bookKeywordBiz;
@Autowired
private ResourceConsr resourceConsr;
@Autowired
private SelfRobotKeywordReplyDao selfRobotKeywordReplyDao;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
log.info("[项目启动后 定时检查用户情况]");
if(!JedisClusterUtils.setnx("KEYWORDDELAYRUNNER_LOCK","KEYWORDDELAYRUNNER_LOCK")){
JedisClusterUtils.expire("KEYWORDDELAYRUNNER_LOCK",15*60);
return;
}
Long bookGroupId = 0L;
String wxid = "";
switch (envStr.toUpperCase()) {
case "TEST":
wxid = "wxid_zus32xb5ukjn22";
bookGroupId = 1621L;
break;
case "UAT":
wxid = "wxid_51ff9f099l9a22";
bookGroupId = 449L;
break;
case "PRO":
wxid = "wxid_x8i897ryabo722";
bookGroupId = 23735L;
break;
}
String finalWxid = wxid;
Long finalBookGroupId = bookGroupId;
new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
Map<String, String> friends = JedisClusterUtils.hgetAll(finalWxid+"_DELAY_PUSH");
if (friends==null){
continue;
}
Set<String> userSet = friends.keySet();
for (String userId : userSet) {
try {
BookGuideDelayDto bookGuideDelayDto = JSONObject.parseObject(friends.get(userId), BookGuideDelayDto.class);
if (bookGuideDelayDto.getExpireTime().before(new Date())) {
// 用户超时未答复 主动推送关键词
SelfRobotKeyword selfRobotKeyword = selfRobotKeywordDao.getNextKeyWord(finalBookGroupId, bookGuideDelayDto.getKeyword());
if (selfRobotKeyword==null){
JedisClusterUtils.hdel(finalWxid+"_DELAY_PUSH",userId);
continue;
}
// log.info("finalBookGroupId:{} bookGuideDelayDto:{}", finalBookGroupId, friends.get(userId));
// 用户否定回复 发送引导语
SendTextMessageVO vo = new SendTextMessageVO();
vo.setContent(selfRobotKeyword.getGuide());
vo.setCode(SendMessageTypeEnum.SELF.getCode());
vo.setWxId(bookGuideDelayDto.getUserWxId());
vo.setAltId(finalWxid);
vo.setWxGroupId(bookGuideDelayDto.getUserWxId());
vo.setIp(bookGuideDelayDto.getIp());
WxGroupSDK.sendTextMessage(vo);
if ("学前".equals(selfRobotKeyword.getKeyword())
&& envStr.toUpperCase().equals("PRO") && finalWxid.equals("wxid_x8i897ryabo722")) {
SendGroupInviteVO sendGroupInviteVO = new SendGroupInviteVO();
sendGroupInviteVO.setAltId(finalWxid);
sendGroupInviteVO.setWxId(bookGuideDelayDto.getUserWxId());
sendGroupInviteVO.setWxGroupId("23084048866@chatroom");
sendGroupInviteVO.setIp(bookGuideDelayDto.getIp());
WxGroupSDK.sendGroupInvite(sendGroupInviteVO);
log.info("临时发送入群连接 sendGroupInviteVO:{}", sendGroupInviteVO);
}
if ("教辅书单".equals(selfRobotKeyword.getKeyword())) {
List<SelfRobotKeywordReply> replyList = selfRobotKeywordReplyDao.getListByKeywordId(selfRobotKeyword.getId());
if (!ListUtils.isEmpty(replyList)) {
for (SelfRobotKeywordReply keywordReply : replyList) {
if (ReplyTypeEnum.RESOURCE.value.equals(keywordReply.getType())) {
SendFileVO sendFileVO = new SendFileVO();
Map<Long, ResourceDTO> map = resourceConsr.mapByIds(Arrays.asList(keywordReply.getResourceId()));
ResourceDTO resourceDTO = map.get(keywordReply.getResourceId());
if (resourceDTO != null) {
sendFileVO.setFileUrl(resourceDTO.getFileUrl());
sendFileVO.setFileName(resourceDTO.getResourceName());
}
sendFileVO.setIp(bookGuideDelayDto.getIp());
sendFileVO.setAltId(finalWxid);
sendFileVO.setWxId(bookGuideDelayDto.getUserWxId());
WxGroupSDK.sendFile(sendFileVO);
}
}
}
}
int time = 60;
if ("背景音乐".equals(selfRobotKeyword.getKeyword())){
time = 600;
}
// 更新缓存中的关键词
BookGuideDelayDto delayDto = BookGuideDelayDto.builder().userWxId(bookGuideDelayDto.getUserWxId())
.expireTime(DateUtils.addSecond(new Date(), time))
.keyword(selfRobotKeyword.getKeyword())
.ip(bookGuideDelayDto.getIp()).build();
JedisClusterUtils.hset(finalWxid + "_DELAY_PUSH", bookGuideDelayDto.getUserWxId(), JSONObject.toJSONString(delayDto));
}
}catch (Exception e){
continue;
}
}
} catch (Exception e) {
// log.info("[KeyWordDelayRunner] 关键词超时推送 失败",e);
}
}
}).start();
}
}
...@@ -93,6 +93,14 @@ ...@@ -93,6 +93,14 @@
order by seq_num order by seq_num
</select> </select>
<select id="getNextKeyWord" parameterType="map" resultMap="BaseResultMap">
SELECT * FROM self_robot_keyword
WHERE book_group_id=#{bookGroupId}
AND seq_num > (SELECT seq_num FROM self_robot_keyword WHERE keyword = #{keyword} AND book_group_id=#{bookGroupId} LIMIT 1)
ORDER BY seq_num
LIMIT 1
</select>
<!--根据社群码和关键词获取--> <!--根据社群码和关键词获取-->
<select id="getByBookGroupIdAndKeyword" parameterType="map" resultMap="BaseResultMap"> <select id="getByBookGroupIdAndKeyword" parameterType="map" resultMap="BaseResultMap">
select <include refid="Base_Column_List"/> select <include refid="Base_Column_List"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment