diff --git a/kernel/av/av.go b/kernel/av/av.go index fbf59218d..348366ce6 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -105,8 +105,14 @@ func NewKey(id, name, icon string, keyType KeyType) *Key { } type Rollup struct { - RelationKeyID string `json:"relationKeyID"` // 关联列 ID - KeyID string `json:"keyID"` // 目标列 ID + 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"` } type Relation struct { diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 710eecaea..581da4d3d 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -774,6 +774,46 @@ func getRowBlockValue(keyValues []*av.KeyValues) (ret *av.Value) { return } +func (tx *Transaction) doUpdateAttrViewColRollup(operation *Operation) (ret *TxErr) { + err := updateAttributeViewColRollup(operation) + if nil != err { + return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()} + } + return +} + +func updateAttributeViewColRollup(operation *Operation) (err error) { + // operation.AvID 汇总列所在 av + // operation.ID 汇总列 ID + // operation.ParentID 汇总列基于的关联列 ID + // operation.KeyID 目标列 ID + // operation.Data 计算方式 + + attrView, err := av.ParseAttributeView(operation.AvID) + if nil != err { + return + } + + rollUpKey, _ := attrView.GetKey(operation.ID) + if nil == rollUpKey { + return + } + + rollUpKey.Rollup = &av.Rollup{ + RelationKeyID: operation.ParentID, + KeyID: operation.KeyID, + } + + if "" != operation.Data { + if err = gulu.JSON.UnmarshalJSON([]byte(operation.Data.(string)), &rollUpKey.Rollup.Calc); nil != err { + return + } + } + + err = av.SaveAttributeView(attrView) + return +} + func (tx *Transaction) doUpdateAttrViewColRelation(operation *Operation) (ret *TxErr) { err := updateAttributeViewColRelation(operation) if nil != err { diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index aaa101fd1..4187e3e88 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -274,6 +274,8 @@ func performTx(tx *Transaction) (ret *TxErr) { ret = tx.doSortAttrViewView(op) case "updateAttrViewColRelation": ret = tx.doUpdateAttrViewColRelation(op) + case "updateAttrViewColRollup": + ret = tx.doUpdateAttrViewColRollup(op) } if nil != ret {