权限管理接口实现
This commit is contained in:
@@ -39,6 +39,21 @@ func newClient(db *gorm.DB, opts ...gen.DOOption) client {
|
||||
_client.Icon = field.NewString(tableName, "icon")
|
||||
_client.Status = field.NewInt(tableName, "status")
|
||||
_client.Type = field.NewInt(tableName, "type")
|
||||
_client.Permissions = clientManyToManyPermissions{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Permissions", "models.Permission"),
|
||||
Parent: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Permissions.Parent", "models.Permission"),
|
||||
},
|
||||
Children: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Permissions.Children", "models.Permission"),
|
||||
},
|
||||
}
|
||||
|
||||
_client.fillFieldMap()
|
||||
|
||||
@@ -61,6 +76,7 @@ type client struct {
|
||||
Icon field.String
|
||||
Status field.Int
|
||||
Type field.Int
|
||||
Permissions clientManyToManyPermissions
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -105,7 +121,7 @@ func (c *client) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (c *client) fillFieldMap() {
|
||||
c.fieldMap = make(map[string]field.Expr, 12)
|
||||
c.fieldMap = make(map[string]field.Expr, 13)
|
||||
c.fieldMap["id"] = c.ID
|
||||
c.fieldMap["created_at"] = c.CreatedAt
|
||||
c.fieldMap["updated_at"] = c.UpdatedAt
|
||||
@@ -118,18 +134,110 @@ func (c *client) fillFieldMap() {
|
||||
c.fieldMap["icon"] = c.Icon
|
||||
c.fieldMap["status"] = c.Status
|
||||
c.fieldMap["type"] = c.Type
|
||||
|
||||
}
|
||||
|
||||
func (c client) clone(db *gorm.DB) client {
|
||||
c.clientDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
c.Permissions.db = db.Session(&gorm.Session{Initialized: true})
|
||||
c.Permissions.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return c
|
||||
}
|
||||
|
||||
func (c client) replaceDB(db *gorm.DB) client {
|
||||
c.clientDo.ReplaceDB(db)
|
||||
c.Permissions.db = db.Session(&gorm.Session{})
|
||||
return c
|
||||
}
|
||||
|
||||
type clientManyToManyPermissions struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissions) Where(conds ...field.Expr) *clientManyToManyPermissions {
|
||||
if len(conds) == 0 {
|
||||
return &a
|
||||
}
|
||||
|
||||
exprs := make([]clause.Expression, 0, len(conds))
|
||||
for _, cond := range conds {
|
||||
exprs = append(exprs, cond.BeCond().(clause.Expression))
|
||||
}
|
||||
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissions) WithContext(ctx context.Context) *clientManyToManyPermissions {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissions) Session(session *gorm.Session) *clientManyToManyPermissions {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissions) Model(m *models.Client) *clientManyToManyPermissionsTx {
|
||||
return &clientManyToManyPermissionsTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissions) Unscoped() *clientManyToManyPermissions {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type clientManyToManyPermissionsTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Find() (result []*models.Permission, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Append(values ...*models.Permission) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Replace(values ...*models.Permission) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Delete(values ...*models.Permission) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a clientManyToManyPermissionsTx) Unscoped() *clientManyToManyPermissionsTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type clientDo struct{ gen.DO }
|
||||
|
||||
func (c clientDo) Debug() *clientDo {
|
||||
|
||||
Reference in New Issue
Block a user