新增 TakeBusinessFields、WriteBusinessFields、SetByPath 与 GetByPath 等工具,支持按映射路径写入请求体与解析响应,并更新相关依赖。
98 lines
2.8 KiB
Go
98 lines
2.8 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 ModelTaskStart = &modelTaskStartDao{}
|
||
|
||
type modelTaskStartDao struct{}
|
||
|
||
// Insert 插入
|
||
func (d *modelTaskStartDao) Insert(ctx context.Context, req *dto.CreateModelTaskStartReq) (id int64, err error) {
|
||
m := new(entity.ModelTaskStart)
|
||
err = gconv.Struct(req, &m)
|
||
if err != nil {
|
||
return
|
||
}
|
||
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelTaskStart).Insert(m)
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.LastInsertId()
|
||
}
|
||
|
||
// Update 更新(按ID)
|
||
func (d *modelTaskStartDao) Update(ctx context.Context, req *dto.UpdateModelTaskStartReq) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelTaskStart).
|
||
OmitEmpty().
|
||
Data(req).
|
||
Where(entity.ModelTaskStartCol.Id, req.Id).
|
||
Update()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
func (d *modelTaskStartDao) Delete(ctx context.Context, req *dto.DeleteModelTaskStartReq) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameModelGateway).Model(ctx, public.TableNameModelTaskStart).
|
||
Where(entity.ModelTaskStartCol.Id, req.Id).
|
||
Delete()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
func (d *modelTaskStartDao) ListByLimitNotTenantId(ctx context.Context, req *dto.GetModelTaskStartListReq, fields ...string) (res []entity.ModelTaskStart, err error) {
|
||
// 获取表前缀
|
||
prefix := g.Cfg().MustGet(ctx, fmt.Sprintf("database.%s.0.prefix", public.DbNameModelGateway)).String()
|
||
table := prefix + public.TableNameModelTaskStart
|
||
// 动态拼接 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
|
||
whereCondition = whereCondition + fmt.Sprintf(" AND %s != '' ", entity.ModelTaskStartCol.TaskId)
|
||
whereCondition = whereCondition + fmt.Sprintf(" AND %s IS NULL ", entity.ModelTaskStartCol.DeletedAt)
|
||
// 排序
|
||
orderSql := fmt.Sprintf(" ORDER BY %s ASC ", entity.ModelTaskStartCol.CreatedAt)
|
||
// 分页
|
||
limitSql := ""
|
||
if req.Page != nil {
|
||
pageNum := int(req.Page.PageNum)
|
||
pageSize := int(req.Page.PageSize)
|
||
offset := (pageNum - 1) * pageSize
|
||
limitSql = fmt.Sprintf(" LIMIT %d OFFSET %d ", pageSize, offset)
|
||
}
|
||
// 查询
|
||
sql := `SELECT ` + field + ` FROM ` + table + ` WHERE 1=1 ` + whereCondition + orderSql + limitSql + ``
|
||
// 执行查询
|
||
result, err := gfdb.DB(ctx, public.DbNameModelGateway).GetAll(ctx, sql)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
err = result.Structs(&res)
|
||
|
||
return
|
||
}
|