package controllers

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

// Collection
type CollectionController struct {
	BaseAuthController
}

// @Title 精选看板、路演banner列表
// @Description 精选看板、路演banner列表接口
// @Success Ret=200 {object} cygx.CollectionBannerListResp
// @router /banner/list [get]
func (this *CollectionController) BannerList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	resp := new(models.CollectionBannerListResp)
	var listA []*models.CollectionBannerResp
	listB := new(models.CollectionBannerResp)
	listA = []*models.CollectionBannerResp{
		&models.CollectionBannerResp{Title: "", IndexImg: "https://hzstatic.hzinsights.com/cygx/banner/xcx/lyhf.png", Path: "/reportPages/trainVideoPages/trainVideoPages"},
	}
	listB.Title = ""
	listB.IndexImg = "https://hzstatic.hzinsights.com/cygx/banner/xcx/jxkb.png"
	listB.IsShowSustainable = true
	resp.ListA = listA
	resp.ListB = listB
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 精选看板跳转详情地址
// @Description 精选看板跳转详情地址接口
// @Success Ret=200 {object} cygx.CollectionBannerListResp
// @router /detail [get]
func (this *CollectionController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	resp := new(models.CollectionDetailResp)
	if user.Mobile != "" {
		userTokenByMobile, _ := services.GetUserTokenByMobile(user.Mobile)
		resp.HttpUrl = utils.COLLECTIONS_INFO_HTTP_URL + "?token=" + userTokenByMobile
	} else {
		resp.HttpUrl = utils.COLLECTIONS_INFO_HTTP_URL
	}
	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 /apply/add [post]
func (this *CollectionController) ApplyAdd() {
	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.ApplyCollectionReq
	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.CygxApplyCollection)
	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.AddCygxApplyCollection(item)
	services.SendCygxApplyCollectionTemplateMsg(user, content, int(newId))
	if err != nil {
		br.Msg = "申请失败"
		br.ErrMsg = "申请失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "记录成功"
}

// @Title  获取申请详情
// @Description 获取申请详情接口
// @Param   ApplyCollectionId   query   int  true       "ID"
// @Success Ret=200 {object} cygx.CygxApplyCollectionDetailResp
// @router /apply/detail [get]
func (this *CollectionController) ApplyDetail() {
	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.CygxApplyCollectionDetailResp)
	applyCollectionId, _ := this.GetInt("ApplyCollectionId")
	if applyCollectionId < 1 {
		br.Msg = "请输入详情ID"
		return
	}
	detail, err := models.GetCygxApplyCollectionDetail(applyCollectionId)
	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
}
