You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
10 KiB
293 lines
10 KiB
package internal |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"fmt" |
|
"github.com/gogf/gf/v2/encoding/gcharset" |
|
"github.com/gogf/gf/v2/encoding/gjson" |
|
"github.com/gogf/gf/v2/errors/gerror" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"go.mongodb.org/mongo-driver/bson" |
|
"time" |
|
"tyj_admin/api/v1/game" |
|
"tyj_admin/internal/consts" |
|
"tyj_admin/internal/dao" |
|
"tyj_admin/internal/model/entity" |
|
) |
|
|
|
func SearchGm(ctx context.Context, req *game.SearchGmReq) (gm int32, err error) { |
|
filter := bson.M{} |
|
server := fmt.Sprint(req.ServerId) |
|
if req.Type == 2 { |
|
var unit []entity.GameUnit |
|
dao.GameUnit.Ctx(ctx).Where("account=?", req.Account).Where("server=?", server).Scan(&unit) |
|
if len(unit) != 1 { |
|
return 0, errors.New("找不到账号") |
|
} |
|
server = fmt.Sprint(unit[0].Server) |
|
filter["_id"] = unit[0].Uid |
|
} else { |
|
uid, err := GetStringIdToUid(ctx, req.Uid) |
|
if err != nil { |
|
return 0, err |
|
} |
|
var unit []entity.GameUnit |
|
dao.GameUnit.Ctx(ctx).Where("uid=?", uid).Scan(&unit) |
|
if len(unit) != 1 { |
|
return 0, errors.New("找不到账号") |
|
} |
|
server = fmt.Sprint(unit[0].Server) |
|
filter["_id"] = unit[0].Uid |
|
} |
|
if MongoDatabaseList[server] == nil { |
|
return 0, errors.New("数据库不存在,请联系管理员") |
|
} |
|
|
|
all1 := make([]map[string]interface{}, 1) |
|
err = MongoDatabaseList[server].Collection("Account").Find(ctx, filter).All(&all1) |
|
if err != nil { |
|
return 0, err |
|
} |
|
if len(all1) == 0 { |
|
return 0, gerror.New("获取失败") |
|
} |
|
gm = all1[0]["AccountType"].(int32) |
|
return gm, err |
|
} |
|
|
|
func SetGmToClient(ctx context.Context, req *game.SetGmToClientReq) (res *game.SetGmToClientRes, err error) { |
|
res = new(game.SetGmToClientRes) |
|
srcCharset := "UTF-8" |
|
g.Client().SetHeader("Content-Type", "application/json;charset=UTF-8") |
|
if req.Uids == "" { |
|
return nil, errors.New("uid为空!") |
|
} |
|
unit, err := GetUnitByStringUid(ctx, req.Uids, req.ServerId) |
|
if err != nil { |
|
fmt.Println("GetAccount:", err) |
|
return nil, err |
|
} |
|
if req.ServerId != unit.Server { |
|
var serverInfo entity.GameLoginUrl |
|
for _, v := range ServerList { |
|
if v.Id == unit.Server { |
|
serverInfo = v |
|
break |
|
} |
|
} |
|
return nil, errors.New("区服选择错误!当前区服在<" + fmt.Sprint(serverInfo.Id) + "><" + serverInfo.Name + ">区") |
|
} |
|
|
|
g.Log().Info(ctx, "SetGmToClient - ServerConfig: ", gjson.MustEncodeString(ServerConfig)) |
|
ip := ServerConfig[fmt.Sprint(req.ServerId)] |
|
url := "http://" + ip + "/GM?Ids=" + fmt.Sprint(unit.Uid) + "&State=" + fmt.Sprint(req.State) |
|
g.Log().Info(ctx, "SetGmToClient - req: ", url) |
|
bytes := g.Client().GetBytes(ctx, url) |
|
src := string(bytes) |
|
if g.IsEmpty(src) { |
|
return |
|
} |
|
|
|
tmp, _ := gcharset.ToUTF8(srcCharset, src) |
|
json, err1 := gjson.DecodeToJson(tmp) |
|
if err1 != nil { |
|
return nil, err1 |
|
} |
|
if json.Get("Error").Int() != 200 { |
|
return nil, errors.New("发送错误: " + json.Get("Error").String()) |
|
} |
|
return |
|
} |
|
|
|
func ChangeGm(ctx context.Context, req *game.ChangeGmReq) (res *game.ChangeGmRes, err error) { |
|
filter := bson.M{} |
|
server := "" |
|
if req.Type == 2 { |
|
var unit []entity.GameUnit |
|
dao.GameUnit.Ctx(ctx).Where("account=?", req.Account).Scan(&unit) |
|
if len(unit) == 0 { |
|
return nil, errors.New("找不到账号") |
|
} |
|
server = fmt.Sprint(unit[0].Server) |
|
filter["_id"] = unit[0].Uid |
|
} else { |
|
uid, err := GetStringIdToUid(ctx, req.Uid) |
|
if err != nil { |
|
return nil, err |
|
} |
|
var unit []entity.GameUnit |
|
dao.GameUnit.Ctx(ctx).Where("uid=?", uid).Scan(&unit) |
|
if len(unit) == 0 { |
|
return nil, errors.New("找不到账号") |
|
} |
|
server = fmt.Sprint(unit[0].Server) |
|
filter["_id"] = unit[0].Uid |
|
} |
|
if MongoDatabaseList[server] == nil { |
|
return nil, errors.New("数据库不存在,请联系管理员") |
|
} |
|
err = MongoDatabaseList[server].Collection("Account").UpdateOne(ctx, filter, bson.M{"$set": bson.M{"AccountType": req.ChangeValue}}) |
|
return |
|
} |
|
|
|
func ChangePwd(ctx context.Context, account string, pwd string, server string) (res *game.ResetPwdRes, err error) { |
|
err = MongoDatabaseList[server].Collection("Account").UpdateOne(ctx, bson.M{"AccountName": account}, bson.M{"$set": bson.M{"Password": pwd}}) |
|
return |
|
} |
|
|
|
/* |
|
* 留存 |
|
*/ |
|
func GetKeepAlive(ctx context.Context, req *game.GetKeepAliveReq) (res *game.GetKeepAliveRes, err error) { |
|
res = new(game.GetKeepAliveRes) |
|
if req.CreateTime == "" { |
|
return nil, gerror.New("请填写注册时间") |
|
} |
|
t, _ := time.ParseInLocation(consts.TIME_FORMAT, req.CreateTime+" 00:00:00", time.Local) |
|
|
|
registersql := fmt.Sprintf("select account, server from game_register where 1=1 ") |
|
if req.Server != 0 { |
|
registersql += fmt.Sprintf(` and server LIKE '%s'`, "%<"+fmt.Sprint(req.Server)+">%") |
|
} |
|
if req.Channel != "" { |
|
registersql += fmt.Sprintf(` and channel="%s"`, req.Channel) |
|
} |
|
registersql += fmt.Sprintf(` and STR_TO_DATE(create_time, "%%Y-%%m-%%d")="%s"`, t.Format(consts.DATE_FORMAT)) |
|
if req.Keep == 2 { |
|
registersql = fmt.Sprintf("select MAX( b.recharge ) AS `recharge`, b.account, MAX( b.server ) AS `server` from game_register as a inner join game_unit as b on a.account=b.account" + |
|
" where b.recharge>0 ") |
|
if req.Server != 0 { |
|
registersql += fmt.Sprintf(` and b.server=%d`, req.Server) |
|
} |
|
if req.Channel != "" { |
|
registersql += fmt.Sprintf(` and b.channel="%s"`, req.Channel) |
|
} |
|
registersql += fmt.Sprintf(` and STR_TO_DATE(a.create_time, "%%Y-%%m-%%d")="%s"`, t.Format(consts.DATE_FORMAT)) |
|
registersql += fmt.Sprintf(` group by b.account`) |
|
} |
|
|
|
res.RegisterNum, err = g.Model().Raw(registersql).Count() |
|
if err != nil { |
|
g.Log().Info(ctx, "GetKeepAlive -- RegisterNum err==", err.Error()) |
|
} |
|
|
|
sql := fmt.Sprintf(`SELECT cd,COUNT(account) as num from (SELECT b.account, FROM_UNIXTIME( a.c_date, "%%Y-%%m-%%d" ) AS cd FROM login_out_log a |
|
INNER JOIN game_unit b ON ( a.uid = b.uid ) |
|
inner join game_register c on (b.account=c.account) |
|
WHERE ( a.e_date - a.c_date > 0 ) AND ( STR_TO_DATE( c.create_time, "%%Y-%%m-%%d" )= '%s') AND ( a.c_date < %d ) AND ( a.c_date >= %d )`, |
|
req.CreateTime, t.AddDate(0, 0, 30).Unix(), t.Unix()) |
|
if req.Server != 0 { |
|
sql += fmt.Sprintf(` And a.server=%d`, req.Server) |
|
} |
|
if req.Channel != "" { |
|
sql += fmt.Sprintf(` And b.channel='%s'`, req.Channel) |
|
} |
|
if req.Keep == 2 { |
|
sql += fmt.Sprintf(` And b.recharge>0`) |
|
} |
|
sql += ` GROUP BY FROM_UNIXTIME( a.c_date, "%Y-%m-%d" ), b.account) as d GROUP BY d.cd` |
|
g.Model().Raw(sql).Scan(&res.Logs) |
|
g.Log().Info(ctx, " a== ", res) |
|
return |
|
} |
|
|
|
/* |
|
* 活跃 |
|
*/ |
|
func GetLive(ctx context.Context, req *game.GetLiveReq) (res *game.GetLiveRes, err error) { |
|
res = new(game.GetLiveRes) |
|
if req.CreateTime == "" { |
|
return nil, gerror.New("请填写起始时间") |
|
} |
|
|
|
t, _ := time.ParseInLocation(consts.TIME_FORMAT, req.CreateTime+" 00:00:00", time.Local) |
|
sql := fmt.Sprintf(`SELECT cd,COUNT(1) as num from (SELECT b.account, FROM_UNIXTIME( a.c_date, "%%Y-%%m-%%d" ) AS cd FROM login_out_log a INNER JOIN game_unit b ON ( a.uid = b.uid ) WHERE ( a.e_date - a.c_date > 60 ) AND ( a.c_date >= %d ) AND ( a.c_date < %d )`, |
|
t.Unix(), t.AddDate(0, 0, 30).Unix()) |
|
if req.Server != 0 { |
|
sql += fmt.Sprintf(` And a.server=%d`, req.Server) |
|
} |
|
if req.Channel != "" { |
|
sql += fmt.Sprintf(` And b.channel=%s`, req.Channel) |
|
} |
|
sql += ` GROUP BY cd, b.account) as d GROUP BY d.cd` |
|
g.Model().Raw(sql).Scan(&res.LiveLogs) |
|
|
|
model := dao.GameRegister.Ctx(ctx). |
|
Fields(`STR_TO_DATE( create_time, "%Y-%m-%d" ) AS cd,COUNT(account) as num`). |
|
Where(`UNIX_TIMESTAMP(create_time)>=?`, t.Unix()). |
|
Where(`UNIX_TIMESTAMP(create_time)<?`, t.AddDate(0, 0, 30).Unix()) |
|
if req.Server != 0 { |
|
model = model.Where(`server like ?`, "%<"+fmt.Sprint(req.Server)+">%") |
|
} |
|
if req.Channel != "" { |
|
model = model.Where(`channel=?`, req.Channel) |
|
} |
|
model.Group(`cd`).Scan(&res.RegisterLogs) |
|
|
|
modelTotal := dao.GameRegister.Ctx(ctx).Where(`UNIX_TIMESTAMP(create_time)<?`, t.Unix()) |
|
if req.Server != 0 { |
|
modelTotal = modelTotal.Where(`server like ?`, "%<"+fmt.Sprint(req.Server)+">%") |
|
} |
|
if req.Channel != "" { |
|
modelTotal = modelTotal.Where(`channel=?`, req.Channel) |
|
} |
|
res.RegisterTotal, err = modelTotal.Group("account").Count() |
|
|
|
HighSql := fmt.Sprintf(`SELECT max(num) AS num, STR_TO_DATE(date, "%%Y-%%m-%%d") AS cd FROM game_online WHERE UNIX_TIMESTAMP(date) >= %d AND UNIX_TIMESTAMP(date) < %d `, |
|
t.Unix(), t.AddDate(0, 0, 30).Unix()) |
|
if req.Server != 0 { |
|
HighSql += fmt.Sprintf(` AND server=%d`, req.Server) |
|
} |
|
if req.Channel != "" { |
|
HighSql += fmt.Sprintf(` AND channel=%s`, req.Channel) |
|
} |
|
HighSql += ` GROUP BY cd` |
|
g.Model().Raw(HighSql).Scan(&res.HighLogs) |
|
return |
|
} |
|
|
|
func HighLogs(ctx context.Context, req *game.GetLiveReq) (res *game.GetLiveRes, err error) { |
|
res = new(game.GetLiveRes) |
|
if req.CreateTime == "" { |
|
return nil, gerror.New("请填写起始时间") |
|
} |
|
|
|
t, _ := time.ParseInLocation(consts.TIME_FORMAT, req.CreateTime+" 00:00:00", time.Local) |
|
HighSql := fmt.Sprintf(`SELECT max(num) AS num, STR_TO_DATE(date, "%%Y-%%m-%%d") AS cd FROM game_online WHERE UNIX_TIMESTAMP(date) >= %d AND UNIX_TIMESTAMP(date) < %d `, |
|
t.Unix(), t.AddDate(0, 0, 30).Unix()) |
|
if req.Server != 0 { |
|
HighSql += fmt.Sprintf(` AND server=%d`, req.Server) |
|
} |
|
if req.Channel != "" { |
|
HighSql += fmt.Sprintf(` AND channel=%s`, req.Channel) |
|
} |
|
HighSql += ` GROUP BY cd` |
|
g.Model().Raw(HighSql).Scan(&res.HighLogs) |
|
return |
|
} |
|
|
|
func RegisterNum(ctx context.Context, req *game.RegisterCountReq) (res *game.RegisterCountRes, err error) { |
|
res = new(game.RegisterCountRes) |
|
|
|
modelTotal := dao.GameRegister.Ctx(ctx) |
|
if req.StartTime != 0 { |
|
modelTotal = modelTotal.Where(`UNIX_TIMESTAMP(create_time)>?`, req.StartTime/1000) |
|
} |
|
if req.EndTime != 0 { |
|
modelTotal = modelTotal.Where(`UNIX_TIMESTAMP(create_time)<?`, req.EndTime/1000) |
|
} |
|
if req.Server != 0 { |
|
modelTotal = modelTotal.Where(`server like ?`, "%<"+fmt.Sprint(req.Server)+">%") |
|
} |
|
if req.Channel != "" { |
|
modelTotal = modelTotal.Where(`channel=?`, req.Channel) |
|
} |
|
err = modelTotal.FieldCount("account", "count"). |
|
Fields("channel", "STR_TO_DATE( create_time, \"%Y-%m-%d\" ) AS cd"). |
|
Group("channel", "STR_TO_DATE( create_time, \"%Y-%m-%d\" )"). |
|
Scan(&res.RegisterList) |
|
|
|
g.Log().Info(ctx, "a== ", res) |
|
return |
|
}
|
|
|