Authored by mlge

种草--马甲点赞修改

... ... @@ -3,11 +3,9 @@ package com.yohobuy.platform.grass.service.impl;
import com.yoho.core.config.ConfigReader;
import com.yohobuy.platform.common.enums.GrassUserTypeEnum;
import com.yohobuy.platform.common.enums.GrassVirtualUserGroupEnum;
import com.yohobuy.platform.dal.grass.IGrassArticleDao;
import com.yohobuy.platform.dal.grass.IGrassUserAchieveDAO;
import com.yohobuy.platform.dal.grass.IGrassUserAttentionDao;
import com.yohobuy.platform.dal.grass.IGrassVirtualUserDao;
import com.yohobuy.platform.dal.grass.*;
import com.yohobuy.platform.dal.grass.model.GrassArticle;
import com.yohobuy.platform.dal.grass.model.GrassArticlePraise;
import com.yohobuy.platform.dal.grass.model.GrassUserAchieve;
import com.yohobuy.platform.dal.grass.model.GrassUserAttention;
import com.yohobuy.platform.grass.service.IGrassVirtualService;
... ... @@ -41,6 +39,9 @@ public class GrassUserVirtualImpl implements IGrassVirtualService{
private IGrassUserAttentionDao grassUserAttentionDao;
@Autowired
private IGrassArticlePraiseDao grassArticlePraiseDao;
@Autowired
private ConfigReader configReader;
public static final long INTERVAL = 60 * 60 * 1000l;//1小时
... ... @@ -67,27 +68,71 @@ public class GrassUserVirtualImpl implements IGrassVirtualService{
return ;
}
List<Integer> articleIds = articleList.stream().map(GrassArticle::getId).collect(Collectors.toList());
List<Integer> articleIds = articleList.stream().map(GrassArticle::getId).distinct().collect(Collectors.toList());
logger.info("addVirtualPraise, update articleList size={}, articleIds={} ", articleList.size(), articleIds);
//点赞阈值
String threshold = configReader.getString("platform.grass.virtualpraise.threshold","");
String s[] = StringUtils.split(threshold,",");
int min = Integer.valueOf(s[0]);
int max = Integer.valueOf(s[1]);
//虚拟马甲用户
List<Integer> allVirtualUids = grassVirtualUserDao.selectVirtualsByGroup(GrassVirtualUserGroupEnum.COMMON.getValue());
if(CollectionUtils.isEmpty(allVirtualUids)){
logger.warn("addVirtualPraise no virtual user ");
return;
}
//这些文章已有的点赞明细
List<GrassArticlePraise> praiseList = grassArticlePraiseDao.selectByArticles(articleIds);
Map<Integer,List<Integer>> articlePraiseMap = new HashMap<>();
for(GrassArticlePraise praise : praiseList){
Integer articleId = praise.getArticleId();
if(articlePraiseMap.get(articleId) == null){
articlePraiseMap.put(articleId, new ArrayList<>());
}
articlePraiseMap.get(articleId).add(praise.getUid());
}
Map<Integer,GrassUserAchieve> commonMap = new HashMap<>();
Map<Integer,GrassUserAchieve> guangMap = new HashMap<>();
List<GrassArticlePraise> newPraiseList = new ArrayList<>();
//点赞
for(GrassArticle article : articleList){
//每篇文章分别取随机数
int random = getRandom(min,max);
Integer articleId = article.getId();
int authorUid = article.getAuthorUid();
int authorType = article.getAuthorType() == null ? 1 : article.getAuthorType() ;
//更新文章
//选择虚拟马甲
List<Integer> oldPraiseList = articlePraiseMap.get(articleId);
List<Integer> tempList = new ArrayList<>();
tempList.addAll(allVirtualUids);
if(CollectionUtils.isNotEmpty(oldPraiseList)){
tempList.removeAll(oldPraiseList);
}
Set<Integer> randomUidList = getRandomFans(tempList, min, max);
if(CollectionUtils.isEmpty(randomUidList)){
article.setPraiseCount(0);//新增加的点赞数是0
logger.info("addVirtualPraise randomUidList is empty articleId={},authorUid={}",articleId,authorUid);
continue;
}
int random = randomUidList.size();
//1)文章点赞明细表
for(Integer virtualUid : randomUidList){
GrassArticlePraise praiseDetail = new GrassArticlePraise();
praiseDetail.setArticleId(articleId);
praiseDetail.setUid(virtualUid);
praiseDetail.setCreateTime((int) (currentTime/1000l));
praiseDetail.setUpdateTime((int) (currentTime/1000l));
praiseDetail.setStatus(0);//0 --点赞 1 ---取消
newPraiseList.add(praiseDetail);
}
//2)更新文章获赞总数
article.setPraiseCount(random);//增加点赞数
article.setUpdateTime(currentTime );
article.setUpdateTime(currentTime);
//3)更新作者 获赞总数
//同一个作者 必须要累加计数 否则更新结果不正确
GrassUserAchieve userAchieve = new GrassUserAchieve();
if(authorType == GrassUserTypeEnum.COMMON.getValue() && commonMap.get(authorUid)!=null){
... ... @@ -118,8 +163,11 @@ public class GrassUserVirtualImpl implements IGrassVirtualService{
List<GrassUserAchieve> userPraiseList = new ArrayList<>();
userPraiseList.addAll(commonMap.values());
userPraiseList.addAll(guangMap.values());
logger.info("start to addVirtualPraise,newPraiseList={}",newPraiseList);
logger.info("start to addVirtualPraise,articleList={}",articleList);
logger.info("start to addVirtualPraise,userPraiseList={}",userPraiseList);
//更新点赞明细表
grassArticlePraiseDao.batchInsertOrUpdate(newPraiseList);
//更新文章表(批量更新文章表)
grassArticleDao.batchUpdatePraiseCount(articleList);
logger.info("addVirtualPraise update grassArticle success!");
... ...