fix: 修复模型查询条件为空时的异常行为
This commit is contained in:
@@ -2,10 +2,12 @@ package dao
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"model-gateway/consts/public"
|
"model-gateway/consts/public"
|
||||||
"model-gateway/model/dto"
|
"model-gateway/model/dto"
|
||||||
"model-gateway/model/entity"
|
"model-gateway/model/entity"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitea.com/red-future/common/db/gfdb"
|
"gitea.com/red-future/common/db/gfdb"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
@@ -58,17 +60,49 @@ func (d *modelDao) Delete(ctx context.Context, req *entity.AsynchModel) (rows in
|
|||||||
|
|
||||||
// Get 按ID获取(带租户隔离,只查当前租户)
|
// Get 按ID获取(带租户隔离,只查当前租户)
|
||||||
func (d *modelDao) Get(ctx context.Context, req *entity.AsynchModel, fields ...string) (m *entity.AsynchModel, err error) {
|
func (d *modelDao) Get(ctx context.Context, req *entity.AsynchModel, fields ...string) (m *entity.AsynchModel, err error) {
|
||||||
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
//r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModel).
|
||||||
OmitEmpty().
|
// OmitEmpty().
|
||||||
Where(entity.AsynchModelCol.Id, req.Id).
|
// Where(entity.AsynchModelCol.Id, req.Id).
|
||||||
Where(entity.AsynchModelCol.Creator, req.Creator).
|
// Where(entity.AsynchModelCol.Creator, req.Creator).
|
||||||
Where(entity.AsynchModelCol.IsChatModel, req.IsChatModel).
|
// Where(entity.AsynchModelCol.IsChatModel, req.IsChatModel).
|
||||||
Where(entity.AsynchModelCol.ModelName, req.ModelName).
|
// Where(entity.AsynchModelCol.ModelName, req.ModelName).
|
||||||
Fields(fields).One()
|
// 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...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = r.Struct(&m)
|
var i []*entity.AsynchModel
|
||||||
|
if err = r.Structs(&i); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, item := range i {
|
||||||
|
m = item
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user