优化表结构,重构模型,重新实现基于白银网关的提取节点流程
This commit is contained in:
@@ -28,12 +28,33 @@ func newPermission(db *gorm.DB, opts ...gen.DOOption) permission {
|
||||
tableName := _permission.permissionDo.TableName()
|
||||
_permission.ALL = field.NewAsterisk(tableName)
|
||||
_permission.ID = field.NewInt32(tableName, "id")
|
||||
_permission.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_permission.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_permission.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
_permission.ParentID = field.NewInt32(tableName, "parent_id")
|
||||
_permission.Name = field.NewString(tableName, "name")
|
||||
_permission.Description = field.NewString(tableName, "description")
|
||||
_permission.CreatedAt = field.NewField(tableName, "created_at")
|
||||
_permission.UpdatedAt = field.NewField(tableName, "updated_at")
|
||||
_permission.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
_permission.Children = permissionHasManyChildren{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Children", "models.Permission"),
|
||||
Parent: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Children.Parent", "models.Permission"),
|
||||
},
|
||||
Children: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Children.Children", "models.Permission"),
|
||||
},
|
||||
}
|
||||
|
||||
_permission.Parent = permissionBelongsToParent{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Parent", "models.Permission"),
|
||||
}
|
||||
|
||||
_permission.fillFieldMap()
|
||||
|
||||
@@ -44,13 +65,16 @@ type permission struct {
|
||||
permissionDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int32 // 权限ID
|
||||
ParentID field.Int32 // 父权限ID
|
||||
Name field.String // 权限名称
|
||||
Description field.String // 权限描述
|
||||
CreatedAt field.Field // 创建时间
|
||||
UpdatedAt field.Field // 更新时间
|
||||
DeletedAt field.Field // 删除时间
|
||||
ID field.Int32
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
ParentID field.Int32
|
||||
Name field.String
|
||||
Description field.String
|
||||
Children permissionHasManyChildren
|
||||
|
||||
Parent permissionBelongsToParent
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -68,12 +92,12 @@ func (p permission) As(alias string) *permission {
|
||||
func (p *permission) updateTableName(table string) *permission {
|
||||
p.ALL = field.NewAsterisk(table)
|
||||
p.ID = field.NewInt32(table, "id")
|
||||
p.CreatedAt = field.NewTime(table, "created_at")
|
||||
p.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
p.DeletedAt = field.NewField(table, "deleted_at")
|
||||
p.ParentID = field.NewInt32(table, "parent_id")
|
||||
p.Name = field.NewString(table, "name")
|
||||
p.Description = field.NewString(table, "description")
|
||||
p.CreatedAt = field.NewField(table, "created_at")
|
||||
p.UpdatedAt = field.NewField(table, "updated_at")
|
||||
p.DeletedAt = field.NewField(table, "deleted_at")
|
||||
|
||||
p.fillFieldMap()
|
||||
|
||||
@@ -90,26 +114,202 @@ func (p *permission) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (p *permission) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 7)
|
||||
p.fieldMap = make(map[string]field.Expr, 9)
|
||||
p.fieldMap["id"] = p.ID
|
||||
p.fieldMap["parent_id"] = p.ParentID
|
||||
p.fieldMap["name"] = p.Name
|
||||
p.fieldMap["description"] = p.Description
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
p.fieldMap["updated_at"] = p.UpdatedAt
|
||||
p.fieldMap["deleted_at"] = p.DeletedAt
|
||||
p.fieldMap["parent_id"] = p.ParentID
|
||||
p.fieldMap["name"] = p.Name
|
||||
p.fieldMap["description"] = p.Description
|
||||
|
||||
}
|
||||
|
||||
func (p permission) clone(db *gorm.DB) permission {
|
||||
p.permissionDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
p.Children.db = db.Session(&gorm.Session{Initialized: true})
|
||||
p.Children.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
p.Parent.db = db.Session(&gorm.Session{Initialized: true})
|
||||
p.Parent.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return p
|
||||
}
|
||||
|
||||
func (p permission) replaceDB(db *gorm.DB) permission {
|
||||
p.permissionDo.ReplaceDB(db)
|
||||
p.Children.db = db.Session(&gorm.Session{})
|
||||
p.Parent.db = db.Session(&gorm.Session{})
|
||||
return p
|
||||
}
|
||||
|
||||
type permissionHasManyChildren struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildren) Where(conds ...field.Expr) *permissionHasManyChildren {
|
||||
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 permissionHasManyChildren) WithContext(ctx context.Context) *permissionHasManyChildren {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildren) Session(session *gorm.Session) *permissionHasManyChildren {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildren) Model(m *models.Permission) *permissionHasManyChildrenTx {
|
||||
return &permissionHasManyChildrenTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildren) Unscoped() *permissionHasManyChildren {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type permissionHasManyChildrenTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a permissionHasManyChildrenTx) Find() (result []*models.Permission, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildrenTx) 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 permissionHasManyChildrenTx) 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 permissionHasManyChildrenTx) 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 permissionHasManyChildrenTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildrenTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a permissionHasManyChildrenTx) Unscoped() *permissionHasManyChildrenTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type permissionBelongsToParent struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParent) Where(conds ...field.Expr) *permissionBelongsToParent {
|
||||
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 permissionBelongsToParent) WithContext(ctx context.Context) *permissionBelongsToParent {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParent) Session(session *gorm.Session) *permissionBelongsToParent {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParent) Model(m *models.Permission) *permissionBelongsToParentTx {
|
||||
return &permissionBelongsToParentTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParent) Unscoped() *permissionBelongsToParent {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type permissionBelongsToParentTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a permissionBelongsToParentTx) Find() (result *models.Permission, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParentTx) 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 permissionBelongsToParentTx) 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 permissionBelongsToParentTx) 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 permissionBelongsToParentTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParentTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a permissionBelongsToParentTx) Unscoped() *permissionBelongsToParentTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type permissionDo struct{ gen.DO }
|
||||
|
||||
func (p permissionDo) Debug() *permissionDo {
|
||||
|
||||
Reference in New Issue
Block a user