From ab36ea0e5da38dd9528cae0a535c78400d81723d Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Fri, 23 May 2025 22:21:28 +0800 Subject: [PATCH] feat: update --- pkg/ast/model/table.go.tpl | 10 +++----- pkg/ast/model/table_funcs.go.tpl | 40 +++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/ast/model/table.go.tpl b/pkg/ast/model/table.go.tpl index f14317b..5b50ae2 100644 --- a/pkg/ast/model/table.go.tpl +++ b/pkg/ast/model/table.go.tpl @@ -1,9 +1,5 @@ package model -import ( - log "github.com/sirupsen/logrus" -) - -func (m *{{.PascalTable}}) log() *log.Entry { - return log.WithField("model", "{{.PascalTable}}Model") -} \ No newline at end of file +var {{.CamelTable}}UpdateExcludeColumns = []Column{ + // table.{{.PascalTable}}.CreatedAt, +} diff --git a/pkg/ast/model/table_funcs.go.tpl b/pkg/ast/model/table_funcs.go.tpl index 4b000f0..b544fac 100644 --- a/pkg/ast/model/table_funcs.go.tpl +++ b/pkg/ast/model/table_funcs.go.tpl @@ -44,8 +44,40 @@ func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalT return nil } -// Delete + func (m *{{.PascalTable}}) Delete(ctx context.Context) error { + stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))) + m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql()) + + if err := stmt.QueryContext(ctx, db, m); err != nil { + m.log().WithField("func","SoftDelete").Errorf("error soft deleting {{.PascalTable}} item: %v", err) + return err + } + + m.log().WithField("func", "SoftDelete").Infof("{{.PascalTable}} item soft deleted successfully") + return nil +} + +// BatchDelete +func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error { + condIds := lo.Map(ids, func(id int64, _ int) Expression { + return Int64(id) + }) + + stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.IN(condIds...)) + m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql()) + + if err := stmt.QueryContext(ctx, db, m); err != nil { + m.log().WithField("func","BatchSoftDelete").Errorf("error soft deleting {{.PascalTable}} items: %v", err) + return err + } + + m.log().WithField("func", "BatchSoftDelete").Infof("{{.PascalTable}} items soft deleted successfully") + return nil +} + + +func (m *{{.PascalTable}}) ForceDelete(ctx context.Context) error { stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))) m.log().WithField("func", "Delete").Info(stmt.DebugSql()) @@ -58,7 +90,7 @@ func (m *{{.PascalTable}}) Delete(ctx context.Context) error { return nil } -func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error { +func (m *{{.PascalTable}}) BatchForceDelete(ctx context.Context, ids []int64) error { condIds := lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) }) @@ -76,7 +108,9 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error { } func (m *{{.PascalTable}}) Update(ctx context.Context) error { - stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(table.{{.PascalTable}}.AllColumns) + m.UpdatedAt = time.Now() + + stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns.Except({{.CamelTable}}UpdateExcludeColumns...)).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(table.{{.PascalTable}}.AllColumns) m.log().WithField("func", "Update").Info(stmt.DebugSql()) if err := stmt.QueryContext(ctx, db, m); err != nil {