...
|
...
|
@@ -4,13 +4,21 @@ import ( |
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"github.com/tiptok/gocomm/common"
|
|
|
"github.com/tiptok/gocomm/pkg/cache"
|
|
|
. "github.com/tiptok/gocomm/pkg/orm/pgx"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
cacheRoleIdKey = func(id int64) string {
|
|
|
return fmt.Sprintf("%v:cache:Role:id:%v", "godevp", id)
|
|
|
}
|
|
|
)
|
|
|
|
|
|
type RoleRepository struct {
|
|
|
*cache.CachedRepository
|
|
|
transactionContext *transaction.TransactionContext
|
|
|
}
|
|
|
|
...
|
...
|
@@ -30,7 +38,10 @@ func (repository *RoleRepository) Save(dm *domain.Role) (*domain.Role, error) { |
|
|
dm.Id = m.Id
|
|
|
return dm, nil
|
|
|
}
|
|
|
if _, err = tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id); err != nil {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
return tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id)
|
|
|
}
|
|
|
if _, err = repository.Query(queryFunc, cacheRoleIdKey(dm.Id)); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return dm, nil
|
...
|
...
|
@@ -41,20 +52,42 @@ func (repository *RoleRepository) Remove(Role *domain.Role) (*domain.Role, error |
|
|
tx = repository.transactionContext.PgTx
|
|
|
RoleModel = &models.Role{Id: Role.Identify().(int64)}
|
|
|
)
|
|
|
if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
return tx.Model(RoleModel).Where("id = ?", Role.Id).Delete()
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, cacheRoleIdKey(Role.Id)); err != nil {
|
|
|
return Role, err
|
|
|
}
|
|
|
//if _, err := tx.Model(RoleModel).Where("id = ?", Role.Id).Delete(); err != nil {
|
|
|
// return Role, err
|
|
|
//}
|
|
|
return Role, nil
|
|
|
}
|
|
|
|
|
|
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
tx := repository.transactionContext.PgDd
|
|
|
RoleModel := new(models.Role)
|
|
|
query := NewQuery(tx.Model(RoleModel), queryOptions)
|
|
|
query.SetWhere("id = ?", "id")
|
|
|
if err := query.First(); err != nil {
|
|
|
return nil, fmt.Errorf("query row not found")
|
|
|
//query := NewQuery(tx.Model(RoleModel), queryOptions)
|
|
|
//query.SetWhere("id = ?", "id")
|
|
|
//if err := query.First(); err != nil {
|
|
|
// return nil, fmt.Errorf("query row not found")
|
|
|
//}
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
query := NewQuery(tx.Model(RoleModel), queryOptions)
|
|
|
query.SetWhere("id = ?", "id")
|
|
|
if err := query.First(); err != nil {
|
|
|
return nil, fmt.Errorf("query row not found")
|
|
|
}
|
|
|
return RoleModel, nil
|
|
|
}
|
|
|
var options []cache.QueryOption
|
|
|
if _, ok := queryOptions["id"]; !ok {
|
|
|
options = append(options, cache.WithNoCacheFlag())
|
|
|
}
|
|
|
if err := repository.QueryCache(cacheRoleIdKey(queryOptions["id"].(int64)), RoleModel, queryFunc, options...); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
if RoleModel.Id == 0 {
|
|
|
return nil, fmt.Errorf("query row not found")
|
|
|
}
|
...
|
...
|
@@ -98,5 +131,5 @@ func NewRoleRepository(transactionContext *transaction.TransactionContext) (*Rol |
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
}
|
|
|
return &RoleRepository{transactionContext: transactionContext}, nil
|
|
|
return &RoleRepository{transactionContext: transactionContext, CachedRepository: cache.NewDefaultCachedRepository()}, nil
|
|
|
} |
...
|
...
|
|