2023-04-12 16:56:55 +08:00
|
|
|
package base
|
|
|
|
|
2023-04-13 14:47:03 +08:00
|
|
|
import (
|
|
|
|
"git.fsdpf.net/go/contracts"
|
2024-05-09 13:15:24 +08:00
|
|
|
"git.fsdpf.net/go/req"
|
2023-04-13 14:47:03 +08:00
|
|
|
)
|
|
|
|
|
2023-04-12 16:56:55 +08:00
|
|
|
type ResRelation struct {
|
|
|
|
Uuid string `db:"uuid"`
|
|
|
|
CategoryUuid string `db:"category_uuid"` // 所属 UUID
|
|
|
|
Name string `db:"name"` // 关联名称
|
|
|
|
Code string `db:"code"` // 关联标识
|
|
|
|
Type string `db:"type"` // 关联类型
|
|
|
|
ResourceCode string `db:"actuallyResource"` // 实际资源资源
|
|
|
|
RelationForeignKey string `db:"actuallyField"` // 实际资源资源字段
|
|
|
|
RelationResource string `db:"relationResource"` // 被关联的资源
|
|
|
|
RelationField string `db:"relationField"` // 被关联的字段
|
|
|
|
UpdatedAt string `db:"updated_at"`
|
|
|
|
CreatedAt string `db:"created_at"`
|
|
|
|
}
|
2023-04-12 19:50:25 +08:00
|
|
|
|
|
|
|
type GetResRelations func(categoryUuid string) []ResRelation
|
2023-04-13 10:03:21 +08:00
|
|
|
|
2023-04-13 14:47:03 +08:00
|
|
|
type GetOrmJoinsByResRelations func(root string, items []ResRelation) []contracts.Join
|
|
|
|
|
2023-04-21 23:27:13 +08:00
|
|
|
// 获取被关联对象的资源
|
2024-05-09 13:15:24 +08:00
|
|
|
type GetResRelationResource func(r ResRelation) (req.Resource, bool)
|
2023-04-21 23:27:13 +08:00
|
|
|
|
2023-04-13 10:03:21 +08:00
|
|
|
// 资源关联依赖
|
2023-04-13 14:47:03 +08:00
|
|
|
func GetJoinResDependencies(root string, items []ResRelation) (dependencies []string) {
|
|
|
|
dependencies = []string{root}
|
2023-04-13 10:03:21 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
done := true
|
|
|
|
for _, item := range items {
|
|
|
|
if item.Type != "inner" && item.Type != "left" && item.Type != "right" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if dependencies[len(dependencies)-1] == item.Code && item.RelationResource != "" {
|
|
|
|
done = false
|
|
|
|
dependencies = append(dependencies, item.RelationResource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dependencies
|
|
|
|
}
|
2023-04-24 13:34:17 +08:00
|
|
|
|
2024-07-04 15:50:15 +08:00
|
|
|
// 获取关联路径, [A, C, B] => [A -> B -> C]
|
2023-04-24 13:34:17 +08:00
|
|
|
func GetModelPrefixForRelations(model string, items []ResRelation) (result []any) {
|
|
|
|
result = append([]any{model}, result...)
|
|
|
|
for _, item := range items {
|
|
|
|
if item.Code == model && (item.Type == "hasMany" || item.Type == "hasOne") {
|
|
|
|
if item.Type == "hasMany" {
|
|
|
|
result = append(result, 0)
|
|
|
|
}
|
|
|
|
return append(GetModelPrefixForRelations(item.RelationResource, items), result...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-07-04 15:50:15 +08:00
|
|
|
// 获取关联路径, [A, C, B] => [A -> B -> C]
|
2023-04-24 13:34:17 +08:00
|
|
|
func GetModelJoinsForRelations(model string, items []ResRelation) (result []string) {
|
|
|
|
result = append([]string{model}, result...)
|
|
|
|
for _, item := range items {
|
|
|
|
if item.Code == model && (item.Type == "left" || item.Type == "right" || item.Type == "inner") {
|
|
|
|
return append(GetModelJoinsForRelations(item.RelationResource, items), result...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-05-06 15:34:47 +08:00
|
|
|
|
|
|
|
func GetDataIndexPrefixForRelations(model string, items []ResRelation) (result []any) {
|
|
|
|
result = append([]any{model}, result...)
|
|
|
|
for _, item := range items {
|
|
|
|
if item.Code == model && (item.Type == "hasMany" || item.Type == "hasOne") {
|
|
|
|
return append(GetDataIndexPrefixForRelations(item.RelationResource, items), result...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|