Browse Source

#2624

fix bug
pull/2765/head
chenyifan01 2 years ago
parent
commit
f39afe6e55
4 changed files with 60 additions and 18 deletions
  1. +33
    -3
      models/admin_operate_log.go
  2. +2
    -2
      models/resource_specification.go
  3. +7
    -1
      services/admin/operate_log/operate_log.go
  4. +18
    -12
      services/cloudbrain/resource/resource_specification.go

+ 33
- 3
models/admin_operate_log.go View File

@@ -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)
}

+ 2
- 2
models/resource_specification.go View File

@@ -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
}



+ 7
- 1
services/admin/operate_log/operate_log.go View File

@@ -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)}
}

+ 18
- 12
services/cloudbrain/resource/resource_specification.go View File

@@ -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,
})
}

Loading…
Cancel
Save