2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2023-03-02 09:12:28 +08:00
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2023-03-02 09:27:07 +08:00
|
|
|
|
// Package av 包含了属性视图(Attribute View)相关的实现。
|
2023-03-02 09:12:28 +08:00
|
|
|
|
package av
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-11 22:11:15 +08:00
|
|
|
|
"errors"
|
2023-07-16 22:03:39 +08:00
|
|
|
|
"fmt"
|
2023-03-02 18:49:05 +08:00
|
|
|
|
"os"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"path/filepath"
|
2023-12-01 11:29:44 +08:00
|
|
|
|
"regexp"
|
2023-07-16 22:03:39 +08:00
|
|
|
|
"strconv"
|
2024-03-08 00:12:10 +08:00
|
|
|
|
"strings"
|
2023-07-23 22:15:30 +08:00
|
|
|
|
"time"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2023-03-03 10:49:45 +08:00
|
|
|
|
"github.com/88250/lute/ast"
|
2024-03-08 20:38:32 +08:00
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
|
|
|
|
"github.com/siyuan-note/logging"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AttributeView 描述了属性视图的结构。
|
|
|
|
|
type AttributeView struct {
|
2023-07-12 21:39:55 +08:00
|
|
|
|
Spec int `json:"spec"` // 格式版本
|
|
|
|
|
ID string `json:"id"` // 属性视图 ID
|
|
|
|
|
Name string `json:"name"` // 属性视图名称
|
|
|
|
|
KeyValues []*KeyValues `json:"keyValues"` // 属性视图属性列值
|
|
|
|
|
ViewID string `json:"viewID"` // 当前视图 ID
|
|
|
|
|
Views []*View `json:"views"` // 视图
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// KeyValues 描述了属性视图属性列值的结构。
|
|
|
|
|
type KeyValues struct {
|
2023-07-12 23:52:25 +08:00
|
|
|
|
Key *Key `json:"key"` // 属性视图属性列
|
|
|
|
|
Values []*Value `json:"values,omitempty"` // 属性视图属性列值
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 22:27:38 +08:00
|
|
|
|
func (kValues *KeyValues) GetValue(blockID string) (ret *Value) {
|
|
|
|
|
for _, v := range kValues.Values {
|
|
|
|
|
if v.BlockID == blockID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:10:05 +08:00
|
|
|
|
type KeyType string
|
|
|
|
|
|
|
|
|
|
const (
|
2023-10-01 10:42:12 +08:00
|
|
|
|
KeyTypeBlock KeyType = "block"
|
|
|
|
|
KeyTypeText KeyType = "text"
|
|
|
|
|
KeyTypeNumber KeyType = "number"
|
|
|
|
|
KeyTypeDate KeyType = "date"
|
|
|
|
|
KeyTypeSelect KeyType = "select"
|
|
|
|
|
KeyTypeMSelect KeyType = "mSelect"
|
|
|
|
|
KeyTypeURL KeyType = "url"
|
|
|
|
|
KeyTypeEmail KeyType = "email"
|
|
|
|
|
KeyTypePhone KeyType = "phone"
|
|
|
|
|
KeyTypeMAsset KeyType = "mAsset"
|
|
|
|
|
KeyTypeTemplate KeyType = "template"
|
2023-10-08 12:16:58 +08:00
|
|
|
|
KeyTypeCreated KeyType = "created"
|
|
|
|
|
KeyTypeUpdated KeyType = "updated"
|
2023-11-17 09:03:17 +08:00
|
|
|
|
KeyTypeCheckbox KeyType = "checkbox"
|
2023-12-15 20:05:14 +08:00
|
|
|
|
KeyTypeRelation KeyType = "relation"
|
|
|
|
|
KeyTypeRollup KeyType = "rollup"
|
2023-07-12 19:10:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Key 描述了属性视图属性列的基础结构。
|
|
|
|
|
type Key struct {
|
|
|
|
|
ID string `json:"id"` // 列 ID
|
|
|
|
|
Name string `json:"name"` // 列名
|
|
|
|
|
Type KeyType `json:"type"` // 列类型
|
|
|
|
|
Icon string `json:"icon"` // 列图标
|
|
|
|
|
|
|
|
|
|
// 以下是某些列类型的特有属性
|
|
|
|
|
|
2023-12-15 20:05:14 +08:00
|
|
|
|
// 单选/多选列
|
2023-12-23 17:57:45 +08:00
|
|
|
|
Options []*SelectOption `json:"options,omitempty"` // 选项列表
|
2023-12-15 20:05:14 +08:00
|
|
|
|
|
|
|
|
|
// 数字列
|
|
|
|
|
NumberFormat NumberFormat `json:"numberFormat"` // 列数字格式化
|
|
|
|
|
|
|
|
|
|
// 模板列
|
|
|
|
|
Template string `json:"template"` // 模板内容
|
|
|
|
|
|
|
|
|
|
// 关联列
|
2023-12-23 17:45:46 +08:00
|
|
|
|
Relation *Relation `json:"relation,omitempty"` // 关联信息
|
2023-12-15 20:05:14 +08:00
|
|
|
|
|
|
|
|
|
// 汇总列
|
2023-12-23 17:45:46 +08:00
|
|
|
|
Rollup *Rollup `json:"rollup,omitempty"` // 汇总信息
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 10:17:52 +08:00
|
|
|
|
func NewKey(id, name, icon string, keyType KeyType) *Key {
|
2023-07-12 19:10:05 +08:00
|
|
|
|
return &Key{
|
2023-07-17 11:21:46 +08:00
|
|
|
|
ID: id,
|
2023-07-12 19:10:05 +08:00
|
|
|
|
Name: name,
|
|
|
|
|
Type: keyType,
|
2023-10-06 10:17:52 +08:00
|
|
|
|
Icon: icon,
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 17:45:46 +08:00
|
|
|
|
type Rollup struct {
|
2023-12-24 21:47:10 +08:00
|
|
|
|
RelationKeyID string `json:"relationKeyID"` // 关联列 ID
|
|
|
|
|
KeyID string `json:"keyID"` // 目标列 ID
|
|
|
|
|
Calc *RollupCalc `json:"calc"` // 计算方式
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RollupCalc struct {
|
|
|
|
|
Operator CalcOperator `json:"operator"`
|
|
|
|
|
Result *Value `json:"result"`
|
2023-12-23 17:45:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Relation struct {
|
|
|
|
|
AvID string `json:"avID"` // 关联的属性视图 ID
|
|
|
|
|
IsTwoWay bool `json:"isTwoWay"` // 是否双向关联
|
|
|
|
|
BackKeyID string `json:"backKeyID"` // 双向关联时回链关联列的 ID
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 17:57:45 +08:00
|
|
|
|
type SelectOption struct {
|
2023-07-12 19:10:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Color string `json:"color"`
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
2023-07-11 19:45:27 +08:00
|
|
|
|
// View 描述了视图的结构。
|
|
|
|
|
type View struct {
|
2024-03-01 20:38:53 +08:00
|
|
|
|
ID string `json:"id"` // 视图 ID
|
|
|
|
|
Icon string `json:"icon"` // 视图图标
|
|
|
|
|
Name string `json:"name"` // 视图名称
|
|
|
|
|
HideAttrViewName bool `json:"hideAttrViewName"` // 是否隐藏属性视图名称
|
2023-07-11 19:45:27 +08:00
|
|
|
|
|
2023-07-12 21:39:55 +08:00
|
|
|
|
LayoutType LayoutType `json:"type"` // 当前布局类型
|
|
|
|
|
Table *LayoutTable `json:"table,omitempty"` // 表格布局
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
// LayoutType 描述了视图布局的类型。
|
|
|
|
|
type LayoutType string
|
2023-03-02 09:16:30 +08:00
|
|
|
|
|
|
|
|
|
const (
|
2023-07-12 10:35:17 +08:00
|
|
|
|
LayoutTypeTable LayoutType = "table" // 属性视图类型 - 表格
|
2023-03-02 09:16:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-12-01 09:17:44 +08:00
|
|
|
|
func NewTableView() (ret *View) {
|
|
|
|
|
ret = &View{
|
|
|
|
|
ID: ast.NewNodeID(),
|
2023-12-14 11:51:31 +08:00
|
|
|
|
Name: getI18nName("table"),
|
2023-12-01 09:17:44 +08:00
|
|
|
|
LayoutType: LayoutTypeTable,
|
|
|
|
|
Table: &LayoutTable{
|
2023-12-08 22:00:23 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Filters: []*ViewFilter{},
|
|
|
|
|
Sorts: []*ViewSort{},
|
|
|
|
|
PageSize: 50,
|
2023-12-01 09:17:44 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-24 21:26:01 +08:00
|
|
|
|
func NewTableViewWithBlockKey(blockKeyID string) (view *View, blockKey, selectKey *Key) {
|
2023-12-14 11:51:31 +08:00
|
|
|
|
name := getI18nName("table")
|
2023-11-30 23:19:43 +08:00
|
|
|
|
view = &View{
|
2023-07-12 21:39:55 +08:00
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Name: name,
|
|
|
|
|
LayoutType: LayoutTypeTable,
|
2023-07-12 10:35:17 +08:00
|
|
|
|
Table: &LayoutTable{
|
2023-12-08 22:00:23 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Filters: []*ViewFilter{},
|
|
|
|
|
Sorts: []*ViewSort{},
|
|
|
|
|
PageSize: 50,
|
2023-07-11 23:40:05 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
2023-12-14 11:51:31 +08:00
|
|
|
|
blockKey = NewKey(blockKeyID, getI18nName("key"), "", KeyTypeBlock)
|
2023-12-01 08:55:32 +08:00
|
|
|
|
view.Table.Columns = []*ViewTableColumn{{ID: blockKeyID}}
|
2024-03-24 21:26:01 +08:00
|
|
|
|
|
|
|
|
|
selectKey = NewKey(ast.NewNodeID(), getI18nName("select"), "", KeyTypeSelect)
|
|
|
|
|
view.Table.Columns = append(view.Table.Columns, &ViewTableColumn{ID: selectKey.ID})
|
2023-11-30 23:19:43 +08:00
|
|
|
|
return
|
2023-07-11 23:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 19:45:27 +08:00
|
|
|
|
// Viewable 描述了视图的接口。
|
|
|
|
|
type Viewable interface {
|
|
|
|
|
Filterable
|
|
|
|
|
Sortable
|
|
|
|
|
Calculable
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
GetType() LayoutType
|
2023-07-11 21:31:19 +08:00
|
|
|
|
GetID() string
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 09:38:50 +08:00
|
|
|
|
func NewAttributeView(id string) (ret *AttributeView) {
|
2024-03-24 21:26:01 +08:00
|
|
|
|
view, blockKey, selectKey := NewTableViewWithBlockKey(ast.NewNodeID())
|
2023-07-12 19:55:18 +08:00
|
|
|
|
ret = &AttributeView{
|
2023-07-12 21:39:55 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: id,
|
2024-03-24 21:26:01 +08:00
|
|
|
|
KeyValues: []*KeyValues{{Key: blockKey}, {Key: selectKey}},
|
2023-07-12 21:39:55 +08:00
|
|
|
|
ViewID: view.ID,
|
|
|
|
|
Views: []*View{view},
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
2023-07-12 19:55:18 +08:00
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 20:38:32 +08:00
|
|
|
|
func GetAttributeViewName(avID string) (ret string, err error) {
|
|
|
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
|
|
|
|
if !filelock.IsExist(avJSONPath) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := filelock.ReadFile(avJSONPath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val := jsoniter.Get(data, "name")
|
|
|
|
|
if nil == val || val.ValueType() == jsoniter.InvalidValue {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret = val.ToString()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 16:02:22 +08:00
|
|
|
|
func IsAttributeViewExist(avID string) bool {
|
|
|
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
|
|
|
|
return filelock.IsExist(avJSONPath)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
2023-07-31 11:20:58 +08:00
|
|
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(avJSONPath) {
|
2023-07-31 11:20:58 +08:00
|
|
|
|
err = ErrViewNotFound
|
|
|
|
|
return
|
2023-03-02 11:32:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
data, readErr := filelock.ReadFile(avJSONPath)
|
|
|
|
|
if nil != readErr {
|
|
|
|
|
logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
ret = &AttributeView{}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
2024-03-08 00:35:28 +08:00
|
|
|
|
if strings.Contains(err.Error(), ".relation.contents of type av.Value") {
|
2024-03-08 15:43:33 +08:00
|
|
|
|
mapAv := map[string]interface{}{}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &mapAv); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v3.0.3 兼容之前旧版本,将 relation.contents[""] 转换为 null
|
|
|
|
|
keyValues := mapAv["keyValues"]
|
|
|
|
|
keyValuesMap := keyValues.([]interface{})
|
|
|
|
|
for _, kv := range keyValuesMap {
|
|
|
|
|
kvMap := kv.(map[string]interface{})
|
|
|
|
|
if values := kvMap["values"]; nil != values {
|
|
|
|
|
valuesMap := values.([]interface{})
|
|
|
|
|
for _, v := range valuesMap {
|
|
|
|
|
if vMap := v.(map[string]interface{}); nil != vMap["relation"] {
|
|
|
|
|
vMap["relation"].(map[string]interface{})["contents"] = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
views := mapAv["views"]
|
|
|
|
|
viewsMap := views.([]interface{})
|
|
|
|
|
for _, view := range viewsMap {
|
|
|
|
|
if table := view.(map[string]interface{})["table"]; nil != table {
|
|
|
|
|
tableMap := table.(map[string]interface{})
|
|
|
|
|
if filters := tableMap["filters"]; nil != filters {
|
|
|
|
|
filtersMap := filters.([]interface{})
|
|
|
|
|
for _, f := range filtersMap {
|
|
|
|
|
if fMap := f.(map[string]interface{}); nil != fMap["value"] {
|
|
|
|
|
if valueMap := fMap["value"].(map[string]interface{}); nil != valueMap["relation"] {
|
|
|
|
|
valueMap["relation"].(map[string]interface{})["contents"] = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err = gulu.JSON.MarshalJSON(mapAv)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 00:12:10 +08:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func SaveAttributeView(av *AttributeView) (err error) {
|
2023-12-31 17:55:40 +08:00
|
|
|
|
if "" == av.ID {
|
|
|
|
|
err = errors.New("av id is empty")
|
|
|
|
|
logging.LogErrorf("save attribute view failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 23:57:09 +08:00
|
|
|
|
// 做一些数据兼容和订正处理
|
2023-10-10 21:55:43 +08:00
|
|
|
|
now := util.CurrentTimeMillis()
|
|
|
|
|
for _, kv := range av.KeyValues {
|
2023-11-17 10:15:30 +08:00
|
|
|
|
switch kv.Key.Type {
|
|
|
|
|
case KeyTypeBlock:
|
2023-10-10 21:55:43 +08:00
|
|
|
|
// 补全 block 的创建时间和更新时间
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if 0 == v.Block.Created {
|
2023-10-13 13:21:53 +08:00
|
|
|
|
if "" == v.Block.ID {
|
2023-10-13 14:03:41 +08:00
|
|
|
|
v.Block.ID = v.BlockID
|
|
|
|
|
if "" == v.Block.ID {
|
|
|
|
|
v.Block.ID = ast.NewNodeID()
|
|
|
|
|
v.BlockID = v.Block.ID
|
|
|
|
|
}
|
2023-10-13 13:21:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 21:55:43 +08:00
|
|
|
|
createdStr := v.Block.ID[:len("20060102150405")]
|
|
|
|
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
|
|
|
|
if nil == parseErr {
|
|
|
|
|
v.Block.Created = created.UnixMilli()
|
|
|
|
|
} else {
|
|
|
|
|
v.Block.Created = now
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if 0 == v.Block.Updated {
|
2024-03-03 16:12:44 +08:00
|
|
|
|
v.Block.Updated = v.Block.Created
|
2023-10-10 21:55:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-17 10:15:30 +08:00
|
|
|
|
case KeyTypeNumber:
|
|
|
|
|
for _, v := range kv.Values {
|
2023-12-18 12:26:38 +08:00
|
|
|
|
if nil != v.Number && 0 != v.Number.Content && !v.Number.IsNotEmpty {
|
2023-11-17 10:15:30 +08:00
|
|
|
|
v.Number.IsNotEmpty = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 21:55:43 +08:00
|
|
|
|
}
|
2023-12-30 17:13:53 +08:00
|
|
|
|
|
|
|
|
|
for _, v := range kv.Values {
|
2024-01-01 14:47:29 +08:00
|
|
|
|
if "" == kv.Key.ID {
|
|
|
|
|
kv.Key.ID = ast.NewNodeID()
|
2024-01-01 14:55:04 +08:00
|
|
|
|
for _, val := range kv.Values {
|
|
|
|
|
val.KeyID = kv.Key.ID
|
|
|
|
|
}
|
|
|
|
|
if "" == v.KeyID {
|
2024-01-01 14:49:00 +08:00
|
|
|
|
v.KeyID = kv.Key.ID
|
|
|
|
|
}
|
2024-01-01 14:55:04 +08:00
|
|
|
|
|
2024-03-07 09:37:49 +08:00
|
|
|
|
// 校验日期 IsNotEmpty
|
|
|
|
|
if KeyTypeDate == kv.Key.Type {
|
2024-03-07 10:05:55 +08:00
|
|
|
|
if nil != v.Date && 0 != v.Date.Content && !v.Date.IsNotEmpty {
|
2024-03-07 09:37:49 +08:00
|
|
|
|
v.Date.IsNotEmpty = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 10:05:55 +08:00
|
|
|
|
// 校验数字 IsNotEmpty
|
|
|
|
|
if KeyTypeNumber == kv.Key.Type {
|
|
|
|
|
if nil != v.Number && 0 != v.Number.Content && !v.Number.IsNotEmpty {
|
|
|
|
|
v.Number.IsNotEmpty = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 00:23:45 +08:00
|
|
|
|
// 清空关联实际值
|
|
|
|
|
if KeyTypeRelation == kv.Key.Type {
|
|
|
|
|
v.Relation.Contents = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空汇总实际值
|
|
|
|
|
if KeyTypeRollup == kv.Key.Type {
|
|
|
|
|
v.Rollup.Contents = nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 14:55:04 +08:00
|
|
|
|
for _, view := range av.Views {
|
2024-01-01 14:55:24 +08:00
|
|
|
|
switch view.LayoutType {
|
|
|
|
|
case LayoutTypeTable:
|
|
|
|
|
for _, column := range view.Table.Columns {
|
|
|
|
|
if "" == column.ID {
|
|
|
|
|
column.ID = kv.Key.ID
|
|
|
|
|
break
|
|
|
|
|
}
|
2024-01-01 14:55:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-30 17:13:53 +08:00
|
|
|
|
}
|
2024-03-03 16:12:44 +08:00
|
|
|
|
|
|
|
|
|
// 补全值的创建时间和更新时间
|
2024-03-03 16:21:31 +08:00
|
|
|
|
if "" == v.ID {
|
|
|
|
|
v.ID = ast.NewNodeID()
|
|
|
|
|
}
|
2024-03-07 09:37:49 +08:00
|
|
|
|
|
|
|
|
|
if 0 == v.CreatedAt {
|
|
|
|
|
createdStr := v.ID[:len("20060102150405")]
|
|
|
|
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
|
|
|
|
if nil == parseErr {
|
|
|
|
|
v.CreatedAt = created.UnixMilli()
|
|
|
|
|
} else {
|
|
|
|
|
v.CreatedAt = now
|
|
|
|
|
}
|
2024-03-03 16:12:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 0 == v.UpdatedAt {
|
2024-04-03 10:54:23 +08:00
|
|
|
|
v.UpdatedAt = v.CreatedAt + 1000
|
2024-03-03 16:12:44 +08:00
|
|
|
|
}
|
2023-12-30 17:13:53 +08:00
|
|
|
|
}
|
2023-10-10 21:55:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 23:12:01 +08:00
|
|
|
|
// 补全过滤器 Value
|
|
|
|
|
for _, view := range av.Views {
|
2024-03-08 00:12:10 +08:00
|
|
|
|
if nil != view.Table {
|
2024-03-06 23:12:01 +08:00
|
|
|
|
for _, f := range view.Table.Filters {
|
|
|
|
|
if nil != f.Value {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if k, _ := av.GetKey(f.Column); nil != k {
|
|
|
|
|
f.Value = &Value{Type: k.Type}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 23:32:23 +08:00
|
|
|
|
// 值去重
|
|
|
|
|
blockValues := av.GetBlockKeyValues()
|
|
|
|
|
blockIDs := map[string]bool{}
|
|
|
|
|
var duplicatedValueIDs []string
|
|
|
|
|
for _, blockValue := range blockValues.Values {
|
|
|
|
|
if !blockIDs[blockValue.BlockID] {
|
|
|
|
|
blockIDs[blockValue.BlockID] = true
|
|
|
|
|
} else {
|
|
|
|
|
duplicatedValueIDs = append(duplicatedValueIDs, blockValue.ID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var tmp []*Value
|
|
|
|
|
for _, blockValue := range blockValues.Values {
|
|
|
|
|
if !gulu.Str.Contains(blockValue.ID, duplicatedValueIDs) {
|
|
|
|
|
tmp = append(tmp, blockValue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
blockValues.Values = tmp
|
|
|
|
|
|
|
|
|
|
// 视图值去重
|
2023-10-16 23:57:09 +08:00
|
|
|
|
for _, view := range av.Views {
|
|
|
|
|
if nil != view.Table {
|
2023-12-08 22:00:23 +08:00
|
|
|
|
// 行去重
|
2023-10-16 23:57:09 +08:00
|
|
|
|
view.Table.RowIDs = gulu.Str.RemoveDuplicatedElem(view.Table.RowIDs)
|
2023-12-08 22:00:23 +08:00
|
|
|
|
// 分页大小
|
|
|
|
|
if 1 > view.Table.PageSize {
|
|
|
|
|
view.Table.PageSize = 50
|
|
|
|
|
}
|
2023-10-16 23:57:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 21:31:29 +08:00
|
|
|
|
var data []byte
|
|
|
|
|
if util.UseSingleLineSave {
|
|
|
|
|
data, err = gulu.JSON.MarshalJSON(av)
|
|
|
|
|
} else {
|
|
|
|
|
data, err = gulu.JSON.MarshalIndentJSON(av, "", "\t")
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
avJSONPath := GetAttributeViewDataPath(av.ID)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if err = filelock.WriteFile(avJSONPath, data); nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("save attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 20:04:51 +08:00
|
|
|
|
func (av *AttributeView) GetView(viewID string) (ret *View) {
|
|
|
|
|
for _, v := range av.Views {
|
|
|
|
|
if v.ID == viewID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 15:57:35 +08:00
|
|
|
|
func (av *AttributeView) GetCurrentView(viewID string) (ret *View, err error) {
|
|
|
|
|
if "" != viewID {
|
|
|
|
|
ret = av.GetView(viewID)
|
|
|
|
|
if nil != ret {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 22:11:15 +08:00
|
|
|
|
for _, v := range av.Views {
|
2023-07-12 23:52:25 +08:00
|
|
|
|
if v.ID == av.ViewID {
|
2023-07-11 22:11:15 +08:00
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 15:57:35 +08:00
|
|
|
|
|
|
|
|
|
if 1 > len(av.Views) {
|
|
|
|
|
err = ErrViewNotFound
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret = av.Views[0]
|
2023-07-11 22:11:15 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 09:37:42 +08:00
|
|
|
|
func (av *AttributeView) ExistBlock(blockID string) bool {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if KeyTypeBlock != kv.Key.Type {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if v.BlockID == blockID {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 22:27:38 +08:00
|
|
|
|
func (av *AttributeView) GetValue(keyID, blockID string) (ret *Value) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if v.BlockID == blockID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:10:05 +08:00
|
|
|
|
func (av *AttributeView) GetKey(keyID string) (ret *Key, err error) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
ret = kv.Key
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = ErrKeyNotFound
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (av *AttributeView) GetBlockKeyValues() (ret *KeyValues) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if KeyTypeBlock == kv.Key.Type {
|
|
|
|
|
ret = kv
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 12:00:55 +08:00
|
|
|
|
func (av *AttributeView) GetKeyValues(keyID string) (ret *KeyValues, err error) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
ret = kv
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = ErrKeyNotFound
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 08:55:32 +08:00
|
|
|
|
func (av *AttributeView) GetBlockKey() (ret *Key) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if KeyTypeBlock == kv.Key.Type {
|
|
|
|
|
ret = kv.Key
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 11:29:44 +08:00
|
|
|
|
func (av *AttributeView) GetDuplicateViewName(masterViewName string) (ret string) {
|
|
|
|
|
ret = masterViewName + " (1)"
|
|
|
|
|
r := regexp.MustCompile("^(.*) \\((\\d+)\\)$")
|
|
|
|
|
m := r.FindStringSubmatch(masterViewName)
|
|
|
|
|
if nil == m || 3 > len(m) {
|
|
|
|
|
return
|
2023-12-01 10:57:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 11:29:44 +08:00
|
|
|
|
num, _ := strconv.Atoi(m[2])
|
|
|
|
|
num++
|
|
|
|
|
ret = fmt.Sprintf("%s (%d)", m[1], num)
|
|
|
|
|
return
|
2023-12-01 10:57:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 12:02:27 +08:00
|
|
|
|
func (av *AttributeView) ShallowClone() (ret *AttributeView) {
|
|
|
|
|
ret = &AttributeView{}
|
|
|
|
|
data, err := gulu.JSON.MarshalJSON(av)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", av.ID, err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.ID = ast.NewNodeID()
|
2024-01-08 12:04:03 +08:00
|
|
|
|
if 1 > len(ret.Views) {
|
|
|
|
|
logging.LogErrorf("attribute view [%s] has no views", av.ID)
|
|
|
|
|
return nil
|
2023-12-15 12:02:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyIDMap := map[string]string{}
|
|
|
|
|
for _, kv := range ret.KeyValues {
|
|
|
|
|
newID := ast.NewNodeID()
|
|
|
|
|
keyIDMap[kv.Key.ID] = newID
|
|
|
|
|
kv.Key.ID = newID
|
|
|
|
|
kv.Values = []*Value{}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-08 12:04:03 +08:00
|
|
|
|
for _, view := range ret.Views {
|
|
|
|
|
view.ID = ast.NewNodeID()
|
|
|
|
|
view.Table.ID = ast.NewNodeID()
|
|
|
|
|
for _, column := range view.Table.Columns {
|
|
|
|
|
column.ID = keyIDMap[column.ID]
|
|
|
|
|
}
|
|
|
|
|
view.Table.RowIDs = []string{}
|
|
|
|
|
|
|
|
|
|
for _, f := range view.Table.Filters {
|
|
|
|
|
f.Column = keyIDMap[f.Column]
|
|
|
|
|
}
|
|
|
|
|
for _, s := range view.Table.Sorts {
|
|
|
|
|
s.Column = keyIDMap[s.Column]
|
|
|
|
|
}
|
2023-12-15 12:02:27 +08:00
|
|
|
|
}
|
2024-01-08 12:04:03 +08:00
|
|
|
|
ret.ViewID = ret.Views[0].ID
|
2023-12-15 12:02:27 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
func GetAttributeViewDataPath(avID string) (ret string) {
|
2023-03-02 18:49:05 +08:00
|
|
|
|
av := filepath.Join(util.DataDir, "storage", "av")
|
|
|
|
|
ret = filepath.Join(av, avID+".json")
|
|
|
|
|
if !gulu.File.IsDir(av) {
|
|
|
|
|
if err := os.MkdirAll(av, 0755); nil != err {
|
|
|
|
|
logging.LogErrorf("create attribute view dir failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 11:51:31 +08:00
|
|
|
|
func getI18nName(name string) string {
|
|
|
|
|
return util.AttrViewLangs[util.Lang][name].(string)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 22:11:15 +08:00
|
|
|
|
var (
|
|
|
|
|
ErrViewNotFound = errors.New("view not found")
|
2023-07-12 19:10:05 +08:00
|
|
|
|
ErrKeyNotFound = errors.New("key not found")
|
2023-07-11 22:11:15 +08:00
|
|
|
|
)
|
2023-10-05 12:02:17 +08:00
|
|
|
|
|
|
|
|
|
const (
|
2024-03-04 14:24:31 +08:00
|
|
|
|
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
|
|
|
|
|
NodeAttrView = "custom-sy-av-view" // 用于标记块所属的属性视图视图 view id Database block support specified view https://github.com/siyuan-note/siyuan/issues/10443
|
2024-03-10 23:00:45 +08:00
|
|
|
|
|
|
|
|
|
NodeAttrViewNames = "av-names" // 用于临时标记块所属的属性视图名称,空格分隔
|
2023-10-05 12:02:17 +08:00
|
|
|
|
)
|