Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a22b7f

Browse files
committed
如果有财富变动,没有领取初始资本的,自动领取
1 parent d5b59dd commit 0a22b7f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/logic/user_rich.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package logic
88

99
import (
10+
"errors"
11+
"fmt"
1012
"model"
1113

1214
. "db"
@@ -25,14 +27,37 @@ func (self UserRichLogic) Add(ctx context.Context) error {
2527
}
2628

2729
// IncrUserRich 增加或减少用户财富
28-
func (UserRichLogic) IncrUserRich(user *model.User, typ, award int, desc string) {
30+
func (self UserRichLogic) IncrUserRich(user *model.User, typ, award int, desc string) {
31+
var (
32+
total int64 = -1
33+
err error
34+
)
35+
36+
if award > 0 && typ == model.MissionTypeReplied {
37+
// 老用户,因为之前的主题被人回复而增加财富,自动帮其领取初始资本
38+
total, err = MasterDB.Where("uid=?", user.Uid).Count(new(model.UserBalanceDetail))
39+
if err != nil {
40+
logger.Errorln("IncrUserRich count error:", err)
41+
return
42+
}
43+
}
44+
2945
session := MasterDB.NewSession()
3046
defer session.Close()
31-
3247
session.Begin()
3348

34-
user.Balance += award
35-
_, err := session.Where("uid=?", user.Uid).Cols("balance").Update(user)
49+
var initialAward int
50+
if total == 0 {
51+
initialAward, err = self.autoCompleteInitial(session, user)
52+
if err != nil {
53+
logger.Errorln("IncrUserRich autoCompleteInitial error:", err)
54+
session.Rollback()
55+
return
56+
}
57+
}
58+
59+
user.Balance += initialAward + award
60+
_, err = session.Where("uid=?", user.Uid).Cols("balance").Update(user)
3661
if err != nil {
3762
logger.Errorln("IncrUserRich update error:", err)
3863
session.Rollback()
@@ -81,3 +106,25 @@ func (UserRichLogic) add(session *xorm.Session, balanceDetail *model.UserBalance
81106
_, err := session.Insert(balanceDetail)
82107
return err
83108
}
109+
110+
func (UserRichLogic) autoCompleteInitial(session *xorm.Session, user *model.User) (int, error) {
111+
mission := &model.Mission{}
112+
_, err := session.Where("id=?", model.InitialMissionId).Get(mission)
113+
if err != nil {
114+
return 0, err
115+
}
116+
if mission.Id == 0 {
117+
return 0, errors.New("初始资本任务不存在!")
118+
}
119+
120+
balanceDetail := &model.UserBalanceDetail{
121+
Uid: user.Uid,
122+
Type: model.MissionTypeInitial,
123+
Num: mission.Fixed,
124+
Balance: mission.Fixed,
125+
Desc: fmt.Sprintf("获得%s %d 铜币", model.BalanceTypeMap[mission.Type], mission.Fixed),
126+
}
127+
_, err = session.Insert(balanceDetail)
128+
129+
return mission.Fixed, err
130+
}

src/model/mission.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
MissionTypeAward = 80
3030
)
3131

32+
const (
33+
InitialMissionId = 1
34+
)
35+
3236
type Mission struct {
3337
Id int `json:"id" xorm:"pk autoincr"`
3438
Name string `json:"name"`

0 commit comments

Comments
 (0)