pg_user_repository.go 3.7 KB
package repository

import (
	"time"

	"github.com/go-pg/pg/v10"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)

type UserRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.UserRepository = (*UserRepository)(nil)

func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
	return &UserRepository{
		transactionContext: tx,
	}
}

func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
	userModel := models.User{
		Id:        user.Id,
		Account:   user.Account,
		AvatarUrl: user.AvatarUrl,
		CompanyId: user.CompanyId,
		AdminType: user.AdminType,
		Name:      user.Name,
		Status:    user.Status,
		UpdateAt:  user.UpdateAt,
		CreateAt:  user.CreateAt,
		DeleteAt:  user.DeleteAt,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&userModel).Insert()
	if err != nil {
		return nil, err
	}
	user.Id = userModel.Id
	return user, nil
}

func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
	userModel := models.User{
		Id:        user.Id,
		Account:   user.Account,
		AvatarUrl: user.AvatarUrl,
		CompanyId: user.CompanyId,
		AdminType: user.AdminType,
		Name:      user.Name,
		Email:     user.Email,
		Status:    user.Status,
		UpdateAt:  user.UpdateAt,
		CreateAt:  user.CreateAt,
		DeleteAt:  user.DeleteAt,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&userModel).WherePK().Update()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (repo *UserRepository) Remove(userId []int64) error {
	nowTime := time.Now()
	tx := repo.transactionContext.PgTx
	uModel := models.User{}
	_, err := tx.Model(&uModel).
		Set("delete_at", nowTime).
		Where("id in (?)", pg.In(userId)).
		Update()
	return err
}

func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
	tx := repo.transactionContext.PgTx
	userModel := models.User{}
	query := tx.Model(&userModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	err := query.First()
	if err == pg.ErrNoRows {
		return nil, ErrNoRows
	}
	if err != nil {
		return nil, err
	}
	result := repo.TransformToUserDomain(&userModel)
	return result, nil
}

func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*domain.User, error) {
	tx := repo.transactionContext.PgTx
	userModel := []models.User{}
	query := tx.Model(&userModel).Where("delete_at isnull").
		Limit(20)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id=?", v)
	}
	if v, ok := queryOptions["adminType"]; ok {
		query.Where("admin_type=?", v)
	}
	if v, ok := queryOptions["account"]; ok {
		query.Where("account like ?", v)
	}
	if v, ok := queryOptions["offset"]; ok {
		query.Offset(v.(int))
	}
	if v, ok := queryOptions["limit"]; ok {
		query.Limit(v.(int))
	}
	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var resultList []*domain.User
	for i := range userModel {
		result := repo.TransformToUserDomain(&userModel[i])
		resultList = append(resultList, result)
	}
	return cnt, resultList, nil
}

func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User {
	return &domain.User{
		Id:        user.Id,
		Account:   user.Account,
		AvatarUrl: user.AvatarUrl,
		CompanyId: user.CompanyId,
		AdminType: user.AdminType,
		Name:      user.Name,
		Email:     user.Email,
		Status:    user.Status,
		UpdateAt:  user.UpdateAt,
		CreateAt:  user.CreateAt,
		DeleteAt:  user.DeleteAt,
	}
}