package activitymanage

import (
	"errors"
	"github.com/codinl/go-logger"
	"github.com/gin-gonic/gin"
	"go-admin/app/admin/models"
	orm "go-admin/common/global"
	"go-admin/tools/app"
	"net/http"
	"time"
)

func UserCouponList(c *gin.Context) {
	req := models.UserCouponReq{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	list, count, err := req.List()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}

	ret := map[string]interface{}{
		"count":     count,
		"list":      list,
		"pageIndex": req.Page,
	}
	app.OK(c, ret, "")
	return
}

func RenewalConfigInfo(c *gin.Context) {
	//req := models.UserCouponReq{}
	//if c.ShouldBindJSON(&req) != nil {
	//	logger.Errorf("para err")
	//	app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
	//	return
	//}

	info, err := models.ActivityRenewalConfigInfo()
	if err != nil {
		logger.Error("activity renewal config err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}

	ret := map[string]interface{}{
		"info": info,
	}
	app.OK(c, ret, "")
	return
}

func RenewalConfigUpdate(c *gin.Context) {
	req := models.ActivityRenewalConfig{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	info, err := models.ActivityRenewalConfigUpdate(req)
	if err != nil {
		logger.Error("activity renewal config err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}

	ret := map[string]interface{}{
		"info": info,
	}
	app.OK(c, ret, "")
	return
}

func UserMemberRenewalStatistic(c *gin.Context) {
	req := struct {
		StartTime time.Time `json:"start_time"`
		EndTime   time.Time `json:"end_time"`
	}{}
	if c.ShouldBindJSON(&req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	var userRenewalLogs []models.UserRenewalLog
	qs := orm.Eloquent.Table("user_renewal_log")
	if !req.StartTime.IsZero() {
		qs = qs.Where("created_at>?", req.StartTime)
	}
	if !req.EndTime.IsZero() {
		qs = qs.Where("created_at<?", req.EndTime)
	}
	err := qs.Find(&userRenewalLogs).Error
	if err != nil {
		logger.Error("user renewal log err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}
	logMap := make(map[uint32]uint32)
	for i, _ := range userRenewalLogs {
		logMap[userRenewalLogs[i].Action]++
	}

	ret := struct {
		Massage       uint32 `json:"massage"`       // 发送消息
		Pop           uint32 `json:"pop"`           // 弹窗
		ActivityPage  uint32 `json:"activity_page"` // 活动页
		PickGold      uint32 `json:"pick_gold"`     // 领取黄金
		PickPlatinum  uint32 `json:"pick_platinum"`
		PickBlackGold uint32 `json:"pick_black_gold"`
		UseGold       uint32 `json:"use_gold"` // 使用黄金
		UsePlatinum   uint32 `json:"use_platinum"`
		UseBlackGold  uint32 `json:"use_black_gold"`
	}{
		Massage:       logMap[9],
		Pop:           logMap[1],
		ActivityPage:  logMap[2],
		PickGold:      logMap[3],
		PickPlatinum:  logMap[4],
		PickBlackGold: logMap[5],
		UseGold:       logMap[6],
		UsePlatinum:   logMap[7],
		UseBlackGold:  logMap[8],
	}

	app.OK(c, ret, "")
	return
}
