init
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package com.tailbet.service.impl;
|
||||
|
||||
import com.tailbet.entity.User;
|
||||
import com.tailbet.entity.DigitWhite;
|
||||
import com.tailbet.mapper.UserMapper;
|
||||
import com.tailbet.mapper.DigitWhiteMapper;
|
||||
import com.tailbet.openim.OpenIMClient;
|
||||
import com.tailbet.service.IPushService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 推送服务实现
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class PushServiceImpl implements IPushService {
|
||||
|
||||
private final UserMapper userMapper;
|
||||
private final DigitWhiteMapper digitWhiteMapper;
|
||||
private final OpenIMClient openIMClient;
|
||||
|
||||
/**
|
||||
* 推送尾数试算完成通知给白名单用户
|
||||
*/
|
||||
@Override
|
||||
public void pushDigitTrialComplete(Long groupId, Long roundId) {
|
||||
List<DigitWhite> whites = digitWhiteMapper.selectList(new LambdaQueryWrapper<DigitWhite>());
|
||||
for (DigitWhite white : whites) {
|
||||
User user = userMapper.selectById(white.getUserId());
|
||||
if (user != null) {
|
||||
PushService.PushMessage message = new PushService.PushMessage();
|
||||
message.setType(PUSH_TYPE_DIGIT_TRIAL);
|
||||
message.setTitle("尾数试算完成");
|
||||
message.setContent("可前往修改尾数");
|
||||
message.setUserId(user.getId());
|
||||
message.setGroupId(groupId);
|
||||
message.setRoundId(roundId);
|
||||
|
||||
sendPush(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送游戏开始通知
|
||||
*/
|
||||
@Override
|
||||
public void pushGameStart(Long groupId, String roundNo) {
|
||||
PushService.PushMessage message = new PushService.PushMessage();
|
||||
message.setType(PUSH_TYPE_GAME_START);
|
||||
message.setTitle("游戏开始");
|
||||
message.setContent(roundNo + "期已开始下注");
|
||||
message.setGroupId(groupId);
|
||||
message.setRoundNo(roundNo);
|
||||
|
||||
// 推送给所有群成员
|
||||
broadcastToGroup(groupId, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送开奖结果通知
|
||||
*/
|
||||
@Override
|
||||
public void pushGameResult(Long groupId, String roundNo, int digit, String playWins) {
|
||||
PushService.PushMessage message = new PushService.PushMessage();
|
||||
message.setType(PUSH_TYPE_RESULT);
|
||||
message.setTitle("开奖结果");
|
||||
message.setContent(roundNo + "期开奖\n尾数: " + digit + "\n中奖玩法: " + playWins);
|
||||
message.setGroupId(groupId);
|
||||
message.setRoundNo(roundNo);
|
||||
message.setDigit(digit);
|
||||
|
||||
// 广播给所有群成员
|
||||
broadcastToGroup(groupId, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送单用户推送
|
||||
*/
|
||||
@Override
|
||||
public void sendPush(PushService.PushMessage message) {
|
||||
try {
|
||||
User user = userMapper.selectById(message.getUserId());
|
||||
if (user == null) {
|
||||
log.warn("推送用户不存在: userId={}", message.getUserId());
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: 根据实际推送通道实现
|
||||
// 1. 如果用户在线,通过OpenIM发送即时消息
|
||||
// 2. 如果用户离线,通过FCM/APNs发送离线推送
|
||||
|
||||
// 通过OpenIM单聊发送
|
||||
openIMClient.sendUserMessage(String.valueOf(user.getId()),
|
||||
message.getTitle() + " - " + message.getContent());
|
||||
|
||||
log.info("推送发送成功: userId={}, type={}", message.getUserId(), message.getType());
|
||||
} catch (Exception e) {
|
||||
log.error("推送发送失败: userId={}, type={}, error={}",
|
||||
message.getUserId(), message.getType(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播推送消息给群成员
|
||||
*/
|
||||
@Override
|
||||
public void broadcastToGroup(Long groupId, PushService.PushMessage message) {
|
||||
try {
|
||||
// 通过OpenIM群消息发送
|
||||
openIMClient.sendGroupMessage(String.valueOf(groupId),
|
||||
message.getContent());
|
||||
log.info("群广播发送成功: groupId={}, type={}", groupId, message.getType());
|
||||
} catch (Exception e) {
|
||||
log.error("群广播发送失败: groupId={}, type={}, error={}",
|
||||
groupId, message.getType(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user