Authored by 张帅

添加定时发布文章推送任务

@@ -913,7 +913,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService { @@ -913,7 +913,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService {
913 } 913 }
914 } 914 }
915 915
916 - private void sendBatchInbox(List<GrassInBoxAddReq> reqs){ 916 + public void sendBatchInbox(List<GrassInBoxAddReq> reqs){
917 try { 917 try {
918 logger.info("before gateway.addBatchGrassInBox.addReqList size is {}", reqs.size()); 918 logger.info("before gateway.addBatchGrassInBox.addReqList size is {}", reqs.size());
919 String url = apiUrl + "/guang/api/addBatchInBox/?client_type=h5"; 919 String url = apiUrl + "/guang/api/addBatchInBox/?client_type=h5";
@@ -937,7 +937,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService { @@ -937,7 +937,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService {
937 } 937 }
938 for (GrassArticleBlock grassArticleBlock : textBlockList) { 938 for (GrassArticleBlock grassArticleBlock : textBlockList) {
939 String textContent = grassArticleBlock.getContentData(); 939 String textContent = grassArticleBlock.getContentData();
940 - Pattern p = Pattern.compile("@[0-9]*?#"); 940 + Pattern p = Pattern.compile("@[0-9]+#");
941 Matcher m = p.matcher(textContent); 941 Matcher m = p.matcher(textContent);
942 while (m.find()){ 942 while (m.find()){
943 String uidStr = m.group(); 943 String uidStr = m.group();
  1 +package com.yohobuy.platform.grass.task;
  2 +
  3 +import com.yoho.core.config.ConfigReader;
  4 +import com.yoho.service.model.sns.model.enums.GrassInboxBusinessTypeEnum;
  5 +import com.yohobuy.platform.common.service.redis.PlatformRedis;
  6 +import com.yohobuy.platform.dal.grass.IGrassArticleBlockDao;
  7 +import com.yohobuy.platform.dal.grass.model.GrassArticle;
  8 +import com.yohobuy.platform.dal.grass.model.GrassArticleBlock;
  9 +import com.yohobuy.platform.grass.service.impl.GrassArticleServiceImpl;
  10 +import com.yohobuy.platform.model.grass.request.GrassInBoxAddReq;
  11 +import org.apache.commons.collections.CollectionUtils;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.scheduling.annotation.Scheduled;
  16 +import org.springframework.stereotype.Component;
  17 +
  18 +import javax.annotation.Resource;
  19 +import java.util.ArrayList;
  20 +import java.util.HashMap;
  21 +import java.util.List;
  22 +import java.util.Map;
  23 +import java.util.concurrent.TimeUnit;
  24 +import java.util.stream.Collectors;
  25 +
  26 +@Component
  27 +public class TimerArticlePushJob {
  28 +
  29 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  30 +
  31 + @Autowired
  32 + private PlatformRedis platformRedis;
  33 +
  34 + @Resource
  35 + private IGrassArticleBlockDao grassArticleBlockDao;
  36 +
  37 + @Autowired
  38 + private GrassArticleServiceImpl grassArticleService;
  39 +
  40 + @Resource(name = "core-config-reader")
  41 + private ConfigReader configReader;
  42 +
  43 + private static final String cacheKey = "yh:platform:cms:grass:TimerArticleList";
  44 +
  45 + @Scheduled(cron = "0 0/10 7-23 * * ? ")
  46 + public void run() {
  47 + logger.info("TimerArticlePushJob run...");
  48 + boolean jobSwitch = configReader.getBoolean("platform.grass.timerArticlePushJob.switch", true);
  49 + if(!jobSwitch){
  50 + logger.info("TimerArticlePushJob jobSwitch is false");
  51 + return;
  52 + }
  53 + List<GrassArticle> articleList = platformRedis.values(cacheKey, null, GrassArticle.class);
  54 + if(CollectionUtils.isEmpty(articleList)){
  55 + logger.info("TimerArticlePushJob articleList size is empty");
  56 + return;
  57 + }
  58 +
  59 + long currentTime = System.currentTimeMillis();
  60 + List<Integer> needPushArticleIds = articleList.stream().filter(grassArticle -> grassArticle.getCreateTime() <= currentTime).map(GrassArticle::getId).distinct().collect(Collectors.toList());
  61 +
  62 + logger.info("TimerArticlePushJob needPushArticleIds size is {}",needPushArticleIds.size());
  63 + //将这次任务需要推送的 从redis移除
  64 + if(CollectionUtils.isNotEmpty(needPushArticleIds)){
  65 + platformRedis.delete(cacheKey,null,needPushArticleIds);
  66 + }
  67 +
  68 + List<GrassArticleBlock> blockList = grassArticleBlockDao.selectByArticleIds(needPushArticleIds);
  69 + Map<Integer, List<GrassArticleBlock>> blockMap = blockList.stream().collect(Collectors.groupingBy(GrassArticleBlock::getArticleId));
  70 + Map<Integer, List<Integer>> atUserMap = new HashMap<>();
  71 +
  72 + //找出有@用户的文章
  73 + for (Map.Entry<Integer, List<GrassArticleBlock>> entry : blockMap.entrySet()) {
  74 + List<Integer> uids = grassArticleService.handleUserWithAt(entry.getValue());
  75 + if(CollectionUtils.isNotEmpty(uids)){
  76 + atUserMap.put(entry.getKey(),uids);
  77 + }
  78 + }
  79 + logger.info("TimerArticlePushJob atUserMap size is {}", atUserMap.size());
  80 +
  81 + Map<Integer, Integer> authorMap = articleList.stream().collect(Collectors.toMap(GrassArticle::getId, GrassArticle::getAuthorUid, (o1, o2) ->o2));
  82 + for (Map.Entry<Integer, List<Integer>> entry : atUserMap.entrySet()) {
  83 + List<Integer> uidList = entry.getValue();
  84 + Integer authorUid = authorMap.get(entry.getKey());
  85 + List<GrassInBoxAddReq> addReqList = new ArrayList<>();
  86 + uidList.forEach(uid -> {
  87 + GrassInBoxAddReq addReq = new GrassInBoxAddReq();
  88 + addReq.setBusinessType(GrassInboxBusinessTypeEnum.SYSTEM_AT_USER.getBusinessType());
  89 + addReq.setUid(uid);
  90 + addReq.setOptUid(authorUid);
  91 + addReqList.add(addReq);
  92 + });
  93 + grassArticleService.sendBatchInbox(addReqList);
  94 + }
  95 +
  96 + logger.info("TimerArticlePushJob success lock is {}");
  97 + }
  98 +
  99 +
  100 + /**
  101 + * 将文章加入定时发布待执行任务列表
  102 + * @param grassArticle description 保证id,createTime,authorUid字段有值
  103 + */
  104 + public void addTimerArticleJob(GrassArticle grassArticle){
  105 + platformRedis.put(cacheKey, null, grassArticle.getId(), grassArticle, 30L, TimeUnit.DAYS);
  106 + }
  107 +
  108 +
  109 + /**
  110 + * 将文章移除待执行任务列表
  111 + * @param articleId
  112 + */
  113 + public void removeTimerArticleJob(Integer articleId){
  114 + platformRedis.delete(cacheKey, null, articleId);
  115 + }
  116 +}