Commit 908db8e3fe81efbc44228bf18a530be48c5bc49c
1 parent
b6e64875
Exists in
master
and in
1 other branch
refactor: orm重构
Showing
13 changed files
with
309 additions
and
240 deletions
Show diff stats
dto/cardholder-record.go
| ... | ... | @@ -3,31 +3,19 @@ package dto |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/service/constd" |
| 5 | 5 | "apigame/util/util-lx/lxtime" |
| 6 | - "fmt" | |
| 6 | + "apigame/util/utildto" | |
| 7 | 7 | ) |
| 8 | 8 | |
| 9 | 9 | // CardHolderRecordBase 开卡包活动日志公共 |
| 10 | 10 | type CardHolderRecordBase struct { |
| 11 | - Id int64 `orm:"auto"` // 日志ID | |
| 12 | - Uid int64 // 玩家唯一ID | |
| 13 | - SequenceId string // 用户序列组ID | |
| 14 | - Cohort string // 用户分组ID | |
| 15 | - ActivityId int64 // 当前活动ID | |
| 16 | - Round int // 当前轮次 | |
| 17 | - CreateTime int64 // 创建时间戳 | |
| 18 | - UpdateTime int64 // 修改时间戳 | |
| 19 | -} | |
| 20 | - | |
| 21 | -func (d *CardHolderRecordBase) SqlPairsBase() map[string]string { | |
| 22 | - m := make(map[string]string) | |
| 23 | - m["uid"] = fmt.Sprintf("%d", d.Uid) | |
| 24 | - m["sequence_id"] = fmt.Sprintf("'%s'", d.SequenceId) | |
| 25 | - m["cohort"] = fmt.Sprintf("'%s'", d.Cohort) | |
| 26 | - m["activity_id"] = fmt.Sprintf("%d", d.ActivityId) | |
| 27 | - m["round"] = fmt.Sprintf("%d", d.Round) | |
| 28 | - m["create_time"] = fmt.Sprintf("%d", d.CreateTime) | |
| 29 | - m["update_time"] = fmt.Sprintf("%d", d.UpdateTime) | |
| 30 | - return m | |
| 11 | + Id int64 `gorm:"primaryKey;comment:日志ID"` | |
| 12 | + Uid int64 `gorm:"comment:玩家唯一ID"` | |
| 13 | + SequenceId string `gorm:"type:varchar(255);comment:用户序列组ID"` | |
| 14 | + Cohort string `gorm:"type:varchar(255);comment:用户分组ID"` | |
| 15 | + ActivityId int64 `gorm:"comment:当前活动ID"` | |
| 16 | + Round int `gorm:"comment:当前轮次"` | |
| 17 | + CreateTime int64 `gorm:"comment:创建时间戳"` | |
| 18 | + UpdateTime int64 `gorm:"comment:修改时间戳"` | |
| 31 | 19 | } |
| 32 | 20 | |
| 33 | 21 | func NewCardHolderRecordBase(uid int64, sequenceId string, cohort string, activityId int64, round int) CardHolderRecordBase { |
| ... | ... | @@ -46,23 +34,15 @@ func NewCardHolderRecordBase(uid int64, sequenceId string, cohort string, activi |
| 46 | 34 | // CardHolderRecordOpen 开卡包活动日志开卡包 |
| 47 | 35 | type CardHolderRecordOpen struct { |
| 48 | 36 | CardHolderRecordBase |
| 49 | - CardholderId int // 卡包ID | |
| 50 | - CardList string // 开卡内容 | |
| 51 | -} | |
| 52 | - | |
| 53 | -func (d *CardHolderRecordOpen) TableName(gameId string) string { | |
| 54 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_OPEN + gameId | |
| 37 | + CardholderId int `gorm:"comment:卡包ID"` | |
| 38 | + CardList string `gorm:"type:varchar(255);comment:开卡内容"` | |
| 55 | 39 | } |
| 56 | 40 | |
| 57 | -func (d *CardHolderRecordOpen) CreateSqlPath() string { | |
| 58 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_OPEN | |
| 41 | +func (d *CardHolderRecordOpen) TableName() string { | |
| 42 | + return utildto.MYSQL_TABLE_TEMPLATE | |
| 59 | 43 | } |
| 60 | - | |
| 61 | -func (d *CardHolderRecordOpen) SqlPairs() map[string]string { | |
| 62 | - m := d.SqlPairsBase() | |
| 63 | - m["cardholder_id"] = fmt.Sprintf("%d", d.CardholderId) | |
| 64 | - m["card_list"] = fmt.Sprintf("'%s'", d.CardList) | |
| 65 | - return m | |
| 44 | +func (d *CardHolderRecordOpen) GetTableName(gameId string) string { | |
| 45 | + return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_OPEN + gameId | |
| 66 | 46 | } |
| 67 | 47 | |
| 68 | 48 | func NewCardHolderRecordOpen(recordBase CardHolderRecordBase, |
| ... | ... | @@ -77,23 +57,15 @@ func NewCardHolderRecordOpen(recordBase CardHolderRecordBase, |
| 77 | 57 | // CardHolderRecordRewardAlbum 开卡包活动日志领取卡组奖励 |
| 78 | 58 | type CardHolderRecordRewardAlbum struct { |
| 79 | 59 | CardHolderRecordBase |
| 80 | - AlbumId string // 卡组ID | |
| 81 | - Award string // 奖励内容 | |
| 60 | + AlbumId string `gorm:"type:varchar(255);comment:卡组ID"` | |
| 61 | + Award string `gorm:"type:varchar(255);comment:奖励内容"` | |
| 82 | 62 | } |
| 83 | 63 | |
| 84 | -func (d *CardHolderRecordRewardAlbum) TableName(gameId string) string { | |
| 85 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDALBUM + gameId | |
| 64 | +func (d *CardHolderRecordRewardAlbum) TableName() string { | |
| 65 | + return utildto.MYSQL_TABLE_TEMPLATE | |
| 86 | 66 | } |
| 87 | - | |
| 88 | -func (d *CardHolderRecordRewardAlbum) CreateSqlPath() string { | |
| 89 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDALBUM | |
| 90 | -} | |
| 91 | - | |
| 92 | -func (d *CardHolderRecordRewardAlbum) SqlPairs() map[string]string { | |
| 93 | - m := d.SqlPairsBase() | |
| 94 | - m["album_id"] = fmt.Sprintf("'%s'", d.AlbumId) | |
| 95 | - m["award"] = fmt.Sprintf("'%s'", d.Award) | |
| 96 | - return m | |
| 67 | +func (d *CardHolderRecordRewardAlbum) GetTableName(gameId string) string { | |
| 68 | + return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDALBUM + gameId | |
| 97 | 69 | } |
| 98 | 70 | |
| 99 | 71 | func NewCardHolderRecordRewardAlbum(recordBase CardHolderRecordBase, |
| ... | ... | @@ -108,21 +80,14 @@ func NewCardHolderRecordRewardAlbum(recordBase CardHolderRecordBase, |
| 108 | 80 | // CardHolderRecordRewardRound 开卡包活动日志领取轮次奖励 |
| 109 | 81 | type CardHolderRecordRewardRound struct { |
| 110 | 82 | CardHolderRecordBase |
| 111 | - Award string // 奖励内容 | |
| 112 | -} | |
| 113 | - | |
| 114 | -func (d *CardHolderRecordRewardRound) TableName(gameId string) string { | |
| 115 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDROUND + gameId | |
| 83 | + Award string `gorm:"type:varchar(255);comment:奖励内容"` | |
| 116 | 84 | } |
| 117 | 85 | |
| 118 | -func (d *CardHolderRecordRewardRound) CreateSqlPath() string { | |
| 119 | - return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDROUND | |
| 86 | +func (d *CardHolderRecordRewardRound) TableName() string { | |
| 87 | + return utildto.MYSQL_TABLE_TEMPLATE | |
| 120 | 88 | } |
| 121 | - | |
| 122 | -func (d *CardHolderRecordRewardRound) SqlPairs() map[string]string { | |
| 123 | - m := d.SqlPairsBase() | |
| 124 | - m["award"] = fmt.Sprintf("'%s'", d.Award) | |
| 125 | - return m | |
| 89 | +func (d *CardHolderRecordRewardRound) GetTableName(gameId string) string { | |
| 90 | + return constd.MYSQL_TABLE_S_CARDHOLDER_RECORD_REWARDROUND + gameId | |
| 126 | 91 | } |
| 127 | 92 | |
| 128 | 93 | func NewCardHolderRecordRewardRound(recordBase CardHolderRecordBase, | ... | ... |
dto/cardholder.go
| ... | ... | @@ -4,33 +4,18 @@ import ( |
| 4 | 4 | "apigame/service/constd" |
| 5 | 5 | "apigame/util/util-lx/lxalilog" |
| 6 | 6 | "apigame/util/util-lx/lxtime" |
| 7 | + "apigame/util/utildto" | |
| 7 | 8 | "encoding/json" |
| 8 | - "fmt" | |
| 9 | - "github.com/astaxie/beego/orm" | |
| 10 | 9 | ) |
| 11 | 10 | |
| 12 | -func InitCardholder() { | |
| 13 | - | |
| 14 | - // create table | |
| 15 | - o := orm.NewOrm() | |
| 16 | - for _, gameId := range constd.GameListCardHolder { | |
| 17 | - CreateTable(o, constd.MYSQL_MERGECOMMON, new(CardHolderData), gameId) | |
| 18 | - | |
| 19 | - CreateTable(o, constd.MYSQL_MERGECOMMON, new(CardHolderRecordOpen), gameId) | |
| 20 | - CreateTable(o, constd.MYSQL_MERGECOMMON, new(CardHolderRecordRewardAlbum), gameId) | |
| 21 | - CreateTable(o, constd.MYSQL_MERGECOMMON, new(CardHolderRecordRewardRound), gameId) | |
| 22 | - } | |
| 23 | - | |
| 24 | -} | |
| 25 | - | |
| 26 | 11 | // CardHolderData 卡牌活动持久数据 |
| 27 | 12 | type CardHolderData struct { |
| 28 | - Uid int64 // 玩家唯一ID | |
| 29 | - ActivityId int64 // 活动ID | |
| 30 | - Details *CardHolderDataDetails // 活动详情 | |
| 31 | - DetailsText string // 活动详情封装 | |
| 32 | - CreateTime int64 // 创建时间戳 | |
| 33 | - UpdateTime int64 // 修改时间戳 | |
| 13 | + Uid int64 `gorm:"primaryKey;comment:玩家唯一ID"` | |
| 14 | + ActivityId int64 `gorm:"comment:活动ID"` | |
| 15 | + Details *CardHolderDataDetails `gorm:"-"` // 活动详情 | |
| 16 | + DetailsText string `gorm:"comment:活动详情封装"` | |
| 17 | + CreateTime int64 `gorm:"comment:创建时间戳"` | |
| 18 | + UpdateTime int64 `gorm:"comment:修改时间戳"` | |
| 34 | 19 | } |
| 35 | 20 | |
| 36 | 21 | // CardHolderDataDetails 卡牌活动详情 |
| ... | ... | @@ -61,21 +46,12 @@ func NewCardHolderDataDetails() *CardHolderDataDetails { |
| 61 | 46 | } |
| 62 | 47 | } |
| 63 | 48 | |
| 64 | -func (d *CardHolderData) TableName(gameId string) string { | |
| 65 | - return constd.MYSQL_TABLE_S_CARDHOLDER_DATA + gameId | |
| 49 | +func (d *CardHolderData) TableName() string { | |
| 50 | + return utildto.MYSQL_TABLE_TEMPLATE | |
| 66 | 51 | } |
| 67 | 52 | |
| 68 | -func (d *CardHolderData) CreateSqlPath() string { | |
| 69 | - return constd.MYSQL_TABLE_S_CARDHOLDER_DATA | |
| 70 | -} | |
| 71 | - | |
| 72 | -func (d *CardHolderData) SqlPairs() map[string]string { | |
| 73 | - m := make(map[string]string) | |
| 74 | - m["activity_id"] = fmt.Sprintf("%d", d.ActivityId) | |
| 75 | - m["details_text"] = fmt.Sprintf("'%s'", d.DetailsText) | |
| 76 | - m["create_time"] = fmt.Sprintf("%d", d.CreateTime) | |
| 77 | - m["update_time"] = fmt.Sprintf("%d", d.UpdateTime) | |
| 78 | - return m | |
| 53 | +func (d *CardHolderData) GetTableName(gameId string) string { | |
| 54 | + return constd.MYSQL_TABLE_S_CARDHOLDER_DATA + gameId | |
| 79 | 55 | } |
| 80 | 56 | |
| 81 | 57 | func (d *CardHolderData) Init(uid int64) { | ... | ... |
dto/dto.go
| 1 | 1 | package dto |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | - "github.com/astaxie/beego/logs" | |
| 5 | - "github.com/astaxie/beego/orm" | |
| 6 | - "io/ioutil" | |
| 7 | - "os" | |
| 8 | - "strings" | |
| 4 | + "apigame/util/utildto" | |
| 5 | + "gorm.io/gorm" | |
| 9 | 6 | ) |
| 10 | 7 | |
| 11 | -func CreateTable(o orm.Ormer, dbName string, tb ITableData, gameId string) (err error) { | |
| 12 | - | |
| 13 | - o.Using(dbName) | |
| 14 | - | |
| 15 | - createSqlPath := "./conf/conf/db/" + tb.CreateSqlPath() + ".sql" | |
| 16 | - createSql, err := readFileSql(createSqlPath, tb.TableName(gameId)) | |
| 17 | - if err != nil { | |
| 18 | - logs.Error(err) | |
| 19 | - return | |
| 20 | - } | |
| 21 | - | |
| 22 | - _, err = o.Raw(createSql).Exec() | |
| 23 | - if err != nil { | |
| 24 | - //logs.Error(err) | |
| 25 | - return | |
| 26 | - } | |
| 27 | - | |
| 28 | - return | |
| 29 | -} | |
| 30 | - | |
| 31 | -func readFileSql(createSqlPath, tableName string) (sql string, err error) { | |
| 32 | - filePtr, err := os.Open(createSqlPath) | |
| 33 | - if err != nil { | |
| 34 | - return | |
| 35 | - } | |
| 36 | - defer filePtr.Close() | |
| 37 | - | |
| 38 | - by, err := ioutil.ReadAll(filePtr) | |
| 39 | - if err != nil { | |
| 40 | - return | |
| 41 | - } | |
| 42 | - | |
| 43 | - sql = strings.Replace(string(by), "{tablename}", tableName, -1) | |
| 44 | - | |
| 45 | - return | |
| 8 | +func InitTable(db *gorm.DB, tb ITableData, gameId string) { | |
| 9 | + tableName := tb.GetTableName(gameId) | |
| 10 | + utildto.InitTable(db, tb, tableName) | |
| 46 | 11 | } | ... | ... |
| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | +package dto | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/service/constd" | |
| 5 | + "apigame/util/utildto" | |
| 6 | + "fmt" | |
| 7 | + "github.com/astaxie/beego" | |
| 8 | + "gorm.io/gorm" | |
| 9 | +) | |
| 10 | + | |
| 11 | +var ( | |
| 12 | + DbCommon *gorm.DB | |
| 13 | + DbConfig *gorm.DB | |
| 14 | +) | |
| 15 | + | |
| 16 | +func InitDto() { | |
| 17 | + | |
| 18 | + // ConnectMySQL | |
| 19 | + if db, err := utildto.ConnectMySQL(beego.AppConfig.String("mysql::mergecommon")); err == nil { | |
| 20 | + DbCommon = db | |
| 21 | + } else { | |
| 22 | + fmt.Println(" InitMysqls Error::" + err.Error()) | |
| 23 | + } | |
| 24 | + if db, err := utildto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG)); err == nil { | |
| 25 | + DbConfig = db | |
| 26 | + } else { | |
| 27 | + | |
| 28 | + } | |
| 29 | + | |
| 30 | + // create table | |
| 31 | + // 卡牌卡包 | |
| 32 | + for _, gameId := range constd.GameListCardHolder { | |
| 33 | + InitTable(DbCommon, new(CardHolderData), gameId) | |
| 34 | + InitTable(DbCommon, new(CardHolderRecordOpen), gameId) | |
| 35 | + InitTable(DbCommon, new(CardHolderRecordRewardAlbum), gameId) | |
| 36 | + InitTable(DbCommon, new(CardHolderRecordRewardRound), gameId) | |
| 37 | + } | |
| 38 | + | |
| 39 | +} | ... | ... |
dto/inits.go
dto/interface.go
| ... | ... | @@ -2,25 +2,23 @@ package dto |
| 2 | 2 | |
| 3 | 3 | // ITableData 数据库表 |
| 4 | 4 | type ITableData interface { |
| 5 | - // TableName 表名 后缀是 gameId | |
| 6 | - TableName(gameId string) string | |
| 7 | - // CreateSqlPath 创建sql位置 | |
| 8 | - CreateSqlPath() string | |
| 5 | + // GetTableName 表名 后缀是 gameId | |
| 6 | + GetTableName(gameId string) string | |
| 9 | 7 | } |
| 10 | 8 | |
| 11 | 9 | type IRecordData interface { |
| 12 | - // TableName 表名 后缀是 gameId | |
| 13 | - TableName(gameId string) string | |
| 14 | - // SqlPairs 数据键值对 | |
| 15 | - SqlPairs() map[string]string | |
| 10 | + // TableName 表名模板 | |
| 11 | + TableName() string | |
| 12 | + // GetTableName 表名 后缀是 gameId | |
| 13 | + GetTableName(gameId string) string | |
| 16 | 14 | } |
| 17 | 15 | |
| 18 | 16 | // IPlayerData 玩家数据表 |
| 19 | 17 | type IPlayerData interface { |
| 20 | - // TableName 表名 后缀是 gameId | |
| 21 | - TableName(gameId string) string | |
| 22 | - // SqlPairs 数据键值对 | |
| 23 | - SqlPairs() map[string]string | |
| 18 | + // TableName 表名模板 | |
| 19 | + TableName() string | |
| 20 | + // GetTableName 表名 后缀是 gameId | |
| 21 | + GetTableName(gameId string) string | |
| 24 | 22 | // Init 新建 |
| 25 | 23 | Init(uid int64) |
| 26 | 24 | } | ... | ... |
dto/player-data.go
| 1 | 1 | package dto |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | - "apigame/service/constd" | |
| 5 | 4 | "apigame/util/util-lx/lxalilog" |
| 6 | - "apigame/util/utmysql" | |
| 7 | - "fmt" | |
| 8 | - "github.com/astaxie/beego/orm" | |
| 5 | + "apigame/util/utildto" | |
| 6 | + "strings" | |
| 9 | 7 | ) |
| 10 | 8 | |
| 11 | 9 | func LoadPlayerData(gameId string, uid int64, data IPlayerData) (has bool, err error) { |
| 12 | - tableName := data.TableName(gameId) | |
| 13 | - sql := fmt.Sprintf("select * from %s where uid=%d limit 1", tableName, uid) | |
| 14 | - has, err = utmysql.FindOneSql(constd.MYSQL_MERGECOMMON, sql, data) | |
| 15 | - if err != nil { | |
| 16 | - lxalilog.Errors(err, sql, gameId, uid) | |
| 17 | - return | |
| 10 | + result := utildto.First(DbCommon, data, data.GetTableName(gameId)) | |
| 11 | + err = result.Error | |
| 12 | + if err == nil { | |
| 13 | + has = true | |
| 14 | + } else { | |
| 15 | + has = false | |
| 16 | + if strings.Contains(err.Error(), "record not found") { | |
| 17 | + err = nil | |
| 18 | + } else { | |
| 19 | + lxalilog.Errors(err, gameId, uid) | |
| 20 | + return | |
| 21 | + } | |
| 18 | 22 | } |
| 19 | 23 | // 档案不存在 新建 |
| 20 | 24 | if !has { |
| 21 | 25 | data.Init(uid) |
| 22 | - CreatePlayerData(gameId, uid, data) | |
| 26 | + CreatePlayerData(gameId, data) | |
| 23 | 27 | } |
| 24 | 28 | return |
| 25 | 29 | } |
| 26 | 30 | |
| 27 | -func CreatePlayerData(gameId string, uid int64, data IPlayerData) { | |
| 28 | - text1 := "" | |
| 29 | - text2 := "" | |
| 30 | - for s, s2 := range data.SqlPairs() { | |
| 31 | - text1 += "," + s | |
| 32 | - text2 += "," + s2 | |
| 33 | - } | |
| 34 | - sql := fmt.Sprintf("insert into %s (uid%s) value (%d%s)", | |
| 35 | - data.TableName(gameId), text1, uid, text2) | |
| 36 | - | |
| 37 | - o := orm.NewOrm() | |
| 38 | - o.Using(constd.MYSQL_MERGECOMMON) | |
| 39 | - | |
| 40 | - _, err := o.Raw(sql).Exec() | |
| 41 | - if err != nil { | |
| 42 | - lxalilog.Errors(err, sql, gameId, uid) | |
| 43 | - return | |
| 44 | - } | |
| 31 | +func CreatePlayerData(gameId string, data IPlayerData) { | |
| 32 | + utildto.Insert(DbCommon, data, data.GetTableName(gameId)) | |
| 45 | 33 | } |
| 46 | 34 | |
| 47 | -func SavePlayerData(gameId string, uid int64, data IPlayerData) { | |
| 48 | - text := "" | |
| 49 | - indexPairs := 0 | |
| 50 | - for s, s2 := range data.SqlPairs() { | |
| 51 | - if indexPairs != 0 { | |
| 52 | - text += "," | |
| 53 | - } | |
| 54 | - text += fmt.Sprintf("%s=%s", s, s2) | |
| 55 | - indexPairs++ | |
| 56 | - } | |
| 57 | - sql := fmt.Sprintf("update %s set %s where uid=%d", data.TableName(gameId), text, uid) | |
| 58 | - | |
| 59 | - o := orm.NewOrm() | |
| 60 | - o.Using(constd.MYSQL_MERGECOMMON) | |
| 61 | - | |
| 62 | - _, err := o.Raw(sql).Exec() | |
| 63 | - if err != nil { | |
| 64 | - lxalilog.Errors(err, sql, gameId, uid) | |
| 65 | - return | |
| 66 | - } | |
| 35 | +func SavePlayerData(gameId string, data IPlayerData) { | |
| 36 | + utildto.Save(DbCommon, data, data.GetTableName(gameId)) | |
| 67 | 37 | } | ... | ... |
dto/record.go
| 1 | 1 | package dto |
| 2 | 2 | |
| 3 | -import ( | |
| 4 | - "apigame/service/constd" | |
| 5 | - "apigame/util/util-lx/lxalilog" | |
| 6 | - "fmt" | |
| 7 | - "github.com/astaxie/beego/orm" | |
| 8 | -) | |
| 3 | +import "apigame/util/utildto" | |
| 9 | 4 | |
| 10 | 5 | func SaveRecord(gameId string, data IRecordData) { |
| 11 | - text1 := "" | |
| 12 | - text2 := "" | |
| 13 | - indexPairs := 0 | |
| 14 | - for s, s2 := range data.SqlPairs() { | |
| 15 | - if indexPairs != 0 { | |
| 16 | - text1 += "," | |
| 17 | - text2 += "," | |
| 18 | - } | |
| 19 | - text1 += s | |
| 20 | - text2 += s2 | |
| 21 | - indexPairs++ | |
| 22 | - } | |
| 23 | - sql := fmt.Sprintf("insert into %s (%s) value (%s)", | |
| 24 | - data.TableName(gameId), text1, text2) | |
| 25 | - | |
| 26 | - o := orm.NewOrm() | |
| 27 | - o.Using(constd.MYSQL_MERGECOMMON) | |
| 28 | - | |
| 29 | - _, err := o.Raw(sql).Exec() | |
| 30 | - if err != nil { | |
| 31 | - lxalilog.Errors(err, sql, gameId) | |
| 32 | - return | |
| 33 | - } | |
| 6 | + utildto.Insert(DbCommon, data, data.GetTableName(gameId)) | |
| 34 | 7 | } | ... | ... |
go.mod
| ... | ... | @@ -7,8 +7,10 @@ require ( |
| 7 | 7 | github.com/aliyun/aliyun-log-go-sdk v0.1.72 |
| 8 | 8 | github.com/astaxie/beego v1.12.3 |
| 9 | 9 | github.com/garyburd/redigo v1.6.4 |
| 10 | - github.com/go-sql-driver/mysql v1.5.0 | |
| 10 | + github.com/go-sql-driver/mysql v1.7.0 | |
| 11 | 11 | github.com/lixu-any/go-tools v0.0.0-20221128094643-07d54c69e9bf |
| 12 | + gorm.io/driver/mysql v1.5.6 | |
| 13 | + gorm.io/gorm v1.25.9 | |
| 12 | 14 | ) |
| 13 | 15 | |
| 14 | 16 | require ( |
| ... | ... | @@ -20,6 +22,8 @@ require ( |
| 20 | 22 | github.com/gogo/protobuf v1.3.2 // indirect |
| 21 | 23 | github.com/golang/protobuf v1.4.2 // indirect |
| 22 | 24 | github.com/hashicorp/golang-lru v0.5.4 // indirect |
| 25 | + github.com/jinzhu/inflection v1.0.0 // indirect | |
| 26 | + github.com/jinzhu/now v1.1.5 // indirect | |
| 23 | 27 | github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect |
| 24 | 28 | github.com/pierrec/lz4 v2.6.0+incompatible // indirect |
| 25 | 29 | github.com/pkg/errors v0.9.1 // indirect | ... | ... |
go.sum
| ... | ... | @@ -128,8 +128,9 @@ github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih |
| 128 | 128 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= |
| 129 | 129 | github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= |
| 130 | 130 | github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= |
| 131 | -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= | |
| 132 | 131 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= |
| 132 | +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= | |
| 133 | +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= | |
| 133 | 134 | github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= |
| 134 | 135 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= |
| 135 | 136 | github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= |
| ... | ... | @@ -203,6 +204,10 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO |
| 203 | 204 | github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= |
| 204 | 205 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= |
| 205 | 206 | github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= |
| 207 | +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | |
| 208 | +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | |
| 209 | +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | |
| 210 | +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | |
| 206 | 211 | github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= |
| 207 | 212 | github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= |
| 208 | 213 | github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= |
| ... | ... | @@ -541,6 +546,11 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
| 541 | 546 | gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
| 542 | 547 | gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= |
| 543 | 548 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
| 549 | +gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8= | |
| 550 | +gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= | |
| 551 | +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= | |
| 552 | +gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8= | |
| 553 | +gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= | |
| 544 | 554 | honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= |
| 545 | 555 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= |
| 546 | 556 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= | ... | ... |
service/cardholder/handle.go
| ... | ... | @@ -92,12 +92,12 @@ func HandleOpen(req *models.ReqCardHolderOpen) (code string, rsp models.RspCardH |
| 92 | 92 | |
| 93 | 93 | // 开始处理抽卡 |
| 94 | 94 | sequenceId, cohort := GetUserSequenceIdAndCohort(req.UID) |
| 95 | - //logs.Debug("wjwdbg 用户序列组IDsequenceId", sequenceId) | |
| 96 | - //logs.Debug("wjwdbg 用户分组IDcohort:", cohort) | |
| 95 | + //logs.Debug("dwjw 用户序列组IDsequenceId", sequenceId) | |
| 96 | + //logs.Debug("dwjw 用户分组IDcohort:", cohort) | |
| 97 | 97 | if confCardholder.IsNew != 0 { |
| 98 | 98 | // 如果是新卡包 按顺序查找数目最少的一张卡抽取 |
| 99 | 99 | cardId := GetNewCard(gameData, config) |
| 100 | - //logs.Debug("wjwdbg 抽取新卡ID:", cardId) | |
| 100 | + //logs.Debug("dwjw 抽取新卡ID:", cardId) | |
| 101 | 101 | // 增加卡牌 |
| 102 | 102 | gameData.Details.Cards[cardId] += 1 |
| 103 | 103 | rsp.NewCards = append(rsp.NewCards, cardId) |
| ... | ... | @@ -113,7 +113,7 @@ func HandleOpen(req *models.ReqCardHolderOpen) (code string, rsp models.RspCardH |
| 113 | 113 | gameData.Details.CardSequenceScales[combineId] = scale + 1 |
| 114 | 114 | // 增加卡牌 |
| 115 | 115 | gameData.Details.Cards[cardIdInt] += 1 |
| 116 | - //logs.Debug("wjwdbg😊 抽取卡牌ID:", cardIdInt) | |
| 116 | + //logs.Debug("dwjw😊 抽取卡牌ID:", cardIdInt) | |
| 117 | 117 | rsp.NewCards = append(rsp.NewCards, cardIdInt) |
| 118 | 118 | } |
| 119 | 119 | } |
| ... | ... | @@ -121,7 +121,7 @@ func HandleOpen(req *models.ReqCardHolderOpen) (code string, rsp models.RspCardH |
| 121 | 121 | // 先抽保底卡 |
| 122 | 122 | if confCardholder.GuaranteedStarCardId != "0" { |
| 123 | 123 | getCardFunc(confCardholder.GuaranteedStarCardId) |
| 124 | - //logs.Debug("wjwdbg 保底卡牌配置ID:", confCardholder.GuaranteedStarCardId) | |
| 124 | + //logs.Debug("dwjw 保底卡牌配置ID:", confCardholder.GuaranteedStarCardId) | |
| 125 | 125 | } |
| 126 | 126 | // 非保底卡 |
| 127 | 127 | if confCardholder.NormalCardNumber > 0 { |
| ... | ... | @@ -133,8 +133,8 @@ func HandleOpen(req *models.ReqCardHolderOpen) (code string, rsp models.RspCardH |
| 133 | 133 | starIds := GetListFromArray(confNormalCardStar.NormalCardSequenceIds, scaleStar, count) |
| 134 | 134 | // 增加星级刻度 |
| 135 | 135 | gameData.Details.StarSequenceScales[combineIdStar] = scaleStar + count |
| 136 | - //logs.Debug("wjwdbg 非保底卡星级ID:", confCardholder.MinimumGuaranteeCardId) | |
| 137 | - //logs.Debug("wjwdbg 非保底卡星级抽取序列:", starIds) | |
| 136 | + //logs.Debug("dwjw 非保底卡星级ID:", confCardholder.MinimumGuaranteeCardId) | |
| 137 | + //logs.Debug("dwjw 非保底卡星级抽取序列:", starIds) | |
| 138 | 138 | // 遍历星级刻度抽取n张卡牌 |
| 139 | 139 | for _, id := range starIds { |
| 140 | 140 | getCardFunc(id) |
| ... | ... | @@ -200,9 +200,9 @@ func HandleOpen(req *models.ReqCardHolderOpen) (code string, rsp models.RspCardH |
| 200 | 200 | // 存档 |
| 201 | 201 | SaveData(req.GameID, gameData) |
| 202 | 202 | |
| 203 | - //logs.Debug("wjwdbg 获取卡牌列表:", rsp.NewCards) | |
| 203 | + //logs.Debug("dwjw 获取卡牌列表:", rsp.NewCards) | |
| 204 | 204 | uttype.Shuffle(rsp.NewCards) |
| 205 | - //logs.Debug("wjwdbg 获取卡牌列表打乱:", rsp.NewCards) | |
| 205 | + //logs.Debug("dwjw 获取卡牌列表打乱:", rsp.NewCards) | |
| 206 | 206 | |
| 207 | 207 | // 返回信息 |
| 208 | 208 | rsp.CardHolderInfo = GetInfo(gameData, config) | ... | ... |
service/cardholder/logic.go
| ... | ... | @@ -14,7 +14,7 @@ func SaveData(gameId string, d *dto.CardHolderData) { |
| 14 | 14 | d.UpdateTime = lxtime.NowUninx() |
| 15 | 15 | d.Encode() |
| 16 | 16 | |
| 17 | - dto.SavePlayerData(gameId, d.Uid, d) | |
| 17 | + dto.SavePlayerData(gameId, d) | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { | ... | ... |
| ... | ... | @@ -0,0 +1,169 @@ |
| 1 | +package utildto | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "fmt" | |
| 5 | + "gorm.io/driver/mysql" | |
| 6 | + "gorm.io/gorm" | |
| 7 | + "gorm.io/gorm/clause" | |
| 8 | + "gorm.io/gorm/migrator" | |
| 9 | + "strings" | |
| 10 | +) | |
| 11 | + | |
| 12 | +const MYSQL_TABLE_TEMPLATE = "all_table_template" // 表名模板 | |
| 13 | + | |
| 14 | +func ConnectMySQL(dsn string) (db *gorm.DB, err error) { | |
| 15 | + return gorm.Open(mysql.Open(dsn), &gorm.Config{}) | |
| 16 | +} | |
| 17 | + | |
| 18 | +func InitTable(db *gorm.DB, value any, tableName string) { | |
| 19 | + m := db.Migrator().(mysql.Migrator).Migrator | |
| 20 | + queryTx, execTx := m.GetQueryAndExecTx() | |
| 21 | + if HasTable(queryTx.Migrator().(mysql.Migrator).Migrator, value, tableName) { | |
| 22 | + fmt.Println(tableName + " exist, skip!") | |
| 23 | + } else { | |
| 24 | + err := CreateTable(execTx, value, tableName) | |
| 25 | + if err != nil { | |
| 26 | + fmt.Println("err", err) | |
| 27 | + } | |
| 28 | + } | |
| 29 | +} | |
| 30 | + | |
| 31 | +func HasTable(m migrator.Migrator, value interface{}, tableName string) bool { | |
| 32 | + var count int64 | |
| 33 | + | |
| 34 | + m.RunWithValue(value, func(stmt *gorm.Statement) error { | |
| 35 | + currentDatabase := m.DB.Migrator().CurrentDatabase() | |
| 36 | + return m.DB.Raw("SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ? AND table_type = ?", currentDatabase, tableName, "BASE TABLE").Row().Scan(&count) | |
| 37 | + }) | |
| 38 | + | |
| 39 | + return count > 0 | |
| 40 | +} | |
| 41 | + | |
| 42 | +func CreateTable(execTx *gorm.DB, value any, tableName string) error { | |
| 43 | + m := execTx.Migrator().(mysql.Migrator).Migrator | |
| 44 | + tx := m.DB.Session(&gorm.Session{}) | |
| 45 | + | |
| 46 | + if err := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) { | |
| 47 | + var ( | |
| 48 | + createTableSQL = "CREATE TABLE ? (" | |
| 49 | + values = []interface{}{clause.Table{Name: tableName}} | |
| 50 | + hasPrimaryKeyInDataType bool | |
| 51 | + ) | |
| 52 | + | |
| 53 | + for _, dbName := range stmt.Schema.DBNames { | |
| 54 | + field := stmt.Schema.FieldsByDBName[dbName] | |
| 55 | + if !field.IgnoreMigration { | |
| 56 | + createTableSQL += "? ?" | |
| 57 | + hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(m.DataTypeOf(field)), "PRIMARY KEY") | |
| 58 | + values = append(values, clause.Column{Name: dbName}, m.DB.Migrator().FullDataTypeOf(field)) | |
| 59 | + createTableSQL += "," | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + if !hasPrimaryKeyInDataType && len(stmt.Schema.PrimaryFields) > 0 { | |
| 64 | + createTableSQL += "PRIMARY KEY ?," | |
| 65 | + primaryKeys := make([]interface{}, 0, len(stmt.Schema.PrimaryFields)) | |
| 66 | + for _, field := range stmt.Schema.PrimaryFields { | |
| 67 | + primaryKeys = append(primaryKeys, clause.Column{Name: field.DBName}) | |
| 68 | + } | |
| 69 | + | |
| 70 | + values = append(values, primaryKeys) | |
| 71 | + } | |
| 72 | + | |
| 73 | + for _, idx := range stmt.Schema.ParseIndexes() { | |
| 74 | + if m.CreateIndexAfterCreateTable { | |
| 75 | + defer func(value interface{}, name string) { | |
| 76 | + if err == nil { | |
| 77 | + err = tx.Migrator().CreateIndex(value, name) | |
| 78 | + } | |
| 79 | + }(value, idx.Name) | |
| 80 | + } else { | |
| 81 | + if idx.Class != "" { | |
| 82 | + createTableSQL += idx.Class + " " | |
| 83 | + } | |
| 84 | + createTableSQL += "INDEX ? ?" | |
| 85 | + | |
| 86 | + if idx.Comment != "" { | |
| 87 | + createTableSQL += fmt.Sprintf(" COMMENT '%s'", idx.Comment) | |
| 88 | + } | |
| 89 | + | |
| 90 | + if idx.Option != "" { | |
| 91 | + createTableSQL += " " + idx.Option | |
| 92 | + } | |
| 93 | + | |
| 94 | + createTableSQL += "," | |
| 95 | + values = append(values, clause.Column{Name: idx.Name}, tx.Migrator().(migrator.BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)) | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + if !m.DB.DisableForeignKeyConstraintWhenMigrating && !m.DB.IgnoreRelationshipsWhenMigrating { | |
| 100 | + for _, rel := range stmt.Schema.Relationships.Relations { | |
| 101 | + if rel.Field.IgnoreMigration { | |
| 102 | + continue | |
| 103 | + } | |
| 104 | + if constraint := rel.ParseConstraint(); constraint != nil { | |
| 105 | + if constraint.Schema == stmt.Schema { | |
| 106 | + sql, vars := constraint.Build() | |
| 107 | + createTableSQL += sql + "," | |
| 108 | + values = append(values, vars...) | |
| 109 | + } | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + for _, uni := range stmt.Schema.ParseUniqueConstraints() { | |
| 115 | + createTableSQL += "CONSTRAINT ? UNIQUE (?)," | |
| 116 | + values = append(values, clause.Column{Name: uni.Name}, clause.Expr{SQL: stmt.Quote(uni.Field.DBName)}) | |
| 117 | + } | |
| 118 | + | |
| 119 | + for _, chk := range stmt.Schema.ParseCheckConstraints() { | |
| 120 | + createTableSQL += "CONSTRAINT ? CHECK (?)," | |
| 121 | + values = append(values, clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint}) | |
| 122 | + } | |
| 123 | + | |
| 124 | + createTableSQL = strings.TrimSuffix(createTableSQL, ",") | |
| 125 | + | |
| 126 | + createTableSQL += ")" | |
| 127 | + | |
| 128 | + if tableOption, ok := m.DB.Get("gorm:table_options"); ok { | |
| 129 | + createTableSQL += fmt.Sprint(tableOption) | |
| 130 | + } | |
| 131 | + | |
| 132 | + err = tx.Exec(createTableSQL, values...).Error | |
| 133 | + return err | |
| 134 | + }); err != nil { | |
| 135 | + return err | |
| 136 | + } | |
| 137 | + return nil | |
| 138 | +} | |
| 139 | + | |
| 140 | +// Insert 插入 | |
| 141 | +func Insert(db *gorm.DB, value any, tableName string) *gorm.DB { | |
| 142 | + stmt := db.Session(&gorm.Session{DryRun: true}).Create(value).Statement | |
| 143 | + stmtSQL := stmt.SQL.String() | |
| 144 | + sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | |
| 145 | + return db.Exec(sql, stmt.Vars...) | |
| 146 | +} | |
| 147 | + | |
| 148 | +// Update 更新 | |
| 149 | +func Update(db *gorm.DB, value any, tableName string) *gorm.DB { | |
| 150 | + stmt := db.Session(&gorm.Session{DryRun: true}).Updates(value).Statement | |
| 151 | + stmtSQL := stmt.SQL.String() | |
| 152 | + sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | |
| 153 | + return db.Exec(sql, stmt.Vars...) | |
| 154 | +} | |
| 155 | + | |
| 156 | +// Save 保存 | |
| 157 | +func Save(db *gorm.DB, value any, tableName string) *gorm.DB { | |
| 158 | + stmt := db.Session(&gorm.Session{DryRun: true}).Save(value).Statement | |
| 159 | + stmtSQL := stmt.SQL.String() | |
| 160 | + sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | |
| 161 | + return db.Exec(sql, stmt.Vars...) | |
| 162 | +} | |
| 163 | + | |
| 164 | +func First(db *gorm.DB, value any, tableName string) *gorm.DB { | |
| 165 | + stmt := db.Session(&gorm.Session{DryRun: true}).First(value).Statement | |
| 166 | + stmtSQL := stmt.SQL.String() | |
| 167 | + sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | |
| 168 | + return db.Raw(sql, stmt.Vars...).Scan(value) | |
| 169 | +} | ... | ... |