Files
kami_backend/internal/logic/camel_oil/account_rotation.go
danial 15e2426e85 feat(camel_oil): 新增骆驼加油账号管理模块
- 实现账号增删改查接口和逻辑
- 支持账号状态更新及状态历史记录功能
- 提供账号列表、历史和统计信息查询API
- 实现账号轮询机制,支持按使用时间轮询获取账号
- 增加账号登录流程及批量登录功能,集成接码平台和平台API
- 管理账号订单容量,支持容量检查与账号登录触发
- 提供账号池状态统计接口
- 账号历史记录查询支持多种变更类型文本展示
- 密码等敏感信息采用脱敏展示
- 完善日志记录和错误处理机制,保证业务稳定运行
2025-11-21 00:49:50 +08:00

49 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package camel_oil
import (
"context"
"github.com/gogf/gf/v2/os/gmlock"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"kami/internal/consts"
"kami/internal/dao"
"kami/internal/model/entity"
"kami/utility/config"
)
// ====================================================================================
// 账号轮询相关方法
// ====================================================================================
// GetAvailableAccount 获取可用账号按last_used_at轮询
// 选择条件:
// 1. status=2在线
// 2. daily_order_count < 10当日未达限额
// 3. daily_order_date=今日(日期匹配)
// 排序last_used_at ASC最早使用的优先实现轮询
func (s *sCamelOil) GetAvailableAccount(ctx context.Context) (account *entity.V1CamelOilAccount, err error) {
gmlock.Lock("camelGetAvailableAccount")
defer gmlock.Unlock("camelGetAvailableAccount")
m := dao.V1CamelOilAccount.Ctx(ctx).DB(config.GetDatabaseV1())
err = m.Where(dao.V1CamelOilAccount.Columns().Status, consts.CamelOilAccountStatusOnline).
WhereLTE(dao.V1CamelOilAccount.Columns().DailyOrderCount, 10).
OrderAsc(dao.V1CamelOilAccount.Columns().LastUsedAt).
Scan(&account)
if err != nil {
return nil, gerror.Wrap(err, "查询可用账号失败")
}
if account == nil {
g.Log().Warning(ctx, "暂无可用账号")
return nil, nil
}
_, _ = m.Where(dao.V1CamelOilAccount.Columns().Id, account.Id).Update(dao.V1CamelOilAccount.Columns().LastUsedAt, gtime.Now())
return account, nil
}