Commit 2e7e26fd6d6accdc996b894dad7014928fe6f527

Authored by 王家文
1 parent 658bcbb7
Exists in master and in 1 other branch dev-wjw

refactor♻️:项目目录重构

common/svconfig/index.go 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +package svconfig
  2 +
  3 +func Init() bool {
  4 +
  5 + return true
  6 +}
... ...
common/svconst/vars.go
1 1 package svconst
2 2  
  3 +import "gorm.io/gorm"
  4 +
3 5 var (
4 6 AppName = ""
  7 +
  8 + DbApi *gorm.DB
  9 + DbCommon *gorm.DB
  10 + DbConfig *gorm.DB
5 11 )
... ...
common/svdto/dto.go 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +package svdto
  2 +
  3 +import (
  4 + "apigame/util/utdto"
  5 + "apigame/util/util-lx/lxalilog"
  6 + "gorm.io/gorm"
  7 +)
  8 +
  9 +func InitTable(db *gorm.DB, tb IDtoData, gameId string) {
  10 + tableName := tb.GetTableName(gameId)
  11 + utdto.InitTable(db, tb, tableName)
  12 +}
  13 +
  14 +func Insert(db *gorm.DB, data IDtoData, gameId string) (err error) {
  15 + result := utdto.Insert(db, data, data.GetTableName(gameId))
  16 + err = result.Error
  17 + if err != nil {
  18 + lxalilog.Errors(err, gameId)
  19 + return
  20 + }
  21 + return
  22 +}
  23 +
  24 +func Update(db *gorm.DB, data IDtoData, gameId string) (err error) {
  25 + result := utdto.Update(db, data, data.GetTableName(gameId))
  26 + err = result.Error
  27 + if err != nil {
  28 + lxalilog.Errors(err, gameId)
  29 + return
  30 + }
  31 + return
  32 +}
  33 +
  34 +func Save(db *gorm.DB, data IDtoData, gameId string) (err error) {
  35 + result := utdto.Save(db, data, data.GetTableName(gameId))
  36 + err = result.Error
  37 + if err != nil {
  38 + lxalilog.Errors(err, gameId)
  39 + return
  40 + }
  41 + return
  42 +}
  43 +
  44 +func First(db *gorm.DB, data IDtoData, gameId string) (has bool, err error) {
  45 + result := utdto.First(db, data, data.GetTableName(gameId))
  46 + has = result.RowsAffected != 0
  47 + err = result.Error
  48 + if err != nil {
  49 + lxalilog.Errors(err, gameId)
  50 + return
  51 + }
  52 + return
  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 +}
... ...
common/svdto/init.go 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +package svdto
  2 +
  3 +import (
  4 + "apigame/common/svconst"
  5 + "apigame/dto"
  6 + "apigame/service/constd"
  7 +)
  8 +
  9 +func Init() {
  10 +
  11 + // create table
  12 + // 卡牌卡包
  13 + for _, gameId := range constd.GameListCardHolder {
  14 + InitTable(svconst.DbCommon, new(dto.CardHolderData), gameId)
  15 + InitTable(svconst.DbCommon, new(dto.CardHolderRecordOpen), gameId)
  16 + InitTable(svconst.DbCommon, new(dto.CardHolderRecordRewardAlbum), gameId)
  17 + InitTable(svconst.DbCommon, new(dto.CardHolderRecordRewardRound), gameId)
  18 + }
  19 +
  20 +}
... ...
common/svdto/interface.go 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +package svdto
  2 +
  3 +// IDtoData dto数据
  4 +type IDtoData interface {
  5 + // TableName 表名模板
  6 + TableName() string
  7 + // GetTableName 表名 后缀是 gameId
  8 + GetTableName(gameId string) string
  9 +}
... ...
common/svdto/record.go 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +package svdto
  2 +
  3 +import (
  4 + "apigame/common/svconst"
  5 + "apigame/util/utdto"
  6 +)
  7 +
  8 +func SaveRecord(gameId string, data IDtoData) {
  9 + utdto.Insert(svconst.DbCommon, data, data.GetTableName(gameId))
  10 +}
