Files
quyun/backend/app/model/users.funcs.gen.go
2025-05-26 12:25:55 +08:00

181 lines
5.3 KiB
Go

package model
import (
"context"
"time"
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
)
// conds
func (m *Users) CondNotDeleted() Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(tblUsers.DeletedAt.IS_NULL())
}
}
func (m *Users) CondDeleted() Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(tblUsers.DeletedAt.IS_NOT_NULL())
}
}
func (m *Users) CondID(id int64) Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(tblUsers.ID.EQ(Int(id)))
}
}
// funcs
func (m *Users) log() *log.Entry {
return log.WithField("model", "Users")
}
func (m *Users) Create(ctx context.Context) error {
m.CreatedAt = time.Now()
m.UpdatedAt = time.Now()
stmt := tblMedias.INSERT(tblUsers.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
m.log().WithField("func", "Create").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func", "Create").Errorf("error creating Users item: %v", err)
return err
}
m.log().WithField("func", "Create").Infof("Users item created successfully")
return nil
}
func (m *Users) BatchCreate(ctx context.Context, models []*Users) error {
stmt := tblUsers.INSERT(tblUsers.MutableColumns).MODELS(models)
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().WithField("func", "Create").Errorf("error creating Users item: %v", err)
return err
}
m.log().WithField("func", "BatchCreate").Infof("Users items created successfully")
return nil
}
func (m *Users) Delete(ctx context.Context) error {
stmt := tblUsers.UPDATE().SET(tblUsers.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblUsers.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 Users item: %v", err)
return err
}
m.log().WithField("func", "SoftDelete").Infof("Users item soft deleted successfully")
return nil
}
// BatchDelete
func (m *Users) BatchDelete(ctx context.Context, ids []int64) error {
condIds := lo.Map(ids, func(id int64, _ int) Expression {
return Int64(id)
})
stmt := tblUsers.UPDATE().SET(tblUsers.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblUsers.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 Users items: %v", err)
return err
}
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("Users items soft deleted successfully")
return nil
}
func (m *Users) ForceDelete(ctx context.Context) error {
stmt := tblUsers.DELETE().WHERE(tblUsers.ID.EQ(Int(m.ID)))
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().WithField("func", "Delete").Errorf("error deleting Users item: %v", err)
return err
}
m.log().WithField("func", "Delete").Infof("Users item deleted successfully")
return nil
}
func (m *Users) BatchForceDelete(ctx context.Context, ids []int64) error {
condIds := lo.Map(ids, func(id int64, _ int) Expression {
return Int64(id)
})
stmt := tblUsers.DELETE().WHERE(tblUsers.ID.IN(condIds...))
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().WithField("func", "BatchForceDelete").Errorf("error deleting Users items: %v", err)
return err
}
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("Users items deleted successfully")
return nil
}
func (m *Users) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := tblUsers.UPDATE(tblUsersUpdateMutableColumns).MODEL(m).WHERE(tblUsers.ID.EQ(Int(m.ID))).RETURNING(tblUsers.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func", "Update").Errorf("error updating Users item: %v", err)
return err
}
m.log().WithField("func", "Update").Infof("Users item updated successfully")
return nil
}
// GetByCond
func (m *Users) GetByCond(ctx context.Context, conds ...Cond) (*Users, error) {
cond := CondTrue(conds...)
stmt := tblUsers.SELECT(tblUsers.AllColumns).WHERE(cond)
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func", "GetByCond").Errorf("error getting Users item by ID: %v", err)
return nil, err
}
m.log().WithField("func", "GetByCond").Infof("Users item retrieved successfully")
return m, nil
}
// GetByID
func (m *Users) GetByID(ctx context.Context, id int64, conds ...Cond) (*Users, error) {
return m.GetByCond(ctx, CondJoin(m.CondID(id), conds...)...)
}
// Count
func (m *Users) Count(ctx context.Context, conds ...Cond) (int64, error) {
cond := CondTrue(conds...)
tbl := tblUsers
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
m.log().Infof("sql: %s", stmt.DebugSql())
var count struct {
Count int64
}
if err := stmt.QueryContext(ctx, db, &count); err != nil {
m.log().Errorf("error counting Users items: %v", err)
return 0, err
}
return count.Count, nil
}