From f39afe6e55d4fdca9a553738d4d1d73dc2b0cd5d Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Thu, 18 Aug 2022 17:09:47 +0800 Subject: [PATCH] #2624 fix bug --- models/admin_operate_log.go | 36 ++++++++++++++++++++-- models/resource_specification.go | 4 +-- services/admin/operate_log/operate_log.go | 8 ++++- .../cloudbrain/resource/resource_specification.go | 30 ++++++++++-------- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/models/admin_operate_log.go b/models/admin_operate_log.go index 5a93fe7d8..439779ea6 100644 --- a/models/admin_operate_log.go +++ b/models/admin_operate_log.go @@ -1,19 +1,49 @@ package models -import "code.gitea.io/gitea/modules/timeutil" +import ( + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/timeutil" + "encoding/json" +) type AdminOperateLog struct { ID int64 `xorm:"pk autoincr"` BizType string OperateType string - OldValue string `xorm:"TEXT JSON"` - NewValue string `xorm:"TEXT JSON"` + OldValue string `xorm:"TEXT"` + NewValue string `xorm:"TEXT"` RelatedId string `xorm:"INDEX"` Comment string CreatedTime timeutil.TimeStamp `xorm:"created"` CreatedBy int64 } +type LogValues struct { + Params []LogValue +} + +type LogValue struct { + Key string + Val interface{} +} + +func (l *LogValues) Add(key string, val interface{}) *LogValues { + l.Params = append(l.Params, LogValue{Key: key, Val: val}) + return l +} + +func (l *LogValues) JsonString() string { + if len(l.Params) == 0 { + return "" + } + b, err := json.Marshal(l) + if err != nil { + log.Error("LogValues JsonString error . %v", err) + return "" + } + return string(b) +} + func InsertAdminOperateLog(log AdminOperateLog) (int64, error) { return x.Insert(&log) } diff --git a/models/resource_specification.go b/models/resource_specification.go index 2bf604045..dca6647ab 100644 --- a/models/resource_specification.go +++ b/models/resource_specification.go @@ -149,7 +149,7 @@ func UpdateResourceSpecificationById(queueId int64, spec ResourceSpecification) return x.ID(queueId).Update(&spec) } func UpdateSpecUnitPriceById(id int64, unitPrice int) error { - _, err := x.Exec("update resource_specification set unit_price = ? where id = ?", unitPrice, id) + _, err := x.Exec("update resource_specification set unit_price = ? ,updated_time = ? where id = ?", unitPrice, timeutil.TimeStampNow(), id) return err } @@ -198,7 +198,7 @@ func GetSpecScenes(specId int64) ([]ResourceSceneBriefRes, error) { } func ResourceSpecOnShelf(id int64, unitPrice int) error { - _, err := x.Exec("update resource_specification set unit_price = ?,status = ? where id = ?", unitPrice, SpecOnShelf, id) + _, err := x.Exec("update resource_specification set unit_price = ?,updated_time = ?,status = ? where id = ?", unitPrice, timeutil.TimeStampNow(), SpecOnShelf, id) return err } diff --git a/services/admin/operate_log/operate_log.go b/services/admin/operate_log/operate_log.go index 5328a6f9a..7b72ec2e2 100644 --- a/services/admin/operate_log/operate_log.go +++ b/services/admin/operate_log/operate_log.go @@ -1,8 +1,14 @@ package operate_log -import "code.gitea.io/gitea/models" +import ( + "code.gitea.io/gitea/models" +) func Log(log models.AdminOperateLog) error { _, err := models.InsertAdminOperateLog(log) return err } + +func NewLogValues() *models.LogValues { + return &models.LogValues{Params: make([]models.LogValue, 0)} +} diff --git a/services/cloudbrain/resource/resource_specification.go b/services/cloudbrain/resource/resource_specification.go index 2037fc2c2..27ce5de52 100644 --- a/services/cloudbrain/resource/resource_specification.go +++ b/services/cloudbrain/resource/resource_specification.go @@ -6,7 +6,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/routers/response" "code.gitea.io/gitea/services/admin/operate_log" - "encoding/json" "fmt" "strings" ) @@ -36,8 +35,7 @@ func UpdateSpecUnitPrice(doerId int64, specId int64, unitPrice int) *response.Bi } if oldSpec.UnitPrice != unitPrice { - newSpec, _ := models.GetResourceSpecification(&models.ResourceSpecification{ID: specId}) - AddSpecOperateLog(doerId, "edit", newSpec, oldSpec, fmt.Sprintf("修改单价从%d积分到%d积分", oldSpec.UnitPrice, newSpec.UnitPrice)) + AddSpecOperateLog(doerId, "edit", operate_log.NewLogValues().Add("unitPrice", unitPrice), operate_log.NewLogValues().Add("unitPrice", oldSpec.UnitPrice), specId, fmt.Sprintf("修改资源规格单价从%d积分到%d积分", oldSpec.UnitPrice, unitPrice)) } return nil } @@ -149,8 +147,9 @@ func ResourceSpecOnShelf(doerId int64, id int64, unitPrice int) *response.BizErr return response.NewBizError(err) } if spec.UnitPrice != unitPrice { - newSpec, _ := models.GetResourceSpecification(&models.ResourceSpecification{ID: id}) - AddSpecOperateLog(doerId, "on-shelf", newSpec, spec, fmt.Sprintf("修改单价从%d积分到%d积分", spec.UnitPrice, newSpec.UnitPrice)) + AddSpecOperateLog(doerId, "on-shelf", operate_log.NewLogValues().Add("UnitPrice", unitPrice), operate_log.NewLogValues().Add("UnitPrice", spec.UnitPrice), id, fmt.Sprintf("定价上架资源规格,单价为%d", unitPrice)) + } else { + AddSpecOperateLog(doerId, "on-shelf", nil, nil, id, "上架资源规格") } return nil @@ -160,20 +159,27 @@ func ResourceSpecOffShelf(doerId int64, id int64) *response.BizError { if err != nil { return response.NewBizError(err) } + AddSpecOperateLog(doerId, "off-shelf", nil, nil, id, "下架资源规格") return nil } -func AddSpecOperateLog(doerId int64, operateType string, newSpec, oldSpec *models.ResourceSpecification, comment string) { - newJson, _ := json.Marshal(newSpec) - oldJson, _ := json.Marshal(oldSpec) +func AddSpecOperateLog(doerId int64, operateType string, newValue, oldValue *models.LogValues, specId int64, comment string) { + var newString = "" + var oldString = "" + if newValue != nil { + newString = newValue.JsonString() + } + if oldValue != nil { + oldString = oldValue.JsonString() + } operate_log.Log(models.AdminOperateLog{ BizType: "SpecOperate", OperateType: operateType, - OldValue: string(newJson), - NewValue: string(oldJson), - RelatedId: fmt.Sprint(newSpec.ID), - Comment: comment, + OldValue: oldString, + NewValue: newString, + RelatedId: fmt.Sprint(specId), CreatedBy: doerId, + Comment: comment, }) }