-
Notifications
You must be signed in to change notification settings - Fork 2
/
credit_history.go
57 lines (51 loc) · 1.67 KB
/
credit_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package model
import "github.com/jinzhu/gorm"
type CreditHistory struct {
gorm.Model
UserId uint `json:"userId"`
Change int `json:"change"`
Credit int `json:"credit"`
Message string `json:"message"`
}
type GameTimes struct {
gorm.Model
UserId uint `json:"userId"`
Change int `json:"change"`
Credit int `json:"credit"`
Message string `json:"message"`
}
type CreditHistoriesPage struct {
CreditHistories []*CreditHistory `json:"list"`
TotalNum uint `json:"totalNum"`
PageId int `json:"pageId"`
}
/*func GetCreditHistoriesByUserId(userId uint, pageId string, pageNum uint) (creditHistoriesPage CreditHistoriesPage, err error) {
if err = db.Limit(pageNum).Where("user_id = ? AND id > ?", userId, pageId).Order("create_time desc").Find(&creditHistoriesPage.CreditHistories).Error; err == nil {
creditHistoriesPage.PageId = creditHistoriesPage.CreditHistories
return creditHistories, nil
}
return nil, err
}*/
func ModifyCreditHistory(userId uint, change int, message string) (*CreditHistory, error) {
tx := db.Begin()
// 根据userId(主键)查找
user := User{}
if err := tx.First(&user, userId).Error; err != nil {
tx.Rollback()
return nil, err
}
// 更新User.credit
credit := user.Credit + change
if err := tx.Model(&user).Select("credit").Updates(map[string]interface{}{"credit": credit}).Error; err != nil {
tx.Rollback()
return nil, err
}
// 创建积分历史记录
creditHistory := CreditHistory{UserId: userId, Change: change, Credit: credit, Message: message}
if err := tx.Create(creditHistory).Error; err != nil {
tx.Rollback()
return nil, err
}
tx.Commit()
return &creditHistory, nil
}