| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package com.kede.common.task;
- import com.kede.common.dao.ChatMsgOptimizeMapper;
- import com.kede.common.model.ChatMsg;
- import com.kede.common.model.ChatMsgUser;
- import com.kede.common.utils.DateUtils;
- import com.kede.common.utils.StringUtils;
- import com.kede.wechat.bo.InputChatMsg;
- import com.kede.wechat.service.ConversationContentService;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.StringJoiner;
- @Component
- @RestController
- public class ConversationContentTask {
- @Resource
- private ConversationContentService conversationContentService;
- @Resource
- private com.kede.common.dao.ChatMsgMapper chatMsgMapper;
- @Resource
- private com.kede.common.dao.ChatMsgUserMapper chatMsgUserMapper;
- @Resource
- private ChatMsgOptimizeMapper chatMsgOptimizeMapper;
- /**
- * 获取数据间隔时间
- * 例如:-2 表示当前日期减二,2025-06-24 结果就是从6-22开始到现在就是三天的数据
- */
- static final int days=-2;
- /**
- * 获取企业微信聊天记录
- */
- //每天下午六点执行
- @Scheduled(cron = "0 0 18 * * ?")
- @RequestMapping(value ="/open/pushMsg", method = RequestMethod.GET)
- public void pushMsg() {
- long startTime = System.currentTimeMillis();
- Integer pageSeq = 0;
- Integer pageLimit = 100;
- Integer count = 0;
- //循环获取数据,每次100条,count不足100条时最后一次循序
- while (count < pageLimit) {
- Map<String, Object> map = conversationContentService.pushChatData(pageSeq, pageLimit,days);
- List<InputChatMsg> list = (List<InputChatMsg>) map.get("list");
- pushChatMsg(list);
- if (map == null) {
- break;
- }
- if ((int)map.get("total")<100) {
- break;
- }
- pageSeq= pageSeq + pageLimit+1;
- }
- long endTime = System.currentTimeMillis();
- //输出耗时计算出耗时分钟
- long time = (endTime - startTime)/1000;
- //换算成分钟
- long f = time / 60;
- long m = time % 60;
- Date earlyToday = DateUtils.getEarlyToday();
- //按当天零点算,减去x天,即=-1的时候取2天数据,今天与昨天
- Date date = DateUtils.getDateBefore(earlyToday, days);
- chatMsgOptimizeMapper.deleteFromTime(date);
- System.out.println("=========================================获取数据完成=====耗时"+f+"分"+m+"秒=========================");
- }
- /**
- * 将数据处理封装插入数据库
- * @param list
- */
- private void pushChatMsg(List<InputChatMsg> list) {
- int index=0;
- //遍历数据 迭代获取数据写入数据库
- for (InputChatMsg chatMsg : list) {
- index++;
- System.out.println("===================第"+index+"条数据\n");
- String from = chatMsg.getFrom();
- //获取发送这名称
- // System.out.println("====>"+chatMsg);
- //如果是同意聊天,删除之前存储的未知名称,获取新的名称
- if (chatMsg.getAction().equals("agree")){
- chatMsgUserMapper.deleteByUserId(from);
- }
- Map<String, Object> map = conversationContentService.pushGetChatName(from);
- String chatName=map.get("name").toString();
- String tolist = chatMsg.getTolist();
- String roomName=null;
- //判定是否有群编号,判定是单聊还是群聊,群聊获取群名称
- if (StringUtils.isNotEmpty(chatMsg.getRoomid())){
- ChatMsg msg = new ChatMsg();
- msg.setRoomid(chatMsg.getRoomid());
- msg.setMsgid(chatMsg.getMsgid());
- roomName = conversationContentService.pushGetRoomName(msg);
- }
- StringJoiner stringJoiner = new StringJoiner(",");
- if (StringUtils.isNotEmpty(tolist)){
- //用逗号分割
- String[] tolistArr = tolist.split(",");
- //获取接收者名称
- int x=0;
- for (String tolistName : tolistArr) {
- Map<String, Object> maps = conversationContentService.pushGetChatName(tolistName);
- x= (int) maps.get("code");
- String name=maps.get("name").toString();
- if (name != null){
- stringJoiner.add(name);
- }
- }
- }
- ChatMsg in= new ChatMsg();
- in.setMsgid(chatMsg.getMsgid());
- in.setActionType(chatMsg.getAction());
- in.setFromId(chatMsg.getFrom());
- in.setFromName(chatName);
- in.setTolist(tolist);
- in.setTolistName(stringJoiner.toString());
- in.setRoomid(chatMsg.getRoomid());
- if (StringUtils.isNotBlank(in.getRoomid())){
- in.setType(1);
- }else{
- in.setType(0);
- }
- in.setRoomName(roomName);
- if (StringUtils.isNotBlank(chatMsg.getRoomid())){
- ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(chatMsg.getRoomid());
- if (chatMsgUser == null&&roomName!=null){
- ChatMsgUser cmu = new ChatMsgUser();
- cmu.setUserId(chatMsg.getRoomid());
- cmu.setName(roomName);
- cmu.setType(3);
- cmu.setCreateTime(new Date());
- chatMsgUserMapper.insert(cmu);
- }
- }
- in.setMsgtime(chatMsg.getMsgtime());
- in.setMsgtype(chatMsg.getMsgtype());
- in.setMsgurl(chatMsg.getMsgurl());
- in.setContent(chatMsg.getContent());
- in.setCreateTime(new Date());
- chatMsgMapper.insert(in);
- }
- }
- }
|