diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index f9635b5ec..0c87a2f7d 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -750,149 +750,117 @@ func updateAttributeViewColumnOptions(operation *Operation) (err error) { return } -//func (tx *Transaction) doRemoveAttrViewColOption(operation *Operation) (ret *TxErr) { -// err := removeAttributeViewColumnOption(operation) -// if nil != err { -// return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} -// } -// return -//} -// -//func removeAttributeViewColumnOption(operation *Operation) (err error) { -// avID := operation.ParentID -// attrView, err := av.ParseAttributeView(avID) -// if nil != err { -// return -// } -// -// optName := operation.Data.(string) -// -// key := attrView.GetKey(operation.ID) -// key. -// -// for _, row := range attrView.Rows { -// for i, cell := range row.Cells { -// if colIndex != i { -// continue -// } -// -// if nil != cell.Value { -// if nil != cell.Value.MSelect && 0 < len(cell.Value.MSelect) && nil != cell.Value.MSelect[0] { -// if optName == cell.Value.MSelect[0].Content { -// cell.Value = nil -// break -// } -// } else if nil != cell.Value.MSelect { -// for j, opt := range cell.Value.MSelect { -// if optName == opt.Content { -// cell.Value.MSelect = append(cell.Value.MSelect[:j], cell.Value.MSelect[j+1:]...) -// break -// } -// } -// } -// } -// break -// } -// } -// -// err = av.SaveAttributeView(attrView) -// return -//} +func (tx *Transaction) doRemoveAttrViewColOption(operation *Operation) (ret *TxErr) { + err := removeAttributeViewColumnOption(operation) + if nil != err { + return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} + } + return +} -// TODO 下面的方法要重写 +func removeAttributeViewColumnOption(operation *Operation) (err error) { + avID := operation.ParentID + attrView, err := av.ParseAttributeView(avID) + if nil != err { + return + } -//func (tx *Transaction) doUpdateAttrViewColOption(operation *Operation) (ret *TxErr) { -// err := updateAttributeViewColumnOption(operation) -// if nil != err { -// return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} -// } -// return -//} -// + optName := operation.Data.(string) -// -//func updateAttributeViewColumnOption(operation *Operation) (err error) { -// avID := operation.ParentID -// attrView, err := av.ParseAttributeView(avID) -// if nil != err { -// return -// } -// -// colID := operation.ID -// data := operation.Data.(map[string]interface{}) -// -// oldName := data["oldName"].(string) -// newName := data["newName"].(string) -// newColor := data["newColor"].(string) -// -// var colIndex int -// for i, col := range attrView.Columns { -// if col.ID != colID { -// continue -// } -// -// colIndex = i -// existOpt := false -// for j, opt := range col.Options { -// if opt.Name == newName { -// existOpt = true -// col.Options = append(col.Options[:j], col.Options[j+1:]...) -// break -// } -// } -// if !existOpt { -// for _, opt := range col.Options { -// if opt.Name != oldName { -// continue -// } -// -// opt.Name = newName -// opt.Color = newColor -// break -// } -// } -// break -// } -// -// for _, row := range attrView.Rows { -// for i, cell := range row.Cells { -// if colIndex != i || nil == cell.Value { -// continue -// } -// -// if nil != cell.Value.MSelect && 0 < len(cell.Value.MSelect) && nil != cell.Value.MSelect[0] { -// if oldName == cell.Value.MSelect[0].Content { -// cell.Value.MSelect[0].Content = newName -// cell.Value.MSelect[0].Color = newColor -// break -// } -// } else if nil != cell.Value.MSelect { -// existInMSelect := false -// for j, opt := range cell.Value.MSelect { -// if opt.Content == newName { -// existInMSelect = true -// cell.Value.MSelect = append(cell.Value.MSelect[:j], cell.Value.MSelect[j+1:]...) -// break -// } -// } -// if !existInMSelect { -// for j, opt := range cell.Value.MSelect { -// if oldName == opt.Content { -// cell.Value.MSelect[j].Content = newName -// cell.Value.MSelect[j].Color = newColor -// break -// } -// } -// } -// } -// break -// } -// } -// -// err = av.SaveAttributeView(attrView) -// return -//} -// + key, err := attrView.GetKey(operation.ID) + if nil != err { + return + } + + for i, opt := range key.Options { + if optName == opt.Name { + key.Options = append(key.Options[:i], key.Options[i+1:]...) + break + } + } + + for _, keyValues := range attrView.KeyValues { + if keyValues.Key.ID != operation.ID { + continue + } + + for _, value := range keyValues.Values { + if nil == value || nil == value.MSelect { + continue + } + + for i, opt := range value.MSelect { + if optName == opt.Content { + value.MSelect = append(value.MSelect[:i], value.MSelect[i+1:]...) + break + } + } + } + break + } + + err = av.SaveAttributeView(attrView) + return +} + +func (tx *Transaction) doUpdateAttrViewColOption(operation *Operation) (ret *TxErr) { + err := updateAttributeViewColumnOption(operation) + if nil != err { + return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()} + } + return +} + +func updateAttributeViewColumnOption(operation *Operation) (err error) { + avID := operation.ParentID + attrView, err := av.ParseAttributeView(avID) + if nil != err { + return + } + + key, err := attrView.GetKey(operation.ID) + if nil != err { + return + } + + data := operation.Data.(map[string]interface{}) + + oldName := data["oldName"].(string) + newName := data["newName"].(string) + newColor := data["newColor"].(string) + + for i, opt := range key.Options { + if oldName == opt.Name { + key.Options[i].Name = newName + key.Options[i].Color = newColor + break + } + } + + for _, keyValues := range attrView.KeyValues { + if keyValues.Key.ID != operation.ID { + continue + } + + for _, value := range keyValues.Values { + if nil == value || nil == value.MSelect { + continue + } + + for i, opt := range value.MSelect { + if oldName == opt.Content { + value.MSelect[i].Content = newName + value.MSelect[i].Color = newColor + break + } + } + } + break + } + + err = av.SaveAttributeView(attrView) + return +} const ( NodeAttrNameAVs = "custom-avs" diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index d61c60825..6ee3d1443 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -247,10 +247,10 @@ func performTx(tx *Transaction) (ret *TxErr) { ret = tx.doUpdateAttrViewCell(op) case "updateAttrViewColOptions": ret = tx.doUpdateAttrViewColOptions(op) - //case "removeAttrViewColOption": - //ret = tx.doRemoveAttrViewColOption(op) - //case "updateAttrViewColOption": - // ret = tx.doUpdateAttrViewColOption(op) + case "removeAttrViewColOption": + ret = tx.doRemoveAttrViewColOption(op) + case "updateAttrViewColOption": + ret = tx.doUpdateAttrViewColOption(op) } if nil != ret {