2026-04-29 15:54:14 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-05-29 18:06:50 +08:00
|
|
|
|
"fmt"
|
2026-05-15 14:56:26 +08:00
|
|
|
|
"model-gateway/consts/public"
|
|
|
|
|
|
"model-gateway/model/dto"
|
|
|
|
|
|
"model-gateway/model/entity"
|
2026-05-29 17:54:19 +08:00
|
|
|
|
"strconv"
|
2026-05-29 18:06:50 +08:00
|
|
|
|
"strings"
|
2026-04-29 15:54:14 +08:00
|
|
|
|
|
|
|
|
|
|
"gitea.com/red-future/common/db/gfdb"
|
2026-05-12 13:45:08 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2026-04-29 15:54:14 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var Model = &modelDao{}
|
|
|
|
|
|
|
|
|
|
|
|
type modelDao struct{}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// Insert 插入
|
|
|
|
|
|
func (d *modelDao) Insert(ctx context.Context, req *entity.AsynchModel) (id int64, err error) {
|
|
|
|
|
|
m := new(entity.AsynchModel)
|
|
|
|
|
|
err = gconv.Struct(req, &m)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
|
|
|
|
|
Insert(m)
|
2026-04-29 15:54:14 +08:00
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
return r.LastInsertId()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// Update 更新
|
|
|
|
|
|
func (d *modelDao) Update(ctx context.Context, req *entity.AsynchModel) (rows int64, err error) {
|
|
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
2026-05-12 13:45:08 +08:00
|
|
|
|
OmitEmpty().
|
2026-05-21 10:41:37 +08:00
|
|
|
|
Data(&req).
|
|
|
|
|
|
Where(entity.AsynchModelCol.Id, req.Id).
|
2026-05-12 13:45:08 +08:00
|
|
|
|
Update()
|
|
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
return r.RowsAffected()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// Delete 删除
|
|
|
|
|
|
func (d *modelDao) Delete(ctx context.Context, req *entity.AsynchModel) (rows int64, err error) {
|
|
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
|
|
|
|
|
OmitEmpty().
|
|
|
|
|
|
Where(entity.AsynchModelCol.Id, req.Id).
|
2026-04-29 15:54:14 +08:00
|
|
|
|
Delete()
|
|
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
return r.RowsAffected()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// Get 按ID获取(带租户隔离,只查当前租户)
|
|
|
|
|
|
func (d *modelDao) Get(ctx context.Context, req *entity.AsynchModel, fields ...string) (m *entity.AsynchModel, err error) {
|
2026-05-29 18:06:50 +08:00
|
|
|
|
//r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
|
|
|
|
|
// OmitEmpty().
|
|
|
|
|
|
// Where(entity.AsynchModelCol.Id, req.Id).
|
|
|
|
|
|
// Where(entity.AsynchModelCol.Creator, req.Creator).
|
|
|
|
|
|
// Where(entity.AsynchModelCol.IsChatModel, req.IsChatModel).
|
|
|
|
|
|
// Where(entity.AsynchModelCol.ModelName, req.ModelName).
|
|
|
|
|
|
// Fields(fields).One()
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
// return
|
|
|
|
|
|
//}
|
|
|
|
|
|
//err = r.Struct(&m)
|
|
|
|
|
|
|
|
|
|
|
|
var whereCondition strings.Builder
|
|
|
|
|
|
var queryParams []interface{}
|
|
|
|
|
|
if !g.IsEmpty(req.Id) {
|
|
|
|
|
|
whereCondition.WriteString(fmt.Sprintf(" AND %s = (?) ", entity.AsynchModelCol.Id))
|
|
|
|
|
|
queryParams = append(queryParams, req.Id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.Creator) {
|
|
|
|
|
|
whereCondition.WriteString(fmt.Sprintf(" AND %s = (?) ", entity.AsynchModelCol.Creator))
|
|
|
|
|
|
queryParams = append(queryParams, req.Creator)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.IsChatModel) {
|
|
|
|
|
|
whereCondition.WriteString(fmt.Sprintf(" AND %s = (?) ", entity.AsynchModelCol.IsChatModel))
|
|
|
|
|
|
queryParams = append(queryParams, req.IsChatModel)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.ModelName) {
|
|
|
|
|
|
whereCondition.WriteString(fmt.Sprintf(" AND %s = (?) ", entity.AsynchModelCol.ModelName))
|
|
|
|
|
|
queryParams = append(queryParams, req.ModelName)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 完整 SQL
|
|
|
|
|
|
sql := `SELECT * FROM "asynch_models" WHERE "deleted_at" IS NULL` + whereCondition.String()
|
|
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx, sql, queryParams...)
|
2026-04-29 15:54:14 +08:00
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-29 18:06:50 +08:00
|
|
|
|
var i []*entity.AsynchModel
|
|
|
|
|
|
if err = r.Structs(&i); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, item := range i {
|
|
|
|
|
|
m = item
|
|
|
|
|
|
}
|
2026-04-29 15:54:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// GetByAcrossTenant 按ID获取(跨租户,查所有租户)
|
|
|
|
|
|
func (d *modelDao) GetByAcrossTenant(ctx context.Context, req *entity.AsynchModel, fields ...string) (m *entity.AsynchModel, err error) {
|
|
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
2026-05-15 14:56:26 +08:00
|
|
|
|
NoTenantId(ctx).
|
2026-05-21 10:41:37 +08:00
|
|
|
|
OmitEmpty().
|
|
|
|
|
|
Where(entity.AsynchModelCol.Id, req.Id).
|
2026-05-15 14:56:26 +08:00
|
|
|
|
Where(entity.AsynchModelCol.Creator, req.Creator).
|
2026-05-21 10:41:37 +08:00
|
|
|
|
Where(entity.AsynchModelCol.IsChatModel, req.IsChatModel).
|
|
|
|
|
|
Where(entity.AsynchModelCol.ModelName, req.ModelName).
|
|
|
|
|
|
Fields(fields).One()
|
2026-04-29 15:54:14 +08:00
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return
|
2026-05-18 19:19:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
err = r.Struct(&m)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// GetByCreatorAndPlatform 按创建者、平台获取
|
2026-05-15 14:56:26 +08:00
|
|
|
|
func (d *modelDao) GetByCreatorAndPlatform(ctx context.Context, req *dto.ListModelReq) (list []*entity.AsynchModel, total int, err error) {
|
|
|
|
|
|
sql := `
|
|
|
|
|
|
SELECT DISTINCT ON (model_name) *
|
|
|
|
|
|
FROM asynch_models
|
|
|
|
|
|
WHERE deleted_at IS NULL
|
|
|
|
|
|
AND (? = '' OR model_name LIKE ?)
|
|
|
|
|
|
`
|
|
|
|
|
|
args := []any{
|
|
|
|
|
|
req.ModelName, "%" + req.ModelName + "%",
|
|
|
|
|
|
}
|
2026-05-29 17:54:19 +08:00
|
|
|
|
|
|
|
|
|
|
// modelType: 传 6 模糊匹配 6%
|
|
|
|
|
|
if req.ModelType > 0 {
|
|
|
|
|
|
prefix := strconv.Itoa(req.ModelType)[:1] // 截取第一位
|
|
|
|
|
|
sql += ` AND model_type::text LIKE ? `
|
|
|
|
|
|
args = append(args, prefix+"%")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if !g.IsEmpty(req.IsPrivate) {
|
|
|
|
|
|
sql += ` AND is_private = ? `
|
|
|
|
|
|
args = append(args, req.IsPrivate)
|
|
|
|
|
|
}
|
2026-05-29 17:54:19 +08:00
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if req.IsOwner != nil && *req.IsOwner == 0 {
|
2026-05-15 16:04:18 +08:00
|
|
|
|
if req.Enabled != nil && *req.Enabled == 1 {
|
|
|
|
|
|
sql += ` AND creator = ? AND is_owner = ? AND enabled=1 `
|
|
|
|
|
|
} else if req.Enabled != nil && *req.Enabled == 0 {
|
|
|
|
|
|
sql += ` AND creator = ? AND is_owner = ? AND enabled=0 `
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sql += ` AND creator = ? AND is_owner = ? `
|
|
|
|
|
|
}
|
2026-05-29 17:54:19 +08:00
|
|
|
|
args = append(args, req.Creator, req.IsOwner)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
} else if req.IsOwner != nil && *req.IsOwner == 1 {
|
|
|
|
|
|
if req.Enabled != nil && *req.Enabled == 1 {
|
|
|
|
|
|
sql += ` AND ((creator = ? AND is_owner = ? AND enabled=1) OR (is_owner = 0 AND enabled=1)) `
|
|
|
|
|
|
} else if req.Enabled != nil && *req.Enabled == 0 {
|
|
|
|
|
|
sql += ` AND ((creator = ? AND is_owner = ? AND enabled=0) OR (is_owner = 0 AND enabled=1)) `
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sql += ` AND ((creator = ? AND is_owner = ?) OR (is_owner = 0 AND enabled=1)) `
|
|
|
|
|
|
}
|
2026-05-29 17:54:19 +08:00
|
|
|
|
args = append(args, req.Creator, req.IsOwner)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sql += ` ORDER BY model_name, is_owner DESC, created_at DESC`
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx, sql, args...)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, 0, err
|
2026-05-12 14:08:29 +08:00
|
|
|
|
}
|
2026-05-12 13:45:08 +08:00
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
err = r.Structs(&list)
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return nil, 0, err
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
total = len(list)
|
2026-05-12 13:45:08 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
// GetByModelNameForTenant 后台任务使用:按 tenant_id + model_name 查询,不依赖 gfdb Hook/Trace/用户上下文
|
|
|
|
|
|
func (d *modelDao) GetByModelNameForTenant(ctx context.Context, tenantId uint64, modelName string) (m *entity.AsynchModel, err error) {
|
|
|
|
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx,
|
|
|
|
|
|
"SELECT * FROM "+public.TableNameModel+" WHERE tenant_id=? AND model_name=? AND deleted_at IS NULL LIMIT 1",
|
|
|
|
|
|
tenantId, modelName,
|
|
|
|
|
|
)
|
2026-04-29 15:54:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
if r.IsEmpty() {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var list []*entity.AsynchModel
|
|
|
|
|
|
if err := r.Structs(&list); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return list[0], nil
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|