package controllers

import (
	"encoding/json"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/services"
	"hongze/hongze_cygx/utils"
	"time"
)

// Banner
type BannerController struct {
	BaseAuthController
}

// @Title 列表
// @Description 列表接口
// @Success Ret=200 {object} cygx.CygxBannerListResp
// @router /list [get]
func (this *BannerController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	resp := new(models.CygxBannerListResp)
	var condition string
	var pars []interface{}
	condition += " AND art.show_type IN (0,1)	 AND art.status = 1 ORDER BY art.list_type ASC   , art.sort ASC  "
	list, err := models.GetCygxBannerList(condition, pars, 0, 99999)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	bannerImgList, err := models.GetCygxBannerImgList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	mapImg := make(map[int]string)
	for _, v := range bannerImgList {
		mapImg[v.ImgId] = v.IndexImg
	}
	for _, v := range list {
		v.IndexImg = mapImg[v.ImgId]
		v.BannerUrlResp = services.GetBannerUrlBody(v.Link)
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 记录点击信息
// @Description 记录点击信息
// @Param	request	body cygx.CygxBannerIdReq true "type json string"
// @Success 200 Ret=200 发布成功
// @router /add/history [post]
func (this *BannerController) History() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req models.CygxBannerIdReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	bannerId := req.BannerId
	if bannerId == 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误，id不可为空"
		return
	}
	go services.AddCygxBannerHistory(user, bannerId)
	br.Ret = 200
	br.Success = true
	br.Msg = "记录成功"
}

// @Title 研选banner列表
// @Description 研选banner列表接口
// @Success Ret=200 {object} cygx.CygxBannerListResp
// @router /listYx [get]
func (this *BannerController) ListYx() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	resp := new(models.BannerUrlYxListResp)
	var listA []*models.BannerUrlYxResp
	var listB []*models.BannerUrlYxResp

	if utils.RunMode == "release" {
		listA = []*models.BannerUrlYxResp{
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/1.png", Path: "pages-purchaser/leaflet/leaflet?leafletImage=https%3A%2F%2Fhzstatic.hzinsights.com%2Fcygx%2Fconfig%2Fresearch_11_0.png"},
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/1.png", Path: "pages-purchaser/leaflet/leaflet?leafletImage=https%3A%2F%2Fhzstatic.hzinsights.com%2Fcygx%2Fconfig%2Fresearch_11_1.png"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/2.png", Path: "pages-purchaser/survey/surveySubmit"},
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/4.png", Path: "pages-purchaser/specialColumn/specialColumn"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/3_long.png", Path: "pages-purchaser/researchList/researchList"},
		}
		listB = []*models.BannerUrlYxResp{
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/3.png", Path: "pages-purchaser/researchList/researchList"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/4_short.png", Path: "pages-purchaser/specialColumn/specialColumn"},
		}
	} else {
		listA = []*models.BannerUrlYxResp{
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/1.png", Path: "pages-purchaser/leaflet/leaflet?leafletImage=https%3A%2F%2Fhzstatic.hzinsights.com%2Fcygx%2Fconfig%2Fresearch_11_0.png"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/1.png", Path: "pages-purchaser/leaflet/leaflet?leafletImage=https%3A%2F%2Fhzstatic.hzinsights.com%2Fcygx%2Fconfig%2Fresearch_11_1.png"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/2.png", Path: "pages-purchaser/survey/surveySubmit"},
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/3_long.png", Path: "pages-purchaser/researchList/researchList"},
		}
		listB = []*models.BannerUrlYxResp{
			//&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/4_short.png", Path: "pages-purchaser/specialColumn/specialColumn"},
			&models.BannerUrlYxResp{IndexImg: "https://hzstatic.hzinsights.com/banner/yx/3.png", Path: "pages-purchaser/researchList/researchList"},
		}
	}

	resp.ListA = listA
	resp.ListB = listB
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 提交我要调研内容
// @Description 提交我要调研内容接口
// @Param	request	body cygx.CygxBannerIdReq true "type json string"
// @Success 200 Ret=200 提交成功
// @router /add/yxSurvey [post]
func (this *BannerController) YxSurvey() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req models.CygxBannerYxSurveyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	content := req.Content
	if content == "" {
		br.Msg = "内容不能为空"
		br.ErrMsg = "内容不能为空"
		return
	}
	item := new(models.CygxBannerYxSurvey)
	item.UserId = user.UserId
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.Mobile = user.Mobile
	item.Email = user.Email
	item.CompanyId = user.CompanyId
	item.CompanyName = user.CompanyName
	item.Content = content
	item.RegisterPlatform = utils.REGISTER_PLATFORM
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	item.RealName = user.RealName
	if sellerItem != nil {
		item.SellerName = sellerItem.RealName
	}
	newId, err := models.AddCygxBannerYxSurvey(item)
	if err != nil {
		br.Msg = "申请失败"
		br.ErrMsg = "申请失败，Err:" + err.Error()
		return
	}
	go services.SendCygxBannerYxSurveyTemplateMsg(user, content, int(newId))
	br.Ret = 200
	br.Success = true
	br.Msg = "记录成功"
}

// @Title 我要调研内容详情
// @Description 我要调研内容详情接口
// @Param   SuveryId   query   int  true       "ID"
// @Success Ret=200 {object} cygx.CygxBannerListResp
// @router /yxSurvey/detail [get]
func (this *BannerController) YxSurveyDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	suveryId, _ := this.GetInt("SuveryId")
	resp := new(models.CygxBannerYxSurveyRespDetailResp)
	detail, err := models.GetCygxBannerYxSurveyDetail(suveryId)
	if err != nil {
		br.Msg = "详情不存在"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	resp.Detail = detail
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
