package v1

import (
	"context"
	"designs/app/common/request"
	"designs/app/common/response"
	"designs/global"
	"designs/service"
	"designs/utils"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
	"math"
	"math/big"
	"time"
)

// 总览
func Summary(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid string `form:"gid" json:"gid" binding:"required"`
		Pf  string `form:"pf" json:"pf" binding:"required"`
	}{})

	//查询用户总数
	var userCount int64
	err := global.App.DB.Table("user").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Count(&userCount).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//查询近七日活跃总数
	now := time.Now()
	sevenDayAgo := now.AddDate(0, 0, -7)
	thirtyDayAgo := now.AddDate(0, 0, -30)

	var activeUserCount7 int64
	err = global.App.DB.Table("user_login").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Where("loginTime", ">=", sevenDayAgo).
		Where("loginTime", "<=", now).
		Distinct("userId").
		Count(&activeUserCount7).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	var activeUserCount30 int64
	err = global.App.DB.Table("user_login").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Where("loginTime", ">=", thirtyDayAgo).
		Where("loginTime", "<=", now).
		Distinct("userId").
		Count(&activeUserCount30).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//从redis中读取缓存
	var avgTimeString string
	avgTimeKey := fmt.Sprintf("%s|%s|avgTime", form.Gid, form.Pf)
	avgTimeString, _ = global.App.Redis.Get(context.Background(), avgTimeKey).Result()
	if avgTimeString == "" {
		//查询 近7日单设备日均使用时长
		res, err := service.UserOnlineSummary(form.Gid, form.Pf, "", sevenDayAgo.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
		}
		var avgTime int
		for _, v := range res {
			avgTime = avgTime + int(v)
		}

		if avgTime != 0 {
			avgTime = int(math.Round(float64(avgTime / len(res))))

			avgTimeString = utils.TimeStampToMDS(avgTime)
		} else {
			avgTimeString = "00.00"
		}
		global.App.Redis.Set(context.Background(), avgTimeKey, avgTimeString, time.Second*3000)
	}
	response.Success(c, gin.H{
		"data": map[string]interface{}{
			"userCount":            userCount,
			"activeUserCount7":     activeUserCount7,
			"activeUserCount30":    activeUserCount30,
			"activeUserCount7Time": avgTimeString,
		},
	})
}

// 时段分布
func TimeDistributionData(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid  string `form:"gid" json:"gid" binding:"required"`
		Pf   string `form:"pf" json:"pf" binding:"required"`
		Type int    `form:"type" json:"type" binding:"required"`
	}{})

	var data interface{}
	if form.Type == 1 {
		//新增用户
		todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetRegisterTimeDistribution(form.Pf, form.Gid)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}

		data = map[string]interface{}{
			"today":                  todayTimeDistribution,
			"yesterday":              yesterdayTimeDistribution,
			"yesterdayCount":         yesterdayCount,
			"yesterdayThisTimeCount": yesterdayThisTimeCount,
			"todayCount":             todayCount,
		}
	} else if form.Type == 2 {
		//活跃设备
		todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActiveTimeDistribution(form.Pf, form.Gid)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data = map[string]interface{}{
			"today":                  todayTimeDistribution,
			"yesterday":              yesterdayTimeDistribution,
			"yesterdayCount":         yesterdayCount,
			"yesterdayThisTimeCount": yesterdayThisTimeCount,
			"todayCount":             todayCount,
		}
	} else if form.Type == 3 {
		//启动次数
		todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActionDistribution(form.Pf, form.Gid)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data = map[string]interface{}{
			"today":                  todayTimeDistribution,
			"yesterday":              yesterdayTimeDistribution,
			"yesterdayCount":         yesterdayCount,
			"yesterdayThisTimeCount": yesterdayThisTimeCount,
			"todayCount":             todayCount,
		}

	} else {
		response.Fail(c, 1003, "type错误")
		return
	}

	response.Success(c, gin.H{
		"data": data,
	})
}

