@@ -22,6 +22,12 @@ type (
2222 {{range .Fields }} {{.CamelName }} {{.DataType }} ` gorm:"{{.Name}} {{- if .IsPrimary}};primary_key{{end}}"` //{{.Comment }}
2323 {{end }}}
2424
25+ // {{.CamelName }} query cond
26+ {{.CamelName }}Query struct {
27+ SearchBase
28+ {{.CamelName }}
29+ }
30+
2531 {{.CamelName }}Model interface {
2632 // Trans Transaction
2733 Trans(ctx context.Context , fn func(ctx context.Context , db *gorm.DB ) (err error)) (err error)
@@ -58,9 +64,13 @@ func New{{.CamelName}}Model(db *gorm.DB {{- if .IsCache}}, c cache.CacheConf{{en
5864 }
5965}
6066
67+ func (m *{{.CamelName }}) TableName() string {
68+ return " `{{.Name}}`"
69+ }
70+
6171func (m *default{{.CamelName }}Model) conn(ctx context.Context , db ... *gorm.DB ) *gorm.DB {
6272 if len (db) == 0 {
63- return m.db.Table (m .table ).Session (&gorm.Session {Context: ctx})
73+ return m.db.Model (&{{ .CamelName }}{} ).Session (&gorm.Session {Context: ctx})
6474 }
6575 return db[0]
6676}
@@ -89,7 +99,7 @@ func (m *default{{.CamelName}}Model) Update(ctx context.Context, data *{{.CamelN
8999 cacheKey := fmt.Sprintf (" %s {{.PrimaryFmt}}" , cache{{.CamelName }}PrimaryPrefix, {{.PrimaryFmtV }})
90100 {{end -}}
91101 return m.Exec (ctx, func() error {
92- return m.conn (ctx, db... ).Updates (data).Error
102+ return m.conn (ctx, db... ).Model (&data) . Updates (data).Error
93103 } {{- if .IsCache }}, cacheKey{{end }})
94104}
95105
@@ -112,7 +122,7 @@ func (m *default{{.CamelName}}Model) ForceDelete(ctx context.Context, {{.Primary
112122}
113123
114124func (m *default{{.CamelName }}Model) Count(ctx context.Context , cond *{{.CamelName }}Query) (total int64, err error) {
115- err = m.conn (ctx, nil ).Scopes (
125+ err = m.conn (ctx).Scopes (
116126 searchPlusScope(cond.SearchPlus , m.table ),
117127 ).Where (cond. {{.CamelName }}).Count (&total).Error
118128 return total, err
@@ -144,11 +154,7 @@ func (m *default{{.CamelName}}Model) FindByPage(ctx context.Context, cond *{{.Ca
144154 searchPlusScope(cond.SearchPlus , m.table ),
145155 ).Where (cond. {{.CamelName }})
146156
147- conn.Count (&total)
148- err = conn.Scopes (
149- pageScope(cond.PageCurrent , cond.PageSize ),
150- orderScope(cond.OrderSort... ),
151- ).Find (&list).Error
157+ total, list, err = pageHandler[*{{.CamelName }}](conn, cond.PageCurrent , cond.PageSize )
152158 return total, list, err
153159}
154160
@@ -174,6 +180,7 @@ func (m *default{{.CamelName}}Model) FindListByCursor(ctx context.Context, cond
174180
175181func (m *default{{.CamelName }}Model) FindAll(ctx context.Context , cond *{{.CamelName }}Query) (list []*{{.CamelName }}, err error) {
176182 conn := m.conn (ctx).Scopes (
183+ searchPlusScope(cond.SearchPlus , m.table ),
177184 orderScope(cond.OrderSort... ),
178185 )
179186 err = conn.Where (cond. {{.CamelName }}).Find (&list).Error
0 commit comments