|
|
|
@@ -5,18 +5,14 @@ import com.accompany.business.dto.lucky.SuperLuckyGiftIncomeAllot;
|
|
|
|
|
import com.accompany.business.message.Lucky25Message;
|
|
|
|
|
import com.accompany.business.model.Gift;
|
|
|
|
|
import com.accompany.business.service.lucky.*;
|
|
|
|
|
import com.accompany.business.service.lucky.rank.Lucky24SendWeekRankService;
|
|
|
|
|
import com.accompany.business.service.mq.RocketMQService;
|
|
|
|
|
import com.accompany.business.service.room.RoomService;
|
|
|
|
|
import com.accompany.common.redis.RedisKey;
|
|
|
|
|
import com.accompany.common.status.BusiStatus;
|
|
|
|
|
import com.accompany.core.exception.ServiceException;
|
|
|
|
|
import com.accompany.core.model.Room;
|
|
|
|
|
import com.accompany.sharding.model.Lucky25Record;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
|
|
|
|
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.redisson.api.RLock;
|
|
|
|
|
import org.redisson.api.RMap;
|
|
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
@@ -25,7 +21,6 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
@@ -48,9 +43,9 @@ public class Lucky25MqService implements InitializingBean {
|
|
|
|
|
@Autowired
|
|
|
|
|
private GiftService giftService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private Lucky24SendWeekRankService lucky24SendWeekRankService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private RocketMQService rocketMQService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private IdentifierGenerator identifierGenerator;
|
|
|
|
|
|
|
|
|
|
private RMap<String, String> statusMap;
|
|
|
|
|
|
|
|
|
@@ -58,13 +53,13 @@ public class Lucky25MqService implements InitializingBean {
|
|
|
|
|
Map<String, String> caches = new HashMap<>(recordList.size());
|
|
|
|
|
List<Lucky25Message> messageList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
DefaultIdentifierGenerator idGenerator = DefaultIdentifierGenerator.getInstance();
|
|
|
|
|
|
|
|
|
|
for (Lucky25Record record: recordList){
|
|
|
|
|
String id = idGenerator.nextUUID(null);
|
|
|
|
|
Long id = identifierGenerator.nextId(null).longValue();
|
|
|
|
|
String idStr = id.toString();
|
|
|
|
|
|
|
|
|
|
Lucky25Message message = new Lucky25Message();
|
|
|
|
|
message.setMessId(id);
|
|
|
|
|
message.setId(id);
|
|
|
|
|
message.setMessId(idStr);
|
|
|
|
|
message.setPartitionId(record.getPartitionId());
|
|
|
|
|
message.setUid(record.getUid());
|
|
|
|
|
message.setReceiverUid(record.getReceiverUid());
|
|
|
|
@@ -81,7 +76,7 @@ public class Lucky25MqService implements InitializingBean {
|
|
|
|
|
|
|
|
|
|
messageList.add(message);
|
|
|
|
|
|
|
|
|
|
caches.put(id, JSON.toJSONString(message));
|
|
|
|
|
caches.put(idStr, JSON.toJSONString(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statusMap.putAll(caches);
|
|
|
|
@@ -90,60 +85,39 @@ public class Lucky25MqService implements InitializingBean {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void handleMq(Lucky25Message giftMessage) {
|
|
|
|
|
// 防止消息被重复消费
|
|
|
|
|
boolean locked = false;
|
|
|
|
|
RLock lock = redissonClient.getLock(RedisKey.lock_lucky_25_message.getKey(giftMessage.getMessId()));
|
|
|
|
|
try {
|
|
|
|
|
locked = lock.tryLock(15, 5, TimeUnit.SECONDS);
|
|
|
|
|
if (!locked) {
|
|
|
|
|
log.warn("handleLucky25Message giftMessage lock fail, mess: {}", JSON.toJSONString(giftMessage));
|
|
|
|
|
throw new ServiceException(BusiStatus.SERVERBUSY);
|
|
|
|
|
}
|
|
|
|
|
log.info("【处理Lucky25 mq】 开始处理 giftMessage: {}", JSON.toJSONString(giftMessage));
|
|
|
|
|
|
|
|
|
|
if (!statusMap.containsKey(giftMessage.getMessId())){
|
|
|
|
|
log.warn("handleLucky25Message giftMessage had handle, mess: " + giftMessage);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info("【处理Lucky25 mq】 开始处理 giftMessage: {}", JSON.toJSONString(giftMessage));
|
|
|
|
|
|
|
|
|
|
Room room = null != giftMessage.getRoomUid()? roomService.getRoomByUid(giftMessage.getRoomUid()): null;
|
|
|
|
|
Gift gift = giftService.getGiftById(giftMessage.getGiftId());
|
|
|
|
|
Date createTime = new Date(giftMessage.getCreateTime());
|
|
|
|
|
|
|
|
|
|
Lucky25Record record = insertRecord(giftMessage);
|
|
|
|
|
|
|
|
|
|
log.info("【处理Lucky25 mq】 record 插入成功 messId:{} recordId:{} record:{}",
|
|
|
|
|
giftMessage.getMessId(), record.getId(), JSON.toJSONString(record));
|
|
|
|
|
|
|
|
|
|
// 收礼者收益
|
|
|
|
|
Lucky25GiftConfig config = sendService.getConfig();
|
|
|
|
|
SuperLuckyGiftIncomeAllot receiverIncomeAllot = incomeAllotService.calculate(config, giftMessage.getPartitionId(), gift, giftMessage.getGiftNum(), Collections.singletonList(record.getReceiverUid()));
|
|
|
|
|
superLuckyGiftSendService.syncSettlement(giftMessage.getUid(), gift, giftMessage.getGiftNum(), giftMessage.getGiftNum(), room, receiverIncomeAllot, createTime);
|
|
|
|
|
|
|
|
|
|
log.info("【处理Lucky25 mq】 收礼收益已发放 messId: {} incomeAllot: {}", giftMessage.getMessId(), JSON.toJSONString(receiverIncomeAllot));
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(config.getFollowUidList()) && config.getFollowUidList().contains(record.getUid())){
|
|
|
|
|
robotMsgService.pushFollowUser(record.getUid(), record.getReceiverUid(), record.getRoomUid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//lucky24SendWeekRankService.updateRank(record);
|
|
|
|
|
|
|
|
|
|
// 删除该标识,表示消息已经消费过
|
|
|
|
|
statusMap.fastRemove(giftMessage.getMessId());
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
log.error("handleLucky25Message giftMessage lock overtime, mess: {}", JSON.toJSONString(giftMessage), e);
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (locked) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
Lucky25Record record = convertToRecord(giftMessage);
|
|
|
|
|
int row = recordService.insertRecordIgnore(record);
|
|
|
|
|
if (row <= 0){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info("【处理Lucky25 mq】 record 插入成功 messId:{} recordId:{} record:{}",
|
|
|
|
|
giftMessage.getMessId(), record.getId(), JSON.toJSONString(record));
|
|
|
|
|
|
|
|
|
|
Room room = null != giftMessage.getRoomUid()? roomService.getRoomByUid(giftMessage.getRoomUid()): null;
|
|
|
|
|
Gift gift = giftService.getGiftById(giftMessage.getGiftId());
|
|
|
|
|
Date createTime = new Date(giftMessage.getCreateTime());
|
|
|
|
|
|
|
|
|
|
// 收礼者收益
|
|
|
|
|
Lucky25GiftConfig config = sendService.getConfig();
|
|
|
|
|
SuperLuckyGiftIncomeAllot receiverIncomeAllot = incomeAllotService.calculate(config, giftMessage.getPartitionId(), gift, giftMessage.getGiftNum(), Collections.singletonList(record.getReceiverUid()));
|
|
|
|
|
superLuckyGiftSendService.syncSettlement(giftMessage.getUid(), gift, giftMessage.getGiftNum(), giftMessage.getGiftNum(), room, receiverIncomeAllot, createTime);
|
|
|
|
|
|
|
|
|
|
log.info("【处理Lucky25 mq】 收礼收益已发放 messId: {} incomeAllot: {}", giftMessage.getMessId(), JSON.toJSONString(receiverIncomeAllot));
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(config.getFollowUidList()) && config.getFollowUidList().contains(record.getUid())){
|
|
|
|
|
robotMsgService.pushFollowUser(record.getUid(), record.getReceiverUid(), record.getRoomUid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除该标识,表示消息已经消费过
|
|
|
|
|
statusMap.fastRemove(giftMessage.getMessId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Lucky25Record insertRecord(Lucky25Message giftMessage) {
|
|
|
|
|
private Lucky25Record convertToRecord(Lucky25Message giftMessage) {
|
|
|
|
|
Lucky25Record record = new Lucky25Record();
|
|
|
|
|
record.setId(null != giftMessage.getId()? giftMessage.getId(): identifierGenerator.nextId(null).longValue());
|
|
|
|
|
record.setMessId(giftMessage.getMessId());
|
|
|
|
|
record.setPartitionId(giftMessage.getPartitionId());
|
|
|
|
|
record.setUid(giftMessage.getUid());
|
|
|
|
@@ -158,9 +132,6 @@ public class Lucky25MqService implements InitializingBean {
|
|
|
|
|
record.setAfterMultiple(giftMessage.getAfterMultiple());
|
|
|
|
|
record.setWinGoldNum(giftMessage.getWinGoldNum());
|
|
|
|
|
record.setCreateTime(new Date(giftMessage.getCreateTime()));
|
|
|
|
|
|
|
|
|
|
recordService.insertRecord(record);
|
|
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|