添加大哥数据入库接口

This commit is contained in:
2025-06-24 18:07:12 +08:00
parent 167c1ec29e
commit a60ae2df1a
10 changed files with 268 additions and 4 deletions

View File

@@ -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<Boolean> addHost(@RequestBody ServerBigBrother bigBrothers){
mqSender.bigBrotherSend(bigBrothers);
return ResultUtils.success(true);
}
}

View File

@@ -35,7 +35,7 @@ public class HostInfoController {
@PostMapping("add_host")
public BaseResponse<Boolean> addHost(@RequestBody List<NewHosts> newHosts){
mqSender.send(newHosts);
mqSender.hostsSend(newHosts);
return ResultUtils.success(true);
}

View File

@@ -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<ServerBigBrother> {
}

View File

@@ -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;
}

View File

@@ -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);
}
}
}

View File

@@ -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<NewHosts> list){
public void hostsSend(List<NewHosts> 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);
}
}
}

View File

@@ -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<ServerBigBrother>{
void saveData(ServerBigBrother bigBrotherList);
}

View File

@@ -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<ServerBigBrotherMapper, ServerBigBrother> implements ServerBigBrotherService{
@Override
@Transactional(rollbackFor = Exception.class)
public void saveData(ServerBigBrother bigBrother) {
ServerBigBrother serverBigBrother = baseMapper.selectOne(Wrappers.<ServerBigBrother>lambdaQuery()
.eq(ServerBigBrother::getDisplayId, bigBrother.getDisplayId())
.eq(ServerBigBrother::getTenantId, bigBrother.getTenantId()));
if(serverBigBrother == null){
save(bigBrother);
} else {
bigBrother.setId(serverBigBrother.getId());
updateById(bigBrother);
}
}
}

View File

@@ -23,7 +23,7 @@ spring:
host: localhost
port: 5672
username: root
password: 123asd
password: kB;V=wOH0iD#s3cW1)
listener:
simple:
acknowledge-mode: manual

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yupi.springbootinit.mapper.ServerBigBrotherMapper">
<resultMap id="BaseResultMap" type="com.yupi.springbootinit.model.entity.ServerBigBrother">
<!--@mbg.generated-->
<!--@Table server_big_brother-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="display_id" jdbcType="VARCHAR" property="displayId" />
<result column="user_id_str" jdbcType="VARCHAR" property="userIdStr" />
<result column="nickname" jdbcType="VARCHAR" property="nickname" />
<result column="level" jdbcType="INTEGER" property="level" />
<result column="hostcoins" jdbcType="INTEGER" property="hostcoins" />
<result column="follower_count" jdbcType="INTEGER" property="followerCount" />
<result column="following_count" jdbcType="INTEGER" property="followingCount" />
<result column="region" jdbcType="VARCHAR" property="region" />
<result column="historic_high_coins" jdbcType="INTEGER" property="historicHighCoins" />
<result column="total_gift_coins" jdbcType="INTEGER" property="totalGiftCoins" />
<result column="host_display_id" jdbcType="VARCHAR" property="hostDisplayId" />
<result column="owner_id" jdbcType="VARCHAR" property="ownerId" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="creator" jdbcType="BIGINT" property="creator" />
<result column="updater" jdbcType="VARCHAR" property="updater" />
<result column="deleted" jdbcType="BOOLEAN" property="deleted" />
<result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
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
</sql>
</mapper>