194 lines
5.7 KiB
Go
194 lines
5.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"model-gateway/consts/public"
|
|
"model-gateway/model/dto"
|
|
"model-gateway/model/entity"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var ModelManage = &modelManageDao{}
|
|
|
|
type modelManageDao struct{}
|
|
|
|
// Insert 插入
|
|
func (d *modelManageDao) Insert(ctx context.Context, req *dto.CreateModelManageReq) (id int64, err error) {
|
|
var e = new(entity.ModelManage)
|
|
err = gconv.Struct(req, &e)
|
|
if err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelManage).Insert(e)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新
|
|
func (d *modelManageDao) Update(ctx context.Context, req *dto.UpdateModelManageReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelManage).OmitEmpty().Data(req).Where(entity.ModelManageCol.Id, req.Id).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除
|
|
func (d *modelManageDao) Delete(ctx context.Context, req *dto.DeleteModelManageReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelManage).Where(entity.ModelManageCol.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
func (d *modelManageDao) Get(ctx context.Context, req *dto.GetModelManage, fields ...string) (res *entity.ModelManage, err error) {
|
|
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelManage).Cache(ctx).
|
|
OmitEmpty().
|
|
Where(entity.ModelManageCol.ModelName, req.ModelName).
|
|
Where(entity.ModelManageCol.ChatModel, req.ChatModel).
|
|
Where(entity.ModelManageCol.Creator, req.Creator).
|
|
Fields(fields).One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
func (d *modelManageDao) GetNotTenantId(ctx context.Context, req *dto.GetModelManageReq, fields ...string) (res *entity.ModelManage, err error) {
|
|
// 获取表前缀
|
|
prefix := g.Cfg().MustGet(ctx, fmt.Sprintf("database.%s.0.prefix", public.DbNameModelGateway)).String()
|
|
table := prefix + public.TableNameModelManage
|
|
// 动态拼接 SELECT 列
|
|
var field string
|
|
if !g.IsEmpty(fields) {
|
|
for k, v := range fields {
|
|
if k == len(fields)-1 {
|
|
field = field + v
|
|
} else {
|
|
field = field + v + ","
|
|
}
|
|
}
|
|
} else {
|
|
field = "*"
|
|
}
|
|
// 动态拼接 WHERE 条件
|
|
var whereCondition string
|
|
var queryParams []interface{}
|
|
if !g.IsEmpty(req.Id) {
|
|
whereCondition = fmt.Sprintf(" AND %s=(?) ", entity.ModelManageCol.Id)
|
|
queryParams = append(queryParams, req.Id)
|
|
}
|
|
whereCondition = whereCondition + " AND " + entity.ModelManageCol.DeletedAt + " IS NULL "
|
|
|
|
sql := `SELECT ` + field + ` FROM ` + table + ` WHERE 1=1 ` + whereCondition + ``
|
|
// 执行查询
|
|
result, err := gfdb.DB(ctx, public.DbNameModelGateway).GetOne(ctx, sql, queryParams...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = result.Struct(&res)
|
|
return
|
|
}
|
|
func (d *modelManageDao) ListNotTenantId(ctx context.Context, req *dto.ListModelManageReq, fields ...string) (res []*entity.ModelManage, total int, err error) {
|
|
// 获取表前缀
|
|
prefix := g.Cfg().MustGet(ctx, fmt.Sprintf("database.%s.0.prefix", public.DbNameModelGateway)).String()
|
|
table := prefix + public.TableNameModelManage
|
|
|
|
// 动态拼接 SELECT 列
|
|
var field string
|
|
if !g.IsEmpty(fields) {
|
|
for k, v := range fields {
|
|
if k == len(fields)-1 {
|
|
field = field + v
|
|
} else {
|
|
field = field + v + ","
|
|
}
|
|
}
|
|
} else {
|
|
field = "*"
|
|
}
|
|
|
|
// 动态拼接 WHERE 条件
|
|
var whereCondition string
|
|
var queryParams []interface{}
|
|
|
|
if !g.IsEmpty(req.ModelName) {
|
|
whereCondition += fmt.Sprintf(" AND %s=(?) ", entity.ModelManageCol.ModelName)
|
|
queryParams = append(queryParams, req.ModelName)
|
|
}
|
|
if !g.IsEmpty(req.ModelType) {
|
|
whereCondition += fmt.Sprintf(" AND %s=(?) ", entity.ModelManageCol.ModelType)
|
|
queryParams = append(queryParams, req.ModelType)
|
|
}
|
|
if !g.IsEmpty(req.Creator) {
|
|
whereCondition += fmt.Sprintf(" AND (%s=(?) OR %s=true) ", entity.ModelManageCol.Creator, entity.ModelManageCol.SystemModel)
|
|
queryParams = append(queryParams, req.Creator)
|
|
}
|
|
whereCondition = whereCondition + " AND " + entity.ModelManageCol.DeletedAt + " IS NULL "
|
|
|
|
// 1. 统计去重后总条数
|
|
countSql := fmt.Sprintf(
|
|
`SELECT COUNT(DISTINCT %s) FROM %s WHERE 1=1 %s`,
|
|
entity.ModelManageCol.ModelName,
|
|
table,
|
|
whereCondition,
|
|
)
|
|
countResult, err := gfdb.DB(ctx, public.DbNameModelGateway).GetOne(ctx, countSql, queryParams...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
type crr struct {
|
|
Count int64 `db:"count"`
|
|
}
|
|
var cr crr
|
|
if err = countResult.Struct(&cr); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = int(cr.Count)
|
|
// 2. 分页处理
|
|
limitSql := ""
|
|
if req.Page != nil {
|
|
pageNum := int(req.Page.PageNum)
|
|
pageSize := int(req.Page.PageSize)
|
|
offset := (pageNum - 1) * pageSize
|
|
limitSql = fmt.Sprintf(" LIMIT ? OFFSET ? ")
|
|
// PG 语法 LIMIT 条数 OFFSET 偏移量
|
|
queryParams = append(queryParams, pageSize, offset)
|
|
}
|
|
|
|
// 排序优先级:1.分组字段ModelName 2.SystemModel升序(false在前,保留用户数据) 3.创建时间倒序
|
|
orderSql := fmt.Sprintf(
|
|
" ORDER BY %s, %s ASC, %s DESC ",
|
|
entity.ModelManageCol.ModelName,
|
|
entity.ModelManageCol.SystemModel,
|
|
entity.ModelManageCol.CreatedAt,
|
|
)
|
|
|
|
// PG DISTINCT ON 按模型名去重,同名只取第一条(用户数据)
|
|
sql := fmt.Sprintf(
|
|
`SELECT DISTINCT ON (%s) %s FROM %s WHERE 1=1 %s %s %s`,
|
|
entity.ModelManageCol.ModelName,
|
|
field,
|
|
table,
|
|
whereCondition,
|
|
orderSql,
|
|
limitSql,
|
|
)
|
|
|
|
// 执行查询
|
|
result, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx, sql, queryParams...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
err = result.Structs(&res)
|
|
return
|
|
}
|