2025-07-22 09:37:37 +08:00

147 lines
3.9 KiB
Go

package repo
import (
"admin/apps/game/model"
"admin/internal/errcode"
"admin/lib/utils"
"github.com/jmoiron/sqlx"
"gorm.io/gorm"
)
type IGameLogRepo interface {
QueryAttrListByEvent(appId int, eventName []string) ([]*AttrInfo, error)
NewEventListQuerier(tableName string, eventName []string, attrList []*AttrInfo) *EventListQuerier
}
func NewGameLogRepo(metaDb *gorm.DB, clickHouseSqlx *sqlx.DB) IGameLogRepo {
return &gameLogRepoImpl{metaDb: metaDb, clickHouseSqlx: clickHouseSqlx}
}
type gameLogRepoImpl struct {
metaDb *gorm.DB
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)
}
normalAttrList := make([]*AttrInfo, 0)
publicAttrList := make([]*AttrInfo, 0)
var eventNameAttr *AttrInfo
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" && attr.AttrName != "xwl_part_event" {
continue
}
if attr.AttrName == "xwl_part_date" {
timeAttr = &AttrInfo{
Name: attr.AttrName,
Alias: "时间",
DataType: attr.AttrFieldType,
IsUserAttr: true,
}
} else if attr.AttrName == "xwl_part_event" {
eventNameAttr = &AttrInfo{
Name: attr.AttrName,
Alias: "操作类型",
DataType: attr.AttrFieldType,
IsUserAttr: true,
}
}
continue
}
find := false
for _, oldAttr := range normalAttrList {
if oldAttr.Name == attr.AttrName {
find = true
break
}
}
if !find {
for _, oldAttr := range publicAttrList {
if oldAttr.Name == attr.AttrName {
find = true
break
}
}
}
if !find {
var isPublicField bool
if len(attr.AttrName) >= 4 {
isPublicField = attr.AttrName[:4] == "pub_"
}
attrInfo := &AttrInfo{
Name: attr.AttrName,
Alias: attr.AttrAlias,
DataType: attr.AttrFieldType,
IsUserAttr: isPublicField,
}
if isPublicField {
publicAttrList = append(publicAttrList, attrInfo)
} else {
normalAttrList = append(normalAttrList, attrInfo)
}
}
}
}
if timeAttr != nil {
normalAttrList = append([]*AttrInfo{timeAttr}, normalAttrList...)
}
if eventNameAttr != nil {
normalAttrList = append([]*AttrInfo{eventNameAttr}, normalAttrList...)
}
return append(normalAttrList, publicAttrList...), 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.clickHouseSqlx = impl.clickHouseSqlx
querier.tableName = tableName
querier.eventName = eventName
querier.attrList = attrList
return querier
}