pg_users_repository.go 4.5 KB
package repository

import (
	"fmt"

	"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"
)

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 (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",
			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",
			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 := tx.Model(usersModel)
	if usersId, ok := queryOptions["usersId"]; ok {
		query = query.Where("users.id = ?", usersId)
	}
	if phone, ok := queryOptions["phone"]; ok {
		query = 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 := 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)
	}
	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
	}
}