From a60ae2df1ab9d513f1fe4f69e6085ba0d2809f48 Mon Sep 17 00:00:00 2001 From: Ziin Date: Tue, 24 Jun 2025 18:07:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A7=E5=93=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=85=A5=E5=BA=93=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BigBrotherController.java | 34 ++++++ .../controller/HostInfoController.java | 2 +- .../mapper/ServerBigBrotherMapper.java | 12 +++ .../model/entity/ServerBigBrother.java | 101 ++++++++++++++++++ .../springbootinit/rabbitMQ/MQReceiver.java | 22 ++++ .../springbootinit/rabbitMQ/MQSender.java | 16 ++- .../service/ServerBigBrotherService.java | 15 +++ .../impl/ServerBigBrotherServiceImpl.java | 35 ++++++ src/main/resources/application-prod.yml | 2 +- .../mapper/ServerBigBrotherMapper.xml | 33 ++++++ 10 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java create mode 100644 src/main/java/com/yupi/springbootinit/mapper/ServerBigBrotherMapper.java create mode 100644 src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java create mode 100644 src/main/java/com/yupi/springbootinit/service/ServerBigBrotherService.java create mode 100644 src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java create mode 100644 src/main/resources/mapper/ServerBigBrotherMapper.xml diff --git a/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java b/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java new file mode 100644 index 0000000..689bd66 --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/controller/BigBrotherController.java @@ -0,0 +1,34 @@ +package com.yupi.springbootinit.controller; + +import com.yupi.springbootinit.common.BaseResponse; +import com.yupi.springbootinit.common.ResultUtils; +import com.yupi.springbootinit.model.entity.NewHosts; +import com.yupi.springbootinit.model.entity.ServerBigBrother; +import com.yupi.springbootinit.rabbitMQ.MQSender; +import com.yupi.springbootinit.service.HostInfoService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/* + * @author: ziin + * @date: 2025/6/24 16:19 + */ +@RestController +@RequestMapping("/big-brother") +@Slf4j +@CrossOrigin +public class BigBrotherController { + + @Resource + private MQSender mqSender; + + + @PostMapping("add_brother") + public BaseResponse addHost(@RequestBody ServerBigBrother bigBrothers){ + mqSender.bigBrotherSend(bigBrothers); + return ResultUtils.success(true); + } +} diff --git a/src/main/java/com/yupi/springbootinit/controller/HostInfoController.java b/src/main/java/com/yupi/springbootinit/controller/HostInfoController.java index 3f88561..94e78a0 100644 --- a/src/main/java/com/yupi/springbootinit/controller/HostInfoController.java +++ b/src/main/java/com/yupi/springbootinit/controller/HostInfoController.java @@ -35,7 +35,7 @@ public class HostInfoController { @PostMapping("add_host") public BaseResponse addHost(@RequestBody List newHosts){ - mqSender.send(newHosts); + mqSender.hostsSend(newHosts); return ResultUtils.success(true); } diff --git a/src/main/java/com/yupi/springbootinit/mapper/ServerBigBrotherMapper.java b/src/main/java/com/yupi/springbootinit/mapper/ServerBigBrotherMapper.java new file mode 100644 index 0000000..e7b9dc7 --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/mapper/ServerBigBrotherMapper.java @@ -0,0 +1,12 @@ +package com.yupi.springbootinit.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.yupi.springbootinit.model.entity.ServerBigBrother; + +/* + * @author: ziin + * @date: 2025/6/24 16:42 + */ + +public interface ServerBigBrotherMapper extends BaseMapper { +} \ No newline at end of file diff --git a/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java b/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java new file mode 100644 index 0000000..3a870af --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/model/entity/ServerBigBrother.java @@ -0,0 +1,101 @@ +package com.yupi.springbootinit.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.util.Date; +import lombok.Data; + +/* +* @author: ziin +* @date: 2025/6/24 16:42 +*/ + +@Data +@TableName(value = "server_big_brother") +public class ServerBigBrother { + /** + * 主键id + */ + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + + /** + * 大哥的display_id + */ + @TableField(value = "display_id") + private String displayId; + + /** + * 大哥的用户id + */ + @TableField(value = "user_id_str") + private String userIdStr; + + /** + * 大哥的用户昵称 + */ + @TableField(value = "nickname") + private String nickname; + + /** + * 大哥的等级 + */ + @TableField(value = "`level`") + private Integer level; + + /** + * 大哥打赏的金币 + */ + @TableField(value = "hostcoins") + private Integer hostcoins; + + /** + * 大哥的粉丝数 + */ + @TableField(value = "follower_count") + private Integer followerCount; + + /** + * 大哥的关注数 + */ + @TableField(value = "following_count") + private Integer followingCount; + + /** + * 大哥所在的地区 + */ + @TableField(value = "region") + private String region; + + /** + * 大哥打赏的历史最高金币 + */ + @TableField(value = "historic_high_coins") + private Integer historicHighCoins; + + /** + * 大哥历史打赏金币总和 + */ + @TableField(value = "total_gift_coins") + private Integer totalGiftCoins; + + /** + * 大哥所在的直播间的主播display_id + */ + @TableField(value = "host_display_id") + private String hostDisplayId; + + /** + * 该数据所属的账号id + */ + @TableField(value = "owner_id") + private String ownerId; + + /** + * 租户 Id + */ + @TableField(value = "tenant_id") + private Long tenantId; +} \ No newline at end of file diff --git a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java index 2f26184..a149856 100644 --- a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java +++ b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQReceiver.java @@ -6,7 +6,9 @@ import com.rabbitmq.client.Channel; import com.yupi.springbootinit.common.ErrorCode; import com.yupi.springbootinit.exception.BusinessException; import com.yupi.springbootinit.model.entity.NewHosts; +import com.yupi.springbootinit.model.entity.ServerBigBrother; import com.yupi.springbootinit.service.HostInfoService; +import com.yupi.springbootinit.service.ServerBigBrotherService; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; @@ -23,6 +25,9 @@ public class MQReceiver { @Resource private HostInfoService hostInfoService; + @Resource + private ServerBigBrotherService serverBigBrotherService; + // //方法:接收消息 // @RabbitListener(queues = "HOST_INFO_QUEUE") @@ -55,4 +60,21 @@ public class MQReceiver { throw new BusinessException(ErrorCode.QUEUE_CONSUMPTION_FAILURE); } } + + @RabbitListener(queues = "BIG_BROTHER_QUEUE") + @Async + public void bigBrotherReceive(ServerBigBrother bigBrotherList, Channel channel, Message message) throws IOException { + long deliveryTag = message.getMessageProperties().getDeliveryTag(); + try { + // 等待所有异步任务完成 + serverBigBrotherService.saveData(bigBrotherList); + channel.basicAck(deliveryTag, false); +// log.info("deliveryTag:{}", deliveryTag); +// log.info("{} 消息消费内容大小-------> {}",DateTime.now(),hosts.size()); + } catch (Exception e) { + channel.basicNack(deliveryTag, false, false); + log.error("消息消费失败"); + throw new BusinessException(ErrorCode.QUEUE_CONSUMPTION_FAILURE); + } + } } diff --git a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQSender.java b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQSender.java index 13e0565..df5eb54 100644 --- a/src/main/java/com/yupi/springbootinit/rabbitMQ/MQSender.java +++ b/src/main/java/com/yupi/springbootinit/rabbitMQ/MQSender.java @@ -4,9 +4,11 @@ import cn.hutool.core.date.DateTime; import com.yupi.springbootinit.common.ErrorCode; import com.yupi.springbootinit.exception.BusinessException; import com.yupi.springbootinit.model.entity.NewHosts; +import com.yupi.springbootinit.model.entity.ServerBigBrother; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -21,7 +23,7 @@ public class MQSender { //方法:发送消息 - public void send(List list){ + public void hostsSend(List list){ try { // log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size()); this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); @@ -30,6 +32,16 @@ public class MQSender { }catch (Exception e){ throw new BusinessException(ErrorCode.QUEUE_ERROR); } - + } + //方法:发送消息 + public void bigBrotherSend(ServerBigBrother bigBrothers){ + try { +// log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size()); + this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); + //指定你队列的名字 + rabbitTemplate.convertAndSend("BIG_BROTHER_QUEUE",bigBrothers); + }catch (Exception e){ + throw new BusinessException(ErrorCode.QUEUE_ERROR); + } } } \ No newline at end of file diff --git a/src/main/java/com/yupi/springbootinit/service/ServerBigBrotherService.java b/src/main/java/com/yupi/springbootinit/service/ServerBigBrotherService.java new file mode 100644 index 0000000..260fc1e --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/service/ServerBigBrotherService.java @@ -0,0 +1,15 @@ +package com.yupi.springbootinit.service; + +import com.yupi.springbootinit.model.entity.ServerBigBrother; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; +/* +* @author: ziin +* @date: 2025/6/24 16:19 +*/ + +public interface ServerBigBrotherService extends IService{ + + void saveData(ServerBigBrother bigBrotherList); +} diff --git a/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java b/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java new file mode 100644 index 0000000..def0bb4 --- /dev/null +++ b/src/main/java/com/yupi/springbootinit/service/impl/ServerBigBrotherServiceImpl.java @@ -0,0 +1,35 @@ +package com.yupi.springbootinit.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.yupi.springbootinit.mapper.ServerBigBrotherMapper; +import com.yupi.springbootinit.model.entity.ServerBigBrother; +import com.yupi.springbootinit.service.ServerBigBrotherService; +import org.springframework.transaction.annotation.Transactional; +/* +* @author: ziin +* @date: 2025/6/24 16:19 +*/ + +@Service +public class ServerBigBrotherServiceImpl extends ServiceImpl implements ServerBigBrotherService{ + + @Override + @Transactional(rollbackFor = Exception.class) + public void saveData(ServerBigBrother bigBrother) { + + ServerBigBrother serverBigBrother = baseMapper.selectOne(Wrappers.lambdaQuery() + .eq(ServerBigBrother::getDisplayId, bigBrother.getDisplayId()) + .eq(ServerBigBrother::getTenantId, bigBrother.getTenantId())); + if(serverBigBrother == null){ + save(bigBrother); + } else { + bigBrother.setId(serverBigBrother.getId()); + updateById(bigBrother); + } + } +} diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 747ffd4..0b7e3e4 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -23,7 +23,7 @@ spring: host: localhost port: 5672 username: root - password: 123asd + password: kB;V=wOH0iD#s3cW1) listener: simple: acknowledge-mode: manual diff --git a/src/main/resources/mapper/ServerBigBrotherMapper.xml b/src/main/resources/mapper/ServerBigBrotherMapper.xml new file mode 100644 index 0000000..fa22aa3 --- /dev/null +++ b/src/main/resources/mapper/ServerBigBrotherMapper.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + id, display_id, user_id_str, nickname, `level`, hostcoins, follower_count, following_count, + region, historic_high_coins, total_gift_coins, host_display_id, owner_id, create_time, + update_time, creator, updater, deleted, tenant_id + + \ No newline at end of file