pg_user_repository.go 4.3 KB
package repository

import (
	"fmt"

	"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,
		Email:        user.Email,
		Status:       user.Status,
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
		CreatedAt:    user.CreatedAt,
		UpdatedAt:    user.UpdatedAt,
		DeletedAt:    user.DeletedAt,
	}
	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,
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
		CreatedAt:    user.CreatedAt,
		UpdatedAt:    user.UpdatedAt,
		DeletedAt:    user.DeletedAt,
	}
	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 {
	tx := repo.transactionContext.PgTx
	uModel := models.User{}
	_, err := tx.Model(&uModel).Where("id in (?)", pg.In(userId)).Delete()
	return err
}

func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
	tx := repo.transactionContext.PgTx
	userModel := models.User{}
	query := tx.Model(&userModel).Where("deleted_at isnull")
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_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)
	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["status"]; ok {
		query.Where("status=?", v)
	}
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
		query.Where("name like ?", fmt.Sprintf("%%%v%%", 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,
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
		UpdatedAt:    user.UpdatedAt,
		DeletedAt:    user.DeletedAt,
		CreatedAt:    user.CreatedAt,
	}
}