pg_users_repository.go 4.5 KB
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"
	"github.com/tiptok/godevp/pkg/domain/users"
	domain "github.com/tiptok/godevp/pkg/domain/users"
	"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
}

func (repository *UsersRepository) nextIdentify() (int64, error) {
	return 0, nil
}
func (repository *UsersRepository) Save(users *users.Users) (*users.Users, error) {
	tx := repository.transactionContext.PgTx
	if users.Identify() == nil {
		_, err := repository.nextIdentify()
		if err != nil {
			return users, err
		}
		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 ("+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 "+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
		}
	}
	return users, nil
}
func (repository *UsersRepository) Remove(users *users.Users) (*users.Users, error) {
	tx := repository.transactionContext.PgTx
	usersModel := new(models.Users)
	usersModel.Id = users.Identify().(int64)
	if _, err := tx.Model(usersModel).WherePK().Delete(); err != nil {
		return users, err
	}
	return users, nil
}
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*users.Users, error) {
	tx := repository.transactionContext.PgTx
	usersModel := new(models.Users)
	query := ormx.NewQuery(tx.Model(usersModel), queryOptions)
	if usersId, ok := queryOptions["usersId"]; ok {
		query.Where("users.id = ?", usersId)
	}
	if phone, ok := queryOptions["phone"]; ok {
		query.Where("users.phone = ?", phone)
	}
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	if usersModel.Id == 0 {
		return nil, nil
	} else {
		return repository.transformPgModelToDomainModel(usersModel)
	}
}
func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*users.Users, error) {
	tx := repository.transactionContext.PgTx
	var usersModels []*models.Users
	userss := make([]*users.Users, 0)
	query := ormx.NewQuery(tx.Model(&usersModels), queryOptions)
	query.SetLimit()
	if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
		return 0, userss, err
	} else {
		for _, usersModel := range usersModels {
			if users, err := repository.transformPgModelToDomainModel(usersModel); err != nil {
				return 0, userss, err
			} else {
				userss = append(userss, users)
			}
		}
		return int64(count), userss, nil
	}
}
func (repository *UsersRepository) transformPgModelToDomainModel(usersModel *models.Users) (*domain.Users, error) {
	return &users.Users{
		Id:         usersModel.Id,
		Name:       usersModel.Name,
		Phone:      usersModel.Phone,
		Passwd:     usersModel.Passwd,
		Roles:      usersModel.Roles,
		Status:     usersModel.Status,
		AdminType:  usersModel.AdminType,
		CreateTime: usersModel.CreateTime,
		UpdateTime: usersModel.UpdateTime,
	}, nil
}
func NewUsersRepository(transactionContext *pgTransaction.TransactionContext) (*UsersRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &UsersRepository{
			transactionContext: transactionContext,
		}, nil
	}
}