fast_domain_repository.go 9.8 KB
package factory

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
)

/*****   1.快速模块  *****/

// FastPgUser  快速返回领域用户
//
// transactionContext 事务
// userId  用户ID
func FastPgUser(transactionContext application.TransactionContext, userId int64, options ...option) (domain.UserRepository, *domain.User, error) {
	var rep domain.UserRepository
	var mod *domain.User
	var err error
	if value, err := CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if userId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"userId": userId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该用户不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
		return nil, nil, err
	}
	return rep, mod, err
}

// FastPgUser  快速返回领域用户基础
//
// transactionContext 事务
// userBaseId  用户基础ID
func FastPgUserBase(transactionContext application.TransactionContext, userBaseId int64) (domain.UserBaseRepository, *domain.UserBase, error) {
	var rep domain.UserBaseRepository
	var mod *domain.UserBase
	var err error
	if value, err := CreateUserBaseRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if userBaseId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"userBaseId": userBaseId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "用户基础不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgRole  快速返回领域角色
//
// transactionContext 事务
// roleId  角色Id
func FastPgRole(transactionContext application.TransactionContext, roleId int64, options ...option) (domain.RoleRepository, *domain.Role, error) {
	var rep domain.RoleRepository
	var mod *domain.Role
	var err error
	if value, err := CreateRoleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if roleId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"roleId": roleId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该角色不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	if err = fastPgDataAuth(transactionContext, mod, options...); err != nil {
		return nil, nil, err
	}
	return rep, mod, err
}

// FastPgRole  快速返回领域组织
//
// transactionContext 事务
// orgId  组织Id
func FastPgOrg(transactionContext application.TransactionContext, orgId int64) (domain.OrgRepository, *domain.Org, error) {
	var rep domain.OrgRepository
	var mod *domain.Org
	var err error
	if value, err := CreateOrgRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if orgId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"orgId": orgId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该组织不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgCompany  快速返回领域公司
//
// transactionContext 事务
// companyId  公司Id
func FastPgCompany(transactionContext application.TransactionContext, companyId int64) (domain.CompanyRepository, *domain.Company, error) {
	var rep domain.CompanyRepository
	var mod *domain.Company
	var err error
	if value, err := CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if companyId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"companyId": companyId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该企业不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgMenu  快速返回领域菜单
//
// transactionContext 事务
// menuId  菜单Id
func FastPgMenu(transactionContext application.TransactionContext, menuId int64) (domain.MenuRepository, *domain.Menu, error) {
	var rep domain.MenuRepository
	var mod *domain.Menu
	var err error
	if value, err := CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if menuId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"menuId": menuId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该菜单不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgUpChain  快速返回区块链
//
// transactionContext 事务
// upChain  upChainId
func FastPgUpChain(transactionContext application.TransactionContext, upChainId int64) (domain.UpChainRepository, *domain.UpChain, error) {
	var rep domain.UpChainRepository
	var mod *domain.UpChain
	var err error
	if value, err := CreateUpChainRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if upChainId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"upChainId": upChainId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该组织不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgCustomizeMenu  快速返回领域自定义菜单
//
// transactionContext 事务
// customizeMenuId  自定义菜单Id
func FastPgCustomizeMenu(transactionContext application.TransactionContext, customizeMenuId int64) (domain.CustomizeMenuRepository, *domain.CustomizeMenu, error) {
	var rep domain.CustomizeMenuRepository
	var mod *domain.CustomizeMenu
	var err error
	if value, err := CreateCustomizeMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if customizeMenuId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"customizeMenuId": customizeMenuId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "自定义菜单不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// FastPgAccountDestroyRecord  快速返回领域账号注销记录
//
// transactionContext 事务
// accountDestroyRecordId  账号注销记录Id
func FastPgAccountDestroyRecord(transactionContext application.TransactionContext, accountDestroyRecordId int64) (domain.AccountDestroyRecordRepository, *domain.AccountDestroyRecord, error) {
	var rep domain.AccountDestroyRecordRepository
	var mod *domain.AccountDestroyRecord
	var err error
	if value, err := CreateAccountDestroyRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		rep = value
	}
	if accountDestroyRecordId > 0 {
		if mod, err = rep.FindOne(map[string]interface{}{"accountDestroyRecord": accountDestroyRecordId}); err != nil {
			if err == domain.ErrorNotFound {
				return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "账号注销记录不存在")
			}
			return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
	}
	return rep, mod, err
}

// fastPgDataAuth  快速数据权限验证
//
// data 待认证的数据
// options 配置项
func fastPgDataAuth(transactionContext application.TransactionContext, data domain.AuthedData, options ...option) error {
	option := NewFastOptions(options...)
	if option.DataAuthRequired && data != nil {
		if data.BelongOrg() != option.OperateInfo.OrgId {
			return application.ThrowError(application.BUSINESS_ERROR, "当前登录的组织机构与操作数据组织机构不一致")
		}
	}
	return nil
}

/*****   2.配置  *****/

type FastOptions struct {
	DataAuthRequired bool
	OperateInfo      *domain.OperateInfo
}

func NewFastOptions(options ...option) *FastOptions {
	o := &FastOptions{
		DataAuthRequired: false,
	}
	for i := 0; i < len(options); i++ {
		options[i](o)
	}
	return o
}

type option func(options *FastOptions)

// 需要数据权限
func WithDataAuthRequired() option {
	return func(options *FastOptions) {
		options.DataAuthRequired = true
	}
}

// WithOperator 操作人
func WithOperator(op *domain.OperateInfo) option {
	return func(options *FastOptions) {
		options.OperateInfo = op
	}
}