Commit b3e4194752ec894e873a97825723ee5232b90fe9
1 parent
d65f881b
Exists in
master
and in
1 other branch
refactor: orm重构
Showing
9 changed files
with
50 additions
and
31 deletions
Show diff stats
dto/dto.go
| @@ -6,12 +6,12 @@ import ( | @@ -6,12 +6,12 @@ import ( | ||
| 6 | "gorm.io/gorm" | 6 | "gorm.io/gorm" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | -func InitTable(db *gorm.DB, gameId string, tb IDtoData) { | 9 | +func InitTable(db *gorm.DB, tb IDtoData, gameId string) { |
| 10 | tableName := tb.GetTableName(gameId) | 10 | tableName := tb.GetTableName(gameId) |
| 11 | utdto.InitTable(db, tb, tableName) | 11 | utdto.InitTable(db, tb, tableName) |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | -func Insert(db *gorm.DB, gameId string, data IDtoData) (err error) { | 14 | +func Insert(db *gorm.DB, data IDtoData, gameId string) (err error) { |
| 15 | result := utdto.Insert(db, data, data.GetTableName(gameId)) | 15 | result := utdto.Insert(db, data, data.GetTableName(gameId)) |
| 16 | err = result.Error | 16 | err = result.Error |
| 17 | if err != nil { | 17 | if err != nil { |
| @@ -21,7 +21,7 @@ func Insert(db *gorm.DB, gameId string, data IDtoData) (err error) { | @@ -21,7 +21,7 @@ func Insert(db *gorm.DB, gameId string, data IDtoData) (err error) { | ||
| 21 | return | 21 | return |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | -func Update(db *gorm.DB, gameId string, data IDtoData) (err error) { | 24 | +func Update(db *gorm.DB, data IDtoData, gameId string) (err error) { |
| 25 | result := utdto.Update(db, data, data.GetTableName(gameId)) | 25 | result := utdto.Update(db, data, data.GetTableName(gameId)) |
| 26 | err = result.Error | 26 | err = result.Error |
| 27 | if err != nil { | 27 | if err != nil { |
| @@ -31,7 +31,7 @@ func Update(db *gorm.DB, gameId string, data IDtoData) (err error) { | @@ -31,7 +31,7 @@ func Update(db *gorm.DB, gameId string, data IDtoData) (err error) { | ||
| 31 | return | 31 | return |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | -func Save(db *gorm.DB, gameId string, data IDtoData) (err error) { | 34 | +func Save(db *gorm.DB, data IDtoData, gameId string) (err error) { |
| 35 | result := utdto.Save(db, data, data.GetTableName(gameId)) | 35 | result := utdto.Save(db, data, data.GetTableName(gameId)) |
| 36 | err = result.Error | 36 | err = result.Error |
| 37 | if err != nil { | 37 | if err != nil { |
| @@ -41,7 +41,7 @@ func Save(db *gorm.DB, gameId string, data IDtoData) (err error) { | @@ -41,7 +41,7 @@ func Save(db *gorm.DB, gameId string, data IDtoData) (err error) { | ||
| 41 | return | 41 | return |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | -func First(db *gorm.DB, gameId string, data IDtoData) (has bool, err error) { | 44 | +func First(db *gorm.DB, data IDtoData, gameId string) (has bool, err error) { |
| 45 | result := utdto.First(db, data, data.GetTableName(gameId)) | 45 | result := utdto.First(db, data, data.GetTableName(gameId)) |
| 46 | has = result.RowsAffected != 0 | 46 | has = result.RowsAffected != 0 |
| 47 | err = result.Error | 47 | err = result.Error |
| @@ -51,3 +51,14 @@ func First(db *gorm.DB, gameId string, data IDtoData) (has bool, err error) { | @@ -51,3 +51,14 @@ func First(db *gorm.DB, gameId string, data IDtoData) (has bool, err error) { | ||
| 51 | } | 51 | } |
| 52 | return | 52 | return |
| 53 | } | 53 | } |
| 54 | + | ||
| 55 | +func Find(db *gorm.DB, data any, tableName string) (has bool, err error) { | ||
| 56 | + result := utdto.Find(db, data, tableName) | ||
| 57 | + has = result.RowsAffected != 0 | ||
| 58 | + err = result.Error | ||
| 59 | + if err != nil { | ||
| 60 | + lxalilog.Errors(err, tableName) | ||
| 61 | + return | ||
| 62 | + } | ||
| 63 | + return | ||
| 64 | +} |
dto/init-dto.go
| @@ -30,10 +30,10 @@ func InitDto() { | @@ -30,10 +30,10 @@ func InitDto() { | ||
| 30 | // create table | 30 | // create table |
| 31 | // 卡牌卡包 | 31 | // 卡牌卡包 |
| 32 | for _, gameId := range constd.GameListCardHolder { | 32 | for _, gameId := range constd.GameListCardHolder { |
| 33 | - InitTable(DbCommon, gameId, new(CardHolderData)) | ||
| 34 | - InitTable(DbCommon, gameId, new(CardHolderRecordOpen)) | ||
| 35 | - InitTable(DbCommon, gameId, new(CardHolderRecordRewardAlbum)) | ||
| 36 | - InitTable(DbCommon, gameId, new(CardHolderRecordRewardRound)) | 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 | } | 37 | } |
| 38 | 38 | ||
| 39 | } | 39 | } |
dto/inits.go
| @@ -63,7 +63,7 @@ func Inits() (err error) { | @@ -63,7 +63,7 @@ func Inits() (err error) { | ||
| 63 | fmt.Println(appname + " dbs::" + conn.Name + " init success") | 63 | fmt.Println(appname + " dbs::" + conn.Name + " init success") |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | - //initAlilog(appname) | 66 | + initAlilog(appname) |
| 67 | 67 | ||
| 68 | //// 初始化数数打点 | 68 | //// 初始化数数打点 |
| 69 | //_ = thinkingdata.InitThinkData() | 69 | //_ = thinkingdata.InitThinkData() |
service/cardholder/config-load.go
| 1 | package cardholder | 1 | package cardholder |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "apigame/dto" | ||
| 4 | "apigame/service/constd" | 5 | "apigame/service/constd" |
| 5 | - "apigame/util/util-lx/lxalilog" | ||
| 6 | - "apigame/util/utmysql" | ||
| 7 | "fmt" | 6 | "fmt" |
| 8 | - "github.com/astaxie/beego/logs" | ||
| 9 | ) | 7 | ) |
| 10 | 8 | ||
| 11 | // Init 初始化 | 9 | // Init 初始化 |
| @@ -36,15 +34,12 @@ func TryUpdateConfig(gameId string) (config *CardActivityConfig, has bool) { | @@ -36,15 +34,12 @@ func TryUpdateConfig(gameId string) (config *CardActivityConfig, has bool) { | ||
| 36 | 34 | ||
| 37 | // LoadConfig 读取mysql配置 | 35 | // LoadConfig 读取mysql配置 |
| 38 | func LoadConfig(gameId string) { | 36 | func LoadConfig(gameId string) { |
| 39 | - configTableName := constd.MYSQL_TABLE_S_CARDHOLDER_CONFIG + gameId | ||
| 40 | // 找到当前开放的活动 | 37 | // 找到当前开放的活动 |
| 41 | configOpen := CardActivityUpdateConfig{Id: 0} | 38 | configOpen := CardActivityUpdateConfig{Id: 0} |
| 42 | { | 39 | { |
| 43 | conf := make([]CardActivityUpdateConfig, 0) | 40 | conf := make([]CardActivityUpdateConfig, 0) |
| 44 | - sql := fmt.Sprintf("select id,status,update_time from %s", configTableName) | ||
| 45 | - err := utmysql.FindSql(constd.MYSQL_MERGECONFIG, sql, &conf) | 41 | + _, err := dto.Find(dto.DbConfig, &conf, new(CardActivityUpdateConfig).GetTableName(gameId)) |
| 46 | if err != nil { | 42 | if err != nil { |
| 47 | - lxalilog.Errors(err, sql, gameId) | ||
| 48 | return | 43 | return |
| 49 | } | 44 | } |
| 50 | for _, config := range conf { | 45 | for _, config := range conf { |
| @@ -61,8 +56,8 @@ func LoadConfig(gameId string) { | @@ -61,8 +56,8 @@ func LoadConfig(gameId string) { | ||
| 61 | } | 56 | } |
| 62 | // 判断是否需要更新 | 57 | // 判断是否需要更新 |
| 63 | needUpdate := false | 58 | needUpdate := false |
| 64 | - configOld, has := GetConfig(gameId) | ||
| 65 | - if has { | 59 | + configOld, hasConfigOld := GetConfig(gameId) |
| 60 | + if hasConfigOld { | ||
| 66 | if configOpen.Id != configOld.Raw.Id || configOpen.UpdateTime != configOld.Raw.UpdateTime { | 61 | if configOpen.Id != configOld.Raw.Id || configOpen.UpdateTime != configOld.Raw.UpdateTime { |
| 67 | needUpdate = true | 62 | needUpdate = true |
| 68 | } | 63 | } |
| @@ -71,15 +66,14 @@ func LoadConfig(gameId string) { | @@ -71,15 +66,14 @@ func LoadConfig(gameId string) { | ||
| 71 | } | 66 | } |
| 72 | // 更新数据 | 67 | // 更新数据 |
| 73 | if needUpdate { | 68 | if needUpdate { |
| 74 | - logs.Debug("__________________尝试更新活动条目ID:", configOpen.Id) | ||
| 75 | - confNew := CardActivityConfigRaw{} | ||
| 76 | - sql := fmt.Sprintf("select * from %s where id=%d limit 1", configTableName, configOpen.Id) | ||
| 77 | - _, err := utmysql.FindOneSql(constd.MYSQL_MERGECONFIG, sql, &confNew) | 69 | + confNew := &CardActivityConfigRaw{} |
| 70 | + hasConfNew, err := dto.First(dto.DbConfig, confNew, gameId) | ||
| 78 | if err != nil { | 71 | if err != nil { |
| 79 | - lxalilog.Errors(err, sql, gameId, configOpen.Id) | ||
| 80 | return | 72 | return |
| 81 | } | 73 | } |
| 82 | - logs.Debug("__________________更新活动条目ID:", confNew.Id) | ||
| 83 | - ConfigDecode(gameId, &confNew) | 74 | + if !hasConfNew { |
| 75 | + return | ||
| 76 | + } | ||
| 77 | + ConfigDecode(gameId, confNew) | ||
| 84 | } | 78 | } |
| 85 | } | 79 | } |
service/cardholder/config.go
| @@ -53,6 +53,14 @@ type CardActivityConfigRaw struct { | @@ -53,6 +53,14 @@ type CardActivityConfigRaw struct { | ||
| 53 | UpdateTime int64 // 修改时间戳 | 53 | UpdateTime int64 // 修改时间戳 |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | +func (d *CardActivityConfigRaw) TableName() string { | ||
| 57 | + return utdto.MYSQL_TABLE_TEMPLATE | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +func (d *CardActivityConfigRaw) GetTableName(gameId string) string { | ||
| 61 | + return constd.MYSQL_TABLE_S_CARDHOLDER_CONFIG + gameId | ||
| 62 | +} | ||
| 63 | + | ||
| 56 | // AlbumConfig 卡组表 | 64 | // AlbumConfig 卡组表 |
| 57 | type AlbumConfig struct { | 65 | type AlbumConfig struct { |
| 58 | SetId int `json:"set_id"` // 卡组名 | 66 | SetId int `json:"set_id"` // 卡组名 |
service/cardholder/handle.go
| @@ -22,6 +22,7 @@ func HandleGetConfig(req *models.ReqCardHolderGetConfig) (code string, rsp model | @@ -22,6 +22,7 @@ func HandleGetConfig(req *models.ReqCardHolderGetConfig) (code string, rsp model | ||
| 22 | rsp.PrepareTime = config.Raw.PreviewTime | 22 | rsp.PrepareTime = config.Raw.PreviewTime |
| 23 | rsp.StartTime = config.Raw.StartTime | 23 | rsp.StartTime = config.Raw.StartTime |
| 24 | rsp.EndTime = config.Raw.EndTime | 24 | rsp.EndTime = config.Raw.EndTime |
| 25 | + rsp.OpenLevel = config.Raw.OpenLevel | ||
| 25 | 26 | ||
| 26 | rsp.Config = config.Client | 27 | rsp.Config = config.Client |
| 27 | 28 |
service/cardholder/logic.go
| @@ -13,12 +13,12 @@ func SaveData(gameId string, d *dto.CardHolderData) { | @@ -13,12 +13,12 @@ func SaveData(gameId string, d *dto.CardHolderData) { | ||
| 13 | d.UpdateTime = lxtime.NowUninx() | 13 | d.UpdateTime = lxtime.NowUninx() |
| 14 | d.Encode() | 14 | d.Encode() |
| 15 | 15 | ||
| 16 | - dto.Save(dto.DbCommon, gameId, d) | 16 | + dto.Save(dto.DbCommon, d, gameId) |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { | 19 | func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { |
| 20 | d = dto.NewCardHolderData(uid) | 20 | d = dto.NewCardHolderData(uid) |
| 21 | - has, err := dto.First(dto.DbCommon, gameId, d) | 21 | + has, err := dto.First(dto.DbCommon, d, gameId) |
| 22 | if err != nil { | 22 | if err != nil { |
| 23 | return | 23 | return |
| 24 | } | 24 | } |
| @@ -26,7 +26,7 @@ func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { | @@ -26,7 +26,7 @@ func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { | ||
| 26 | d.Decode() | 26 | d.Decode() |
| 27 | } else { | 27 | } else { |
| 28 | d.Init(uid) | 28 | d.Init(uid) |
| 29 | - dto.Insert(dto.DbCommon, gameId, d) | 29 | + dto.Insert(dto.DbCommon, d, gameId) |
| 30 | } | 30 | } |
| 31 | return | 31 | return |
| 32 | } | 32 | } |
util/utdto/action.go
util/utdto/base.go
| @@ -35,3 +35,10 @@ func First(db *gorm.DB, value any, tableName string) *gorm.DB { | @@ -35,3 +35,10 @@ func First(db *gorm.DB, value any, tableName string) *gorm.DB { | ||
| 35 | sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | 35 | sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) |
| 36 | return db.Raw(sql, stmt.Vars...).Scan(value) | 36 | return db.Raw(sql, stmt.Vars...).Scan(value) |
| 37 | } | 37 | } |
| 38 | + | ||
| 39 | +func Find(db *gorm.DB, value any, tableName string) *gorm.DB { | ||
| 40 | + stmt := db.Session(&gorm.Session{DryRun: true}).Find(value).Statement | ||
| 41 | + stmtSQL := stmt.SQL.String() | ||
| 42 | + sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | ||
| 43 | + return db.Raw(sql, stmt.Vars...).Scan(value) | ||
| 44 | +} |