账单-雪花主键-mq-组织do和insertIgnore拆分

This commit is contained in:
2025-09-25 11:32:29 +08:00
parent 4f3ddab6f7
commit ff07d21e02
3 changed files with 28 additions and 27 deletions

View File

@@ -8,7 +8,6 @@ import com.accompany.core.model.Users;
import com.accompany.sharding.mapper.BillRecordMapper;
import com.accompany.sharding.model.BillRecord;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
@@ -37,59 +36,57 @@ public class BillMessageService implements InitializingBean {
private UsersService usersService;
public void sendBillMessage(BillRecord billRecord){
String messId = DefaultIdentifierGenerator.getInstance().nextUUID(null);
billRecord.setMessId(messId);
BillMessage message = new BillMessage();
BeanUtils.copyProperties(billRecord, message);
recordMessMap.fastPut(messId, message);
recordMessMap.fastPut(message.getMessId(), message);
rocketMQService.sendBillRecordMessage(message);
};
public void handleBillMessage(BillMessage billMessage) {
BillRecord billRecord = insertBillRecordIgnore(billMessage);
log.info("【处理账单mq】 billRecord 插入成功 id:{} messId: {} mess:{}",
billRecord.getBillId(), billMessage.getMessId(), JSON.toJSONString(billMessage));
BillRecord billRecord = buildBillRecord(billMessage);
int row = insertBillRecordIgnore(billRecord);
if (row <= 0){
return;
}
recordMessMap.fastRemove(billMessage.getMessId());
}
private BillRecord insertBillRecordIgnore(BillMessage billMessage) {
long startTime = System.currentTimeMillis();
long copyPropertiesTime = 0;
long getUserTime = 0;
long insertTime = 0;
private BillRecord buildBillRecord(BillMessage billMessage) {
BillRecord billRecord = new BillRecord();
BeanUtils.copyProperties(billMessage, billRecord);
copyPropertiesTime = System.currentTimeMillis();
Users u = usersService.getUsersByUid(billMessage.getUid());
getUserTime = System.currentTimeMillis();
if (null != u){
billRecord.setPartitionId(u.getPartitionId());
}
int insertRow = billRecordMapper.insertIgnore(billRecord);
insertTime = System.currentTimeMillis();
return billRecord;
}
private int insertBillRecordIgnore(BillRecord billRecord) {
long startTime = System.currentTimeMillis();
int insertRow = billRecordMapper.insertIgnore(billRecord);
long endTime = System.currentTimeMillis();
log.info("insertBillRecordIgnore row {} performance - copy: {}ms, getUser: {}ms, insert: {}ms, total: {}ms",
log.info("insertBillRecordIgnore row {} performance - total: {}ms",
insertRow,
copyPropertiesTime - startTime,
getUserTime - copyPropertiesTime,
insertTime - getUserTime,
endTime - startTime);
return billRecord;
if (insertRow > 0) {
log.info("【处理账单mq】 billRecord 插入成功 id:{} messId: {} billRecord:{}",
billRecord.getBillId(), billRecord.getMessId(), JSON.toJSONString(billRecord));
}
return insertRow;
}
@Override
public void afterPropertiesSet() throws Exception {
recordMessMap = redissonClient.getMap(RedisKey.bill_record_message.getKey());
}
}

View File

@@ -145,8 +145,12 @@ public class BillRecordService extends ServiceImpl<BillRecordMapper, BillRecord>
}
Double beforeA = null != afterA? DoubleUtil.sub(afterA, a) : null;
Long billId = identifierGenerator.nextId(null).longValue();
String messId = billId.toString();
BillRecord billRecord = new BillRecord();
billRecord.setBillId(identifierGenerator.nextId(null).longValue());
billRecord.setBillId(billId);
billRecord.setMessId(messId);
billRecord.setBillType(billType.getValue());
billRecord.setUid(uid);
billRecord.setTargetUid(targetUid);