[feat] 新增数据缓存标识

This commit is contained in:
what-00 2023-04-12 19:50:25 +08:00
parent e82ca51b08
commit 3deedff8bc
27 changed files with 108 additions and 73 deletions

View File

@ -40,3 +40,5 @@ type DataList struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetDataList func(code string) (DataList, bool)

View File

@ -32,3 +32,5 @@ type DataListField struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetDataListFields func(listUuid string) []DataListField

View File

@ -22,3 +22,5 @@ type DataListImportField struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetDataListImportFields func(listUuid string) []DataListImportField

View File

@ -34,3 +34,5 @@ type DataListOperation struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetDataListOperations func(listUuid string) []DataListOperation

View File

@ -15,3 +15,5 @@ type DataListTab struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetDataListTabs func(listUuid string) []DataListTab

View File

@ -17,3 +17,5 @@ type DataListXlsxField struct {
ConvertByJs string `db:"convertByJs"` // 字段数据转换组件
ConvertByJsSetting res_type.ResFieldByMap `db:"convertByJsSetting"` // 字段数据转换组件参数
}
type GetDataListXlsxFields func(listUuid string) []DataListXlsxField

View File

@ -29,3 +29,5 @@ type FsmTransition struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetFsmTransition func(uuid string) (FsmTransition, bool)

View File

@ -23,3 +23,5 @@ type Layout struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetLayout func(code string) (Layout, bool)

View File

@ -33,3 +33,5 @@ type LayoutField struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetLayoutFields func(uuid string) []LayoutField

View File

@ -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

View File

@ -15,3 +15,5 @@ type PrintTemplate struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetPrintTemplate func(uuid string) (PrintTemplate, bool)

View File

@ -19,3 +19,5 @@ type PrintTemplateField struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetPrintTemplateFields func(uuid string) []PrintTemplateField

View File

@ -17,3 +17,5 @@ type ResApi struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResApi func(code string) (ResApi, bool)

View File

@ -16,3 +16,5 @@ type ResApiApproval struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResApiApproval func(code string) (ResApiApproval, bool)

View File

@ -11,3 +11,5 @@ type ResApiField struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResApiFields func(apiUuid string) []ResApiField

View File

@ -17,3 +17,5 @@ type ResCondition struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResConditions func(categoryUuid string) []ResCondition

View File

@ -12,3 +12,5 @@ type ResConfigure struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResConfigures func(uuid string) []ResConfigure

View File

@ -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)
}

View File

@ -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()")
}

26
base/res_fields.go Normal file
View File

@ -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
}

View File

@ -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

View File

@ -13,3 +13,5 @@ type ResRelation struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResRelations func(categoryUuid string) []ResRelation

View File

@ -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

View File

@ -15,3 +15,5 @@ type ResWidgetOperation struct {
UpdatedAt string `db:"updated_at"`
CreatedAt string `db:"created_at"`
}
type GetResWidgetOperations func(roles ...string) []ResWidgetOperation

View File

@ -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)
}
})
}

View File

@ -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 订阅的主题

View File

@ -2,7 +2,7 @@ package contracts
import (
"reflect"
"git.fsdpf.net/go/db"
"git.fsdpf.net/go/db/schema"
)