// 30日趋势
func MouthDistributionData(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid  string `form:"gid" json:"gid" binding:"required"`
		Pf   string `form:"pf" json:"pf" binding:"required"`
		Type int    `form:"type" json:"type" binding:"required"`
	}{})
	now := time.Now()
	EndTime := now.AddDate(0, 0, 1).Format("2006-01-02")
	StartTime := now.AddDate(0, 0, -29).Format("2006-01-02")

	data := make(map[string]interface{})
	if form.Type == 1 {
		//查询新增设备趋势图
		TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["timeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg

	} else if form.Type == 2 {
		//查询活跃用户趋势图
		TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["timeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 3 {
		//查询启动次数
		TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["timeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg

	} else if form.Type == 4 {
		//查询单用户使用时长
		TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["timeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 5 {
		//查询用户留存率
		//todo

	} else {
		response.Fail(c, 1003, "type错误")
		return
	}

	response.Success(c, gin.H{
		"data": data,
	})
}

// 用户趋势 总览
func UserTrendsOverview(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid       string `form:"gid" json:"gid" binding:"required"`
		Pf        string `form:"pf" json:"pf" binding:"required"`
		StartTime string `form:"startTime" json:"startTime" binding:"required"`
		EndTime   string `form:"endTime" json:"endTime" binding:"required"`
	}{})

	//查询用户新增
	var registerCount int64
	err := global.App.DB.Table("user").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Where("createdAt", ">=", form.StartTime).
		Where("createdAt", "<=", form.EndTime).
		Count(&registerCount).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//查询活跃设备
	var activeCount int64
	err = global.App.DB.Table("user_online").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Where("logTime", ">=", form.StartTime).
		Where("logTime", "<=", form.EndTime).
		Distinct("userId").Count(&activeCount).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//查询启动次数
	var loginCount int64
	err = global.App.DB.Table("user_login").
		Where("gid", form.Gid).
		Where("pf", form.Pf).
		Where("loginTime", ">=", form.StartTime).
		Where("loginTime", "<=", form.EndTime).
		Count(&loginCount).Error
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	//查询平均启动时长
	//查询活跃用户月趋势图
	_, _, activeTime, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//查询 DAU/MAU
	//todo 查询方式需要更新
	dauMau := 0.3

	response.Success(c, gin.H{
		"data": map[string]interface{}{
			"registerCount": registerCount,
			"activeCount":   activeCount,
			"loginCount":    loginCount,
			"activeTime":    activeTime,
			"dauMau":        dauMau,
		},
	})
}

// 数据趋势
func DataTrades(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid       string `form:"gid" json:"gid" binding:"required"`
		Pf        string `form:"pf" json:"pf" binding:"required"`
		StartTime string `form:"startTime" json:"startTime" binding:"required"`
		EndTime   string `form:"endTime" json:"endTime" binding:"required"`
		Type      int    `form:"type" json:"type" binding:"required"`
	}{})
	data := make(map[string]interface{})

	form.EndTime = form.EndTime + " 23:59:59"

	if form.Type == 1 {
		//查询新增设备趋势图
		TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 2 {
		//查询活跃用户趋势图
		TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 3 {
		//查询活跃用户周趋势图
		TimeDistribution, count, avg, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg

	} else if form.Type == 4 {
		//查询活跃用户月趋势图
		TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 5 {
		//查询启动次数
		TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg
	} else if form.Type == 6 {
		//查询平均启动时间
		TimeDistribution, count, avg, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
		data["imeDistribution"] = TimeDistribution
		data["count"] = count
		data["avg"] = avg

	} else {
		response.Fail(c, 1003, "type 错误")
		return
	}

	response.Success(c, gin.H{
		"data": data,
	})
}

// 数据趋势的整合
func DataTradesDetail(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid       string `form:"gid" json:"gid" binding:"required"`
		Pf        string `form:"pf" json:"pf" binding:"required"`
		StartTime string `form:"startTime" json:"startTime" binding:"required"`
		EndTime   string `form:"endTime" json:"endTime" binding:"required"`
	}{})
	form.EndTime = form.EndTime + " 23:59:59"
	type dayData struct {
		NewUser         int `json:"newUser"`
		ActiveUser      int `json:"activeUser"`
		ActiveUserWeek  int `json:"activeUserWeek"`
		ActiveUserMouth int `json:"activeUserMouth"`
		ActiveStart     int `json:"activeStart"`
		AvgTime         int `json:"avgTime"`
	}
	var data = make(map[string]dayData)

	//查询新增设备趋势图
	NewUser, _, _, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	//查询活跃用户趋势图
	ActiveUser, _, _, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	//查询活跃用户周趋势图
	ActiveUserWeek, _, _, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	//查询活跃用户月趋势图
	ActiveUserMouth, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	//查询启动次数
	ActiveStart, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	//查询平均启动时间
	AvgTime, _, _, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}
	for k := range AvgTime {
		data[k] = dayData{
			NewUser:         NewUser[k],
			ActiveUser:      ActiveUser[k],
			ActiveUserWeek:  ActiveUserWeek[k],
			ActiveUserMouth: ActiveUserMouth[k],
			ActiveStart:     ActiveStart[k],
			AvgTime:         AvgTime[k],
		}
	}

	response.Success(c, gin.H{
		"data": data,
	})

}

