Skip to content

Commit 1136c1c

Browse files
committed
🎨 Improve database field default filling #11966
1 parent 120b0c1 commit 1136c1c

2 files changed

Lines changed: 35 additions & 49 deletions

File tree

kernel/av/filter.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ func (value *Value) filter(other *Value, relativeDate, relativeDate2 *RelativeDa
328328
}
329329
case KeyTypeNumber:
330330
if nil != value.Number && nil != other && nil != other.Number {
331+
if !other.Number.IsNotEmpty {
332+
return true
333+
}
334+
331335
switch operator {
332336
case FilterOperatorIsEqual:
333337
return value.Number.Content == other.Number.Content && value.Number.IsNotEmpty == other.Number.IsNotEmpty
@@ -402,48 +406,39 @@ func (value *Value) filter(other *Value, relativeDate, relativeDate2 *RelativeDa
402406
}
403407
case KeyTypeSelect, KeyTypeMSelect:
404408
if nil != value.MSelect {
405-
if nil != other && nil != other.MSelect {
406-
switch operator {
407-
case FilterOperatorIsEqual, FilterOperatorContains:
408-
contains := false
409-
for _, v := range value.MSelect {
410-
for _, v2 := range other.MSelect {
411-
if v.Content == v2.Content {
412-
contains = true
413-
break
414-
}
409+
if nil == other || nil == other.MSelect || 1 > len(other.MSelect) {
410+
return true
411+
}
412+
413+
switch operator {
414+
case FilterOperatorIsEqual, FilterOperatorContains:
415+
contains := false
416+
for _, v := range value.MSelect {
417+
for _, v2 := range other.MSelect {
418+
if v.Content == v2.Content {
419+
contains = true
420+
break
415421
}
416422
}
417-
return contains
418-
case FilterOperatorIsNotEqual, FilterOperatorDoesNotContain:
419-
contains := false
420-
for _, v := range value.MSelect {
421-
for _, v2 := range other.MSelect {
422-
if v.Content == v2.Content {
423-
contains = true
424-
break
425-
}
423+
}
424+
return contains
425+
case FilterOperatorIsNotEqual, FilterOperatorDoesNotContain:
426+
contains := false
427+
for _, v := range value.MSelect {
428+
for _, v2 := range other.MSelect {
429+
if v.Content == v2.Content {
430+
contains = true
431+
break
426432
}
427433
}
428-
return !contains
429-
case FilterOperatorIsEmpty:
430-
return 0 == len(value.MSelect) || 1 == len(value.MSelect) && "" == value.MSelect[0].Content
431-
case FilterOperatorIsNotEmpty:
432-
return 0 != len(value.MSelect) && !(1 == len(value.MSelect) && "" == value.MSelect[0].Content)
433434
}
434-
return false
435-
}
436-
437-
// 没有设置比较值
438-
439-
switch operator {
440-
case FilterOperatorIsEqual, FilterOperatorIsNotEqual, FilterOperatorContains, FilterOperatorDoesNotContain:
441-
return true
435+
return !contains
442436
case FilterOperatorIsEmpty:
443437
return 0 == len(value.MSelect) || 1 == len(value.MSelect) && "" == value.MSelect[0].Content
444438
case FilterOperatorIsNotEmpty:
445439
return 0 != len(value.MSelect) && !(1 == len(value.MSelect) && "" == value.MSelect[0].Content)
446440
}
441+
return false
447442
}
448443
case KeyTypeURL:
449444
if nil != value.URL && nil != other && nil != other.URL {
@@ -854,7 +849,6 @@ func (filter *ViewFilter) GetAffectValue(key *Key, addingBlockID string) (ret *V
854849
ret.CreatedAt = util.CurrentTimeMillis()
855850
ret.UpdatedAt = ret.CreatedAt + 1000
856851

857-
// 没有默认值则使用过滤条件的值
858852
switch filter.Value.Type {
859853
case KeyTypeBlock:
860854
switch filter.Operator {
@@ -947,9 +941,13 @@ func (filter *ViewFilter) GetAffectValue(key *Key, addingBlockID string) (ret *V
947941
}
948942
ret.MSelect = []*ValueSelect{valueSelect}
949943
case FilterOperatorIsNotEqual:
944+
return nil
945+
case FilterOperatorContains:
950946
if 0 < len(filter.Value.MSelect) {
951-
ret.MSelect = []*ValueSelect{}
947+
ret.MSelect = []*ValueSelect{{Content: filter.Value.MSelect[0].Content, Color: filter.Value.MSelect[0].Color}}
952948
}
949+
case FilterOperatorDoesNotContain:
950+
return nil
953951
case FilterOperatorIsEmpty:
954952
ret.MSelect = []*ValueSelect{}
955953
case FilterOperatorIsNotEmpty:

kernel/model/attribute_view.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,10 @@ func getAttrViewAddingBlockDefaultValues(attrView *av.AttributeView, view, group
115115
continue
116116
}
117117

118-
var newValue *av.Value
119-
120-
switch keyValues.Key.Type {
121-
case av.KeyTypeNumber:
122-
newValue = filter.GetAffectValue(keyValues.Key, addingItemID)
123-
if nil == newValue {
124-
newValue = getNewValueByNearItem(nearItem, keyValues.Key, addingItemID)
125-
}
126-
default:
127-
if nil != nearItem {
128-
newValue = getNewValueByNearItem(nearItem, keyValues.Key, addingItemID)
129-
} else {
130-
newValue = filter.GetAffectValue(keyValues.Key, addingItemID)
131-
}
118+
newValue := filter.GetAffectValue(keyValues.Key, addingItemID)
119+
if nil == newValue {
120+
newValue = getNewValueByNearItem(nearItem, keyValues.Key, addingItemID)
132121
}
133-
134122
if nil != newValue {
135123
ret[keyValues.Key.ID] = newValue
136124
}

0 commit comments

Comments
 (0)