From 3deedff8bc6dff0dd4807f8c243027c5aa3c4130 Mon Sep 17 00:00:00 2001 From: what-00 Date: Wed, 12 Apr 2023 19:50:25 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/data_list.go | 2 ++ base/data_list_field.go | 2 ++ base/data_list_import_field.go | 2 ++ base/data_list_operation.go | 2 ++ base/data_list_tab.go | 2 ++ base/data_list_xlsx_field.go | 2 ++ base/fsm_transition.go | 2 ++ base/layout.go | 2 ++ base/layout_field.go | 2 ++ base/menu.go | 2 ++ base/print_template.go | 2 ++ base/print_template_field.go | 2 ++ base/res_api.go | 2 ++ base/res_api_approval.go | 2 ++ base/res_api_field.go | 2 ++ base/res_condition.go | 2 ++ base/res_configure.go | 2 ++ base/res_field.go | 45 +++++++++++----------------------- base/res_field_sql_defined.go | 14 ----------- base/res_fields.go | 26 ++++++++++++++++++++ base/res_listener.go | 2 ++ base/res_relation.go | 2 ++ base/res_role.go | 2 ++ base/res_widget_operations.go | 2 ++ base/resource.go | 30 +++++++++++++++++++---- queue.go | 22 ----------------- resource.go | 2 +- 27 files changed, 108 insertions(+), 73 deletions(-) delete mode 100644 base/res_field_sql_defined.go create mode 100644 base/res_fields.go diff --git a/base/data_list.go b/base/data_list.go index 65656c1..67bfdce 100644 --- a/base/data_list.go +++ b/base/data_list.go @@ -40,3 +40,5 @@ type DataList struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetDataList func(code string) (DataList, bool) diff --git a/base/data_list_field.go b/base/data_list_field.go index a09e74b..9d81a35 100644 --- a/base/data_list_field.go +++ b/base/data_list_field.go @@ -32,3 +32,5 @@ type DataListField struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetDataListFields func(listUuid string) []DataListField diff --git a/base/data_list_import_field.go b/base/data_list_import_field.go index 24ba841..ca7c10a 100644 --- a/base/data_list_import_field.go +++ b/base/data_list_import_field.go @@ -22,3 +22,5 @@ type DataListImportField struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetDataListImportFields func(listUuid string) []DataListImportField diff --git a/base/data_list_operation.go b/base/data_list_operation.go index de28a9e..2928374 100644 --- a/base/data_list_operation.go +++ b/base/data_list_operation.go @@ -34,3 +34,5 @@ type DataListOperation struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetDataListOperations func(listUuid string) []DataListOperation diff --git a/base/data_list_tab.go b/base/data_list_tab.go index eeaff0a..cdeb218 100644 --- a/base/data_list_tab.go +++ b/base/data_list_tab.go @@ -15,3 +15,5 @@ type DataListTab struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetDataListTabs func(listUuid string) []DataListTab diff --git a/base/data_list_xlsx_field.go b/base/data_list_xlsx_field.go index 2cae432..aabda87 100644 --- a/base/data_list_xlsx_field.go +++ b/base/data_list_xlsx_field.go @@ -17,3 +17,5 @@ type DataListXlsxField struct { ConvertByJs string `db:"convertByJs"` // 字段数据转换组件 ConvertByJsSetting res_type.ResFieldByMap `db:"convertByJsSetting"` // 字段数据转换组件参数 } + +type GetDataListXlsxFields func(listUuid string) []DataListXlsxField diff --git a/base/fsm_transition.go b/base/fsm_transition.go index 25ed08f..f8bc100 100644 --- a/base/fsm_transition.go +++ b/base/fsm_transition.go @@ -29,3 +29,5 @@ type FsmTransition struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetFsmTransition func(uuid string) (FsmTransition, bool) diff --git a/base/layout.go b/base/layout.go index 5d5599f..c110790 100644 --- a/base/layout.go +++ b/base/layout.go @@ -23,3 +23,5 @@ type Layout struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetLayout func(code string) (Layout, bool) diff --git a/base/layout_field.go b/base/layout_field.go index 2112039..a5175df 100644 --- a/base/layout_field.go +++ b/base/layout_field.go @@ -33,3 +33,5 @@ type LayoutField struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetLayoutFields func(uuid string) []LayoutField diff --git a/base/menu.go b/base/menu.go index 647779c..1e82910 100644 --- a/base/menu.go +++ b/base/menu.go @@ -21,3 +21,5 @@ type Menu struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetMenus func(platform string, roles ...string) []Menu diff --git a/base/print_template.go b/base/print_template.go index 0c5894e..30dca4c 100644 --- a/base/print_template.go +++ b/base/print_template.go @@ -15,3 +15,5 @@ type PrintTemplate struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetPrintTemplate func(uuid string) (PrintTemplate, bool) diff --git a/base/print_template_field.go b/base/print_template_field.go index 9eb0ac6..c8d4ac3 100644 --- a/base/print_template_field.go +++ b/base/print_template_field.go @@ -19,3 +19,5 @@ type PrintTemplateField struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetPrintTemplateFields func(uuid string) []PrintTemplateField diff --git a/base/res_api.go b/base/res_api.go index d402bd6..8317af4 100644 --- a/base/res_api.go +++ b/base/res_api.go @@ -17,3 +17,5 @@ type ResApi struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResApi func(code string) (ResApi, bool) diff --git a/base/res_api_approval.go b/base/res_api_approval.go index 1944771..3be43de 100644 --- a/base/res_api_approval.go +++ b/base/res_api_approval.go @@ -16,3 +16,5 @@ type ResApiApproval struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResApiApproval func(code string) (ResApiApproval, bool) diff --git a/base/res_api_field.go b/base/res_api_field.go index cb0d410..907afb0 100644 --- a/base/res_api_field.go +++ b/base/res_api_field.go @@ -11,3 +11,5 @@ type ResApiField struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResApiFields func(apiUuid string) []ResApiField diff --git a/base/res_condition.go b/base/res_condition.go index 3fe4f54..277c11c 100644 --- a/base/res_condition.go +++ b/base/res_condition.go @@ -17,3 +17,5 @@ type ResCondition struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResConditions func(categoryUuid string) []ResCondition diff --git a/base/res_configure.go b/base/res_configure.go index 1e38a11..464e59f 100644 --- a/base/res_configure.go +++ b/base/res_configure.go @@ -12,3 +12,5 @@ type ResConfigure struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResConfigures func(uuid string) []ResConfigure diff --git a/base/res_field.go b/base/res_field.go index e087d30..ef081e5 100644 --- a/base/res_field.go +++ b/base/res_field.go @@ -1,50 +1,30 @@ package base import ( - "database/sql/driver" "encoding/json" "fmt" "reflect" "strconv" "strings" - "github.com/spf13/cast" - "git.fsdpf.net/go/contracts" "git.fsdpf.net/go/contracts/res_type" "git.fsdpf.net/go/db" "git.fsdpf.net/go/db/schema" + "github.com/google/uuid" + "github.com/spf13/cast" ) // 资源字段 type ResField struct { - Uuid string `db:"uuid"` - Name string `db:"name"` - Code string `db:"code"` - CodeResource string `db:"codeResource"` - DataType res_type.ResDataType `db:"table_type"` - Length string `db:"length"` - Comment string `db:"comment"` - Default string `db:"default"` -} - -type ResFields []ResField - -func (c *ResFields) Scan(value any) error { - if value == nil { - return nil - } - - if err := json.Unmarshal(value.([]byte), c); err != nil { - return fmt.Errorf("ResFields json.Unmarshal Error, %s", err) - } - - return nil -} - -func (c ResFields) Value() (driver.Value, error) { - b, err := json.Marshal(c) - return string(b), err + Uuid string `db:"uuid"` + Name string `db:"name"` + Code string `db:"code"` + CodeResource string `db:"codeResource"` + DataType res_type.ResFieldByString `db:"table_type"` + Length string `db:"length"` + Comment string `db:"comment"` + Default string `db:"default"` } func (this ResField) ToStructField(tags ...string) reflect.StructField { @@ -195,7 +175,10 @@ func (this ResField) GetRawDefault(driver string) db.Expression { if len(this.Default) > 4 && strings.ToLower(this.Default[0:4]) == "sql:" { sql := strings.ToLower(this.Default[4:]) if sql == "uuid()" { - return Uuid(driver) + if driver == "sqlite" { + return db.Raw("'" + uuid.NewString() + "'") + } + return db.Raw("uuid()") } return db.Raw(sql) } diff --git a/base/res_field_sql_defined.go b/base/res_field_sql_defined.go deleted file mode 100644 index 0256c15..0000000 --- a/base/res_field_sql_defined.go +++ /dev/null @@ -1,14 +0,0 @@ -package base - -import ( - "github.com/google/uuid" - - "git.fsdpf.net/go/db" -) - -func Uuid(driver string) db.Expression { - if driver == "sqlite" { - return db.Raw("'" + uuid.NewString() + "'") - } - return db.Raw("uuid()") -} diff --git a/base/res_fields.go b/base/res_fields.go new file mode 100644 index 0000000..32df8c1 --- /dev/null +++ b/base/res_fields.go @@ -0,0 +1,26 @@ +package base + +import ( + "database/sql/driver" + "encoding/json" + "fmt" +) + +type ResFields []ResField + +func (c *ResFields) Scan(value any) error { + if value == nil { + return nil + } + + if err := json.Unmarshal(value.([]byte), c); err != nil { + return fmt.Errorf("ResFields json.Unmarshal Error, %s", err) + } + + return nil +} + +func (c ResFields) Value() (driver.Value, error) { + b, err := json.Marshal(c) + return string(b), err +} diff --git a/base/res_listener.go b/base/res_listener.go index 815fab5..836f720 100644 --- a/base/res_listener.go +++ b/base/res_listener.go @@ -13,3 +13,5 @@ type ResListener struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResListens func(categoryUuid string, event ...string) []ResListener diff --git a/base/res_relation.go b/base/res_relation.go index 5d9d2ba..1369398 100644 --- a/base/res_relation.go +++ b/base/res_relation.go @@ -13,3 +13,5 @@ type ResRelation struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResRelations func(categoryUuid string) []ResRelation diff --git a/base/res_role.go b/base/res_role.go index bc33ae2..83823da 100644 --- a/base/res_role.go +++ b/base/res_role.go @@ -7,3 +7,5 @@ type ResRole struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResRoles func(uuid string, roles ...string) []ResRole diff --git a/base/res_widget_operations.go b/base/res_widget_operations.go index bc0d25c..c31be0d 100644 --- a/base/res_widget_operations.go +++ b/base/res_widget_operations.go @@ -15,3 +15,5 @@ type ResWidgetOperation struct { UpdatedAt string `db:"updated_at"` CreatedAt string `db:"created_at"` } + +type GetResWidgetOperations func(roles ...string) []ResWidgetOperation diff --git a/base/resource.go b/base/resource.go index e917b6e..341ebca 100644 --- a/base/resource.go +++ b/base/resource.go @@ -12,9 +12,29 @@ import ( "git.fsdpf.net/go/contracts" "git.fsdpf.net/go/contracts/res_type" "git.fsdpf.net/go/db" - "git.fsdpf.net/go/queue" ) +// 资源变更事件 +const ResChangeEventTopic = "res-change-event-topic" + +// 资源变更数据 +const ResChangeRecordTopic = "res-change-record-topic" + +type ResChangeEventTopicPayload struct { + Type string // insert | delete | update + Res Resource // 变更资源 + Result sql.Result // 执行结果 +} + +type ResChangeRecordTopicPayload struct { + Type string // insert | delete | update + User contracts.User // 操作用户 + Res Resource // 变更资源 + Result sql.Result // 执行结果 + Old []map[string]any // 旧数据 + New []map[string]any // 新数据 +} + // 资源 type Resource struct { container *do.Injector @@ -290,12 +310,12 @@ func (this Resource) onResEvent(builder *db.Builder) { // 全局触发器 // 1. 清除系统缓存 - if err := do.MustInvoke[contracts.Queue](this.container).Publish(contracts.ResQueueTopic, contracts.ResQueuePayload{ + if err := do.MustInvoke[contracts.Queue](this.container).Publish(ResChangeEventTopic, ResChangeEventTopicPayload{ Type: t, Res: this, Result: result, }); err != nil { - log.Println("Queue Publish Err:", contracts.ResQueueTopic, err) + log.Println("Queue Publish Err:", ResChangeEventTopic, err) } }) } @@ -326,7 +346,7 @@ func (this Resource) onUserEvent(builder *db.Builder, user contracts.User) { } // 触发消息队列 - if err := do.MustInvoke[contracts.Queue](this.container).Publish(contracts.UserQueuetTopic, contracts.UserQueuePayload{ + if err := do.MustInvoke[contracts.Queue](this.container).Publish(ResChangeRecordTopic, ResChangeRecordTopicPayload{ Type: t, User: user, Res: this, @@ -334,7 +354,7 @@ func (this Resource) onUserEvent(builder *db.Builder, user contracts.User) { New: data, Result: result, }); err != nil { - log.Println("Queue Publish Err:", contracts.UserQueuetTopic, err) + log.Println("Queue Publish Err:", ResChangeRecordTopic, err) } }) } diff --git a/queue.go b/queue.go index 633d3df..4bef333 100644 --- a/queue.go +++ b/queue.go @@ -1,27 +1,5 @@ package contracts -import ( - "database/sql" -) - -const ResQueueTopic = "res-queue-topic" -const UserQueueTopic = "user-queue-topic" - -type ResQueuePayload struct { - Type string // insert | delete | update - Res Resource // 变更资源 - Result sql.Result // 执行结果 -} - -type UserQueuePayload struct { - Type string // insert | delete | update | select - User User // 操作用户 - Res Resource // 变更资源 - Result sql.Result // 执行结果 - Old []map[string]any // 旧数据 - New []map[string]any // 新数据 -} - type Queue interface { // 消息推送 // @param topic 订阅的主题 diff --git a/resource.go b/resource.go index 383dfb7..42ec05a 100644 --- a/resource.go +++ b/resource.go @@ -2,7 +2,7 @@ package contracts import ( "reflect" - + "git.fsdpf.net/go/db" "git.fsdpf.net/go/db/schema" )