|
|
package com.yohobuy.platform.grass.task;
|
|
|
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.service.model.sns.model.enums.GrassInboxBusinessTypeEnum;
|
|
|
import com.yohobuy.platform.common.service.redis.PlatformRedis;
|
|
|
import com.yohobuy.platform.dal.grass.IGrassArticleBlockDao;
|
|
|
import com.yohobuy.platform.dal.grass.model.GrassArticle;
|
|
|
import com.yohobuy.platform.dal.grass.model.GrassArticleBlock;
|
|
|
import com.yohobuy.platform.grass.service.impl.GrassArticleServiceImpl;
|
|
|
import com.yohobuy.platform.model.grass.request.GrassInBoxAddReq;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
public class TimerArticlePushJob {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
@Autowired
|
|
|
private PlatformRedis platformRedis;
|
|
|
|
|
|
@Resource
|
|
|
private IGrassArticleBlockDao grassArticleBlockDao;
|
|
|
|
|
|
@Autowired
|
|
|
private GrassArticleServiceImpl grassArticleService;
|
|
|
|
|
|
@Resource(name = "core-config-reader")
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
private static final String cacheKey = "yh:platform:cms:grass:TimerArticleList";
|
|
|
|
|
|
@Scheduled(cron = "0 0/10 7-23 * * ? ")
|
|
|
public void run() {
|
|
|
logger.info("TimerArticlePushJob run...");
|
|
|
boolean jobSwitch = configReader.getBoolean("platform.grass.timerArticlePushJob.switch", true);
|
|
|
if(!jobSwitch){
|
|
|
logger.info("TimerArticlePushJob jobSwitch is false");
|
|
|
return;
|
|
|
}
|
|
|
List<GrassArticle> articleList = platformRedis.values(cacheKey, null, GrassArticle.class);
|
|
|
if(CollectionUtils.isEmpty(articleList)){
|
|
|
logger.info("TimerArticlePushJob articleList size is empty");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
List<Integer> needPushArticleIds = articleList.stream().filter(grassArticle -> grassArticle.getCreateTime() <= currentTime).map(GrassArticle::getId).distinct().collect(Collectors.toList());
|
|
|
|
|
|
logger.info("TimerArticlePushJob needPushArticleIds size is {}",needPushArticleIds.size());
|
|
|
//将这次任务需要推送的 从redis移除
|
|
|
if(CollectionUtils.isNotEmpty(needPushArticleIds)){
|
|
|
platformRedis.delete(cacheKey,null,needPushArticleIds);
|
|
|
}
|
|
|
|
|
|
List<GrassArticleBlock> blockList = grassArticleBlockDao.selectByArticleIds(needPushArticleIds);
|
|
|
Map<Integer, List<GrassArticleBlock>> blockMap = blockList.stream().collect(Collectors.groupingBy(GrassArticleBlock::getArticleId));
|
|
|
Map<Integer, List<Integer>> atUserMap = new HashMap<>();
|
|
|
|
|
|
//找出有@用户的文章
|
|
|
for (Map.Entry<Integer, List<GrassArticleBlock>> entry : blockMap.entrySet()) {
|
|
|
List<Integer> uids = grassArticleService.handleUserWithAt(entry.getValue());
|
|
|
if(CollectionUtils.isNotEmpty(uids)){
|
|
|
atUserMap.put(entry.getKey(),uids);
|
|
|
}
|
|
|
}
|
|
|
logger.info("TimerArticlePushJob atUserMap size is {}", atUserMap.size());
|
|
|
|
|
|
Map<Integer, Integer> authorMap = articleList.stream().collect(Collectors.toMap(GrassArticle::getId, GrassArticle::getAuthorUid, (o1, o2) ->o2));
|
|
|
for (Map.Entry<Integer, List<Integer>> entry : atUserMap.entrySet()) {
|
|
|
List<Integer> uidList = entry.getValue();
|
|
|
Integer authorUid = authorMap.get(entry.getKey());
|
|
|
List<GrassInBoxAddReq> addReqList = new ArrayList<>();
|
|
|
uidList.forEach(uid -> {
|
|
|
GrassInBoxAddReq addReq = new GrassInBoxAddReq();
|
|
|
addReq.setBusinessType(GrassInboxBusinessTypeEnum.SYSTEM_AT_USER.getBusinessType());
|
|
|
addReq.setUid(uid);
|
|
|
addReq.setOptUid(authorUid);
|
|
|
addReqList.add(addReq);
|
|
|
});
|
|
|
grassArticleService.sendBatchInbox(addReqList);
|
|
|
}
|
|
|
|
|
|
logger.info("TimerArticlePushJob success lock is {}");
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 将文章加入定时发布待执行任务列表
|
|
|
* @param grassArticle description 保证id,createTime,authorUid字段有值
|
|
|
*/
|
|
|
public void addTimerArticleJob(GrassArticle grassArticle){
|
|
|
platformRedis.put(cacheKey, null, grassArticle.getId(), grassArticle, 30L, TimeUnit.DAYS);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 将文章移除待执行任务列表
|
|
|
* @param articleId
|
|
|
*/
|
|
|
public void removeTimerArticleJob(Integer articleId){
|
|
|
platformRedis.delete(cacheKey, null, articleId);
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|