BUGFIX: 机器人余额优化;

This commit is contained in:
shahaibo
2023-10-12 15:50:20 +08:00
parent 381d40c346
commit 770e9d1d23

View File

@@ -175,6 +175,8 @@ public class ChatRobotServiceImpl implements ChatRobotService {
chatRobot.setPromptTokens(data.getLong("prompt_tokens"));
chatRobot.setSessionId(data.getString("session_id"));
BigDecimal totalCost = data.getBigDecimal("total_cost");
// 校验本次余额够不够
checkBalance(totalCost,chatSendDTO.getUser_id());
chatRobot.setTotalCost(totalCost);
chatRobot.setTotalTokens(data.getLong("total_tokens"));
chatRobot.setUserId(chatSendDTO.getUser_id());
@@ -232,6 +234,21 @@ public class ChatRobotServiceImpl implements ChatRobotService {
throw new BusinessException("ChatRobot exception!");
}
private void checkBalance(BigDecimal totalCost, Long userId) {
QueryWrapper<ChatRobot> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
queryWrapper.ge("create_time", LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));
queryWrapper.select("user_id", "SUM(total_cost) as total_cost");
queryWrapper.groupBy("user_id");
List<ChatRobot> chatRobots = chatRobotMapper.selectList(queryWrapper);
if(!CollectionUtils.isEmpty(chatRobots)) {
BigDecimal totalCostUsed = chatRobots.get(0).getTotalCost();
if(totalCostUsed.add(totalCost).compareTo(BigDecimal.valueOf(5)) > 0) {
throw new BusinessException("Your balance is insufficient");
}
}
}
@Override
public String chatBufferFlush(ChatFlushDTO chatFlushDTO) {
log.info(chatBufferFlushUrl);
@@ -283,11 +300,6 @@ public class ChatRobotServiceImpl implements ChatRobotService {
@Override
public BigDecimal getBloodBars(Long userId) {
// QueryWrapper<ChatRobot> queryWrapper = new QueryWrapper<>();
// queryWrapper.select("user_id as userId", "SUM(total_cost) as totalCost");
// queryWrapper.lambda().eq(ChatRobot::getTotalCost, userId);
// queryWrapper.lambda().eq(ChatRobot::getSuccessful, 1);
// queryWrapper.lambda().ge(ChatRobot::getCreateTime, LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));
QueryWrapper<ChatRobot> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
queryWrapper.ge("create_time", LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));