diff --git a/common/svconfig/index.go b/common/svconfig/index.go new file mode 100644 index 0000000..b2ad036 --- /dev/null +++ b/common/svconfig/index.go @@ -0,0 +1,6 @@ +package svconfig + +func Init() bool { + + return true +} diff --git a/common/svconst/vars.go b/common/svconst/vars.go index 09d5280..efde083 100644 --- a/common/svconst/vars.go +++ b/common/svconst/vars.go @@ -1,5 +1,11 @@ package svconst +import "gorm.io/gorm" + var ( AppName = "" + + DbApi *gorm.DB + DbCommon *gorm.DB + DbConfig *gorm.DB ) diff --git a/common/svdto/dto.go b/common/svdto/dto.go new file mode 100644 index 0000000..8ac0c5a --- /dev/null +++ b/common/svdto/dto.go @@ -0,0 +1,64 @@ +package svdto + +import ( + "apigame/util/utdto" + "apigame/util/util-lx/lxalilog" + "gorm.io/gorm" +) + +func InitTable(db *gorm.DB, tb IDtoData, gameId string) { + tableName := tb.GetTableName(gameId) + utdto.InitTable(db, tb, tableName) +} + +func Insert(db *gorm.DB, data IDtoData, gameId string) (err error) { + result := utdto.Insert(db, data, data.GetTableName(gameId)) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Update(db *gorm.DB, data IDtoData, gameId string) (err error) { + result := utdto.Update(db, data, data.GetTableName(gameId)) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Save(db *gorm.DB, data IDtoData, gameId string) (err error) { + result := utdto.Save(db, data, data.GetTableName(gameId)) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func First(db *gorm.DB, data IDtoData, gameId string) (has bool, err error) { + result := utdto.First(db, data, data.GetTableName(gameId)) + has = result.RowsAffected != 0 + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Find(db *gorm.DB, data any, tableName string) (has bool, err error) { + result := utdto.Find(db, data, tableName) + has = result.RowsAffected != 0 + err = result.Error + if err != nil { + lxalilog.Errors(err, tableName) + return + } + return +} diff --git a/common/svdto/init.go b/common/svdto/init.go new file mode 100644 index 0000000..d2231c2 --- /dev/null +++ b/common/svdto/init.go @@ -0,0 +1,20 @@ +package svdto + +import ( + "apigame/common/svconst" + "apigame/dto" + "apigame/service/constd" +) + +func Init() { + + // create table + // 卡牌卡包 + for _, gameId := range constd.GameListCardHolder { + InitTable(svconst.DbCommon, new(dto.CardHolderData), gameId) + InitTable(svconst.DbCommon, new(dto.CardHolderRecordOpen), gameId) + InitTable(svconst.DbCommon, new(dto.CardHolderRecordRewardAlbum), gameId) + InitTable(svconst.DbCommon, new(dto.CardHolderRecordRewardRound), gameId) + } + +} diff --git a/common/svdto/interface.go b/common/svdto/interface.go new file mode 100644 index 0000000..e57ba41 --- /dev/null +++ b/common/svdto/interface.go @@ -0,0 +1,9 @@ +package svdto + +// IDtoData dto数据 +type IDtoData interface { + // TableName 表名模板 + TableName() string + // GetTableName 表名 后缀是 gameId + GetTableName(gameId string) string +} diff --git a/common/svdto/record.go b/common/svdto/record.go new file mode 100644 index 0000000..a22134e --- /dev/null +++ b/common/svdto/record.go @@ -0,0 +1,10 @@ +package svdto + +import ( + "apigame/common/svconst" + "apigame/util/utdto" +) + +func SaveRecord(gameId string, data IDtoData) { + utdto.Insert(svconst.DbCommon, data, data.GetTableName(gameId)) +} diff --git a/common/svmysql/index.go b/common/svmysql/index.go index 0c6b64e..7624fc8 100644 --- a/common/svmysql/index.go +++ b/common/svmysql/index.go @@ -1 +1,34 @@ package svmysql + +import ( + "apigame/common/svconst" + "apigame/service/constd" + "apigame/util/utdto" + "fmt" + "github.com/astaxie/beego" +) + +func Init() bool { + + // ConnectMySQL + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_DBAPI)); err == nil { + svconst.DbApi = db + } else { + fmt.Println("svmysql.Init DbApi Error::" + err.Error()) + return false + } + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::mergecommon")); err == nil { + svconst.DbCommon = db + } else { + fmt.Println("svmysql.Init DbCommon Error::" + err.Error()) + return false + } + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG)); err == nil { + svconst.DbConfig = db + } else { + fmt.Println("svmysql.Init DbConfig Error::" + err.Error()) + return false + } + return true + +} diff --git a/common/thinkingdata/index.go b/common/thinkingdata/index.go new file mode 100644 index 0000000..2b16552 --- /dev/null +++ b/common/thinkingdata/index.go @@ -0,0 +1,119 @@ +package thinkingdata + +import ( + "crypto/tls" + "encoding/json" + "errors" + "fmt" + "gitee.com/lxgow/lxconv" + "github.com/astaxie/beego" + "github.com/astaxie/beego/httplib" + lconv "github.com/lixu-any/go-tools/conv" + "time" +) + +// index +// 创建时间:2023/10/23 10:06 +// 创建人:lixu + +// index 数数科技 +// 创建时间:2023/7/18 16:03 +// 创建人:lixu + +// GetNowDateTime 当前日期时间 +func GetNowDateTime() string { + tim := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05") + return fmt.Sprintf("%s", tim) +} + +type MThinkDataConfig struct { + Url string `json:"url"` + Debug int `json:"debug"` +} + +type MRespData struct { + Msg string `json:"Msg"` + Code int `json:"code"` +} + +var THINKINGDATACONFIG MThinkDataConfig + +// InitThinkData 初始化数数 +func InitThinkData() (err error) { + thinkingdata, _ := beego.AppConfig.GetSection("thinkingdata") + THINKINGDATACONFIG.Url = thinkingdata["url"] + THINKINGDATACONFIG.Debug = lxconv.ParseInt(thinkingdata["debug"]) + return +} + +// AddOneData 数据上报 +func AddOneData(appid, account_id, distinct_id string, event string, data map[string]interface{}, typ string) (resp MRespData, err error) { + + _data := map[string]interface{}{ + "#type": typ, + //"#event_name": event, + "#time": GetNowDateTime(), + "#account_id": account_id, + "#distinct_id": distinct_id, + "properties": data, + } + + if typ == "track" && event != "" { + _data["#event_name"] = event + } + + postdata := map[string]interface{}{ + "appid": appid, + "debug": THINKINGDATACONFIG.Debug, + "data": _data, + } + + req := httplib.Post(THINKINGDATACONFIG.Url + "/sync_json") + + req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) + + _, err = req.JSONBody(&postdata) + if err != nil { + err = errors.New("thinkingdata-AddOneData req.JSONBody() error::" + err.Error() + "::" + lconv.JsonEncode(postdata)) + return + } + + var ( + bytes []byte + ) + + bytes, err = req.Bytes() + if err != nil { + err = errors.New("thinkingdata-AddOneData req.Bytes() error::" + err.Error() + "::" + lconv.JsonEncode(postdata) + "::" + string(bytes)) + return + } + + _ = json.Unmarshal(bytes, &resp) + + if resp.Code != 0 { + err = errors.New("thinkingdata-AddOneData resp.Code error:: " + lconv.JsonEncode(postdata) + "::" + string(bytes)) + return + } + + return +} + +// TrackOne 向事件表传入一个事件 +func TrackOne(appid, account_id string, event string, data map[string]interface{}) (resp MRespData, err error) { + return AddOneData(appid, account_id, "", event, data, "track") +} + +// UserSet 对用户表进行操作,覆盖一个或多个用户属性,如果该属性已有值存在,覆盖先前值 +func UserSet(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) { + return AddOneData(appid, account_id, "", "", data, "user_set") +} + +// UserSetOnce 对用户表进行操作,初始化一个或多个用户属性,如果该属性已有值存在,则忽略本次操作 +func UserSetOnce(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) { + return AddOneData(appid, account_id, "", "", data, "user_setOnce") +} + +// UserAdd 对用户表进行操作,为一个或多个数值型用户属性做累加计算 +func UserAdd(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) { + return AddOneData(appid, account_id, "", "", data, "user_add") +} diff --git a/dto/dto.go b/dto/dto.go deleted file mode 100644 index 16a92b5..0000000 --- a/dto/dto.go +++ /dev/null @@ -1,64 +0,0 @@ -package dto - -import ( - "apigame/util/utdto" - "apigame/util/util-lx/lxalilog" - "gorm.io/gorm" -) - -func InitTable(db *gorm.DB, tb IDtoData, gameId string) { - tableName := tb.GetTableName(gameId) - utdto.InitTable(db, tb, tableName) -} - -func Insert(db *gorm.DB, data IDtoData, gameId string) (err error) { - result := utdto.Insert(db, data, data.GetTableName(gameId)) - err = result.Error - if err != nil { - lxalilog.Errors(err, gameId) - return - } - return -} - -func Update(db *gorm.DB, data IDtoData, gameId string) (err error) { - result := utdto.Update(db, data, data.GetTableName(gameId)) - err = result.Error - if err != nil { - lxalilog.Errors(err, gameId) - return - } - return -} - -func Save(db *gorm.DB, data IDtoData, gameId string) (err error) { - result := utdto.Save(db, data, data.GetTableName(gameId)) - err = result.Error - if err != nil { - lxalilog.Errors(err, gameId) - return - } - return -} - -func First(db *gorm.DB, data IDtoData, gameId string) (has bool, err error) { - result := utdto.First(db, data, data.GetTableName(gameId)) - has = result.RowsAffected != 0 - err = result.Error - if err != nil { - lxalilog.Errors(err, gameId) - return - } - return -} - -func Find(db *gorm.DB, data any, tableName string) (has bool, err error) { - result := utdto.Find(db, data, tableName) - has = result.RowsAffected != 0 - err = result.Error - if err != nil { - lxalilog.Errors(err, tableName) - return - } - return -} diff --git a/dto/init-dto.go b/dto/init-dto.go deleted file mode 100644 index 33cfe80..0000000 --- a/dto/init-dto.go +++ /dev/null @@ -1,39 +0,0 @@ -package dto - -import ( - "apigame/service/constd" - "apigame/util/utdto" - "fmt" - "github.com/astaxie/beego" - "gorm.io/gorm" -) - -var ( - DbCommon *gorm.DB - DbConfig *gorm.DB -) - -func InitDto() { - - // ConnectMySQL - if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::mergecommon")); err == nil { - DbCommon = db - } else { - fmt.Println(" InitMysqls Error::" + err.Error()) - } - if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG)); err == nil { - DbConfig = db - } else { - - } - - // create table - // 卡牌卡包 - for _, gameId := range constd.GameListCardHolder { - InitTable(DbCommon, new(CardHolderData), gameId) - InitTable(DbCommon, new(CardHolderRecordOpen), gameId) - InitTable(DbCommon, new(CardHolderRecordRewardAlbum), gameId) - InitTable(DbCommon, new(CardHolderRecordRewardRound), gameId) - } - -} diff --git a/dto/inits.go b/dto/inits.go deleted file mode 100644 index d9e3740..0000000 --- a/dto/inits.go +++ /dev/null @@ -1,46 +0,0 @@ -package dto - -import ( - "apigame/service/constd" - "apigame/util/util-lx/lxmysql" - "fmt" - "github.com/astaxie/beego" -) - -func Inits() (err error) { - - appname := beego.AppConfig.String("appname") - - //初始化MYSQL - var conns []lxmysql.MapMysqlConn - - //默认数据库 - conns = append(conns, lxmysql.MapMysqlConn{ - Name: constd.MYSQL_DEFAULT, - SqlCon: beego.AppConfig.String("mysql::mergecommon"), - }) - - conns = append(conns, lxmysql.MapMysqlConn{ - Name: constd.MYSQL_MERGECONFIG, - SqlCon: beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG), - }) - - err = lxmysql.InitMysqls(conns, beego.AppConfig.String("redis::cachedb")) - if err != nil { - fmt.Println(" InitMysqls Error::" + err.Error()) - } - - for _, conn := range conns { - fmt.Println(appname + " dbs::" + conn.Name + " init success") - } - - //// 初始化数数打点 - //_ = thinkingdata.InitThinkData() - // - //_ = config.InitLxLimit() - - // dto - InitDto() - - return -} diff --git a/dto/interface.go b/dto/interface.go deleted file mode 100644 index e270302..0000000 --- a/dto/interface.go +++ /dev/null @@ -1,9 +0,0 @@ -package dto - -// IDtoData dto数据 -type IDtoData interface { - // TableName 表名模板 - TableName() string - // GetTableName 表名 后缀是 gameId - GetTableName(gameId string) string -} diff --git a/dto/record.go b/dto/record.go deleted file mode 100644 index 5657970..0000000 --- a/dto/record.go +++ /dev/null @@ -1,7 +0,0 @@ -package dto - -import "apigame/util/utdto" - -func SaveRecord(gameId string, data IDtoData) { - utdto.Insert(DbCommon, data, data.GetTableName(gameId)) -} diff --git a/main.go b/main.go index fb81a6c..02089e9 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,10 @@ package main import ( "apigame/common/svconst" + "apigame/common/svdto" "apigame/common/svlog" + "apigame/common/svmysql" "apigame/common/svredis" - "apigame/dto" _ "apigame/routers" "apigame/service/cardholder" "github.com/astaxie/beego" @@ -47,7 +48,16 @@ func Init() { return } - _ = dto.Inits() + if !svmysql.Init() { + return + } + + //// 初始化数数打点 + //_ = thinkingdata.InitThinkData() + // + //_ = config.InitLxLimit() + + svdto.Init() // 初始化配置 cardholder.Init() diff --git a/service/cardholder/config-load.go b/service/cardholder/config-load.go index ba9c258..c5d0ca8 100644 --- a/service/cardholder/config-load.go +++ b/service/cardholder/config-load.go @@ -1,7 +1,8 @@ package cardholder import ( - "apigame/dto" + "apigame/common/svconst" + "apigame/common/svdto" "apigame/service/constd" "fmt" ) @@ -38,7 +39,7 @@ func LoadConfig(gameId string) { configOpen := CardActivityUpdateConfig{Id: 0} { conf := make([]CardActivityUpdateConfig, 0) - _, err := dto.Find(dto.DbConfig, &conf, new(CardActivityUpdateConfig).GetTableName(gameId)) + _, err := svdto.Find(svconst.DbConfig, &conf, new(CardActivityUpdateConfig).GetTableName(gameId)) if err != nil { return } @@ -67,7 +68,7 @@ func LoadConfig(gameId string) { // 更新数据 if needUpdate { confNew := &CardActivityConfigRaw{} - hasConfNew, err := dto.First(dto.DbConfig, confNew, gameId) + hasConfNew, err := svdto.First(svconst.DbConfig, confNew, gameId) if err != nil { return } diff --git a/service/cardholder/logic.go b/service/cardholder/logic.go index c5fc141..64fc297 100644 --- a/service/cardholder/logic.go +++ b/service/cardholder/logic.go @@ -1,6 +1,8 @@ package cardholder import ( + "apigame/common/svconst" + "apigame/common/svdto" "apigame/dto" "apigame/models" "apigame/service/constd" @@ -16,12 +18,12 @@ func SaveData(gameId string, d *dto.CardHolderData) { d.UpdateTime = lxtime.NowUninx() d.Encode() - dto.Save(dto.DbCommon, d, gameId) + svdto.Save(svconst.DbCommon, d, gameId) } func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { d = dto.NewCardHolderData(uid) - has, err := dto.First(dto.DbCommon, d, gameId) + has, err := svdto.First(svconst.DbCommon, d, gameId) if err != nil { return } @@ -29,7 +31,7 @@ func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) { d.Decode() } else { d.Init(uid) - dto.Insert(dto.DbCommon, d, gameId) + svdto.Insert(svconst.DbCommon, d, gameId) } return } @@ -228,7 +230,7 @@ func DoOpen(gameId string, // 记录开卡包日志 { recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round) - dto.SaveRecord(gameId, dto.NewCardHolderRecordOpen(recordBase, + svdto.SaveRecord(gameId, dto.NewCardHolderRecordOpen(recordBase, openMode, utstring.StringToInt(confCardholder.Id), zjson.Str(newCards))) } @@ -276,7 +278,7 @@ func DoOpenCheckAward(gameId string, { // 记录日志 recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round) - dto.SaveRecord(gameId, dto.NewCardHolderRecordRewardAlbum(recordBase, + svdto.SaveRecord(gameId, dto.NewCardHolderRecordRewardAlbum(recordBase, albumId, award)) } } @@ -287,7 +289,7 @@ func DoOpenCheckAward(gameId string, // 记录日志 recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round) - dto.SaveRecord(gameId, dto.NewCardHolderRecordRewardRound(recordBase, + svdto.SaveRecord(gameId, dto.NewCardHolderRecordRewardRound(recordBase, awardRound)) // 处理轮次完成 diff --git a/service/constd/mysql.go b/service/constd/mysql.go index 5e8b02a..773671c 100644 --- a/service/constd/mysql.go +++ b/service/constd/mysql.go @@ -6,6 +6,7 @@ package constd const ( MYSQL_DEFAULT = "default" + MYSQL_DBAPI = "api" MYSQL_MERGECOMMON = "merge_common" MYSQL_MERGECONFIG = "merge_config" MYSQL_DEFAULT_QUERY_MAXCOUNT = 100 -- libgit2 0.21.0