Files
tiesheng-springboot/springboot-message/src/main/java/com/tiesheng/message/service/TsMessageService.java
2024-08-23 15:04:20 +08:00

79 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tiesheng.message.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.tiesheng.message.pojos.UserChannel;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* @author hao
*/
@Service
public class TsMessageService {
@Autowired
List<TsMessageSender> messageSenderList;
/**
* 发送所有消息
*
* @param userIds
* @param body
*/
public void all(List<String> userIds, JSONObject body) {
messageSenderList.stream().filter(TsMessageSender::support)
.forEach(sender -> {
for (String user : userIds) {
sender.send(user, body);
}
});
}
/**
* 发送消息
*
* @param channels
* @param body
* @param channels 消息通道如果为all时表示发送全部通道
*/
public void multiple(List<UserChannel> channels, JSONObject body) {
if (ArrayUtil.isEmpty(channels)) {
return;
}
messageSenderList.stream().filter(TsMessageSender::support)
.forEach(sender -> channels.stream().filter(it -> Objects.equals(it.getChannel(), sender.getChannel()))
.forEach(it -> sender.send(it.getUser(), body)));
}
/**
* 发送消息
*
* @param userChannel
* @param body
*/
public ApiResp<String> send(UserChannel userChannel, JSONObject body) {
if (StrUtil.isEmpty(userChannel.getUser()) || StrUtil.isEmpty(userChannel.getChannel())) {
return ApiResp.resp130("消息对象或消息通道不存在");
}
TsMessageSender messageSender = CollUtil.findOne(messageSenderList,
sender -> Objects.equals(sender.getChannel(), userChannel.getChannel()) && sender.support());
if (messageSender != null) {
return messageSender.send(userChannel.getUser(), body);
}
return ApiResp.resp130("消息未成功发送");
}
}