Files
2026-05-20 16:13:58 +08:00

712 lines
21 KiB
Go

package innerserverser
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"go.mongodb.org/mongo-driver/bson"
"io"
"net/http"
"os"
"sort"
"stat/api/proto"
"stat/common/log"
"stat/common/tgbot"
"stat/common/utils"
"stat/models/appInfoMod"
"stat/models/dailystatmod"
"stat/statg"
"strconv"
"time"
)
func InnerRequestBotcmd(url string, reqproto interface{}, bind interface{}) error {
data, err := json.Marshal(reqproto)
if err != nil {
log.Error("InnerRequestBotcmd json.Marshal", log.Any("err", err))
return err
}
//resp, err = InnerPlainPost(url, data)
bodyReader := bytes.NewReader(data)
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
if err != nil {
log.Error("InnerRequestBotcmd http.NewRequest", log.Any("err", err))
return err
}
timeout := 30 * time.Second
req.Header.Set("Content-Type", "application/json")
client := http.Client{
Timeout: timeout,
}
resp, err := client.Do(req)
if err != nil {
log.Error("InnerRequestBotcmd http.NewRequest", log.Any("err", err))
return err
}
if resp.StatusCode != 200 {
err = fmt.Errorf("error status %s, http code %d", resp.Status, resp.StatusCode)
log.Error("InnerRequestBotcmd http.NewRequest resp", log.Any("err", err))
return err
}
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
log.Error("InnerRequestBotcmd io.ReadAll", log.Any("err", err))
return err
}
return json.Unmarshal(data, bind)
}
// bili请求过来的
func InnerRecgaregeBotcmdList(ctx context.Context, req *proto.InnerRechargeBotcmdReq) (
*proto.InnerDailyRechargeList, error) {
if req.InnerId != int32(statg.Conf.ServerIndex) {
err := fmt.Errorf("InnerRecgaregeBotcmdList req.InnerId %d", req.InnerId)
log.Error("InnerRecgaregeBotcmdList req.InnerId", log.Any("err", err))
return nil, err
}
resp := &proto.InnerDailyRechargeList{}
resp.List = make([]*proto.InnerDailyRecharge, 0)
list, appinfo, err := GetDbListBotcmd(ctx, &req.EndTime, &req.BeginTime)
if err != nil {
log.Error("InnerRecgaregeBotcmdList:GetDbList", log.E(err))
return nil, err
}
for _, info := range list {
if info.AppId == 889 {
continue
}
name := ""
if data, ok := appinfo[info.AppId]; ok {
name = data.Name
}
temp := &proto.InnerDailyRecharge{
AppId: info.AppId,
Name: name,
Type: "",
DateTime: info.SumDate.Format("2006-01-02"),
DailyVistCount: info.TotalVisitCount,
DailyRegister: info.NewTotalCount,
DailyAppAmount: info.TotalAmount,
DailyGameAmount: info.GameRechageAmount,
RechargeOrder: info.SuccessRechargeOrder,
}
resp.List = append(resp.List, temp)
}
return resp, nil
}
func GetDbListBotcmd(ctx context.Context, day *time.Time, yesterday *time.Time) (
[]*dailystatmod.DailyStatMod, map[int32]*appInfoMod.AppInfo, error) {
list := make([]*dailystatmod.DailyStatMod, 0)
appinfo, serr := appInfoMod.GetAllAppIdMap(ctx, bson.M{})
if serr != nil {
log.Error("GetDbListBotcmd appInfoMod.GetAllAppInfo", log.Any("serr", serr))
return nil, nil, serr
}
for _, info := range appinfo {
//data, serr := dailystatmod.FindOneDailyStat(ctx, info.AppId, yesterday)
rechargelist, serr := dailystatmod.GetDailyStatTotalList(ctx, &info.AppId, yesterday, day)
if serr != nil {
log.Error("GetDbListBotcmd dailystatmod.GetDailyStatTotalList", log.Any("serr", serr))
continue
}
if rechargelist != nil {
list = append(list, rechargelist...)
} else {
log.Error("GetDbListBotcmd rechargelist nil")
}
}
return list, appinfo, nil
}
func NewXlslFileBot(f *excelize.File, list []*proto.InnerDailyRecharge,
titlestyle, cellstyle int) error {
// 设置列宽
f.SetColWidth(sheetName, "A", "AAA", 19)
// 设置表头行高
f.SetRowHeight(sheetName, 1, 25)
f.SetCellStyle(sheetName, "A1", "ZZ1", titlestyle)
f.SetCellStyle(sheetName, "A2", "ZZ2", titlestyle)
f.SetCellStyle(sheetName, "A3", "ZZ3", titlestyle)
idx := f.NewSheet(sheetName)
//f.MergeCell(sheetName, "A1", "A3")
f.SetCellValue(sheetName, "A1", "日期")
//f.SetCellStyle(sheetName, "B2", "AAA2", cellstyle)
f.SetCellStyle(sheetName, "A4", "A50", cellstyle)
f.SetActiveSheet(idx)
now := time.Now()
day := now.Format("2006-01")
//begin, end := utils.MonthDateRange(now)
beginday := utils.BeginOfMonth(now).Day()
endday := utils.EndOfMonth(now).Day()
//设置日期
index := "A"
for i := beginday; i <= endday; i++ {
tmp := fmt.Sprintf("%s%d", index, i+3)
rowday := fmt.Sprintf("%s-%02d", day, i)
f.SetCellValue(sheetName, tmp, rowday)
}
//新文件设置固定的主题和style
row := 1
col := 2
count := col
for _, info := range list {
begincol := count
count = count + 4
titleindex := 0
titlemap := make(map[string]string, 0)
for i := begincol; i <= count; i++ {
if titleindex < len(exportTransWaterTitle) {
titlecell, serr := utils.CoordinatesToCellName(begincol+titleindex, row+2)
if serr != nil {
return serr
}
titlemap[titlecell] = exportTransWaterTitle[titleindex]
}
titleindex++
}
for key, value := range titlemap {
f.SetCellValue(sheetName, key, value)
}
info.BeginCol = begincol
// 第一行产品
begincell, serr := utils.CoordinatesToCellName(begincol, row)
if serr != nil {
return serr
}
endcell, serr := utils.CoordinatesToCellName(count, row)
if serr != nil {
return serr
}
// 第一行appid
appidbegincell, serr := utils.CoordinatesToCellName(begincol, row+1)
if serr != nil {
return serr
}
appidendcell, serr := utils.CoordinatesToCellName(count, row+1)
if serr != nil {
return serr
}
f.MergeCell(sheetName, begincell, endcell)
/*if info.AppId != 0 {
if info.AppId == 888 {
f.SetCellValue(sheetName, begincell, info.Name+"(集团)")
} else {
f.SetCellValue(sheetName, begincell, info.Name+"(内部)")
}
} else {
*/
f.SetCellValue(sheetName, begincell, info.Name)
//}
f.MergeCell(sheetName, appidbegincell, appidendcell)
//tmp := fmt.Sprintf("%s1", string(index0))
f.SetCellValue(sheetName, appidbegincell, info.AppId)
count += 2
}
return nil
}
// bili请求麻豆知乎信息只在bili启动麻豆知乎只响应数据
func InnerServerBotDateMonthStat(ctx context.Context, cmd *tgbot.Command) error {
if statg.Conf.ServerIndex != statg.ServerIndexBili {
log.Error("InnerServerBotDateMonthStat serverindex",
log.Any("serverindex", statg.Conf.ServerIndex))
return nil
}
log.Info("InnerServerBotDateMonthStat start")
sBegin := cmd.GetFlagStringValue("-begin")
if sBegin == "" {
return tgbot.NewUserError(cmd, "请输入查询开始日期,格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
sEnd := cmd.GetFlagStringValue("-end")
if sEnd == "" {
return tgbot.NewUserError(cmd, "请输入查询结束日期,格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
beginmonth, err := time.ParseInLocation("2006-01-02", sBegin, time.Local)
if err != nil {
return tgbot.NewUserError(cmd, "解析开始日期出错, 格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
//beginmonth = beginmonth.AddDate(0, 0, -1)
endmonth, err := time.ParseInLocation("2006-01-02", sEnd, time.Local)
if err != nil {
return tgbot.NewUserError(cmd, "解析结束日期出错, 格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
list := make([]*proto.InnerDailyRecharge, 0)
for id, info := range statg.Conf.InnerServers {
if id == statg.ServerIndexBili {
continue
}
if id != statg.ServerIndexBili && info == "" {
log.Error("InnerServerBotDateMonthStat server not found",
log.Any("statg.Conf.InnerServers", id))
continue
}
req := &proto.InnerRechargeBotcmdReq{
InnerId: int32(id),
BeginTime: beginmonth,
EndTime: endmonth,
}
url := info + "/api/stat/inner/rechargebotcmd/list"
log.Info("InnerServerBotDateMonthStat", log.Any("req", url))
res := &proto.InnerDailyRechargeResp{}
err = InnerRequestBotcmd(url, req, res)
if err != nil {
log.Error("InnerServerBotDateMonthStat InnerRequest",
log.Any("InnerRequest", err))
continue
}
if res == nil {
log.Error("InnerServerBotDateMonthStat InnerRequest data not found")
continue
}
if res.Data.List != nil && len(res.Data.List) > 0 {
list = append(list, res.Data.List...)
}
}
bililist, appinfo, err := GetDbListBotcmd(ctx, &endmonth, &beginmonth)
if err != nil {
log.Error("InnerServerBotDateMonthStat:GetDbList", log.E(err))
return err
}
for _, info := range bililist {
name := ""
if data, ok := appinfo[info.AppId]; ok {
name = data.Name
}
temp := &proto.InnerDailyRecharge{
AppId: info.AppId,
Name: name,
Type: "",
DateTime: info.SumDate.Format("2006-01-02"),
DailyVistCount: info.TotalVisitCount,
DailyRegister: info.NewTotalCount,
DailyAppAmount: info.TotalAmount,
DailyGameAmount: info.GameRechageAmount,
RechargeOrder: info.SuccessRechargeOrder,
}
list = append(list, temp)
}
listmap := make(map[string]*proto.InnerDailyMap)
for _, info := range list {
if value, ok := listmap[info.DateTime]; ok {
value.List = append(value.List, info)
} else {
tempmap := &proto.InnerDailyMap{
Id: info.DateTime,
List: make([]*proto.InnerDailyRecharge, 0),
}
tempmap.List = append(tempmap.List, info)
listmap[info.DateTime] = tempmap
}
}
// 中间会不同日期加入新的appid要用所有的appid组成数组创建新xlsx
tempcell := make([]*proto.InnerDailyRecharge, 0)
tempmap0 := make(map[int32]*proto.InnerDailyRecharge)
for _, value := range listmap {
temp := &proto.InnerDailyRecharge{AppId: -1}
for _, info := range value.List {
temp.AppId = 0
temp.Name = "天宇总充值"
temp.DateTime = info.DateTime
temp.CellType = 1
temp.DailyVistCount += info.DailyVistCount
temp.DailyRegister += info.DailyRegister
temp.DailyAppAmount += info.DailyAppAmount
temp.DailyGameAmount += info.DailyGameAmount
temp.RechargeOrder += info.RechargeOrder
tempmap0[info.AppId] = info
}
tempmap0[temp.AppId] = temp
value.List = append(value.List, temp)
for i, j := 0, len(value.List)-1; i < j; i, j = i+1, j-1 {
value.List[i], value.List[j] = value.List[j], value.List[i]
}
}
for _, value := range tempmap0 {
tempcell = append(tempcell, value)
}
sort.Slice(tempcell, func(i, j int) bool {
return tempcell[i].AppId < tempcell[j].AppId
})
var f *excelize.File
dir := beginmonth.Format("2006-01")
dayrow := beginmonth.Format("2006-01-02")
filePath := "/usr/local" + "/bakmonthbot/" + dir + "/"
filename := filePath + dayrow + ".xlsx"
if !DocExsit(filePath) {
err = os.MkdirAll(filePath, os.ModePerm)
if err != nil {
log.Error("InnerServerBotDateMonthStat:export save file to disk failed",
log.Any("fileName", filePath),
log.E(err))
return err
}
}
f = excelize.NewFile()
if f == nil {
log.Error("InnerServerBotDateMonthStat f failed")
err = errors.New("InnerServerBotDateMonthStat f failed")
return err
}
// 设置表头样式
headStyleID, _ := f.NewStyle(titlestyle)
// 设置表头样式
headStyleID0, _ := f.NewStyle(cellstyle)
if tempcell != nil && len(tempcell) > 0 {
err = NewXlslFileBot(f, tempcell, headStyleID, headStyleID0)
if err != nil {
log.Error("InnerServerBotDateMonthStat excelize.OpenFile failed",
log.Any("fileName", filePath),
log.E(err))
return err
}
}
for key, value := range listmap {
cols := f.GetRows(sheetName)
if len(cols) < 2 || len(cols[1]) < len(value.List) {
return errors.New("InnerServerBotcmdMonthStat cols < 0")
}
for rowindex := 0; rowindex < len(cols); rowindex++ {
if cols[rowindex][0] != "" && cols[rowindex][0] == key {
for _, info := range value.List {
info.BeginRow = rowindex + 1
info.IsOld = true
}
}
}
for colindex := 0; colindex < len(cols[1]); colindex++ {
for _, info := range value.List {
appid := strconv.Itoa(int(info.AppId))
if cols[1][colindex] != "" && cols[1][colindex] == appid {
info.BeginCol = colindex + 1
info.IsOld = true
}
}
}
for _, info := range value.List {
cellday, serr := utils.CoordinatesToCellName(info.BeginCol, info.BeginRow)
if serr != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellregister, serr := utils.CoordinatesToCellName(info.BeginCol+1, info.BeginRow)
if serr != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellrecharge, serr := utils.CoordinatesToCellName(info.BeginCol+2, info.BeginRow)
if serr != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellgame, serr := utils.CoordinatesToCellName(info.BeginCol+3, info.BeginRow)
if serr != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellorder, serr := utils.CoordinatesToCellName(info.BeginCol+4, info.BeginRow)
if serr != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
f.SetCellStyle(sheetName, cellday, cellday, headStyleID0)
totalamount := fmt.Sprintf("%.2f", float64(info.DailyVistCount)*0.01)
f.SetCellValue(sheetName, cellday, totalamount)
f.SetCellStyle(sheetName, cellregister, cellday, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyRegister)*0.01)
f.SetCellValue(sheetName, cellregister, totalamount)
f.SetCellStyle(sheetName, cellrecharge, cellday, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyAppAmount)*0.0001)
f.SetCellValue(sheetName, cellrecharge, totalamount)
if info.AppId == 0 || info.AppId == 888 {
f.SetCellStyle(sheetName, cellgame, cellday, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyGameAmount)*0.0001)
f.SetCellValue(sheetName, cellgame, totalamount)
}
f.SetCellStyle(sheetName, cellorder, cellday, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.RechargeOrder)*0.01)
f.SetCellValue(sheetName, cellorder, totalamount)
}
}
err = f.SaveAs(filename)
if err != nil {
log.Error("InnerServerBotDateMonthStat CoordinatesToCellName ", log.Any("err", err))
return err
}
if statg.RchssendBot != nil {
err = statg.RchssendBot.Sendrchstochat("天宇公司综合日报间隔日期", filename, tgbot.FileTypeDocument)
//statg.Bot.Sendrchstochat("充值", fileName, tgbot.FileTypeDocument)
if err != nil {
log.Error("InnerServerBotDateMonthStat Sendrchstochat", log.Any("Sendrchstochat", err))
}
}
log.Info("InnerServerBotDateMonthStat end")
return nil
}
// bili请求麻豆知乎信息只在bili启动麻豆知乎只响应数据
func InnerServerBotDateDayStat(ctx context.Context, cmd *tgbot.Command) error {
if statg.Conf.ServerIndex != statg.ServerIndexBili {
log.Error("InnerServerBotDateDayStat serverindex",
log.Any("serverindex", statg.Conf.ServerIndex))
return nil
}
log.Info("InnerServerBotDateDayStat start")
sBegin := cmd.GetFlagStringValue("-date")
if sBegin == "" {
return tgbot.NewUserError(cmd, "请输入查询开始日期,格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
beginmonth, err := time.ParseInLocation("2006-01-02", sBegin, time.Local)
if err != nil {
return tgbot.NewUserError(cmd, "解析开始日期出错, 格式:\n"+cmd.Help+"\n"+cmd.Desc)
}
endmonth := beginmonth.AddDate(0, 0, 1)
list := make([]*proto.InnerDailyRecharge, 0)
for id, info := range statg.Conf.InnerServers {
if id == statg.ServerIndexBili {
continue
}
if id != statg.ServerIndexBili && info == "" {
log.Error("InnerServerBotDateDayStat server not found",
log.Any("InnerServerBotDateDayStat", id))
continue
}
req := &proto.InnerRechargeBotcmdReq{
InnerId: int32(id),
BeginTime: beginmonth,
EndTime: endmonth,
}
url := info + "/api/stat/inner/rechargebotcmd/list"
log.Info("InnerServerBotDateDayStat", log.Any("req", url))
res := &proto.InnerDailyRechargeResp{}
err = InnerRequestBotcmd(url, req, res)
if err != nil {
log.Error("InnerServerBotDateDayStat InnerRequest",
log.Any("InnerRequest", err))
continue
}
if res == nil {
log.Error("InnerServerBotDateDayStat InnerRequest data not found")
continue
}
if res.Data.List != nil && len(res.Data.List) > 0 {
list = append(list, res.Data.List...)
}
}
bililist, appinfo, err := GetDbListBotcmd(ctx, &endmonth, &beginmonth)
if err != nil {
log.Error("InnerServerBotDateDayStat:GetDbListBotcmd", log.E(err))
return err
}
for _, info := range bililist {
name := ""
if data, ok := appinfo[info.AppId]; ok {
name = data.Name
}
temp := &proto.InnerDailyRecharge{
AppId: info.AppId,
Name: name,
Type: "",
DateTime: info.SumDate.Format("2006-01-02"),
DailyVistCount: info.TotalVisitCount,
DailyRegister: info.NewTotalCount,
DailyAppAmount: info.TotalAmount,
DailyGameAmount: info.GameRechageAmount,
RechargeOrder: info.SuccessRechargeOrder,
}
list = append(list, temp)
}
temp := &proto.InnerDailyRecharge{}
for _, info := range list {
temp.AppId = 0
temp.CellType = 1
temp.Name = "天宇总充值"
temp.DailyVistCount += info.DailyVistCount
temp.DailyRegister += info.DailyRegister
temp.DailyAppAmount += info.DailyAppAmount
temp.DailyGameAmount += info.DailyGameAmount
temp.RechargeOrder += info.RechargeOrder
}
list = append(list, temp)
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
}
var f *excelize.File
dir := beginmonth.Format("2006-01")
dayrow := beginmonth.Format("2006-01-02")
filePath := "/usr/local" + "/bakmonthday/" + dir + "/"
filename := filePath + dayrow + ".xlsx"
if !DocExsit(filePath) {
err = os.MkdirAll(filePath, os.ModePerm)
if err != nil {
log.Error("InnerServerBotDateDayStat:export save file to disk failed",
log.Any("fileName", filePath),
log.E(err))
return err
}
}
f = excelize.NewFile()
if f == nil {
log.Error("InnerServerBotDateDayStat f failed")
err = errors.New("InnerServerBotDateDayStat f failed")
return err
}
// 设置表头样式
headStyleID, _ := f.NewStyle(titlestyle)
// 设置表头样式
headStyleID0, _ := f.NewStyle(cellstyle)
err = NewXlslFileBot(f, list, headStyleID, headStyleID0)
if err != nil {
log.Error("InnerServerBotDateDayStat excelize.OpenFile failed",
log.Any("fileName", filePath),
log.E(err))
return err
}
row := 0
cols := f.GetRows(sheetName)
for rowindex, col := range cols {
for colindex, rowCell := range col {
if rowCell == "" {
continue
}
if colindex == 0 {
if rowCell == dayrow {
row = rowindex + 1
}
}
}
}
if row == 0 {
return errors.New("InnerServerBotDateDayStat row <= 0")
}
for _, info := range list {
cellday, serr := utils.CoordinatesToCellName(info.BeginCol, row)
if serr != nil {
log.Error("InnerServerBotDateDayStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellregister, serr := utils.CoordinatesToCellName(info.BeginCol+1, row)
if serr != nil {
log.Error("InnerServerBotDateDayStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellrecharge, serr := utils.CoordinatesToCellName(info.BeginCol+2, row)
if serr != nil {
log.Error("InnerServerBotDateDayStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellgame, serr := utils.CoordinatesToCellName(info.BeginCol+3, row)
if serr != nil {
log.Error("InnerServerBotDateDayStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
cellorder, serr := utils.CoordinatesToCellName(info.BeginCol+4, row)
if serr != nil {
log.Error("InnerServerBotDateDayStat CoordinatesToCellName ", log.Any("err", serr))
return serr
}
f.SetCellStyle(sheetName, cellday, cellday, headStyleID0)
totalamount := fmt.Sprintf("%.2f", float64(info.DailyVistCount)*0.01)
f.SetCellValue(sheetName, cellday, totalamount)
f.SetCellStyle(sheetName, cellregister, cellregister, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyRegister)*0.01)
f.SetCellValue(sheetName, cellregister, totalamount)
f.SetCellStyle(sheetName, cellrecharge, cellrecharge, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyAppAmount)*0.0001)
f.SetCellValue(sheetName, cellrecharge, totalamount)
if info.AppId == 0 || info.AppId == 888 {
f.SetCellStyle(sheetName, cellgame, cellgame, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.DailyGameAmount)*0.0001)
f.SetCellValue(sheetName, cellgame, totalamount)
}
f.SetCellStyle(sheetName, cellorder, cellorder, headStyleID0)
totalamount = fmt.Sprintf("%.2f", float64(info.RechargeOrder)*0.01)
f.SetCellValue(sheetName, cellorder, totalamount)
}
err = f.SaveAs(filename)
if err != nil {
log.Error("InnerServerBotDateDayStat SaveAs ", log.Any("err", err))
return err
}
if statg.RchssendBot != nil {
err = statg.RchssendBot.Sendrchstochat("天宇公司综合日报具体date", filename,
tgbot.FileTypeDocument)
if err != nil {
log.Error("InnerServerBotDateDayStat Sendrchstochat", log.Any("Sendrchstochat", err))
}
}
log.Info("InnerServerBotDateDayStat end")
return nil
}