MCPcopy Index your code
hub / github.com/TruthHun/BookStack / Sign

Method Sign

models/sign.go:95–179  ·  view source on GitHub ↗

执行签到。使用事务

(uid int, fromApp bool)

Source from the content-addressed store, hash-verified

93
94// 执行签到。使用事务
95func (m *Sign) Sign(uid int, fromApp bool) (reward int, err error) {
96 s := &Sign{}
97 o := orm.NewOrm()
98 now := time.Now()
99 day, _ := strconv.Atoi(now.Format(signDayLayout))
100 // 1. 检测用户有没有签到
101 o.QueryTable(s).Filter("uid", uid).Filter("day", day).One(s)
102 if m.IsSignToday(uid) {
103 err = errors.New(messageSigned)
104 return
105 }
106 isContinuousSign := m.IsContinuousSign(uid) // 昨天有没有断签
107
108 // 2. 查询用户签到了多少天
109 user := NewMember()
110 cols := []string{"member_id", "total_sign", "total_continuous_sign", "history_total_continuous_sign"}
111 o.QueryTable(user).Filter("member_id", uid).One(user, cols...)
112 if user.MemberId < 0 {
113 err = errors.New(messageNotExistUser)
114 return
115 }
116 // 3. 查询奖励规则
117 rule := s.GetSignRule()
118 // 4. 更新用户签到记录、签到天数和连续签到天数
119 o.Begin()
120 defer func() {
121 if err != nil {
122 beego.Error(err)
123 err = errors.New(messageSignInnerErr)
124 o.Rollback()
125 } else {
126 o.Commit()
127 }
128 }()
129 user.TotalSign += 1
130 s.Day = day
131 s.Uid = uid
132 s.CreatedAt = now
133 s.FromApp = fromApp
134
135 // 奖励计算
136 if isContinuousSign { //连续签到
137 user.TotalContinuousSign += 1
138 extra := user.TotalContinuousSign * rule.ContinuousReward
139 if extra >= rule.MaxContinuousReward {
140 extra = rule.MaxContinuousReward
141 }
142 s.Reward = rule.BasicReward + extra
143 } else { // 未连续签到
144 user.TotalContinuousSign = 1
145 s.Reward = rule.BasicReward + rule.ContinuousReward
146 }
147
148 if user.TotalContinuousSign > user.HistoryTotalContinuousSign {
149 user.HistoryTotalContinuousSign = user.TotalContinuousSign
150 }
151
152 if fromApp {

Callers 2

SignTodayMethod · 0.80
SignTodayMethod · 0.80

Calls 7

IsSignTodayMethod · 0.95
IsContinuousSignMethod · 0.95
GetSignRuleMethod · 0.95
NewMemberFunction · 0.85
NewReadingTimeFunction · 0.85
InsertMethod · 0.45
UpdateMethod · 0.45

Tested by

no test coverage detected