pg_users_repository.go 3.6 KB
package repository

import (
	"fmt"

	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)

type UsersRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *UsersRepository) Save(dm *domain.Users) (*domain.Users, error) {
	var (
		err error
		m   = &models.Users{}
		tx  = repository.transactionContext.PgTx
	)
	if err = GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if _, err = tx.Model(m).Returning("*").Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if _, err = tx.Model(m).Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *UsersRepository) Remove(Users *domain.Users) (*domain.Users, error) {
	var (
		tx         = repository.transactionContext.PgTx
		UsersModel = &models.Users{Id: Users.Identify().(int64)}
	)
	if _, err := tx.Model(UsersModel).Where("id = ?", Users.Id).Delete(); err != nil {
		return Users, err
	}
	return Users, nil
}

func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*domain.Users, error) {
	tx := repository.transactionContext.PgTx
	UsersModel := new(models.Users)
	query := NewQuery(tx.Model(UsersModel), queryOptions)
	query.SetWhere("id = ?", "id")
	query.SetWhere("open_id = ?", "openId")
	query.SetWhere("status = ?", "status")
	query.SetWhere("phone = ?", "phone")
	query.SetWhere("company_id = ?", "companyId")
	query.SetWhere(`delete_at is null`, "deleteAtIsNull")
	query.SetWhere("is_senior = ?", "isSenior")
	query.SetWhere("company_id <> ?", "isNot")

	if err := query.First(); err != nil {
		return nil, fmt.Errorf("query row not found")
	}
	if UsersModel.Id == 0 {
		return nil, fmt.Errorf("query row not found")
	}
	return repository.transformPgModelToDomainModel(UsersModel)
}

func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Users, error) {
	tx := repository.transactionContext.PgTx
	var UsersModels []*models.Users
	Userss := make([]*domain.Users, 0)
	query := NewQuery(tx.Model(&UsersModels), queryOptions).
		SetWhere("id = ?", "id").
		SetWhere("phone = ?", "phone").
		SetWhere("company_id = ?", "companyId").
		SetWhere(`status = ?`, "status").
		SetWhere(`delete_at is null`, "deleteAtIsNull").
		SetWhere("is_senior = ?", "isSenior").
		SetWhere("company_id <> ?", "isNot").
		SetOrder("create_at", "sortByCreateTime").
		SetOrder("update_at", "sortByUpdateTime")

	if v, ok := queryOptions["inCompanyIds"]; ok {
		query.Where("company_id in (?)", pg.In(v))
	}
	query.SetWhere("admin_type=?", "adminType")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, Userss, err
	}
	for _, UsersModel := range UsersModels {
		if Users, err := repository.transformPgModelToDomainModel(UsersModel); err != nil {
			return 0, Userss, err
		} else {
			Userss = append(Userss, Users)
		}
	}
	return int64(query.AffectRow), Userss, nil
}

func (repository *UsersRepository) transformPgModelToDomainModel(UsersModel *models.Users) (*domain.Users, error) {
	m := &domain.Users{}
	err := GobModelTransform(m, UsersModel)
	return m, err
}

func NewUsersRepository(transactionContext *transaction.TransactionContext) (*UsersRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &UsersRepository{transactionContext: transactionContext}, nil
}