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; // type=0 湖南,1=内蒙 for (int type = 0; type < 2; type++) { //循环获取数据,每次100条,count不足100条时最后一次循序 while (count < pageLimit) { Map map = conversationContentService.pushChatData(pageSeq, pageLimit,days,type); List list = (List) map.get("list"); pushChatMsg(list,type); if (map == null) { break; } if ((int)map.get("total")<100) { break; } pageSeq= pageSeq + pageLimit+1; } if (type==0){ System.out.println("=========================================湖南获取数据完成====="); }else{ System.out.println("=========================================内蒙获取数据完成====="); } } 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+"秒========================="); } /** * 将数据处理封装插入数据库 */ private void pushChatMsg(List list, Integer type) { 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 map = conversationContentService.pushGetChatName(from, type); 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,type); } StringJoiner stringJoiner = new StringJoiner(","); if (StringUtils.isNotEmpty(tolist)){ //用逗号分割 String[] tolistArr = tolist.split(","); //获取接收者名称 int x=0; for (String tolistName : tolistArr) { Map maps = conversationContentService.pushGetChatName(tolistName,type); 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.setProvinceType(chatMsg.getProvinceType()); 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(),chatMsg.getProvinceType()); if (chatMsgUser == null&&roomName!=null){ ChatMsgUser cmu = new ChatMsgUser(); cmu.setUserId(chatMsg.getRoomid()); cmu.setName(roomName); cmu.setType(3); cmu.setProvinceType(chatMsg.getProvinceType()); 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); } } @RequestMapping(value ="/open/pushMsgName", method = RequestMethod.GET) public void pushMsgName() { List chatMsgUsers = chatMsgUserMapper.selectByProvinceType(0); for (ChatMsgUser chatMsgUser : chatMsgUsers) { String name = chatMsgUser.getName(); if (name.contains("未知")){ System.out.println(name); Map map = conversationContentService.pushGetUserName(chatMsgUser.getUserId(), 0); String str = (String) map.get("name"); if (!str.contains("未知")){ chatMsgUser.setName(str); System.out.println(str); chatMsgUserMapper.update(chatMsgUser); } } } } }