Browse Source

增加部分功能

wucan 3 months ago
parent
commit
48decd825e

+ 5 - 2
bootstrap/db.go

@@ -3,6 +3,7 @@ package bootstrap
 import (
 	"designs/config"
 	"designs/global"
+	"fmt"
 	"io"
 	"log"
 	"os"
@@ -61,6 +62,8 @@ func initMySqlGorm() *gorm.DB {
 		global.App.Log.Error("mysql connect failed, err:", zap.Any("err", err))
 		return nil
 	} else {
+
+		fmt.Println("mysql connect success")
 		sqlDB, _ := db.DB()
 		sqlDB.SetMaxIdleConns(config.GetInt("database.maxIdleConns"))
 		sqlDB.SetMaxOpenConns(config.GetInt("database.maxOpenConns"))
@@ -121,8 +124,8 @@ func getGormLogWriter() logger.Writer {
 // 数据库表初始化
 func initMySqlTables(db *gorm.DB) {
 	err := db.AutoMigrate(
-	// models.User{},
-	// models.Media{},
+		// models.User{},
+		// models.Media{},
 	)
 	if err != nil {
 		global.App.Log.Error("migrate table failed", zap.Any("err", err))

+ 4 - 1
controller/v1/gameAction.go

@@ -241,7 +241,10 @@ func UserActionOptionList(c *gin.Context) {
 	optionIdToName := make(map[string]string)
 	global.App.DB.Table("game_action_option").
 		LeftJoin("game_action", "game_action.id = game_action_option.actionId").
-		Where("gid", form.Gid).Scan(&optionName)
+		Where("gid", form.Gid).
+		Where("game_action.actionId", form.ActionId).
+		Select("game_action_option.optionName", "game_action_option.actionId", "game_action_option.optionId").
+		Scan(&optionName)
 	for _, v := range optionName {
 		optionIdToName[v.OptionId] = v.OptionName
 	}

+ 193 - 25
controller/v1/gameConfig.go

@@ -71,18 +71,17 @@ func AddGidConfig(c *gin.Context) {
 	if game.Gid == "" {
 		//新增
 		gameNew := model.Game{
-			Gid:        form.Gid,
-			Pid:        form.Pid,
-			GameName:   form.GameName,
-			WxAppid:    form.WxAppid,
-			WxSecret:   form.WxSecret,
-			TtAppid:    form.TtAppid,
-			TtSecret:   form.TtSecret,
-			TtTplId:    form.TtTplId,
-			WxTplId:    form.WxTplId,
-			CreatedAt:  model.XTime{Time: now},
-			UpdatedAt:  model.XTime{Time: now},
-			ConfigPath: form.ConfigPath,
+			Gid:       form.Gid,
+			Pid:       form.Pid,
+			GameName:  form.GameName,
+			WxAppid:   form.WxAppid,
+			WxSecret:  form.WxSecret,
+			TtAppid:   form.TtAppid,
+			TtSecret:  form.TtSecret,
+			TtTplId:   form.TtTplId,
+			WxTplId:   form.WxTplId,
+			CreatedAt: model.XTime{Time: now},
+			UpdatedAt: model.XTime{Time: now},
 		}
 
 		err := global.App.DB.Table("game").Create(&gameNew).Error
@@ -93,18 +92,17 @@ func AddGidConfig(c *gin.Context) {
 	} else {
 		//更新
 		err := global.App.DB.Table("game").Where("gid", form.Gid).Updates(&model.Game{
-			Gid:        form.Gid,
-			Pid:        form.Pid,
-			GameName:   form.GameName,
-			WxAppid:    form.WxAppid,
-			WxSecret:   form.WxSecret,
-			TtAppid:    form.TtAppid,
-			TtSecret:   form.TtSecret,
-			TtTplId:    form.TtTplId,
-			WxTplId:    form.WxTplId,
-			ConfigPath: form.ConfigPath,
-			CreatedAt:  model.XTime{Time: now},
-			UpdatedAt:  model.XTime{Time: now},
+			Gid:       form.Gid,
+			Pid:       form.Pid,
+			GameName:  form.GameName,
+			WxAppid:   form.WxAppid,
+			WxSecret:  form.WxSecret,
+			TtAppid:   form.TtAppid,
+			TtSecret:  form.TtSecret,
+			TtTplId:   form.TtTplId,
+			WxTplId:   form.WxTplId,
+			CreatedAt: model.XTime{Time: now},
+			UpdatedAt: model.XTime{Time: now},
 		}).Error
 		if err != nil {
 			response.Fail(c, 1001, err.Error())
@@ -122,7 +120,6 @@ func AddGidConfig(c *gin.Context) {
 	gameConfigData["ttSecret"] = form.TtSecret
 	gameConfigData["ttTplId"] = form.TtTplId
 	gameConfigData["wxTplId"] = form.WxTplId
-	gameConfigData["configPath"] = form.ConfigPath
 	gidKey := config.Get("app.gid") + form.Gid
 	err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
 	if err != nil {
@@ -135,6 +132,177 @@ func AddGidConfig(c *gin.Context) {
 	})
 }
 
+// 新增游戏gid
+func AddPid(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Pid     string `form:"pid" json:"pid" binding:"required"`
+		PidName string `form:"pidName" json:"pidName" binding:"required"`
+	}{})
+
+	//查重
+	var gid model.Game
+	global.App.DB.Table("game_pid").
+		Where("pid", form.Pid).
+		Or("pidName = ? ", form.PidName).
+		First(&gid)
+
+	if gid.Gid != "" {
+		response.Fail(c, 1001, "gid 已存在")
+		return
+	}
+
+	err := global.App.DB.Table("game_pid").Create(&model.GamePid{
+		Pid:     form.Pid,
+		PidName: form.PidName,
+	}).Error
+	if err != nil {
+		response.Fail(c, 1001, "创建pid失败"+err.Error())
+		return
+	}
+
+	response.Success(c, gin.H{})
+}
+
+func DelPid(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Pid string `form:"pid" json:"pid" binding:"required"`
+	}{})
+
+	//有没有在使用
+	var count int64
+	global.App.DB.Table("game").Where("pid", form.Pid).Count(&count)
+
+	if count > 0 {
+		response.Fail(c, 1001, "gid已经使用,无法删除")
+		return
+	}
+
+	err := global.App.DB.Table("game_pid").Where("pid", form.Pid).Delete(&model.GamePid{}).Error
+	if err != nil {
+		response.Fail(c, 1001, "删除失败"+err.Error())
+		return
+	}
+
+	response.Success(c, gin.H{})
+}
+
+func PidList(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Search string `form:"search" json:"search" binding:""`
+		Offset int    `form:"offset" json:"offset" binding:""`
+		Limit  int    `form:"limit" json:"limit" binding:""`
+	}{})
+
+	query := global.App.DB.Table("game_pid")
+	if form.Search != "" {
+		query = query.Where("pid", "like", "%"+form.Search+"%").
+			Or("pidName LIKE ?", "%"+form.Search+"%")
+	}
+
+	var count int64
+	var res []model.GamePid
+
+	err := query.Count(&count).Error
+	if err != nil {
+		response.Fail(c, 1001, "查询总数失败"+err.Error())
+		return
+	}
+
+	err = query.Offset(form.Offset).Limit(form.Limit).Scan(&res).Error
+	if err != nil {
+		response.Fail(c, 1002, err.Error())
+		return
+	}
+
+	response.Success(c, gin.H{
+		"data":  res,
+		"count": count,
+	})
+
+}
+
+func PidToGidList(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Search string `form:"search" binding:""`
+		Active bool   `form:"active" binding:"" `
+	}{})
+
+	type game struct {
+		Id       int    `json:"id" gorm:"primary_key"`
+		Pid      string `json:"pid" gorm:"column:pid; "`
+		Gid      string `json:"gid" gorm:"column:gid; "`
+		GameName string `json:"gameName" gorm:"column:gameName; "`
+		PidName  string `json:"pidName" gorm:"column:pidName; "`
+	}
+
+	var gameList []game
+	query := global.App.DB.Table("game")
+
+	if form.Search != "" {
+		query = query.Where("gid", "like", "%"+form.Search+"%").
+			Or("pid LIKE ?", "%"+form.Search+"%").
+			Or("gameName LIKE ?", "%"+form.Search+"%")
+	}
+
+	//验证gid权限
+	permission := c.GetString("permission")
+	var PermissionSlice []string
+	json.Unmarshal([]byte(permission), &PermissionSlice)
+
+	if !utils.InArray("all", PermissionSlice) {
+		fmt.Println(PermissionSlice)
+		//需要验证gid权限
+		query = query.WhereIn("gid", PermissionSlice)
+	}
+
+	if form.Active == true {
+		//获取近七天有数据的gid
+		ActiveGid := service.GetActiveGid()
+		query = query.WhereIn("gid", ActiveGid)
+	}
+
+	err := query.
+		LeftJoin("game_pid", "game_pid.pid = game.pid").
+		Select("game.id", "game.gid", "game.gameName", "game.pid", "pidName").Where("id", ">", 0).Scan(&gameList).Error
+	if err != nil {
+		response.Fail(c, 1002, err.Error())
+		return
+	}
+
+	type res struct {
+		PidName string `json:"pidName" gorm:"column:pidName; "`
+		Pid     string `json:"pid" gorm:"column:pid; "`
+		GidList []game `json:"gidList" gorm:"-"`
+	}
+
+	var Res []res
+
+	var pidList []string
+
+	for _, game := range gameList {
+		if !utils.InArray(game.Pid, pidList) {
+			pidList = append(pidList, game.Pid)
+
+			Res = append(Res, res{
+				PidName: game.PidName,
+				Pid:     game.Pid,
+			})
+		}
+	}
+	for _, game1 := range gameList {
+		for k, value := range Res {
+			if value.Pid == game1.Pid {
+				Res[k].GidList = append(Res[k].GidList, game1)
+			}
+		}
+	}
+
+	response.Success(c, gin.H{
+		"data": Res,
+	})
+
+}
+
 /* 获取配置 */
 func GetGidConfig(c *gin.Context) {
 	form := request.Check(c, &struct {

+ 129 - 19
controller/v1/test.go

@@ -2,6 +2,7 @@ package v1
 
 import (
 	"context"
+	"designs/app/common/request"
 	"designs/config"
 	"designs/crons"
 	"designs/global"
@@ -234,6 +235,9 @@ func WriteDBDataToFile(c *gin.Context) {
 // 对打点数据进行汇算
 func ActionDataSummary(c *gin.Context) {
 
+	service.SeeAdsSummary("20250706")
+	return
+
 	now := time.Now()
 	for i := 1; i <= 30; i++ {
 		lastDay := now.AddDate(0, 0, -i).Format("20060102")
@@ -246,6 +250,51 @@ func ActionDataSummary(c *gin.Context) {
 	response.Success(c, gin.H{})
 }
 
+func CopyGameConfig(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Gid string `json:"gid" binding:""`
+	}{})
+
+	form.Gid = "snake"
+
+	////查询出所有的Key
+	//redisKey := "config_game:snake:" + "*"
+	//
+	//fmt.Println(redisKey)
+	//val, err := global.App.Redis.Keys(context.Background(), redisKey).Result()
+	//if err != nil {
+	//	response.Fail(c, 1001, err.Error())
+	//	return
+	//}
+	//
+	//for _, key := range val {
+	//	data, _ := global.App.Redis.Get(context.Background(), key).Result()
+	//
+	//	global.App.DB.Table("snake").Create(&map[string]interface{}{
+	//		"key":  key,
+	//		"data": data,
+	//	})
+	//}
+
+	type aaa struct {
+		Key  string `json:"key"`
+		Data string `json:"data"`
+	}
+
+	var data []aaa
+	global.App.DB.Table("snake").Scan(&data)
+
+	for _, v := range data {
+		err := global.App.Redis.Set(context.Background(), v.Key, v.Data, 0).Err()
+		if err != nil {
+			response.Fail(c, 1001, err.Error())
+			return
+		}
+	}
+
+	response.Success(c, gin.H{"data": data})
+}
+
 func BehaviorTimeSummary(c *gin.Context) {
 	//先查出所有gid
 	ActiveGid := service.GetActiveGid()
@@ -267,8 +316,8 @@ func BehaviorSummary(c *gin.Context) {
 	//先查出所有gid
 	ActiveGid := service.GetActiveGid()
 
-	now := time.Now().AddDate(0, 0, -1)
-	start := now.AddDate(0, 0, -45)
+	//now := time.Now().AddDate(0, 0, -1)
+	//start := now.AddDate(0, 0, -45)
 
 	pf := "wx"
 	//循环一下,查出userId 跟相关的在线数据
@@ -277,13 +326,13 @@ func BehaviorSummary(c *gin.Context) {
 		start1 := time.Now()
 		fmt.Println("处理数据开始,gid :", gid)
 
-		//查询 30天内的在线数据
-
-		SummaryData, err := service.UserOnlineSummary(gid, pf, "", start.Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))
-		if err != nil {
-			response.Fail(c, 1001, err.Error())
-			return
-		}
+		////查询 30天内的在线数据
+		//
+		//SummaryData, err := service.UserOnlineSummary(gid, pf, "", start.Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))
+		//if err != nil {
+		//	response.Fail(c, 1001, err.Error())
+		//	return
+		//}
 
 		//aaa, _ := json.Marshal(SummaryData)
 		//global.App.Log.Error(string(aaa))
@@ -298,7 +347,7 @@ func BehaviorSummary(c *gin.Context) {
 		}
 		loginDataMap := make(map[int]int)
 
-		err = global.App.DB.Table("user_login").
+		err := global.App.DB.Table("user_login").
 			Where("gid", gid).
 			Where("pf", pf).
 			Group("userId").
@@ -357,8 +406,8 @@ func BehaviorSummary(c *gin.Context) {
 		userBehavior := make(map[int]model.UserBehavior)
 		for _, users := range userIdList {
 			userBehavior[users.UserId] = model.UserBehavior{
-				ID:         users.ID,
-				Duration:   int(SummaryData[users.UserId]),
+				ID: users.ID,
+				//Duration:   int(SummaryData[users.UserId]),
 				StartNum:   loginDataMap[users.UserId],
 				AdCount:    seeAdsDataMap[users.UserId],
 				AdExpCount: seeAdsDataMap2[users.UserId],
@@ -371,8 +420,8 @@ func BehaviorSummary(c *gin.Context) {
 		}
 
 		err = global.App.DB.Table("user_behavior").Clauses(clause.OnConflict{
-			Columns:   []clause.Column{{Name: "id"}},                                                       // 冲突列(主键)
-			DoUpdates: clause.AssignmentColumns([]string{"duration", "startNum", "adCount", "adExpCount"}), // 需要更新的列
+			Columns:   []clause.Column{{Name: "id"}},                                           // 冲突列(主键)
+			DoUpdates: clause.AssignmentColumns([]string{"startNum", "adCount", "adExpCount"}), // 需要更新的列
 		}).CreateInBatches(&userBehaviorList, 1000).Error
 		if err != nil {
 			//global.App.Log.Error(userBehavior)
@@ -388,10 +437,71 @@ func BehaviorSummary(c *gin.Context) {
 
 func TestClickHouse(c *gin.Context) {
 
-	sql := "CREATE TABLE user_see_ads\n(\n    `pf` LowCardinality(String) COMMENT '登录路径',\n    `gid` LowCardinality(String) COMMENT '游戏ID',\n    `userId` UInt32 COMMENT '用户ID',\n    `date` Date COMMENT '日期',\n    `createdAt` DateTime DEFAULT now() COMMENT '时间',\n    `adsId` Nullable(String) COMMENT '广告ID',\n    \n    -- 修复字段声明顺序:Nullable 应包裹 LowCardinality\n    `adsType` LowCardinality(Nullable(String)) COMMENT '类型',\n    `adsScene` LowCardinality(Nullable(String)) COMMENT '广告场景',\n    \n    `adsState` Nullable(Int8) COMMENT '广告状态: 0失败 1成功 2完整观看',\n    `startTime` Nullable(DateTime) COMMENT '广告开始时间',\n    `cost` UInt32 DEFAULT 0 COMMENT '广告收益'\n)\nENGINE = MergeTree()\nPARTITION BY (toYear(date), toMonth(date))\nORDER BY (date, pf, gid, userId)\nPRIMARY KEY (date, pf, gid)\nSETTINGS \n    index_granularity = 8192,\n    index_granularity_bytes = 10485760;\n"
-	err := global.App.Clickhouse.Exec(sql).Error
+	//sql := "CREATE TABLE IF NOT EXISTS example_table (pf FixedString(10), gid FixedString(20), userId Int64) ENGINE = MergeTree() ORDER BY (pf, gid, userId);"
+	//err := global.App.Clickhouse.Exec(sql).Error
+
+	now := time.Now()
+	//for i := 100; i >= 0; i-- {
+	//	global.App.Clickhouse.Table("example_table").Create(map[string]interface{}{
+	//		"gid":    "aaa",
+	//		"pf":     "wx",
+	//		"userId": 1001,
+	//	})
+	//
+	//	fmt.Println(time.Since(now))
+	//
+	//}
+
+	type ssss struct {
+		UserId int    `json:"userId" gorm:"not null;column:userId;"`
+		Gid    string `json:"gid" gorm:"not null;column:gid;"`
+		Pf     string `json:"pf" gorm:"not null;column:pf;"`
+	}
+
+	var insert []ssss
+	for i := 100; i >= 0; i-- {
+		insert = append(insert, ssss{
+			UserId: 1001,
+			Gid:    "aaa",
+			Pf:     "wx",
+		})
+	}
+
+	global.App.Clickhouse.Table("example_table").CreateInBatches(&insert, 1000)
+	fmt.Println(time.Since(now))
+
+	//var res []struct {
+	//	UserId int    `json:"userId" gorm:"not null;column:userId;"`
+	//	Gid    string `json:"gid" gorm:"not null;column:gid;"`
+	//	Pf     string `json:"pf" gorm:"not null;column:pf;"`
+	//}
+	//global.App.Clickhouse.Table("example_table").Scan(&res)
+	//
+	//response.Success(c, gin.H{
+	//	"res": res,
+	//})
+}
+
+func TestLevel(c *gin.Context) {
+	//block := [][]string{
+	//	{
+	//		"*0", "*0",
+	//	},
+	//	{
+	//		"e0", "e0", "f0", "f0",
+	//	},
+	//	{
+	//		"*0", "*0",
+	//	},
+	//}
+	////随机移动
+
+}
+
+func move() {
+
+}
+
+func check() {
 
-	response.Success(c, gin.H{
-		"res": err,
-	})
 }

+ 36 - 92
controller/v1/user.go

@@ -11,29 +11,25 @@ import (
 	"designs/service"
 	"designs/utils"
 	"fmt"
-	"github.com/go-redis/redis/v8"
 	"go.mongodb.org/mongo-driver/v2/bson"
 	"os"
 	"path/filepath"
-	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
 )
 
 type GameConfig struct {
-	Gid       string `form:"gid" json:"gid" binding:"required"`             //游戏id
-	Pid       string `form:"pid" json:"pid" binding:""`                     //游戏pid
-	GameName  string `form:"gameName" json:"gameName" binding:"required"`   //游戏名称
-	WxAppid   string `form:"wxAppid" json:"wxAppid" binding:"required"`     //微信appid
-	WxSecret  string `form:"wxSecret" json:"wxSecret" binding:"required"`   //微信secret
-	TtAppid   string `form:"ttAppid" json:"ttAppid" binding:"required"`     //抖音appid
-	TtSecret  string `form:"ttSecret" json:"ttSecret" binding:"required"`   //抖音secret
-	AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
+	Gid       string `form:"gid" json:"gid" binding:"required"`           //游戏id
+	Pid       string `form:"pid" json:"pid" binding:"required"`           //游戏pid
+	GameName  string `form:"gameName" json:"gameName" binding:"required"` //游戏名称
+	WxAppid   string `form:"wxAppid" json:"wxAppid" binding:""`           //微信appid
+	WxSecret  string `form:"wxSecret" json:"wxSecret" binding:""`         //微信secret
+	TtAppid   string `form:"ttAppid" json:"ttAppid" binding:""`           //抖音appid
+	TtSecret  string `form:"ttSecret" json:"ttSecret" binding:""`         //抖音secret
+	AppSecret string `form:"appSecret" json:"appSecret" binding:""`       //游戏配置密钥
 	TtTplId   string `form:"ttTplId" json:"ttTplId" binding:""`
 	WxTplId   string `form:"wxTplId" json:"wxTplId" binding:""`
-
-	ConfigPath string `form:"configPath" json:"configPath" binding:""`
 }
 
 type GetGameCfg struct {
@@ -149,111 +145,59 @@ func UserList(c *gin.Context) {
 		Limit  int    `form:"limit" json:"limit" binding:"required"`
 	}{})
 
+	var users []model.User
+
 	var res []User
-	//读取表
-	userTotalKey := config.Get("app.user_total")
-	if form.Gid != "" && form.Pf != "" {
-		userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
+	var count int64
+	query := global.App.DB.Table("user").Where("gid", form.Gid).Where("pf", form.Pf)
+
+	if form.Search != "" {
+		query = query.WhereRaw(global.App.DB.Where("userId", form.Search).Or("openId", form.Search).SubQuery())
 	}
 
-	//读取黑名单
-	blackListKey := config.Get("app.black_list_table")
-	black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
+	err := query.Count(&count).Error
 	if err != nil {
-		response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
+		response.Fail(c, 1001, "查询总人数失败"+err.Error())
 		return
 	}
 
-	var data []redis.Z
-	if form.Search != "" {
-		data, _ = global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, 0, 1000000).Result()
-
-		//读取出所有的数据,然后过滤nickname
-		for _, val := range data {
-			userKey := val.Member.(string)
-			userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
-			if err != nil {
-				global.App.Log.Error("GetUserData err")
-				response.Fail(c, 1003, "GetUserData err"+err.Error())
-				return
-			}
-
-			userMsg := strings.Split(userKey, ":")
-			openId := userMsg[len(userMsg)-1]
-			gid := userMsg[0]
-			pf := userMsg[1]
-
-			if userData["nickname"] == form.Search || openId == form.Search {
-
-				//查看是否在黑名单
-				var inBlack bool
-				if utils.InArray(userKey, black) {
-					inBlack = true
-				}
-
-				//查看是否加了权限
-				optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
-				option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
-				if err != nil {
-					response.Fail(c, 1003, err.Error())
-					return
-				}
-
-				res = append(res, User{
-					UserId:   userData["userId"],
-					Gid:      userData["gid"],
-					Pf:       userData["pf"],
-					NickName: userData["nickName"],
-					Head:     userData["head"],
-					OpenId:   openId,
-					InBlack:  inBlack,
-					Option:   option["option"],
-				})
-			}
-
-		}
-
-		response.Success(c, gin.H{
-			"data":  res,
-			"count": len(res),
-		})
+	err = query.Offset(form.Offset).Limit(form.Limit).Scan(&users).Error
+	if err != nil {
+		response.Fail(c, 1001, "查询数据失败"+err.Error())
 		return
-	} else {
-		data, _ = global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
-
 	}
 
-	count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
+	//读取黑名单
+	blackListKey := config.Get("app.black_list_table")
+	black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
 	if err != nil {
-		response.Fail(c, 1001, err.Error())
+		response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
 		return
 	}
 
-	for _, val := range data {
-		userKey := val.Member.(string)
-		userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
+	for _, user := range users {
+		gid := user.Gid
+		pf := user.Pf
+		openId := user.OpenId
+		//查看是否加了权限
+		optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
+		option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
 		if err != nil {
-			global.App.Log.Error("GetUserData err")
-			response.Fail(c, 1003, "GetUserData err"+err.Error())
+			response.Fail(c, 1003, err.Error())
 			return
 		}
 
 		//查看是否在黑名单
 		var inBlack bool
+		userKey := gid + ":" + pf + ":" + config.Get("app.user_table_key") + openId
 		if utils.InArray(userKey, black) {
 			inBlack = true
 		}
 
-		userMsg := strings.Split(userKey, ":")
-		openId := userMsg[len(userMsg)-1]
-		gid := userMsg[0]
-		pf := userMsg[1]
-
-		//查看是否加了权限
-		optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
-		option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
+		userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
 		if err != nil {
-			response.Fail(c, 1003, err.Error())
+			global.App.Log.Error("GetUserData err")
+			response.Fail(c, 1003, "GetUserData err"+err.Error())
 			return
 		}
 

+ 4 - 3
controller/v1/userBehavior.go

@@ -1231,9 +1231,10 @@ func SplitOnlineData(c *gin.Context) {
 
 				//fmt.Println(LogTime, lineData[2])
 				onlineData = append(onlineData, model.UserOnlineSplit{
-					Pf:      pf,
-					UserId:  userId,
-					Type:    types,
+					Pf:     pf,
+					UserId: userId,
+					Type:   types,
+
 					LogTime: LogTime,
 				})
 			}

+ 0 - 10
crons/userAction.go

@@ -57,16 +57,6 @@ func OnlineDatabaseDelete() {
 	global.App.Log.Info(date, "数据表清理完成", tableList)
 }
 
-//// 对留存数据进行汇算
-//func RemainDataSummary() {
-//	//计算上一日的留存
-//	lastDay := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
-//
-//	//读取到所有的gid ,根据GID计算这些数据
-//
-//
-//}
-
 //对广告数据进行汇算
 
 func AdsDataSummary() {

+ 59 - 5
crons/userRelated.go

@@ -33,13 +33,64 @@ func BehaviorSummary(pf string, ActiveGid []string, start time.Time, end time.Ti
 		fmt.Println("处理数据开始,gid :", gid)
 
 		//查询 30天内的在线数据
-
 		SummaryData, err := service.UserOnlineSummary(gid, pf, "", start.Format("2006-01-02 15:04:05"), end.Format("2006-01-02 15:04:05"))
 		if err != nil {
 			global.App.Log.Error("更新用户在线时长错误(查询汇总信息错误)", err.Error())
 			return
 		}
 
+		//查询用户的登录次数
+		var loginData []struct {
+			UserId     int `json:"userId" gorm:"not null;column:userId;"`
+			LoginCount int `json:"loginCount" gorm:"not null;column:loginCount;"`
+		}
+		loginDataMap := make(map[int]int)
+
+		err = global.App.DB.Table("user_login").
+			Where("gid", gid).
+			Where("pf", pf).
+			Group("userId").
+			Select("count(*) as loginCount", "userId").
+			Scan(&loginData).Error
+		if err != nil {
+			global.App.Log.Error("更新用户在线时长错误(查询汇总信息错误)", err.Error())
+
+			return
+		}
+
+		for _, login := range loginData {
+			loginDataMap[login.UserId] = login.LoginCount
+		}
+
+		fmt.Println("查询登录数据完成:", time.Since(start1))
+
+		//查询用户的看广告次数,广告看完次数
+		var seeAdsData []struct {
+			UserId      int `json:"userId" gorm:"not null;column:userId;"`
+			SeeAdsCount int `json:"seeAdsCount" gorm:"not null;column:seeAdsCount;"`
+			State2Count int `json:"state2Count" gorm:"not null;column:state2Count;"`
+		}
+
+		seeAdsDataMap := make(map[int]int)
+		seeAdsDataMap2 := make(map[int]int)
+		err = global.App.DB.Table("user_see_ads").
+			Where("gid", gid).
+			Where("pf", pf).
+			Group("userId").
+			Select("count(*) as seeAdsCount", "SUM(CASE WHEN adsState = 2 THEN 1 ELSE 0 END) AS state2Count", "userId").
+			Scan(&seeAdsData).Error
+		if err != nil {
+			global.App.Log.Error("更新用户在线时长错误(查询汇总信息错误)", err.Error())
+			return
+		}
+
+		for _, ads := range seeAdsData {
+			seeAdsDataMap[ads.UserId] = ads.SeeAdsCount
+			seeAdsDataMap2[ads.UserId] = ads.State2Count
+		}
+
+		fmt.Println("查询广告数据完成:", time.Since(start1))
+
 		//格式化,存入数据库
 		var userIdList []model.User
 		err = global.App.DB.Table("user").Where("gid", gid).
@@ -55,8 +106,11 @@ func BehaviorSummary(pf string, ActiveGid []string, start time.Time, end time.Ti
 		userBehavior := make(map[int]model.UserBehavior)
 		for _, users := range userIdList {
 			userBehavior[users.UserId] = model.UserBehavior{
-				ID:       users.ID,
-				Duration: int(SummaryData[users.UserId]),
+				ID:         users.ID,
+				Duration:   int(SummaryData[users.UserId]),
+				StartNum:   loginDataMap[users.UserId],
+				AdCount:    seeAdsDataMap[users.UserId],
+				AdExpCount: seeAdsDataMap2[users.UserId],
 			}
 		}
 
@@ -66,8 +120,8 @@ func BehaviorSummary(pf string, ActiveGid []string, start time.Time, end time.Ti
 		}
 
 		err = global.App.DB.Table("user_behavior").Clauses(clause.OnConflict{
-			Columns:   []clause.Column{{Name: "id"}},                  // 冲突列(主键)
-			DoUpdates: clause.AssignmentColumns([]string{"duration"}), // 需要更新的列
+			Columns:   []clause.Column{{Name: "id"}},                                                       // 冲突列(主键)
+			DoUpdates: clause.AssignmentColumns([]string{"duration", "startNum", "adCount", "adExpCount"}), // 需要更新的列
 		}).CreateInBatches(&userBehaviorList, 1000).Error
 		if err != nil {
 			global.App.Log.Error("更新用户在线时长错误", err.Error())

+ 17 - 13
model/game.go

@@ -1,17 +1,21 @@
 package model
 
 type Game struct {
-	Id         int    `json:"id" gorm:"primary_key"`
-	Pid        string `json:"pid" gorm:"column:pid; "`
-	Gid        string `json:"gid" gorm:"column:gid; "`
-	GameName   string `json:"gameName" gorm:"column:gameName; "`
-	WxAppid    string `json:"wxAppid" gorm:"column:wxAppid; "`
-	WxSecret   string `json:"wxSecret" gorm:"column:wxSecret; "`
-	TtAppid    string `json:"ttAppid" gorm:"column:ttAppid; "`
-	TtSecret   string `json:"ttSecret" gorm:"column:ttSecret; "`
-	TtTplId    string `json:"ttTplId" gorm:"column:ttTplId; "`
-	WxTplId    string `json:"wxTplId" gorm:"column:wxTplId; "`
-	ConfigPath string `json:"configPath" gorm:"column:configPath; "`
-	CreatedAt  XTime  `json:"createdAt" gorm:"column:createdAt; "`
-	UpdatedAt  XTime  `json:"updatedAt" gorm:"column:updatedAt; "`
+	Id        int    `json:"id" gorm:"primary_key"`
+	Pid       string `json:"pid" gorm:"column:pid; "`
+	Gid       string `json:"gid" gorm:"column:gid; "`
+	GameName  string `json:"gameName" gorm:"column:gameName; "`
+	WxAppid   string `json:"wxAppid" gorm:"column:wxAppid; "`
+	WxSecret  string `json:"wxSecret" gorm:"column:wxSecret; "`
+	TtAppid   string `json:"ttAppid" gorm:"column:ttAppid; "`
+	TtSecret  string `json:"ttSecret" gorm:"column:ttSecret; "`
+	TtTplId   string `json:"ttTplId" gorm:"column:ttTplId; "`
+	WxTplId   string `json:"wxTplId" gorm:"column:wxTplId; "`
+	CreatedAt XTime  `json:"createdAt" gorm:"column:createdAt; "`
+	UpdatedAt XTime  `json:"updatedAt" gorm:"column:updatedAt; "`
+}
+
+type GamePid struct {
+	Pid     string `json:"pid" gorm:"column:pid; "`
+	PidName string `json:"pidName" gorm:"column:pidName; "`
 }

+ 6 - 2
route/api.go

@@ -22,6 +22,10 @@ func SetApiGroupRoutes(router *gin.RouterGroup) {
 	GroupV1.Use(middleware.TokenAuthMiddleware()).Use(middleware.CheckAuth()).Use()
 	{
 		GroupV1.POST("/user/gidList", v1.GidList)
+		GroupV1.POST("/user/addPid", v1.AddPid)
+		GroupV1.POST("/user/delPid", v1.DelPid)
+		GroupV1.POST("/user/pidList", v1.PidList)
+		GroupV1.POST("/user/pidToGidList", v1.PidToGidList)
 
 		GroupV1.POST("/user/addUserToBlackList", v1.AddUserToBlackList)
 		GroupV1.POST("/user/deleteUserToBlackList", v1.DeleteUserFormBlackList)
@@ -111,8 +115,8 @@ func SetApiGroupRoutes(router *gin.RouterGroup) {
 	router.POST("/OnlineToFile", v1.OnlineToFile)                   //测试接口
 	router.POST("/SplitOnlineData", v1.SplitOnlineData)             //迁移数据到online拆分表
 	router.POST("/InitGidConfig", v1.InitGidConfig)
-	//router.POST("/test", v1.ActionDataSummary)
-	router.POST("/test", v1.BehaviorTimeSummary)
+	//router.POST("/test", v1.CopyGameConfig)
+	//router.POST("/test", v1.BehaviorSummary)
 	router.POST("/TestClickHouse", v1.TestClickHouse)
 
 }

+ 1 - 0
service/userSeeAds.go

@@ -98,6 +98,7 @@ func SeeAdsSummary(lastDay string) {
 		fmt.Println(err.Error())
 		return
 	}
+
 }
 
 func GetTodayAdsSummary(gid string, pf []string) (count, todayCount int64, err error) {