@@ -1,19 +1,49 @@ | |||||
package models | 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 { | type AdminOperateLog struct { | ||||
ID int64 `xorm:"pk autoincr"` | ID int64 `xorm:"pk autoincr"` | ||||
BizType string | BizType string | ||||
OperateType 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"` | RelatedId string `xorm:"INDEX"` | ||||
Comment string | Comment string | ||||
CreatedTime timeutil.TimeStamp `xorm:"created"` | CreatedTime timeutil.TimeStamp `xorm:"created"` | ||||
CreatedBy int64 | 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) { | func InsertAdminOperateLog(log AdminOperateLog) (int64, error) { | ||||
return x.Insert(&log) | return x.Insert(&log) | ||||
} | } |
@@ -149,7 +149,7 @@ func UpdateResourceSpecificationById(queueId int64, spec ResourceSpecification) | |||||
return x.ID(queueId).Update(&spec) | return x.ID(queueId).Update(&spec) | ||||
} | } | ||||
func UpdateSpecUnitPriceById(id int64, unitPrice int) error { | 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 | return err | ||||
} | } | ||||
@@ -198,7 +198,7 @@ func GetSpecScenes(specId int64) ([]ResourceSceneBriefRes, error) { | |||||
} | } | ||||
func ResourceSpecOnShelf(id int64, unitPrice int) 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 | return err | ||||
} | } | ||||
@@ -1,8 +1,14 @@ | |||||
package operate_log | package operate_log | ||||
import "code.gitea.io/gitea/models" | |||||
import ( | |||||
"code.gitea.io/gitea/models" | |||||
) | |||||
func Log(log models.AdminOperateLog) error { | func Log(log models.AdminOperateLog) error { | ||||
_, err := models.InsertAdminOperateLog(log) | _, err := models.InsertAdminOperateLog(log) | ||||
return err | return err | ||||
} | } | ||||
func NewLogValues() *models.LogValues { | |||||
return &models.LogValues{Params: make([]models.LogValue, 0)} | |||||
} |
@@ -6,7 +6,6 @@ import ( | |||||
"code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
"code.gitea.io/gitea/routers/response" | "code.gitea.io/gitea/routers/response" | ||||
"code.gitea.io/gitea/services/admin/operate_log" | "code.gitea.io/gitea/services/admin/operate_log" | ||||
"encoding/json" | |||||
"fmt" | "fmt" | ||||
"strings" | "strings" | ||||
) | ) | ||||
@@ -36,8 +35,7 @@ func UpdateSpecUnitPrice(doerId int64, specId int64, unitPrice int) *response.Bi | |||||
} | } | ||||
if oldSpec.UnitPrice != unitPrice { | 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 | return nil | ||||
} | } | ||||
@@ -149,8 +147,9 @@ func ResourceSpecOnShelf(doerId int64, id int64, unitPrice int) *response.BizErr | |||||
return response.NewBizError(err) | return response.NewBizError(err) | ||||
} | } | ||||
if spec.UnitPrice != unitPrice { | 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 | return nil | ||||
@@ -160,20 +159,27 @@ func ResourceSpecOffShelf(doerId int64, id int64) *response.BizError { | |||||
if err != nil { | if err != nil { | ||||
return response.NewBizError(err) | return response.NewBizError(err) | ||||
} | } | ||||
AddSpecOperateLog(doerId, "off-shelf", nil, nil, id, "下架资源规格") | |||||
return nil | 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{ | operate_log.Log(models.AdminOperateLog{ | ||||
BizType: "SpecOperate", | BizType: "SpecOperate", | ||||
OperateType: operateType, | 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, | CreatedBy: doerId, | ||||
Comment: comment, | |||||
}) | }) | ||||
} | } |