package controllers

import (
	"net/http"
	"time"

	"git.rosy.net.cn/jx-callback/business/jxstore/cms"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/jxutils/weixinmsg"
	"git.rosy.net.cn/jx-callback/business/model"
	"github.com/astaxie/beego/server/web"
)

type MsgController struct {
	web.Controller
}

// @Title 发送微信消息
// @Description 发送微信消息
// @Param	token			header		string	true	"认证token"
// @Param	storeIDs		formData	string	true	"门店 ID列表"
// @Param	title			formData	string	true	"消息标题"
// @Param	content			formData	string	true	"消息内容"
// @Param	messageType		formData	int		true	"消息类型，1为普通给商家发的消息，3为活动内容通知, 4为知识库"
// @Param	actInfo			formData	string	false	"如果是活动内容通知需传"
// @Param	imgs			formData	string	false	"图片s"
// @Param	isAsync			formData	bool	false	"是否异步操作，缺省否"
// @Param	isContinueWhenError	formData	bool	false	"单个失败是否继续，缺省false"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendStoreMessage [post]
func (c *MsgController) SendStoreMessage() {
	c.callSendStoreMessage(func(params *tMsgSendStoreMessageParams) (retVal interface{}, errCode string, err error) {
		var storeIDs []int
		var imgs []string
		if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDs, params.Imgs, &imgs); err != nil {
			return retVal, "", err
		}
		retVal, err = weixinmsg.SendStoreMessage(params.Ctx, params.Title, params.Content, storeIDs, imgs, params.ActInfo, params.MessageType, params.IsAsync, params.IsContinueWhenError)
		return retVal, "", err
	})
}

// @Title 发送微信消息(知识库
// @Description 发送微信消息(知识库
// @Param	token			header		string	true	"认证token"
// @Param	storeIDs		formData	string	true	"门店 ID列表"
// @Param	title			formData	string	true	"消息标题"
// @Param	knowIDs			formData	string	true	"消息类型，1为普通给商家发的消息，3为活动内容通知"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendStoreMessageKnowledge [post]
func (c *MsgController) SendStoreMessageKnowledge() {
	c.callSendStoreMessageKnowledge(func(params *tMsgSendStoreMessageKnowledgeParams) (retVal interface{}, errCode string, err error) {
		var knowIDs, storeIDs []int
		if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDs, params.KnowIDs, &knowIDs); err != nil {
			return retVal, "", err
		}
		retVal, err = weixinmsg.SendStoreMessageKnowledge(params.Ctx, params.Title, knowIDs, storeIDs)
		return retVal, "", err
	})
}

// @Title 发送用户微信消息
// @Description 发送用户微信消息
// @Param	token				header		string	true	"认证token"
// @Param	userIDs				formData	string	true	"用户 ID列表"
// @Param	title				formData	string	true	"消息标题"
// @Param	content				formData	string	true	"消息内容"
// @Param	isAsync				formData	bool	false	"是否异步操作，缺省否"
// @Param	isContinueWhenError	formData	bool	false	"单个失败是否继续，缺省false"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /SendUserMessage [post]
func (c *MsgController) SendUserMessage() {
	c.callSendUserMessage(func(params *tMsgSendUserMessageParams) (retVal interface{}, errCode string, err error) {
		var userIDs []string
		if err = jxutils.Strings2Objs(params.UserIDs, &userIDs); err != nil {
			return retVal, "", err
		}
		retVal, err = weixinmsg.SendUserMessage(params.Ctx, params.Title, params.Content, userIDs, params.IsAsync, params.IsContinueWhenError)
		return retVal, "", err
	})
}

