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;
/**
*
* @param publishId
* @return
*/
public Boolean publishMsg(String publishId) {
return this.sendMsg(publishId, 1);
}
/**
* 浏览动态消息
*
* @param publishId
* @return
*/
public Boolean queryPublishMsg(String publishId) {
return this.sendMsg(publishId, 2);
}
/**
* 点赞动态消息
*
* @param publishId
* @return
*/
public Boolean likePublishMsg(String publishId) {
return this.sendMsg(publishId, 3);
}
/**
* 取消点赞动态消息
*
* @param publishId
* @return
*/
public Boolean disLikePublishMsg(String publishId) {
return this.sendMsg(publishId, 6);
}
/**
* 喜欢动态消息
*
* @param publishId
* @return
*/
public Boolean lovePublishMsg(String publishId) {
return this.sendMsg(publishId, 4);
}
/**
* 取消喜欢动态消息
*
* @param publishId
* @return
*/
public Boolean disLovePublishMsg(String publishId) {
return this.sendMsg(publishId, 7);
}
/**
* 评论动态消息
*
* @param publishId
* @return
*/
public Boolean commentPublishMsg(String publishId) {
return this.sendMsg(publishId, 5);
}
/**
*
* @param publishId
* @param type 1-发动态,2-浏览动态, 3-点赞, 4-喜欢, 5-评论,6-取消点赞,7-取消喜欢
* @return
*/
private Boolean sendMsg(String publishId, Integer type) {
try {
User user = UserThreadLocal.get();
Publish publish = this.quanZiApi.queryPublishById(publishId);
//构建消息
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(“发送消息失败! publishId = ” + publishId + “, type = ” + type, e);
return false;
}
return true;
}
}