samba/util/model/clubResourceLog.go

83 lines
2.2 KiB
Go
Raw Normal View History

2025-06-04 09:51:39 +08:00
package model
import (
"encoding/json"
"samba/pkg/log"
"samba/pkg/servername"
"samba/pkg/service"
"samba/proto"
"samba/util/routingKey"
"samba/util/util"
"time"
)
/*
drop table samba.club_resource;
CREATE TABLE samba.club_resource
(
`uid` Int64,
`club_id` Int64,
`change_res` Int64,
`res_type` String,
`hold_res` Int64,
`change_reason` String,
`game_no` String DEFAULT ,
`room_type` Int32 DEFAULT 0,
`room_id` Int32 DEFAULT 0,
`time` DateTime
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(time)
ORDER BY intHash32(uid);
*/
type ClubResourceRecord struct {
UserId int64 `json:"uid"` // 玩家id
ClubId int `json:"club_id"`
ResType string `json:"res_type"` // 资源类型
ResValue int64 `json:"change_res"` // 操作金币
Reason string `json:"change_reason"` // 操作原因
HoldResValue int64 `json:"hold_res"` // 操作金币
RoomId int `json:"room_id"` // 房间id
RoomType int `json:"room_type"` // 房间类型
GameNo string `json:"game_no"` // game_no
Time time.Time `json:"time"` // 时间戳
}
func NewClubResourceRecord(res *proto.ReqAddClubResource, holdValue int64) *ClubResourceRecord {
return &ClubResourceRecord{
UserId: res.UserId,
ClubId: res.ClubId,
ResType: res.ResType,
ResValue: res.ResValue,
Reason: res.Reason,
HoldResValue: holdValue,
RoomId: res.RoomId,
RoomType: res.RoomType,
GameNo: res.GameNo,
Time: time.Now(),
}
}
func (t *ClubResourceRecord) send(srv service.IService, data string) {
req := proto.NtfClickHouseSql{Sql: data}
msg := util.MakeMessage(proto.NtfClickHouseSqlId, req, t.UserId, t.RoomId)
byteData, err := json.Marshal(msg)
if err != nil {
log.Error(err.Error())
}
//log.Debug(string(byteData))
//srv := gDBServices.Load(t.UserId)
err = srv.Publish(util.Direct(servername.ClickHouse), routingKey.ClickHouseKey(t.UserId), byteData)
if err != nil {
log.Error(err.Error())
}
}
func (t *ClubResourceRecord) Flush(srv service.IService) {
if sql, err := util.Struct2InsertSql("`samba`.`club_resource`", t); err == nil {
t.send(srv, sql)
} else {
log.Error(err.Error())
}
}