// @Title 门店读微信消息
// @Description 门店读微信消息（此API是自动调用的）
// @Param	token			header		string	true	"认证token"
// @Param	msgID			formData	int		true	"消息ID"
// @Param	msgStatusID		formData	int		true	"消息状态ID"
// @Param	redirectURL		formData	string	false	"重定向URL"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /ReadStoreMessage [put]
func (c *MsgController) ReadStoreMessage() {
	redirectURL := ""
	c.callReadStoreMessage(func(params *tMsgReadStoreMessageParams) (retVal interface{}, errCode string, err error) {
		retVal, err = cms.ReadStoreMessage(params.Ctx, params.MsgID, params.MsgStatusID)
		retRedirectURL := retVal.(string)
		if retRedirectURL != "" {
			redirectURL = retRedirectURL
		} else {
			redirectURL = params.RedirectURL
		}
		if redirectURL != "" {
			errCode = model.ErrorCodeIgnore
		}
		return retVal, errCode, err
	})
	if redirectURL != "" {
		c.Redirect(redirectURL, http.StatusTemporaryRedirect)
	}
}

// @Title 得到发送的微信消息列表
// @Description 得到发送的微信消息列表
// @Param	token			header		string	true	"认证token"
// @Param	keyword			query		string	false	"查询关键字（可以为空，为空表示不限制）"
// @Param	msgIDs			query		string	false	"msg IDs列表"
// @Param	storeIDs		query		string	false	"门店 ID列表"
// @Param	types			query		string	false	"类型列表，当前只有一个类型，写死[1]"
// @Param	fromTime		query		string	false	"创建起始时间"
// @Param	toTime			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 /GetStoreMessages [get]
func (c *MsgController) GetStoreMessages() {
	c.callGetStoreMessages(func(params *tMsgGetStoreMessagesParams) (retVal interface{}, errCode string, err error) {
		var msgIDs, storeIDs, types []int
		var timeList []time.Time
		if err = jxutils.Strings2Objs(params.MsgIDs, &msgIDs, params.StoreIDs, &storeIDs, params.Types, &types); err != nil {
			return retVal, "", err
		}
		if timeList, err = jxutils.BatchStr2Time(params.FromTime, params.ToTime); err != nil {
			return retVal, "", err
		}
		retVal, err = cms.GetStoreMessages(params.Ctx, msgIDs, storeIDs, types, timeList[0], timeList[1], params.Keyword, params.Offset, params.PageSize)
		return retVal, "", err
	})
}

// @Title 得到发送的微信消息状态列表
// @Description 得到发送的微信消息状态列表
// @Param	token			header		string	true	"认证token"
// @Param	keyword			query		string	false	"查询关键字（可以为空，为空表示不限制）"
// @Param	msgIDs			query		string	false	"msg IDs列表"
// @Param	storeIDs		query		string	false	"门店 ID列表"
// @Param	fromTime		query		string	false	"创建起始时间"
// @Param	toTime			query		string	false	"创建结束时间"
// @Param	confirmStatuss	query		string	false	"确认状态，0表示没点，1表示确认，-1表示拒绝"
// @Param	fromReadCount	query		int		false	"已读次数开始"
// @Param	toReadCount		query		int		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 /GetStoreMessageStatuses [get]
func (c *MsgController) GetStoreMessageStatuses() {
	c.callGetStoreMessageStatuses(func(params *tMsgGetStoreMessageStatusesParams) (retVal interface{}, errCode string, err error) {
		var msgIDs, storeIDs, confirmStatuss []int
		var timeList []time.Time
		if err = jxutils.Strings2Objs(params.MsgIDs, &msgIDs, params.StoreIDs, &storeIDs, params.ConfirmStatuss, &confirmStatuss); err != nil {
			return retVal, "", err
		}
		if timeList, err = jxutils.BatchStr2Time(params.FromTime, params.ToTime); err != nil {
			return retVal, "", err
		}
		if _, ok := params.MapData["fromReadCount"]; !ok {
			params.FromReadCount = -1
		}
		if _, ok := params.MapData["toReadCount"]; !ok {
			params.ToReadCount = -1
		}
		retVal, err = cms.GetStoreMessageStatuses(params.Ctx, msgIDs, storeIDs, confirmStatuss, params.FromReadCount, params.ToReadCount, timeList[0], timeList[1], params.Keyword, params.Offset, params.PageSize)
		return retVal, "", err
	})
}
