pg_users_dao.go 1.2 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"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 UsersDao struct {
	transactionContext *transaction.TransactionContext
}

func (dao *UsersDao) UserAccessPartners(queryOptions map[string]interface{}) ([]*domain.Users, error) {
	tx := dao.transactionContext.PgTx
	m := new(models.Users)
	var users *domain.Users
	query := NewQuery(tx.Model(m), queryOptions)
	if inParterIds, ok := queryOptions["inParterIds"]; ok {
		query.Relation("PartnerInfo", func(q *orm.Query) (*orm.Query, error) {
			return q.Where("id in (?)", pg.In(inParterIds.([]int64))), nil
		})
	}
	err := query.Select(&users)
	return nil, err
}

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