BUGFIX: 机器人余额优化;
This commit is contained in:
@@ -175,6 +175,8 @@ public class ChatRobotServiceImpl implements ChatRobotService {
|
|||||||
chatRobot.setPromptTokens(data.getLong("prompt_tokens"));
|
chatRobot.setPromptTokens(data.getLong("prompt_tokens"));
|
||||||
chatRobot.setSessionId(data.getString("session_id"));
|
chatRobot.setSessionId(data.getString("session_id"));
|
||||||
BigDecimal totalCost = data.getBigDecimal("total_cost");
|
BigDecimal totalCost = data.getBigDecimal("total_cost");
|
||||||
|
// 校验本次余额够不够
|
||||||
|
checkBalance(totalCost,chatSendDTO.getUser_id());
|
||||||
chatRobot.setTotalCost(totalCost);
|
chatRobot.setTotalCost(totalCost);
|
||||||
chatRobot.setTotalTokens(data.getLong("total_tokens"));
|
chatRobot.setTotalTokens(data.getLong("total_tokens"));
|
||||||
chatRobot.setUserId(chatSendDTO.getUser_id());
|
chatRobot.setUserId(chatSendDTO.getUser_id());
|
||||||
@@ -232,6 +234,21 @@ public class ChatRobotServiceImpl implements ChatRobotService {
|
|||||||
throw new BusinessException("ChatRobot exception!");
|
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
|
@Override
|
||||||
public String chatBufferFlush(ChatFlushDTO chatFlushDTO) {
|
public String chatBufferFlush(ChatFlushDTO chatFlushDTO) {
|
||||||
log.info(chatBufferFlushUrl);
|
log.info(chatBufferFlushUrl);
|
||||||
@@ -283,11 +300,6 @@ public class ChatRobotServiceImpl implements ChatRobotService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BigDecimal getBloodBars(Long userId) {
|
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<ChatRobot> queryWrapper = new QueryWrapper<>();
|
||||||
queryWrapper.eq("user_id", userId);
|
queryWrapper.eq("user_id", userId);
|
||||||
queryWrapper.ge("create_time", LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));
|
queryWrapper.ge("create_time", LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));
|
||||||
|
|||||||
Reference in New Issue
Block a user