117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package botser
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"stat/api/proto"
|
|
"stat/common/utils"
|
|
"stat/models/appInfoMod"
|
|
"stat/models/botdailyhourstat"
|
|
"stat/statg"
|
|
)
|
|
|
|
func RecvTgMessage(ctx context.Context, param *proto.SendTgMessageReq) (*proto.SendTgMessageRes, error) {
|
|
name := statg.GlobalEndpoints.GetAppShortNameById(param.AppId)
|
|
text := fmt.Sprintf("应用【%s:%d】消息:\n%s", name, param.AppId, param.Message)
|
|
|
|
_ = statg.Bot.SendProjectMessage(text)
|
|
|
|
return &proto.SendTgMessageRes{}, nil
|
|
}
|
|
|
|
func GetBotDailyHourOne(ctx context.Context, param *proto.BotDailyHourOneReq) (*proto.BotDailyHourOneResp, error) {
|
|
var err error
|
|
ret := &proto.BotDailyHourOneResp{}
|
|
filter := bson.M{}
|
|
if !param.Id.IsZero() {
|
|
filter["_id"] = param.Id
|
|
} else if !param.SumDate.IsZero() && param.AppId > 0 {
|
|
filter["sumDate"] = param.SumDate
|
|
filter["appId"] = param.AppId
|
|
} else {
|
|
return nil, fmt.Errorf("param error")
|
|
}
|
|
if ret.Stat, err = botdailyhourstat.FindOneBotHourStatEx(ctx, filter); err != nil {
|
|
return nil, err
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func GetBotDailyHourList(ctx context.Context, param *proto.BotDailyHourListReq) (*proto.BotDailyHourListResp, error) {
|
|
var err error
|
|
ret := &proto.BotDailyHourListResp{}
|
|
|
|
skip, limit := utils.ToDBPage(param.PageNum, param.PageSize)
|
|
opts := options.Find().SetSkip(skip).SetLimit(limit)
|
|
|
|
filter := bson.M{
|
|
"sumDate": bson.M{
|
|
"$gte": param.SumDateStart,
|
|
"$lte": param.SumDateStop,
|
|
},
|
|
}
|
|
if param.AppId != nil {
|
|
filter["appId"] = *param.AppId
|
|
}
|
|
|
|
if ret.Total, err = botdailyhourstat.CountBotHourStats(ctx, filter); err != nil {
|
|
return nil, err
|
|
}
|
|
if ret.List, err = botdailyhourstat.FindManyBotHourStat(ctx, filter, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func GetBotMonthStatList(ctx context.Context, param *proto.BotMonthStatReq) (*proto.BotMonthStatResp, error) {
|
|
var err error
|
|
ret := &proto.BotMonthStatResp{
|
|
List: make(botdailyhourstat.BotHourStatList, 0),
|
|
}
|
|
appIds := make([]int32, 0)
|
|
appMap, _ := appInfoMod.GetAllAppIdNameMap(ctx)
|
|
if param.AppId != nil {
|
|
if _, exists := appMap[*param.AppId]; exists {
|
|
appIds = append(appIds, *param.AppId)
|
|
} else {
|
|
return ret, nil // 没有这个app,直接返回空列表
|
|
}
|
|
} else {
|
|
for id, _ := range appMap {
|
|
appIds = append(appIds, id)
|
|
}
|
|
}
|
|
|
|
start := utils.BeginMonthWithTime(param.Month)
|
|
end := start.AddDate(0, 1, 0)
|
|
start = start.AddDate(0, 0, 1)
|
|
|
|
for _, appId := range appIds {
|
|
base := &botdailyhourstat.BotHourStat{
|
|
AppId: appId,
|
|
ReportTime: param.Month,
|
|
SumDate: param.Month,
|
|
TodayGame: &botdailyhourstat.GameItem{},
|
|
}
|
|
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) {
|
|
filter := bson.M{"sumDate": d, "appId": appId}
|
|
var stat *botdailyhourstat.BotHourStat
|
|
stat, err = botdailyhourstat.FindOneBotHourStat(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if stat.ID.IsZero() {
|
|
if stat, err = BotDailyHourStat(ctx, appId, d, d.Before(utils.BeginOfYesterday())); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
base.ACC(stat)
|
|
}
|
|
ret.List = append(ret.List, base)
|
|
}
|
|
return ret, nil
|
|
}
|