Преглед на файлове

Merge remote-tracking branch 'origin/dev' into dev

Vanessa преди 1 година
родител
ревизия
0557a4232c
променени са 1 файла, в които са добавени 79 реда и са изтрити 63 реда
  1. 79 63
      kernel/model/attribute_view.go

+ 79 - 63
kernel/model/attribute_view.go

@@ -1577,26 +1577,44 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
 		}
 	}
 
-	if !destAdded {
-		if operation.IsTwoWay {
-			name := strings.TrimSpace(operation.Name)
-			if "" == name {
-				name = srcAv.Name + " " + operation.Format
+	if !destAdded && operation.IsTwoWay {
+		// 新建双向关联目标字段
+		name := strings.TrimSpace(operation.Name)
+		if "" == name {
+			name = srcAv.Name + " " + operation.Format
+		}
+
+		destKeyValues := &av.KeyValues{
+			Key: &av.Key{
+				ID:       operation.BackRelationKeyID,
+				Name:     name,
+				Type:     av.KeyTypeRelation,
+				Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID},
+			},
+		}
+		destAv.KeyValues = append(destAv.KeyValues, destKeyValues)
+
+		for _, v := range destAv.Views {
+			switch v.LayoutType {
+			case av.LayoutTypeTable:
+				v.Table.Columns = append(v.Table.Columns, &av.ViewTableColumn{ID: operation.BackRelationKeyID})
 			}
+		}
 
-			destAv.KeyValues = append(destAv.KeyValues, &av.KeyValues{
-				Key: &av.Key{
-					ID:       operation.BackRelationKeyID,
-					Name:     name,
-					Type:     av.KeyTypeRelation,
-					Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID},
-				},
-			})
+		now := time.Now().UnixMilli()
+		// 和现有值进行关联
+		for _, keyValues := range srcAv.KeyValues {
+			if keyValues.Key.ID != operation.KeyID {
+				continue
+			}
 
-			for _, v := range destAv.Views {
-				switch v.LayoutType {
-				case av.LayoutTypeTable:
-					v.Table.Columns = append(v.Table.Columns, &av.ViewTableColumn{ID: operation.BackRelationKeyID})
+			srcKeyValues := keyValues
+			for _, srcVal := range srcKeyValues.Values {
+				for _, blockID := range srcVal.Relation.BlockIDs {
+					destVal := &av.Value{ID: ast.NewNodeID(), KeyID: destKeyValues.Key.ID, BlockID: blockID, Type: keyValues.Key.Type, Relation: &av.ValueRelation{}, CreatedAt: now, UpdatedAt: now + 1000}
+					destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, srcVal.BlockID)
+					destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
+					destKeyValues.Values = append(destKeyValues.Values, destVal)
 				}
 			}
 		}
@@ -3195,64 +3213,62 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
 	val.SetUpdatedAt(now)
 
 	key, _ := attrView.GetKey(val.KeyID)
-	if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation {
-		destAv, _ := av.ParseAttributeView(key.Relation.AvID)
-		if nil != destAv {
-			if key.Relation.IsTwoWay {
-				// relationChangeMode
-				// 0:关联列值不变(仅排序),不影响目标值
-				// 1:关联列值增加,增加目标值
-				// 2:关联列值减少,减少目标值
-
-				if 1 == relationChangeMode {
-					addBlockIDs := val.Relation.BlockIDs
-					for _, bID := range oldRelationBlockIDs {
-						addBlockIDs = gulu.Str.RemoveElem(addBlockIDs, bID)
-					}
+	if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation && key.Relation.IsTwoWay {
+		// 双向关联需要同时更新目标字段的值
 
-					for _, blockID := range addBlockIDs {
-						for _, keyValues := range destAv.KeyValues {
-							if keyValues.Key.ID != key.Relation.BackKeyID {
-								continue
-							}
+		if destAv, _ := av.ParseAttributeView(key.Relation.AvID); nil != destAv {
+			// relationChangeMode
+			// 0:关联列值不变(仅排序),不影响目标值
+			// 1:关联列值增加,增加目标值
+			// 2:关联列值减少,减少目标值
 
-							destVal := keyValues.GetValue(blockID)
-							if nil == destVal {
-								destVal = &av.Value{ID: ast.NewNodeID(), KeyID: keyValues.Key.ID, BlockID: blockID, Type: keyValues.Key.Type, Relation: &av.ValueRelation{}}
-								keyValues.Values = append(keyValues.Values, destVal)
-							}
+			if 1 == relationChangeMode {
+				addBlockIDs := val.Relation.BlockIDs
+				for _, bID := range oldRelationBlockIDs {
+					addBlockIDs = gulu.Str.RemoveElem(addBlockIDs, bID)
+				}
 
-							destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, rowID)
-							destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
-							destVal.SetUpdatedAt(now)
-							break
+				for _, blockID := range addBlockIDs {
+					for _, keyValues := range destAv.KeyValues {
+						if keyValues.Key.ID != key.Relation.BackKeyID {
+							continue
 						}
+
+						destVal := keyValues.GetValue(blockID)
+						if nil == destVal {
+							destVal = &av.Value{ID: ast.NewNodeID(), KeyID: keyValues.Key.ID, BlockID: blockID, Type: keyValues.Key.Type, Relation: &av.ValueRelation{}, CreatedAt: now, UpdatedAt: now + 1000}
+							keyValues.Values = append(keyValues.Values, destVal)
+						}
+
+						destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, rowID)
+						destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
+						break
 					}
-				} else if 2 == relationChangeMode {
-					removeBlockIDs := oldRelationBlockIDs
-					for _, bID := range val.Relation.BlockIDs {
-						removeBlockIDs = gulu.Str.RemoveElem(removeBlockIDs, bID)
-					}
+				}
+			} else if 2 == relationChangeMode {
+				removeBlockIDs := oldRelationBlockIDs
+				for _, bID := range val.Relation.BlockIDs {
+					removeBlockIDs = gulu.Str.RemoveElem(removeBlockIDs, bID)
+				}
 
-					for _, blockID := range removeBlockIDs {
-						for _, keyValues := range destAv.KeyValues {
-							if keyValues.Key.ID != key.Relation.BackKeyID {
-								continue
-							}
+				for _, blockID := range removeBlockIDs {
+					for _, keyValues := range destAv.KeyValues {
+						if keyValues.Key.ID != key.Relation.BackKeyID {
+							continue
+						}
 
-							for _, value := range keyValues.Values {
-								if value.BlockID == blockID {
-									value.Relation.BlockIDs = gulu.Str.RemoveElem(value.Relation.BlockIDs, rowID)
-									value.SetUpdatedAt(now)
-									break
-								}
+						for _, value := range keyValues.Values {
+							if value.BlockID == blockID {
+								value.Relation.BlockIDs = gulu.Str.RemoveElem(value.Relation.BlockIDs, rowID)
+								value.SetUpdatedAt(now)
+								break
 							}
 						}
 					}
 				}
-
-				av.SaveAttributeView(destAv)
 			}
+
+			av.SaveAttributeView(destAv)
 		}
 	}