func RemainDataBydDay(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid       string `form:"gid" json:"gid" binding:"required"`
		Pf        string `form:"pf" json:"pf" binding:"required"`
		StartTime string `form:"startTime" json:"startTime" binding:"required"`
		EndTime   string `form:"endTime" json:"endTime" binding:"required"`
		Type      int    `form:"type" json:"type" binding:"required"`
	}{})

	data, err := service.RemainDataBydDay(form.Type, form.Pf, form.Gid, form.StartTime, form.EndTime)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	response.Success(c, gin.H{
		"data": data,
	})
}

type AdData struct {
	Pid           string `json:"pid"`
	Aid           string `json:"aid"`
	Cid           string `json:"cid"`
	ReportUrl     string `json:"reportUrl"`
	Reported      bool   `json:"reported"`
	Duration      int64  `json:"duration"`
	AdReqCount    uint8  `json:"adReqCount"`
	AdEposedcount uint8  `json:"adEposedcount"`
	CreateTime    int    `json:"createTime"`
}

type UserBehavior struct {
	Id                 string `bson:"_id,omitempty"`
	Gid                string `bson:"gid" json:"gid"`
	Pf                 string `bson:"pf" json:"pf"`
	OpenId             string `bson:"openId" json:"openId"`
	RelatedAid         int64  `bson:"relatedAid" json:"relatedAid"` //目前关联的aid
	TotalDuration      int    `bson:"totalDuration" json:"totalDuration"`
	TotalAdReqCount    int    `bson:"totalAdReqCount" json:"totalAdReqCount"`
	TotalAdEposedCount int    `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
	CreateDate         string `bson:"createDate" json:"createDate"`
	CreateTime         int    `json:"createTime" bson:"createTime"`
	StartNum           int    `bson:"startNum" json:"startNum"`

	ActiveStatus     bool              `bson:"activeStatus" json:"activeStatus"`         //激活状态
	ConversionStatus bool              `bson:"conversionStatus" json:"conversionStatus"` //转化状态
	RemainData       map[string]string `json:"remainData" bson:"remainData"`             //留存数据
}
type AdRelated struct {
	Id         string `bson:"_id" json:"_id"`
	UserId     string `bson:"userId" json:"userId"`
	Aid        int64  `json:"aid" bson:"aid"`                //广告的推广计划id,即广告ID，广告平台配置落地页参数
	Cid        string `json:"cid" bson:"cid"`                //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
	Pid        int64  `json:"pid" bson:"pid"`                //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
	CreateTime int64  `bson:"create_time" json:"createTime"` //当前计划的创建时间

	StartNum int     `bson:"startNum" json:"startNum"`   //启动次数
	Revenue  float32 `bson:"revenue" json:"revenue"`     //当日预估收益
	Duration int64   `bson:"duration" json:"duration"`   //当日在线时长
	ReqCount int     `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
	ExpCount int     `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
}
type BehaviorFilter struct {
	Gt  int `json:"gt"`
	Gte int `json:"gte"`
	Lte int `json:"lte"`
	Lt  int `json:"lt"`
	Ne  int `json:"ne"`
	//$gt 大于
	//$gte：大于或等于
	//$lt：小于
	//$lte：小于或等于
	//$ne：不等于
}

func BehaviorList(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid    string `form:"gid" json:"gid" binding:"required"`
		Pf     string `form:"pf" json:"pf" binding:"required"`
		OpenId string `form:"openId" json:"openId" binding:""`
		Offset int    `form:"offset" json:"offset" binding:""`
		Limit  int    `form:"limit" json:"limit" binding:""`

		ActiveStatus       string      `form:"activeStatus" json:"activeStatus" binding:""`         //all true false
		ConversionStatus   string      `form:"conversionStatus" json:"conversionStatus" binding:""` //all true false
		AdFromCount        interface{} `form:"adFromCount" json:"adFromCount" binding:""`
		TotalDuration      interface{} `form:"totalDuration" json:"totalDuration" binding:""`
		TotalAdReqCount    interface{} `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
		TotalAdEposedCount interface{} `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
		CreateTime         interface{} `form:"createTime" json:"createTime" binding:""`
		StartNum           interface{} `form:"startNum" json:"startNum" binding:""`
	}{})

	collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")

	ctx := context.Background()
	filter := bson.M{"gid": form.Gid}

	if form.Pf != "" {
		filter["pf"] = form.Pf
	}

	if form.OpenId != "" {
		filter["openId"] = bson.M{"$regex": form.OpenId}
	}

	if form.ActiveStatus == "true" {
		filter["activeStatus"] = true
	} else if form.ActiveStatus == "false" {
		filter["activeStatus"] = false
	}

	if form.ActiveStatus == "true" {
		filter["activeStatus"] = true
	} else if form.ActiveStatus == "false" {
		filter["activeStatus"] = false
	}

	if form.AdFromCount != nil {
		fmt.Println(form.AdFromCount)
		marsh, _ := json.Marshal(form.AdFromCount)
		var adFromCount BehaviorFilter
		json.Unmarshal(marsh, &adFromCount)

		filters := bson.M{}
		if adFromCount.Gt != 0 {
			filters["$gt"] = adFromCount.Gt
		}
		if adFromCount.Gte != 0 {
			filters["$gte"] = adFromCount.Gte
		}
		if adFromCount.Lte != 0 {
			filters["$lte"] = adFromCount.Lte
		}
		if adFromCount.Lt != 0 {
			filters["$lt"] = adFromCount.Lt
		}
		if adFromCount.Ne != 0 {
			filters["$ne"] = adFromCount.Ne
		}

		if len(filters) > 0 {
			filter["adFromCount"] = filters
		}
	}

	if form.TotalAdReqCount != nil {
		marsh, _ := json.Marshal(form.TotalAdReqCount)
		var totalAdReqCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdReqCount)

		filters := bson.M{}
		if totalAdReqCount.Gt != 0 {
			filters["$gt"] = totalAdReqCount.Gt
		}
		if totalAdReqCount.Gte != 0 {
			filters["$gte"] = totalAdReqCount.Gte
		}
		if totalAdReqCount.Lte != 0 {
			filters["$lte"] = totalAdReqCount.Lte
		}
		if totalAdReqCount.Lt != 0 {
			filters["$lt"] = totalAdReqCount.Lt
		}
		if totalAdReqCount.Ne != 0 {
			filters["$ne"] = totalAdReqCount.Ne
		}

		if len(filters) > 0 {
			filter["totalAdReqCount"] = filters
		}
	}

	if form.TotalAdEposedCount != nil {
		marsh, _ := json.Marshal(form.TotalAdEposedCount)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["totalAdEposedCount"] = filters
		}
	}

	if form.CreateTime != nil {
		marsh, _ := json.Marshal(form.CreateTime)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["createTime"] = filters
		}
	}

	if form.StartNum != nil {
		marsh, _ := json.Marshal(form.StartNum)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["startNum"] = filters
		}
	}

	if form.TotalDuration != nil {
		marsh, _ := json.Marshal(form.TotalDuration)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["totalDuration"] = filters
		}
	}

	option := options.Find()
	option.SetLimit(int64(form.Limit))
	option.SetSkip(int64(form.Offset))

	cur, err := collection.Find(ctx, filter, option)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	count, err := collection.CountDocuments(ctx, filter)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	var data []UserBehavior
	err = cur.All(ctx, &data)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	response.Success(c, gin.H{
		"data":  data,
		"count": count,
	})
}

func AdRelatedList(c *gin.Context) {
	form := request.Check(c, &struct {
		//Gid    string `form:"gid" json:"gid" binding:"required"`
		//Pf     string `form:"pf" json:"pf" binding:"required"`
		//OpenId string `form:"search" json:"search" binding:""`
		Offset int `form:"offset" json:"offset" binding:""`
		Limit  int `form:"limit" json:"limit" binding:""`

		Pid int    `form:"pid" json:"pid" binding:""`
		Aid int    `form:"aid" json:"aid" binding:""`
		Cid string `form:"cid" json:"cid" binding:""`

		CreateTime interface{} `form:"createTime" json:"createTime" binding:""`
		StartNum   interface{} `form:"startNum" json:"startNum" binding:""`
		Revenue    interface{} `form:"revenue" json:"revenue" binding:""`
		Duration   interface{} `form:"duration" json:"duration" binding:""`
		ReqCount   interface{} `form:"reqCount" json:"reqCount" binding:""`
		ExpCount   interface{} `form:"expCount" json:"expCount" binding:""`
	}{})

	collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")

	ctx := context.Background()
	filter := bson.M{}
	if form.Pid != 0 {
		filter["pid"] = form.Pid
	}
	if form.Aid != 0 {
		filter["aid"] = form.Aid
	}
	if form.Cid != "" {
		filter["cid"] = form.Cid
	}

	if form.CreateTime != nil {
		marsh, _ := json.Marshal(form.CreateTime)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["createTime"] = filters
		}
	}

	if form.StartNum != nil {
		marsh, _ := json.Marshal(form.StartNum)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["startNum"] = filters
		}
	}

	if form.Revenue != nil {
		marsh, _ := json.Marshal(form.Revenue)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["revenue"] = filters
		}
	}

	if form.Duration != nil {
		marsh, _ := json.Marshal(form.Duration)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["duration"] = filters
		}
	}

	if form.ReqCount != nil {
		marsh, _ := json.Marshal(form.ReqCount)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["reqCount"] = filters
		}
	}

	if form.ExpCount != nil {
		marsh, _ := json.Marshal(form.ExpCount)
		var totalAdEposedCount BehaviorFilter
		json.Unmarshal(marsh, &totalAdEposedCount)

		filters := bson.M{}
		if totalAdEposedCount.Gt != 0 {
			filters["$gt"] = totalAdEposedCount.Gt
		}
		if totalAdEposedCount.Gte != 0 {
			filters["$gte"] = totalAdEposedCount.Gte
		}
		if totalAdEposedCount.Lte != 0 {
			filters["$lte"] = totalAdEposedCount.Lte
		}
		if totalAdEposedCount.Lt != 0 {
			filters["$lt"] = totalAdEposedCount.Lt
		}
		if totalAdEposedCount.Ne != 0 {
			filters["$ne"] = totalAdEposedCount.Ne
		}

		if len(filters) > 0 {
			filter["expCount"] = filters
		}
	}

	option := options.Find()
	option.SetLimit(int64(form.Limit))
	option.SetSkip(int64(form.Offset))

	cur, err := collection.Find(ctx, filter, option)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	count, err := collection.CountDocuments(ctx, filter)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	var data []AdRelated
	err = cur.All(ctx, &data)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	response.Success(c, gin.H{
		"data":  data,
		"count": count,
	})
}

// ConversionCondition 转化条件
type ConversionCondition struct {
	Id               string   `bson:"_id" json:"id"`
	Gid              string   `bson:"gid" json:"gid"`
	Pid              *big.Int `bson:"pid" json:"pid"`
	Aid              *big.Int `bson:"aid" json:"aid"`
	Type             string   `bson:"type"  json:"type"`
	StartNum         int      `bson:"start_num" json:"start_num"` //启动次数
	EstimatedRevenue float32  `bson:"revenue" json:"revenue"`     //当日预估收益
	Duration         int64    `bson:"duration" json:"duration"`   //当日在线时长
	ReqRewardedAd    int      `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
	ExpRewardedAd    int      `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
}

func SetGameCondition(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid              string  `form:"gid" json:"gid" binding:"required"`
		Pid              string  `form:"pid" json:"pid" binding:""`
		Aid              string  `form:"aid" json:"aid" binding:""`
		Type             string  `form:"type" json:"type" binding:"required"`
		StartNum         int     `form:"start_num" json:"start_num" binding:""`
		EstimatedRevenue float32 `form:"revenue" json:"revenue" binding:""`
		Duration         int64   `form:"duration" json:"duration" binding:""`
		ReqRewardedAd    int     `form:"req_count" json:"req_count" binding:""`
		ExpRewardedAd    int     `form:"exp_count" json:"exp_count" binding:""`
	}{})

	id := fmt.Sprintf("%s|%s|%s|%s", form.Gid, form.Pid, form.Aid, form.Type)

	PidInt := new(big.Int)
	PidInt.SetString(form.Pid, 10)
	AidInt := new(big.Int)
	AidInt.SetString(form.Aid, 10)

	collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")
	filter := bson.M{"_id": id}

	var conversionCondition ConversionCondition
	err := collection.FindOne(context.TODO(), filter).Decode(&conversionCondition)
	//if err != nil {
	//	response.Fail(c, 1002, err.Error())
	//	return
	//}

	if conversionCondition.Id != "" {
		//存在，更新
		update := bson.M{
			"$set": struct {
				StartNum         int     `bson:"start_num"` //启动次数
				EstimatedRevenue float32 `bson:"revenue"`   //当日预估收益
				Duration         int64   `bson:"duration"`  //当日在线时长
				ReqRewardedAd    int     `bson:"req_count"` //当日的激励视频广告请求次数
				ExpRewardedAd    int     `bson:"exp_count"` //当日的激励视频广告曝光次数
			}{
				StartNum:         form.StartNum,
				EstimatedRevenue: form.EstimatedRevenue,
				Duration:         form.Duration,
				ReqRewardedAd:    form.ReqRewardedAd,
				ExpRewardedAd:    form.ExpRewardedAd,
			},
		}
		_, err = collection.UpdateOne(context.TODO(), filter, update)
		if err != nil {
			response.Fail(c, 1003, err.Error())
			return
		}
	} else {
		//不存在，新增
		insert := ConversionCondition{
			Id:               id,
			Pid:              PidInt,
			Aid:              AidInt,
			Gid:              form.Gid,
			Type:             form.Type,
			StartNum:         form.StartNum,
			EstimatedRevenue: form.EstimatedRevenue,
			Duration:         form.Duration,
			ReqRewardedAd:    form.ReqRewardedAd,
			ExpRewardedAd:    form.ExpRewardedAd,
		}

		_, err = collection.InsertOne(context.TODO(), insert)
		if err != nil {
			response.Fail(c, 1001, err.Error())
			return
		}
	}

	response.Success(c, gin.H{})
}

func GameConditionList(c *gin.Context) {
	form := request.Check(c, &struct {
		Gid    string `form:"gid" json:"gid" binding:"required"`
		Pid    int64  `form:"pid" json:"pid" binding:""`
		Aid    int64  `form:"aid" json:"aid" binding:""`
		Type   string `form:"type" json:"type" binding:""`
		Offset int    `form:"offset" json:"offset" binding:""`
		Limit  int    `form:"limit" json:"limit" binding:"required"`
	}{})

	collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")

	filter := bson.M{"gid": form.Gid, "type": form.Type}
	if form.Type != "" {
		filter["type"] = form.Type
	}
	if form.Pid != 0 {
		filter["pid"] = form.Pid
	}
	if form.Aid != 0 {
		filter["aid"] = form.Aid
	}

	ctx := context.Background()
	option := options.Find()
	option.SetLimit(int64(form.Limit))
	option.SetSkip(int64(form.Offset))

	cur, err := collection.Find(ctx, filter, option)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	count, err := collection.CountDocuments(ctx, filter)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	var data []ConversionCondition
	err = cur.All(ctx, &data)
	if err != nil {
		response.Fail(c, 1001, err.Error())
		return
	}

	response.Success(c, gin.H{
		"data":  data,
		"count": count,
	})
}