... ...
common/svmysql/index.go
1 1 package svmysql
  2 +
  3 +import (
  4 + "apigame/common/svconst"
  5 + "apigame/service/constd"
  6 + "apigame/util/utdto"
  7 + "fmt"
  8 + "github.com/astaxie/beego"
  9 +)
  10 +
  11 +func Init() bool {
  12 +
  13 + // ConnectMySQL
  14 + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_DBAPI)); err == nil {
  15 + svconst.DbApi = db
  16 + } else {
  17 + fmt.Println("svmysql.Init DbApi Error::" + err.Error())
  18 + return false
  19 + }
  20 + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::mergecommon")); err == nil {
  21 + svconst.DbCommon = db
  22 + } else {
  23 + fmt.Println("svmysql.Init DbCommon Error::" + err.Error())
  24 + return false
  25 + }
  26 + if db, err := utdto.ConnectMySQL(beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG)); err == nil {
  27 + svconst.DbConfig = db
  28 + } else {
  29 + fmt.Println("svmysql.Init DbConfig Error::" + err.Error())
  30 + return false
  31 + }
  32 + return true
  33 +
  34 +}
... ...
common/thinkingdata/index.go 0 → 100644
... ... @@ -0,0 +1,119 @@
  1 +package thinkingdata
  2 +
  3 +import (
  4 + "crypto/tls"
  5 + "encoding/json"
  6 + "errors"
  7 + "fmt"
  8 + "gitee.com/lxgow/lxconv"
  9 + "github.com/astaxie/beego"
  10 + "github.com/astaxie/beego/httplib"
  11 + lconv "github.com/lixu-any/go-tools/conv"
  12 + "time"
  13 +)
  14 +
  15 +// index
  16 +// 创建时间:2023/10/23 10:06
  17 +// 创建人:lixu
  18 +
  19 +// index 数数科技
  20 +// 创建时间:2023/7/18 16:03
  21 +// 创建人:lixu
  22 +
  23 +// GetNowDateTime 当前日期时间
  24 +func GetNowDateTime() string {
  25 + tim := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
  26 + return fmt.Sprintf("%s", tim)
  27 +}
  28 +
  29 +type MThinkDataConfig struct {
  30 + Url string `json:"url"`
  31 + Debug int `json:"debug"`
  32 +}
  33 +
  34 +type MRespData struct {
  35 + Msg string `json:"Msg"`
  36 + Code int `json:"code"`
  37 +}
  38 +
  39 +var THINKINGDATACONFIG MThinkDataConfig
  40 +
  41 +// InitThinkData 初始化数数
  42 +func InitThinkData() (err error) {
  43 + thinkingdata, _ := beego.AppConfig.GetSection("thinkingdata")
  44 + THINKINGDATACONFIG.Url = thinkingdata["url"]
  45 + THINKINGDATACONFIG.Debug = lxconv.ParseInt(thinkingdata["debug"])
  46 + return
  47 +}
  48 +
  49 +// AddOneData 数据上报
  50 +func AddOneData(appid, account_id, distinct_id string, event string, data map[string]interface{}, typ string) (resp MRespData, err error) {
  51 +
  52 + _data := map[string]interface{}{
  53 + "#type": typ,
  54 + //"#event_name": event,
  55 + "#time": GetNowDateTime(),
  56 + "#account_id": account_id,
  57 + "#distinct_id": distinct_id,
  58 + "properties": data,
  59 + }
  60 +
  61 + if typ == "track" && event != "" {
  62 + _data["#event_name"] = event
  63 + }
  64 +
  65 + postdata := map[string]interface{}{
  66 + "appid": appid,
  67 + "debug": THINKINGDATACONFIG.Debug,
  68 + "data": _data,
  69 + }
  70 +
  71 + req := httplib.Post(THINKINGDATACONFIG.Url + "/sync_json")
  72 +
  73 + req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  74 +
  75 + _, err = req.JSONBody(&postdata)
  76 + if err != nil {
  77 + err = errors.New("thinkingdata-AddOneData req.JSONBody() error::" + err.Error() + "::" + lconv.JsonEncode(postdata))
  78 + return
  79 + }
  80 +
  81 + var (
  82 + bytes []byte
  83 + )
  84 +
  85 + bytes, err = req.Bytes()
  86 + if err != nil {
  87 + err = errors.New("thinkingdata-AddOneData req.Bytes() error::" + err.Error() + "::" + lconv.JsonEncode(postdata) + "::" + string(bytes))
  88 + return
  89 + }
  90 +
  91 + _ = json.Unmarshal(bytes, &resp)
  92 +
  93 + if resp.Code != 0 {
  94 + err = errors.New("thinkingdata-AddOneData resp.Code error:: " + lconv.JsonEncode(postdata) + "::" + string(bytes))
  95 + return
  96 + }
  97 +
  98 + return
  99 +}
  100 +
  101 +// TrackOne 向事件表传入一个事件
  102 +func TrackOne(appid, account_id string, event string, data map[string]interface{}) (resp MRespData, err error) {
  103 + return AddOneData(appid, account_id, "", event, data, "track")
  104 +}
  105 +
  106 +// UserSet 对用户表进行操作,覆盖一个或多个用户属性,如果该属性已有值存在,覆盖先前值
  107 +func UserSet(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) {
  108 + return AddOneData(appid, account_id, "", "", data, "user_set")
  109 +}
  110 +
  111 +// UserSetOnce 对用户表进行操作,初始化一个或多个用户属性,如果该属性已有值存在,则忽略本次操作
  112 +func UserSetOnce(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) {
  113 + return AddOneData(appid, account_id, "", "", data, "user_setOnce")
  114 +}
  115 +
  116 +// UserAdd 对用户表进行操作,为一个或多个数值型用户属性做累加计算
  117 +func UserAdd(appid, account_id string, data map[string]interface{}) (resp MRespData, err error) {
  118 + return AddOneData(appid, account_id, "", "", data, "user_add")
  119 +}
