...
|
...
|
@@ -3,6 +3,8 @@ package repository |
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"github.com/linmadan/egglib-go/persistent/cache"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
...
|
...
|
@@ -13,6 +15,7 @@ import ( |
|
|
)
|
|
|
|
|
|
type RoleRepository struct {
|
|
|
*cache.CachedRepository
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
...
|
...
|
@@ -78,31 +81,37 @@ func (repository *RoleRepository) Save(role *domain.Role) (*domain.Role, error) |
|
|
return role, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&role.RoleId,
|
|
|
&role.CompanyId,
|
|
|
&role.OrgId,
|
|
|
&role.RoleType,
|
|
|
&role.RoleName,
|
|
|
pg.Array(&role.AccessMenus),
|
|
|
&role.Desc,
|
|
|
&role.Ext,
|
|
|
&role.CreatedAt,
|
|
|
&role.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
role.CompanyId,
|
|
|
role.OrgId,
|
|
|
role.RoleType,
|
|
|
role.RoleName,
|
|
|
pg.Array(role.AccessMenus),
|
|
|
role.Desc,
|
|
|
role.Ext,
|
|
|
role.CreatedAt,
|
|
|
role.UpdatedAt,
|
|
|
role.Identify(),
|
|
|
); err != nil {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&role.RoleId,
|
|
|
&role.CompanyId,
|
|
|
&role.OrgId,
|
|
|
&role.RoleType,
|
|
|
&role.RoleName,
|
|
|
pg.Array(&role.AccessMenus),
|
|
|
&role.Desc,
|
|
|
&role.Ext,
|
|
|
&role.CreatedAt,
|
|
|
&role.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE users.role SET %s WHERE role_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
role.CompanyId,
|
|
|
role.OrgId,
|
|
|
role.RoleType,
|
|
|
role.RoleName,
|
|
|
pg.Array(role.AccessMenus),
|
|
|
role.Desc,
|
|
|
role.Ext,
|
|
|
role.CreatedAt,
|
|
|
role.UpdatedAt,
|
|
|
role.Identify(),
|
|
|
); err != nil {
|
|
|
return role, err
|
|
|
}
|
|
|
return role, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
|
|
|
return role, err
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -112,7 +121,13 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error |
|
|
tx := repository.transactionContext.PgTx
|
|
|
roleModel := new(models.Role)
|
|
|
roleModel.RoleId = role.Identify().(int64)
|
|
|
if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.Model(roleModel).WherePK().Delete(); err != nil {
|
|
|
return role, err
|
|
|
}
|
|
|
return role, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, role.CacheKeyFunc()); err != nil {
|
|
|
return role, err
|
|
|
}
|
|
|
return role, nil
|
...
|
...
|
@@ -120,22 +135,34 @@ func (repository *RoleRepository) Remove(role *domain.Role) (*domain.Role, error |
|
|
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
roleModel := new(models.Role)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("role_id = ?", "roleId")
|
|
|
query.SetWhereByQueryOption("company_id = ?", "companyId")
|
|
|
query.SetWhereByQueryOption("org_id = ?", "orgId")
|
|
|
query.SetWhereByQueryOption("role_name = ?", "roleName")
|
|
|
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
|
|
|
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
|
|
|
query.AllWithDeleted()
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(roleModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("role_id = ?", "roleId")
|
|
|
query.SetWhereByQueryOption("company_id = ?", "companyId")
|
|
|
query.SetWhereByQueryOption("org_id = ?", "orgId")
|
|
|
query.SetWhereByQueryOption("role_name = ?", "roleName")
|
|
|
query.SetWhereByQueryOption("(role_type & ?) >0", "roleType")
|
|
|
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
|
|
|
query.AllWithDeleted()
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
return roleModel, nil
|
|
|
}
|
|
|
|
|
|
var cacheModel = &domain.Role{}
|
|
|
if _, ok := queryOptions["roleId"]; ok {
|
|
|
cacheModel.RoleId = queryOptions["roleId"].(int64)
|
|
|
}
|
|
|
if err := repository.QueryCache(cacheModel.CacheKeyFunc, roleModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
if roleModel.RoleId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
...
|
...
|
@@ -176,6 +203,7 @@ func NewRoleRepository(transactionContext *pgTransaction.TransactionContext) (*R |
|
|
} else {
|
|
|
return &RoleRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
CachedRepository: cache.NewDefaultCachedRepository(),
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|