103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
|
|
package pull
|
||
|
|
|
||
|
|
import (
|
||
|
|
"ai-agent/workflow/consts/public"
|
||
|
|
pullDto "ai-agent/workflow/model/dto/pull"
|
||
|
|
"ai-agent/workflow/model/entity"
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gitea.com/red-future/common/db/gfdb"
|
||
|
|
"github.com/gogf/gf/v2/util/gconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
var ActivePullDao = &activePullDao{}
|
||
|
|
|
||
|
|
type activePullDao struct{}
|
||
|
|
|
||
|
|
// Insert 创建执行记录
|
||
|
|
func (d *activePullDao) Insert(ctx context.Context, req *pullDto.CreateActivePullReq) (id int64, err error) {
|
||
|
|
var activePull = new(entity.ActivePull)
|
||
|
|
err = gconv.Struct(req, &activePull)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameActivePull).Insert(activePull)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.LastInsertId()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *activePullDao) Update(ctx context.Context, req *pullDto.UpdateActivePullReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameActivePull).OmitEmpty().Data(&req).Where(entity.ActivePullCol.Id, req.Id).Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *activePullDao) Delete(ctx context.Context, req *pullDto.DeleteActivePullReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameActivePull).Where(entity.ActivePullCol.Id, req.Id).Delete()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *activePullDao) List(ctx context.Context, req *pullDto.ListActivePullReq, fields ...string) (res []*entity.ActivePull, total int, err error) {
|
||
|
|
model := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameActivePull).Fields(fields).OmitEmpty()
|
||
|
|
model.OrderDesc(entity.ActivePullCol.CreatedAt)
|
||
|
|
if req.Page != nil {
|
||
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||
|
|
}
|
||
|
|
r, total, err := model.AllAndCount(false)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Structs(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *activePullDao) ListNative(ctx context.Context, req *pullDto.ListActivePullReq, fields ...string) (res []*entity.ActivePull, total int, err error) {
|
||
|
|
db := gfdb.DB(ctx, public.DbNameBlackDeacon)
|
||
|
|
|
||
|
|
// Select fields
|
||
|
|
selectFields := "*"
|
||
|
|
if len(fields) > 0 {
|
||
|
|
selectFields = strings.Join(fields, ",")
|
||
|
|
}
|
||
|
|
|
||
|
|
// Build count query first for total
|
||
|
|
countSql := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE deleted_at is null", "black_deacon_"+public.TableNameActivePull)
|
||
|
|
countResult, err := db.GetAll(ctx, countSql)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
if len(countResult) > 0 {
|
||
|
|
total = countResult[0]["COUNT(*)"].Int()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Build data query with native SQL
|
||
|
|
sql := fmt.Sprintf("SELECT %s FROM %s WHERE deleted_at is null ORDER BY created_at DESC", selectFields, "black_deacon_"+public.TableNameActivePull)
|
||
|
|
if req.Page != nil && req.Page.PageNum > 0 && req.Page.PageSize > 0 {
|
||
|
|
offset := (req.Page.PageNum - 1) * req.Page.PageSize
|
||
|
|
sql += fmt.Sprintf(" LIMIT %d OFFSET %d", req.Page.PageSize, offset)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Execute query with GetAll
|
||
|
|
result, err := db.GetAll(ctx, sql)
|
||
|
|
if err != nil {
|
||
|
|
return nil, total, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Scan to entity slice
|
||
|
|
var models []*entity.ActivePull
|
||
|
|
if err = result.Structs(&models); err != nil {
|
||
|
|
return nil, total, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return models, total, nil
|
||
|
|
}
|