package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"strconv"
)

//func init() {
//	SendWxMsgWithCygxMicroRoadshowVideo(52)
//}

// SendWxMsgWithCygxMicroRoadshowVideo  后台发布新的产业视频时，对关注了该产业的用户，发送模板消息。
func SendWxMsgWithCygxMicroRoadshowVideo(videoId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("err：", err)
			go alarm_msg.SendAlarmMsg("查研观向处理活动是否进行模板消息推送，发送模版消息失败，Err："+err.Error(), 3)
			utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error()))
		}
	}()
	detail, e := cygx.GetMicroRoadshowVideoByVideoId(videoId)
	if e != nil {
		err = errors.New("获取活动详情失败" + e.Error())
		return
	}
	if detail == nil {
		err = errors.New("获取活动详情失败")
		return
	}
	if detail.IsSendWxMsg == 1 {
		return
	}
	permissionStr, e := company.GetPermissionIdById(strconv.Itoa(detail.ChartPermissionId))
	if e != nil {
		err = errors.New("获取主客观权限失败" + e.Error())
		return
	}
	// 获取所有有权的用户的 openid
	openidPowerList, e := cygx.GetCygxUserRecordPower(permissionStr)
	if e != nil {
		err = errors.New("获取所有有权的用户的 openid失败" + e.Error())
		return
	}

	//获取关注对应产业的用户信息
	industryFllowList, e := cygx.GetCygxUserindustryFllowOpenid(detail.IndustryId)
	if e != nil {
		err = errors.New("获取关注对应产业的用户信息失败 " + e.Error())
		return
	}
	mapOpenidFllow := make(map[int]string)
	for _, v := range industryFllowList {
		mapOpenidFllow[v.UserId] = v.OpenId
	}

	//获取拒绝接收推送的的用户的 openid
	mapOpenidRefuset := make(map[int]string)
	openidRefusetList, e := cygx.GetCygxUserRefusetOpenid()
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("获取拒绝接收推送的的用户的 openid 失败" + e.Error())
		return
	}
	for _, v := range openidRefusetList {
		mapOpenidRefuset[v.UserId] = v.OpenId
	}

	//获取提交过推送规则的用户的 openid
	mapUserIdChooseSend := make(map[int]int)
	chooseSendtList, err := cygx.GetCygxXzsChooseSend("")
	if err != nil && err.Error() != utils.ErrNoRow() {
		return err
	}
	for _, v := range chooseSendtList {
		mapUserIdChooseSend[v.UserId] = v.UserId
	}

	openIdArr := make([]string, len(openidPowerList))
	for i, v := range openidPowerList {
		if _, ok := mapOpenidRefuset[v.UserId]; ok {
			continue //如果用户选择了拒绝推送消息，那么就不做模板消息推送
		}
		if _, ok := mapUserIdChooseSend[v.UserId]; ok {
			if _, ok := mapOpenidFllow[v.UserId]; !ok {
				continue //如果用户提交过关注信息，而且这个产业他没有关注，那么不做推送
			}
		}
		openIdArr[i] = v.OpenId
	}
	first := "【" + detail.IndustryName + "】产业，发布了5min逻辑解析视频，欢迎查看"
	keyword1 := "5min【" + detail.IndustryName + "】逻辑解析"
	keyword2 := "已发布"
	remark := "点击进入产业资源包详情页"
	redirectUrl := utils.WX_MSG_PATH_INDUSTRY_DETAIL + strconv.Itoa(detail.IndustryId) + "&IsSendWx=1"

	sendInfo := new(services.SendWxTemplate)
	sendInfo.First = first
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Remark = remark
	sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
	sendInfo.RedirectUrl = redirectUrl
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = strconv.Itoa(detail.VideoId)
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ROADSHOW_VIDEO
	sendInfo.OpenIdArr = openIdArr
	e = services.SendTemplateMsg(sendInfo)
	if e != nil {
		err = errors.New("推送模板消息失败" + e.Error())
		return
	}
	e = cygx.UpdateIsSendWxMsg(videoId, 1)
	if e != nil {
		err = errors.New("修改推送状态失败" + e.Error())
		return
	}
	return
}
