...
|
...
|
@@ -2,6 +2,8 @@ package repository |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/ormx"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
...
|
...
|
@@ -10,6 +12,15 @@ import ( |
|
|
"github.com/tiptok/godevp/pkg/infrastructure/pg/models"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
usersFields = ormx.FieldNames((*models.Users)(nil))
|
|
|
usersRowString = strings.Join(usersFields, ",")
|
|
|
usersRowInsertFields = ormx.Remove(usersFields, "id")
|
|
|
usersRowInsertString = strings.Join(usersRowInsertFields, ",")
|
|
|
usersRowsInsertPlaceHolder = ormx.ParamPlaceHolder(len(usersRowInsertFields))
|
|
|
usersRowsUpdate = strings.Join(ormx.Remove(usersFields, "id"), "=?,") + "=?"
|
|
|
)
|
|
|
|
|
|
type UsersRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
...
|
...
|
@@ -26,14 +37,14 @@ func (repository *UsersRepository) Save(users *users.Users) (*users.Users, error |
|
|
}
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
|
|
|
"INSERT INTO users (name, phone, passwd, roles, status, admin_type, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
|
|
|
"INSERT INTO users ("+usersRowInsertString+") VALUES ("+usersRowsInsertPlaceHolder+") RETURNING "+usersRowString,
|
|
|
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime); err != nil {
|
|
|
return users, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
|
|
|
"UPDATE users SET name=?, phone=?, passwd=?, roles=?, status=?, admin_type=?, create_time=?, update_time=? WHERE id=? RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
|
|
|
"UPDATE users SET "+usersRowsUpdate+" WHERE id=? RETURNING "+usersRowString,
|
|
|
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime, users.Identify()); err != nil {
|
|
|
return users, err
|
|
|
}
|
...
|
...
|
@@ -52,12 +63,12 @@ func (repository *UsersRepository) Remove(users *users.Users) (*users.Users, err |
|
|
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*users.Users, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
usersModel := new(models.Users)
|
|
|
query := tx.Model(usersModel)
|
|
|
query := ormx.NewQuery(tx.Model(usersModel), queryOptions)
|
|
|
if usersId, ok := queryOptions["usersId"]; ok {
|
|
|
query = query.Where("users.id = ?", usersId)
|
|
|
query.Where("users.id = ?", usersId)
|
|
|
}
|
|
|
if phone, ok := queryOptions["phone"]; ok {
|
|
|
query = query.Where("users.phone = ?", phone)
|
|
|
query.Where("users.phone = ?", phone)
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
...
|
...
|
@@ -76,23 +87,8 @@ func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (in |
|
|
tx := repository.transactionContext.PgTx
|
|
|
var usersModels []*models.Users
|
|
|
userss := make([]*users.Users, 0)
|
|
|
query := tx.Model(&usersModels)
|
|
|
if offset, ok := queryOptions["offset"]; ok {
|
|
|
offset := offset.(int)
|
|
|
if offset > -1 {
|
|
|
query = query.Offset(offset)
|
|
|
}
|
|
|
} else {
|
|
|
query = query.Offset(0)
|
|
|
}
|
|
|
if limit, ok := queryOptions["limit"]; ok {
|
|
|
limit := limit.(int)
|
|
|
if limit > -1 {
|
|
|
query = query.Limit(limit)
|
|
|
}
|
|
|
} else {
|
|
|
query = query.Limit(20)
|
|
|
}
|
|
|
query := ormx.NewQuery(tx.Model(&usersModels), queryOptions)
|
|
|
query.SetLimit()
|
|
|
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
|
|
|
return 0, userss, err
|
|
|
} else {
|
...
|
...
|
|