ad.go 1.43 KB
package apis

import (
	"log"
	"net/http"

	. "client/icesimba.ad/models"
	"github.com/gin-gonic/gin"
)

type Obj struct {
	AdId      string     `json:"adid"`
	Url       string     `json:"url"`
	Materials []Material `json:"materials"`
}

func QueryAdMaterialApi(c *gin.Context) {

	var (
		code string
		msg  string
	)

	gameId := c.Request.FormValue("game_id")
	positionId := c.Request.FormValue("position_id")
	platform := c.Request.FormValue("platform")
	channel := c.Request.FormValue("channel")
	obj := Obj{}
	if gameId == "" || positionId == "" {
		code = "106000"
		msg = "missing parameter"
	} else {
		sell := SellPlan{
			GameId:     gameId,
			PositionId: positionId,
		}
		sells, err := sell.QueryAdIdByPositionId()
		if err != nil || len(sells) <= 0 {
			code = "1006001"
			msg = "this position is no ads"
		} else {
			sell, err = SellsFilter(sells)
			if err != nil {
				code = "1006001"
				msg = "this position is no ads"
			} else {
				code = "0"
				obj.AdId = sell.AdId
				obj.Materials, _ = sell.QueryMaterialByAdId()
				obj.Url, _ = GetAdDownloadUrl(sell.AdId, platform, channel)
			}
		}
		if err != nil {
			log.Printf("QueryAdMaterialApi error:" + err.Error() + "\n")
		}
	}

	if code == "0" {
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"obj":  obj,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  msg,
		})
	}
	log.Printf("return json \n" + "code:" + code + "\t msg:" + msg + "\n")
	log.Println(obj)
}