Skip to content

Commit d81975e

Browse files
committed
直接在tableFor里创建表
1 parent fc90840 commit d81975e

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

niu/db.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ import (
44
"fmt"
55
"github.com/RomiChan/syncx"
66
"github.com/jinzhu/gorm"
7+
"github.com/sirupsen/logrus"
78
)
89

910
var (
1011
migratedGroups = syncx.Map[string, bool]{} // key: string, value: bool
11-
tableHooks []tableHook
1212
)
1313

14-
type tableHook func(gid int64) error
15-
16-
type model struct {
17-
*gorm.DB
18-
}
19-
2014
func ensureTable[T userInfo | AuctionInfo](gid int64, prefix string) error {
2115
table := fmt.Sprintf("group_%d_%s_info", gid, prefix)
2216
if _, ok := migratedGroups.Load(table); ok {
@@ -32,29 +26,25 @@ func ensureTable[T userInfo | AuctionInfo](gid int64, prefix string) error {
3226
return nil
3327
}
3428

35-
func ensureUserInfo(gid int64) error {
36-
return ensureTable[userInfo](gid, ur)
37-
}
29+
func tableFor(gid int64, prefix string) *gorm.DB {
3830

39-
func ensureAuctionInfo(gid int64) error {
40-
return ensureTable[AuctionInfo](gid, ac)
41-
}
42-
43-
// registerTableHook 注册钩子
44-
func registerTableHook(h ...tableHook) {
45-
tableHooks = append(tableHooks, h...)
46-
}
47-
48-
func tableFor(gid int64, prefix string) *model {
49-
// 先跑钩子
50-
for _, h := range tableHooks {
51-
if err := h(gid); err != nil {
52-
panic(fmt.Sprintf("执行钩子失败: %v", err))
31+
switch prefix {
32+
case ur:
33+
err := ensureTable[userInfo](gid, ur)
34+
if err != nil {
35+
logrus.Error("ensureTable error: %v", err)
36+
return nil
37+
}
38+
case ac:
39+
err := ensureTable[AuctionInfo](gid, ac)
40+
if err != nil {
41+
logrus.Error("ensureTable error: %v", err)
42+
return nil
5343
}
5444
}
5545

5646
tableName := fmt.Sprintf("group_%d_%s_info", gid, prefix)
57-
return &model{db.Table(tableName)}
47+
return db.Table(tableName)
5848
}
5949

6050
func listUsers(gid int64) (users, error) {

niu/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
)
2323

2424
var (
25-
db *model
25+
db *gorm.DB
2626
globalLock sync.Mutex
2727

2828
errCancelFail = errors.New("遇到不可抗力因素,注销失败!")
@@ -84,9 +84,7 @@ func init() {
8484
panic(err)
8585
}
8686

87-
db = &model{sdb.LogMode(false)}
88-
89-
registerTableHook(ensureUserInfo, ensureAuctionInfo)
87+
db = sdb.LogMode(false)
9088
}
9189

9290
// DeleteWordNiuNiu ...
@@ -449,11 +447,13 @@ func Auction(gid, uid int64, index int) (string, error) {
449447
return "", err
450448
}
451449

450+
bs := fmt.Sprintf("恭喜你购买成功,当前长度为%.2fcm", niu.Length)
451+
452452
if info.Money >= 500 {
453-
return fmt.Sprintf("恭喜你购买成功,当前长度为%.2fcm,此次购买将赠送你2个伟哥,2个媚药", niu.Length), nil
453+
return fmt.Sprintf("%s,此次购买将赠送你2个伟哥,2个媚药", bs), nil
454454
}
455455

456-
return fmt.Sprintf("恭喜你购买成功,当前长度为%.2fcm", niu.Length), nil
456+
return bs, nil
457457
}
458458

459459
// Bag 牛牛背包

0 commit comments

Comments
 (0)