177 lines
5.2 KiB
Go
177 lines
5.2 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"quyun/database/table"
|
|
|
|
. "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(table.Users.DeletedAt.IS_NULL())
|
|
}
|
|
}
|
|
|
|
func (m *Users) CondID(id int64) Cond {
|
|
return func(cond BoolExpression) BoolExpression {
|
|
return cond.AND(table.Users.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 := table.Medias.INSERT(table.Users.MutableColumns).MODEL(m).RETURNING(table.Medias.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 := table.Users.INSERT(table.Users.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 := table.Users.UPDATE().SET(table.Users.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Users.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 := table.Users.UPDATE().SET(table.Users.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Users.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").Infof("Users items soft deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Users) ForceDelete(ctx context.Context) error {
|
|
stmt := table.Users.DELETE().WHERE(table.Users.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 := table.Users.DELETE().WHERE(table.Users.ID.IN(condIds...))
|
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
|
|
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
|
m.log().WithField("func", "BatchDelete").Errorf("error deleting Users items: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "BatchDelete").Infof("Users items deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Users) Update(ctx context.Context) error {
|
|
m.UpdatedAt = time.Now()
|
|
|
|
stmt := table.Users.UPDATE(table.Users.MutableColumns.Except(usersUpdateExcludeColumns...)).SET(m).WHERE(table.Users.ID.EQ(Int(m.ID))).RETURNING(table.Users.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 := table.Users.SELECT(table.Users.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 := table.Users
|
|
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
|
|
}
|