This commit is contained in:
Daniel 2025-06-11 17:39:32 +08:00
parent 23c01d0d3f
commit a72c257227
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
6 changed files with 163 additions and 272 deletions

View file

@ -18,7 +18,6 @@ import (
"fmt"
"sort"
"strings"
"time"
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/av"
@ -121,142 +120,8 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
avCache := map[string]*av.AttributeView{}
avCache[attrView.ID] = attrView
for _, row := range ret.Rows {
for _, value := range row.Cells {
switch value.ValueType {
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
if nil != value.Value.Block {
for k, v := range ials[row.ID] {
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
value.Value.Block.Content = v
break
}
}
}
case av.KeyTypeRollup: // 渲染汇总列
rollupKey, _ := attrView.GetKey(value.Value.KeyID)
if nil == rollupKey || nil == rollupKey.Rollup {
break
}
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
if nil == relKey || nil == relKey.Relation {
break
}
relVal := attrView.GetValue(relKey.ID, row.ID)
if nil == relVal || nil == relVal.Relation {
break
}
destAv := avCache[relKey.Relation.AvID]
if nil == destAv {
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
if nil != destAv {
avCache[relKey.Relation.AvID] = destAv
}
}
if nil == destAv {
break
}
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
if nil == destKey {
continue
}
for _, blockID := range relVal.Relation.BlockIDs {
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
if nil == destVal {
if destAv.ExistBlock(blockID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
}
if nil == destVal {
continue
}
}
if av.KeyTypeNumber == destKey.Type {
destVal.Number.Format = destKey.NumberFormat
destVal.Number.FormatNumber()
}
value.Value.Rollup.Contents = append(value.Value.Rollup.Contents, destVal.Clone())
}
value.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
// 将汇总列的值保存到 rowsValues 中,后续渲染模板列的时候会用到,下同
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
keyValues := rowsValues[row.ID]
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: rollupKey.ID, BlockID: row.ID, Type: av.KeyTypeRollup, Rollup: value.Value.Rollup}}})
rowsValues[row.ID] = keyValues
case av.KeyTypeRelation: // 渲染关联列
relKey, _ := attrView.GetKey(value.Value.KeyID)
if nil != relKey && nil != relKey.Relation {
destAv := avCache[relKey.Relation.AvID]
if nil == destAv {
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
if nil != destAv {
avCache[relKey.Relation.AvID] = destAv
}
}
if nil != destAv {
blocks := map[string]*av.Value{}
blockValues := destAv.GetBlockKeyValues()
if nil != blockValues {
for _, blockValue := range blockValues.Values {
blocks[blockValue.BlockID] = blockValue
}
for _, blockID := range value.Value.Relation.BlockIDs {
if val := blocks[blockID]; nil != val {
value.Value.Relation.Contents = append(value.Value.Relation.Contents, val)
}
}
}
}
}
keyValues := rowsValues[row.ID]
keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: relKey.ID, BlockID: row.ID, Type: av.KeyTypeRelation, Relation: value.Value.Relation}}})
rowsValues[row.ID] = keyValues
case av.KeyTypeCreated: // 渲染创建时间
createdStr := row.ID[:len("20060102150405")]
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
if nil == parseErr {
value.Value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
value.Value.Created.IsNotEmpty = true
} else {
value.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
}
keyValues := rowsValues[row.ID]
createdKey, _ := attrView.GetKey(value.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: createdKey.ID, BlockID: row.ID, Type: av.KeyTypeCreated, Created: value.Value.Created}}})
rowsValues[row.ID] = keyValues
case av.KeyTypeUpdated: // 渲染更新时间
ial := ials[row.ID]
if nil == ial {
ial = map[string]string{}
}
block := row.GetBlockValue()
updatedStr := ial["updated"]
if "" == updatedStr && nil != block {
value.Value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
value.Value.Updated.IsNotEmpty = true
} else {
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
if nil == parseErr {
value.Value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
value.Value.Updated.IsNotEmpty = true
} else {
value.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
}
}
keyValues := rowsValues[row.ID]
updatedKey, _ := attrView.GetKey(value.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: updatedKey.ID, BlockID: row.ID, Type: av.KeyTypeUpdated, Updated: value.Value.Updated}}})
rowsValues[row.ID] = keyValues
}
for _, cell := range row.Cells {
fillAttributeViewAutoGeneratedValues(attrView, ials, cell.Value, row, rowsValues, &avCache)
}
}