package controllers

import (
	"git.rosy.net.cn/baseapi/platformapi/weixinapi"
	"git.rosy.net.cn/jx-callback/business/jxstore/knowledge"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/server/web"
)

type KnowController struct {
	web.Controller
}

// @Title 查询知识条目
// @Description 查询知识条目
// @Param	token			header		string	true	"认证token"
// @Param	keyword 		query		string	false	"关键字"
// @Param	offset			query		int		false	"门店列表起始序号（以0开始，缺省为0）"
// @Param	pageSize		query		int		false	"门店列表页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetKnowledgeDepot [get]
func (c *KnowController) GetKnowledgeDepot() {
	c.callGetKnowledgeDepot(func(params *tKnowGetKnowledgeDepotParams) (retVal interface{}, errCode string, err error) {
		retVal, err = knowledge.GetKnowledgeDepot(params.Ctx, params.Keyword, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 添加知识条目
// @Description 添加知识条目
// @Param	token		header		string	true	"认证token"
// @Param	title		formData	string	false	"标题"
// @Param	content		formData	string	false	"内容"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddKnowledgeDepot [post]
func (c *KnowController) AddKnowledgeDepot() {
	c.callAddKnowledgeDepot(func(params *tKnowAddKnowledgeDepotParams) (retVal interface{}, errCode string, err error) {
		err = knowledge.AddKnowledgeDepot(params.Ctx, params.Title, params.Content)
		return retVal, "", err
	})
}

// @Title 更新知识条目
// @Description 更新知识条目
// @Param	token		header		string	true	"认证token"
// @Param	id		 	formData	int		true	"id"
// @Param	content		formData	string	false	"内容"
// @Param	isDel		formData	bool	true	"是否是删除"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateKnowledgeDepot [post]
func (c *KnowController) UpdateKnowledgeDepot() {
	c.callUpdateKnowledgeDepot(func(params *tKnowUpdateKnowledgeDepotParams) (retVal interface{}, errCode string, err error) {
		err = knowledge.UpdateKnowledgeDepot(params.Ctx, params.Id, params.Content, params.IsDel)
		return retVal, "", err
	})
}

// @Title 查询素材列表
// @Description 查询素材列表
// @Param	token			header		string	true	"认证token"
// @Param	offset			query		int		false	"门店列表起始序号（以0开始，缺省为0）"
// @Param	pageSize		query		int		false	"门店列表页大小（缺省为50，-1表示全部）"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMaterialList [get]
func (c *KnowController) GetMaterialList() {
	c.callGetMaterialList(func(params *tKnowGetMaterialListParams) (retVal interface{}, errCode string, err error) {
		retVal, err = api.WeixinAPI.CBBatchgetMaterial(weixinapi.MaterialTypeNews, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 添加素材
// @Description 添加素材
// @Param	token				header		string	    true	"认证token"
// @Param	knowIDs				formData	string		false	"知识条目IDs"
// @Param	title				formData	string		true	"素材标题"
// @Param	digest				formData	string		false	"素材摘要"
// @Param	author				formData	string		false	"作者"
// @Param	thumbMediaID		formData	string		true	"封面缩略图ID"
// @Param	showCoverPic		formData	int			false	"内容中是否显示封面，0不显示，1显示"
// @Param	needOpenComment		formData	int			false	"是否打开评论，0不打开，1打开"
// @Param	onlyFansCanComment	formData	int			false	"是否只允许粉丝才能评论"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddMaterial [post]
func (c *KnowController) AddMaterial() {
	c.callAddMaterial(func(params *tKnowAddMaterialParams) (retVal interface{}, errCode string, err error) {
		new := &weixinapi.CBAddNewsParam{
			Title:              params.Title,
			Digest:             params.Digest,
			Author:             params.Author,
			ThumbMediaID:       params.ThumbMediaID,
			ShowCoverPic:       params.ShowCoverPic,
			NeedOpenComment:    params.NeedOpenComment,
			OnlyFansCanComment: params.OnlyFansCanComment,
		}
		var knowIDs []int
		jxutils.Strings2Objs(params.KnowIDs, &knowIDs)
		err = knowledge.AddMaterial(params.Ctx, knowIDs, new)
		return retVal, "", err
	})
}

// @Title 公众号上传图片
// @Description 公众号上传图片
// @Param	token		header	string	false	"认证token"
// @Param	isThumb		query	bool	false	"是否是缩略图"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SNSUploadImg [post]
func (c *KnowController) SNSUploadImg() {
	c.callSNSUploadImg(func(params *tKnowSNSUploadImgParams) (retVal interface{}, errCode string, err error) {
		files := c.Ctx.Request.MultipartForm.File["userfiles"]
		retVal, err = knowledge.SNSUploadImg(params.Ctx, files, params.IsThumb)
		return retVal, "", err
	})
}

// @Title 发送消息
// @Description 发送消息
// @Param	token		header		string	true	"认证token"
// @Param	storeIDs	formData	string	false	"门店IDs"
// @Param	mediaID		formData	string	false	"素材ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendSNSMediaMsg [post]
func (c *KnowController) SendSNSMediaMsg() {
	c.callSendSNSMediaMsg(func(params *tKnowSendSNSMediaMsgParams) (retVal interface{}, errCode string, err error) {
		var storeIDs []int
		jxutils.Strings2Objs(params.StoreIDs, &storeIDs)
		err = knowledge.SendSNSMediaMsg(params.Ctx, storeIDs, params.MediaID)
		return retVal, "", err
	})
}
