2025-07-17 17:09:09 +08:00

135 lines
3.8 KiB
Go

package repo
import (
"admin/apps/game/model"
"admin/internal/errcode"
"admin/lib/utils"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/jmoiron/sqlx"
"gorm.io/gorm"
"sort"
)
type IGameLogRepo interface {
QueryAttrListByEvent(appId int, eventName []string) ([]*AttrInfo, error)
NewEventListQuerier(tableName string, eventName []string, attrList []*AttrInfo) *EventListQuerier
}
func NewGameLogRepo(metaDb *gorm.DB, ckDb driver.Conn, clickHouseSqlx *sqlx.DB) IGameLogRepo {
return &gameLogRepoImpl{metaDb: metaDb, db: ckDb, clickHouseSqlx: clickHouseSqlx}
}
type gameLogRepoImpl struct {
metaDb *gorm.DB
db driver.Conn
clickHouseSqlx *sqlx.DB
}
type GetEventAttrResult struct {
AppId int
EventName string
EventAlias string
AttrName string
AttrAlias string
AttrFieldType int
AttrIsPublicField int
}
func (impl *gameLogRepoImpl) QueryAttrListByEvent(appId int, eventName []string) ([]*AttrInfo, error) {
resultList := make([]*GetEventAttrResult, 0)
err := impl.metaDb.Model(&model.EventAttribute{}).Select("meta_attr_relation.app_id,"+
"meta_event.event_name as event_name,"+
"meta_event.show_name as event_alias,"+
"attribute.attribute_name as attr_name,"+
"attribute.show_name as attr_alias,"+
"attribute.data_type as attr_field_type,"+
"attribute.attribute_type as attr_is_public_field",
).
Joins("inner join meta_event on "+
"meta_event.event_name=meta_attr_relation.event_name and "+
"meta_event.appid=? and meta_attr_relation.app_id=?", appId, appId).
Joins("inner join attribute on "+
"attribute.attribute_name=meta_attr_relation.event_attr and "+
"attribute.app_id=?", appId).
Scan(&resultList).Error
if err != nil {
return nil, errcode.New(errcode.DBError, "join tables get event list error:%v", err)
}
attrList := make([]*AttrInfo, 0)
var timeAttr *AttrInfo
for _, attr := range resultList {
if len(eventName) == 0 || utils.IsInSlice(eventName, attr.EventName) {
if len(attr.AttrName) >= 4 && attr.AttrName[:4] == "xwl_" {
if attr.AttrName != "xwl_part_date" {
continue
}
timeAttr = &AttrInfo{
Name: attr.AttrName,
Alias: "时间",
DataType: attr.AttrFieldType,
IsUserAttr: attr.AttrIsPublicField != 2,
}
continue
}
attrList = append(attrList, &AttrInfo{
Name: attr.AttrName,
Alias: attr.AttrAlias,
DataType: attr.AttrFieldType,
IsUserAttr: attr.AttrIsPublicField != 2,
})
}
}
sort.SliceStable(attrList, func(i, j int) bool {
if attrList[i].IsUserAttr && !attrList[j].IsUserAttr {
return false
}
if attrList[i].IsUserAttr && attrList[j].IsUserAttr {
return false
}
if attrList[i].IsUserAttr && attrList[j].IsUserAttr {
return false
}
return true
})
if timeAttr != nil {
attrList = append([]*AttrInfo{timeAttr}, attrList...)
}
//retList := make(map[string][]*AttrInfo)
//for _, attr := range resultList {
// attrList, find := retList[attr.EventName]
// if !find {
// attrList = make([]*AttrInfo, 0, 1)
// }
// attrList = append(attrList, &AttrInfo{
// Name: attr.AttrName,
// Alias: attr.AttrAlias,
// DataType: attr.AttrFieldType,
// IsUserAttr: attr.AttrIsPublicField != 2,
// })
// retList[attr.EventName] = attrList
//}
return attrList, nil
}
type AttrInfo struct {
Name string
Alias string
DataType int // 1:number 3:string 4:time
IsUserAttr bool
}
func (impl *gameLogRepoImpl) NewEventListQuerier(tableName string, eventName []string, attrList []*AttrInfo) *EventListQuerier {
querier := new(EventListQuerier)
querier.db = impl.db
querier.clickHouseSqlx = impl.clickHouseSqlx
querier.tableName = tableName
querier.eventName = eventName
querier.attrList = attrList
return querier
}