service 加入推薦算法 優(yōu)化朋友圈 初級(jí)算法
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class QuanziMQService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Reference(version = “1.0.0”)
private QuanZiApi quanZiApi;
/**
* 發(fā)布動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean publishMsg(String publishId) {
return this.sendMsg(publishId, 1);
}
/**
* 瀏覽動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean queryPublishMsg(String publishId) {
return this.sendMsg(publishId, 2);
}
/**
* 點(diǎn)贊動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean likePublishMsg(String publishId) {
return this.sendMsg(publishId, 3);
}
/**
* 取消點(diǎn)贊動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean disLikePublishMsg(String publishId) {
return this.sendMsg(publishId, 6);
}
/**
* 喜歡動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean lovePublishMsg(String publishId) {
return this.sendMsg(publishId, 4);
}
/**
* 取消喜歡動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean disLovePublishMsg(String publishId) {
return this.sendMsg(publishId, 7);
}
/**
* 評(píng)論動(dòng)態(tài)消息
*
* @param publishId
* @return
*/
public Boolean commentPublishMsg(String publishId) {
return this.sendMsg(publishId, 5);
}
/**
*
* @param publishId
* @param type 1-發(fā)動(dòng)態(tài),2-瀏覽動(dòng)態(tài), 3-點(diǎn)贊, 4-喜歡, 5-評(píng)論,6-取消點(diǎn)贊,7-取消喜歡
* @return
*/
private Boolean sendMsg(String publishId, Integer type) {
try {
User user = UserThreadLocal.get();
Publish publish = this.quanZiApi.queryPublishById(publishId);
//構(gòu)建消息
Map msg = new HashMap();
msg.put(“userId”, user.getId());
msg.put(“date”, System.currentTimeMillis());
msg.put(“publishId”, publishId);
msg.put(“pid”, publish.getPid());
msg.put(“type”, type);
this.rocketMQTemplate.convertAndSend(“tanhua-quanzi”, msg);
} catch (Exception e) {
log.error(“發(fā)送消息失敗! publishId = ” + publishId + “, type = ” + type, e);
return false;
}
return true;
}
}