package advertising

import (
	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
	"applet/app/utils"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/model"
	"github.com/gin-gonic/gin"
)

func FunctionList(c *gin.Context) {
	var req *md.AdvertisingFunctionListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var resp md.AdvertisingFunctionListResp
	noticeList := make([]md.AdvertisingFunctionList, 0)
	resp.SelectData = []map[string]string{
		{"name": "APP开屏(开屏广告)", "value": "open_advertisement"},
		{"name": "首页砸蛋(激励视频)", "value": "incentive_eggSmash"},
		{"name": "首页签到(激励视频)", "value": "incentive_eggSign"},
		{"name": "提现(激励视频)", "value": "incentive_withdraw"},
		{"name": "积分兑换(插屏)", "value": "interstitial_integralExchange"},
		{"name": "动态IM(信息流)", "value": "information_im"},
		{"name": "短视频(短视频)", "value": "short_video"},
		{"name": "短剧(短剧)", "value": "short_playlet"},
	}
	NewAdvertisingSpaceDb := implement.NewAdvertisingSpaceDb(db.Db)
	adv, _, _ := NewAdvertisingSpaceDb.FindUserFeedbackCateAndTotal("1", "1000", "", "")
	advList := make([]map[string]string, 0)
	if adv != nil {
		for _, v := range *adv {
			tmp := map[string]string{
				"name":  v.Name,
				"value": utils.IntToStr(v.Id),
			}
			advList = append(advList, tmp)
		}
	}
	resp.AdvData = advList
	NewJpushNoticeDb := implement.NewAdvertisingFunctionDb(db.Db)
	notice, total, _ := NewJpushNoticeDb.FindUserFeedbackFunctionAndTotal(req.Page, req.Limit)
	resp.Total = total
	if notice != nil {
		for _, v := range *notice {
			tmp := md.AdvertisingFunctionList{
				Id:   utils.Int64ToStr(v.Id),
				AdId: utils.IntToStr(v.AdId),
				Type: v.Type,
			}
			noticeList = append(noticeList, tmp)
		}
	}
	resp.List = noticeList
	e.OutSuc(c, resp, nil)
	return
}
func FunctionDel(c *gin.Context) {
	var req *md.ArticleCateDelReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	db.Db.In("id", req.Id).Delete(&model.AdvertisingFunction{})
	e.OutSuc(c, "success", nil)
	return
}
func FunctionSave(c *gin.Context) {
	var req *md.AdvertisingFunctionList
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var data = new(model.AdvertisingFunction)
	if utils.StrToInt(req.Id) > 0 {
		NewAdvertisingSpaceDb := implement.NewAdvertisingFunctionDb(db.Db)
		space, _ := NewAdvertisingSpaceDb.GetAdvertisingFunction(req.Id)
		if space == nil {
			e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
			return
		}
		data = space
	} else {
		db.Db.Insert(data)
	}
	data.Type = req.Type
	data.AdId = utils.StrToInt(req.AdId)
	db.Db.Where("id=?", data.Id).Update(data)
	e.OutSuc(c, "success", nil)
	return
}