diff --git a/src/main/java/com/ai/da/service/impl/ChatRobotServiceImpl.java b/src/main/java/com/ai/da/service/impl/ChatRobotServiceImpl.java index 17656cfd..0b7476ca 100644 --- a/src/main/java/com/ai/da/service/impl/ChatRobotServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/ChatRobotServiceImpl.java @@ -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 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 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 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 queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", userId); queryWrapper.ge("create_time", LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));