pg_admin_permission.go 1.9 KB
package repository

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type AdminPermissionRepository struct {
	transactionContext *transaction.TransactionContext
}

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

func NewAdminPermissionRepository(transactionContext *transaction.TransactionContext) (*AdminPermissionRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &AdminPermissionRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permissionModel *models.AdminPermission) (domain.AdminPermission, error) {
	result := domain.AdminPermission{
		Id:       permissionModel.Id,
		ParentId: permissionModel.ParentId,
		Name:     permissionModel.Name,
		Icon:     permissionModel.Icon,
		Code:     permissionModel.Code,
	}
	return result, nil
}

func (reponsitory AdminPermissionRepository) Find(queryOptions domain.AdminPermissionFindQuery) ([]domain.AdminPermission, error) {
	db := reponsitory.transactionContext.PgDd
	permissionModels := make([]models.AdminPermission, 0)
	query := db.Model(&permissionModels)
	if queryOptions.ParentId != nil {
		query = query.Where("parent_id=?", queryOptions.ParentId)
	}
	if len(queryOptions.NotCode) > 0 {
		query = query.WhereIn("code not in (?) ", queryOptions.NotCode)
	}
	if len(queryOptions.IdsIn) > 0 {
		query = query.WhereIn(" id in (?) ", queryOptions.IdsIn)
	}
	if err := query.Select(); err != nil {
		return nil, err
	}
	var result []domain.AdminPermission
	for i := range permissionModels {
		v, _ := reponsitory.transformPgModelToDomainModel(&permissionModels[i])
		result = append(result, v)
	}
	return result, nil
}