ConversationContentTask.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.kede.common.task;
  2. import com.kede.common.dao.ChatMsgOptimizeMapper;
  3. import com.kede.common.model.ChatMsg;
  4. import com.kede.common.model.ChatMsgUser;
  5. import com.kede.common.utils.DateUtils;
  6. import com.kede.common.utils.StringUtils;
  7. import com.kede.wechat.bo.InputChatMsg;
  8. import com.kede.wechat.service.ConversationContentService;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import javax.annotation.Resource;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.StringJoiner;
  19. @Component
  20. @RestController
  21. public class ConversationContentTask {
  22. @Resource
  23. private ConversationContentService conversationContentService;
  24. @Resource
  25. private com.kede.common.dao.ChatMsgMapper chatMsgMapper;
  26. @Resource
  27. private com.kede.common.dao.ChatMsgUserMapper chatMsgUserMapper;
  28. @Resource
  29. private ChatMsgOptimizeMapper chatMsgOptimizeMapper;
  30. /**
  31. * 获取数据间隔时间
  32. * 例如:-2 表示当前日期减二,2025-06-24 结果就是从6-22开始到现在就是三天的数据
  33. */
  34. static final int days=-2;
  35. /**
  36. * 获取企业微信聊天记录
  37. */
  38. //每天下午六点执行
  39. @Scheduled(cron = "0 0 18 * * ?")
  40. @RequestMapping(value ="/open/pushMsg", method = RequestMethod.GET)
  41. public void pushMsg() {
  42. long startTime = System.currentTimeMillis();
  43. Integer pageSeq = 0;
  44. Integer pageLimit = 100;
  45. Integer count = 0;
  46. //循环获取数据,每次100条,count不足100条时最后一次循序
  47. while (count < pageLimit) {
  48. Map<String, Object> map = conversationContentService.pushChatData(pageSeq, pageLimit,days);
  49. List<InputChatMsg> list = (List<InputChatMsg>) map.get("list");
  50. pushChatMsg(list);
  51. if (map == null) {
  52. break;
  53. }
  54. if ((int)map.get("total")<100) {
  55. break;
  56. }
  57. pageSeq= pageSeq + pageLimit+1;
  58. }
  59. long endTime = System.currentTimeMillis();
  60. //输出耗时计算出耗时分钟
  61. long time = (endTime - startTime)/1000;
  62. //换算成分钟
  63. long f = time / 60;
  64. long m = time % 60;
  65. Date earlyToday = DateUtils.getEarlyToday();
  66. //按当天零点算,减去x天,即=-1的时候取2天数据,今天与昨天
  67. Date date = DateUtils.getDateBefore(earlyToday, days);
  68. chatMsgOptimizeMapper.deleteFromTime(date);
  69. System.out.println("=========================================获取数据完成=====耗时"+f+"分"+m+"秒=========================");
  70. }
  71. /**
  72. * 将数据处理封装插入数据库
  73. * @param list
  74. */
  75. private void pushChatMsg(List<InputChatMsg> list) {
  76. int index=0;
  77. //遍历数据 迭代获取数据写入数据库
  78. for (InputChatMsg chatMsg : list) {
  79. index++;
  80. System.out.println("===================第"+index+"条数据\n");
  81. String from = chatMsg.getFrom();
  82. //获取发送这名称
  83. // System.out.println("====>"+chatMsg);
  84. //如果是同意聊天,删除之前存储的未知名称,获取新的名称
  85. if (chatMsg.getAction().equals("agree")){
  86. chatMsgUserMapper.deleteByUserId(from);
  87. }
  88. Map<String, Object> map = conversationContentService.pushGetChatName(from);
  89. String chatName=map.get("name").toString();
  90. String tolist = chatMsg.getTolist();
  91. String roomName=null;
  92. //判定是否有群编号,判定是单聊还是群聊,群聊获取群名称
  93. if (StringUtils.isNotEmpty(chatMsg.getRoomid())){
  94. ChatMsg msg = new ChatMsg();
  95. msg.setRoomid(chatMsg.getRoomid());
  96. msg.setMsgid(chatMsg.getMsgid());
  97. roomName = conversationContentService.pushGetRoomName(msg);
  98. }
  99. StringJoiner stringJoiner = new StringJoiner(",");
  100. if (StringUtils.isNotEmpty(tolist)){
  101. //用逗号分割
  102. String[] tolistArr = tolist.split(",");
  103. //获取接收者名称
  104. int x=0;
  105. for (String tolistName : tolistArr) {
  106. Map<String, Object> maps = conversationContentService.pushGetChatName(tolistName);
  107. x= (int) maps.get("code");
  108. String name=maps.get("name").toString();
  109. if (name != null){
  110. stringJoiner.add(name);
  111. }
  112. }
  113. }
  114. ChatMsg in= new ChatMsg();
  115. in.setMsgid(chatMsg.getMsgid());
  116. in.setActionType(chatMsg.getAction());
  117. in.setFromId(chatMsg.getFrom());
  118. in.setFromName(chatName);
  119. in.setTolist(tolist);
  120. in.setTolistName(stringJoiner.toString());
  121. in.setRoomid(chatMsg.getRoomid());
  122. if (StringUtils.isNotBlank(in.getRoomid())){
  123. in.setType(1);
  124. }else{
  125. in.setType(0);
  126. }
  127. in.setRoomName(roomName);
  128. if (StringUtils.isNotBlank(chatMsg.getRoomid())){
  129. ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(chatMsg.getRoomid());
  130. if (chatMsgUser == null&&roomName!=null){
  131. ChatMsgUser cmu = new ChatMsgUser();
  132. cmu.setUserId(chatMsg.getRoomid());
  133. cmu.setName(roomName);
  134. cmu.setType(3);
  135. cmu.setCreateTime(new Date());
  136. chatMsgUserMapper.insert(cmu);
  137. }
  138. }
  139. in.setMsgtime(chatMsg.getMsgtime());
  140. in.setMsgtype(chatMsg.getMsgtype());
  141. in.setMsgurl(chatMsg.getMsgurl());
  142. in.setContent(chatMsg.getContent());
  143. in.setCreateTime(new Date());
  144. chatMsgMapper.insert(in);
  145. }
  146. }
  147. }