... ...
dto/dto.go
... ... @@ -1,64 +0,0 @@
1   -package dto
2   -
3   -import (
4   - "apigame/util/utdto"
5   - "apigame/util/util-lx/lxalilog"
6   - "gorm.io/gorm"
7   -)
8   -
9   -func InitTable(db *gorm.DB, tb IDtoData, gameId string) {
10   - tableName := tb.GetTableName(gameId)
11   - utdto.InitTable(db, tb, tableName)
12   -}
13   -
14   -func Insert(db *gorm.DB, data IDtoData, gameId string) (err error) {
15   - result := utdto.Insert(db, data, data.GetTableName(gameId))
16   - err = result.Error
17   - if err != nil {
18   - lxalilog.Errors(err, gameId)
19   - return
20   - }
21   - return
22   -}
23   -
24   -func Update(db *gorm.DB, data IDtoData, gameId string) (err error) {
25   - result := utdto.Update(db, data, data.GetTableName(gameId))
26   - err = result.Error
27   - if err != nil {
28   - lxalilog.Errors(err, gameId)
29   - return
30   - }
31   - return
32   -}
33   -
34   -func Save(db *gorm.DB, data IDtoData, gameId string) (err error) {
35   - result := utdto.Save(db, data, data.GetTableName(gameId))
36   - err = result.Error
37   - if err != nil {
38   - lxalilog.Errors(err, gameId)
39   - return
40   - }
41   - return
42   -}
43   -
44   -func First(db *gorm.DB, data IDtoData, gameId string) (has bool, err error) {
45   - result := utdto.First(db, data, data.GetTableName(gameId))
46   - has = result.RowsAffected != 0
47   - err = result.Error
48   - if err != nil {
49   - lxalilog.Errors(err, gameId)
50   - return
51   - }
52   - return
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
... ... @@ -1,39 +0,0 @@
1   -package dto
2   -
3   -import (
4   - "apigame/service/constd"
5   - "apigame/util/utdto"
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 := utdto.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 := utdto.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
... ... @@ -1,46 +0,0 @@
1   -package dto
2   -
3   -import (
4   - "apigame/service/constd"
5   - "apigame/util/util-lx/lxmysql"
6   - "fmt"
7   - "github.com/astaxie/beego"
8   -)
9   -
10   -func Inits() (err error) {
11   -
12   - appname := beego.AppConfig.String("appname")
13   -
14   - //初始化MYSQL
15   - var conns []lxmysql.MapMysqlConn
16   -
17   - //默认数据库
18   - conns = append(conns, lxmysql.MapMysqlConn{
19   - Name: constd.MYSQL_DEFAULT,
20   - SqlCon: beego.AppConfig.String("mysql::mergecommon"),
21   - })
22   -
23   - conns = append(conns, lxmysql.MapMysqlConn{
24   - Name: constd.MYSQL_MERGECONFIG,
25   - SqlCon: beego.AppConfig.String("mysql::" + constd.MYSQL_MERGECONFIG),
26   - })
27   -
28   - err = lxmysql.InitMysqls(conns, beego.AppConfig.String("redis::cachedb"))
29   - if err != nil {
30   - fmt.Println(" InitMysqls Error::" + err.Error())
31   - }
32   -
33   - for _, conn := range conns {
34   - fmt.Println(appname + " dbs::" + conn.Name + " init success")
35   - }
36   -
37   - //// 初始化数数打点
38   - //_ = thinkingdata.InitThinkData()
39   - //
40   - //_ = config.InitLxLimit()
41   -
42   - // dto
43   - InitDto()
44   -
45   - return
46   -}
dto/interface.go
... ... @@ -1,9 +0,0 @@
1   -package dto
2   -
3   -// IDtoData dto数据
4   -type IDtoData interface {
5   - // TableName 表名模板
6   - TableName() string
7   - // GetTableName 表名 后缀是 gameId
8   - GetTableName(gameId string) string
9   -}
dto/record.go
... ... @@ -1,7 +0,0 @@
1   -package dto
2   -
3   -import "apigame/util/utdto"
4   -
5   -func SaveRecord(gameId string, data IDtoData) {
6   - utdto.Insert(DbCommon, data, data.GetTableName(gameId))
7   -}
main.go
... ... @@ -2,9 +2,10 @@ package main
2 2  
3 3 import (
4 4 "apigame/common/svconst"
  5 + "apigame/common/svdto"
5 6 "apigame/common/svlog"
  7 + "apigame/common/svmysql"
6 8 "apigame/common/svredis"
7   - "apigame/dto"
8 9 _ "apigame/routers"
9 10 "apigame/service/cardholder"
10 11 "github.com/astaxie/beego"
... ... @@ -47,7 +48,16 @@ func Init() {
47 48 return
48 49 }
49 50  
50   - _ = dto.Inits()
  51 + if !svmysql.Init() {
  52 + return
  53 + }
  54 +
  55 + //// 初始化数数打点
  56 + //_ = thinkingdata.InitThinkData()
  57 + //
  58 + //_ = config.InitLxLimit()
  59 +
  60 + svdto.Init()
51 61  
52 62 // 初始化配置
53 63 cardholder.Init()
... ...
service/cardholder/config-load.go
1 1 package cardholder
2 2  
3 3 import (
4   - "apigame/dto"
  4 + "apigame/common/svconst"
  5 + "apigame/common/svdto"
5 6 "apigame/service/constd"
6 7 "fmt"
7 8 )
... ... @@ -38,7 +39,7 @@ func LoadConfig(gameId string) {
38 39 configOpen := CardActivityUpdateConfig{Id: 0}
39 40 {
40 41 conf := make([]CardActivityUpdateConfig, 0)
41   - _, err := dto.Find(dto.DbConfig, &conf, new(CardActivityUpdateConfig).GetTableName(gameId))
  42 + _, err := svdto.Find(svconst.DbConfig, &conf, new(CardActivityUpdateConfig).GetTableName(gameId))
42 43 if err != nil {
43 44 return
44 45 }
... ... @@ -67,7 +68,7 @@ func LoadConfig(gameId string) {
67 68 // 更新数据
68 69 if needUpdate {
69 70 confNew := &CardActivityConfigRaw{}
70   - hasConfNew, err := dto.First(dto.DbConfig, confNew, gameId)
  71 + hasConfNew, err := svdto.First(svconst.DbConfig, confNew, gameId)
71 72 if err != nil {
72 73 return
73 74 }
... ...
service/cardholder/logic.go
1 1 package cardholder
2 2  
3 3 import (
  4 + "apigame/common/svconst"
  5 + "apigame/common/svdto"
4 6 "apigame/dto"
5 7 "apigame/models"
6 8 "apigame/service/constd"
... ... @@ -16,12 +18,12 @@ func SaveData(gameId string, d *dto.CardHolderData) {
16 18 d.UpdateTime = lxtime.NowUninx()
17 19 d.Encode()
18 20  
19   - dto.Save(dto.DbCommon, d, gameId)
  21 + svdto.Save(svconst.DbCommon, d, gameId)
20 22 }
21 23  
22 24 func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) {
23 25 d = dto.NewCardHolderData(uid)
24   - has, err := dto.First(dto.DbCommon, d, gameId)
  26 + has, err := svdto.First(svconst.DbCommon, d, gameId)
25 27 if err != nil {
26 28 return
27 29 }
... ... @@ -29,7 +31,7 @@ func _LoadData(gameId string, uid int64) (d *dto.CardHolderData) {
29 31 d.Decode()
30 32 } else {
31 33 d.Init(uid)
32   - dto.Insert(dto.DbCommon, d, gameId)
  34 + svdto.Insert(svconst.DbCommon, d, gameId)
33 35 }
34 36 return
35 37 }
... ... @@ -228,7 +230,7 @@ func DoOpen(gameId string,
228 230 // 记录开卡包日志
229 231 {
230 232 recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round)
231   - dto.SaveRecord(gameId, dto.NewCardHolderRecordOpen(recordBase,
  233 + svdto.SaveRecord(gameId, dto.NewCardHolderRecordOpen(recordBase,
232 234 openMode, utstring.StringToInt(confCardholder.Id), zjson.Str(newCards)))
233 235 }
234 236  
... ... @@ -276,7 +278,7 @@ func DoOpenCheckAward(gameId string,
276 278 {
277 279 // 记录日志
278 280 recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round)
279   - dto.SaveRecord(gameId, dto.NewCardHolderRecordRewardAlbum(recordBase,
  281 + svdto.SaveRecord(gameId, dto.NewCardHolderRecordRewardAlbum(recordBase,
280 282 albumId, award))
281 283 }
282 284 }
... ... @@ -287,7 +289,7 @@ func DoOpenCheckAward(gameId string,
287 289  
288 290 // 记录日志
289 291 recordBase := dto.NewCardHolderRecordBase(gameData.Uid, sequenceId, cohort, config.Id, gameData.Details.Round)
290   - dto.SaveRecord(gameId, dto.NewCardHolderRecordRewardRound(recordBase,
  292 + svdto.SaveRecord(gameId, dto.NewCardHolderRecordRewardRound(recordBase,
291 293 awardRound))
292 294  
293 295 // 处理轮次完成
... ...
service/constd/mysql.go
... ... @@ -6,6 +6,7 @@ package constd
6 6  
7 7 const (
8 8 MYSQL_DEFAULT = "default"
  9 + MYSQL_DBAPI = "api"
9 10 MYSQL_MERGECOMMON = "merge_common"
10 11 MYSQL_MERGECONFIG = "merge_config"
11 12 MYSQL_DEFAULT_QUERY_MAXCOUNT = 100
... ...