package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"time"
)

// SendResearchActivitiesTemplateMsg 线下调研活动报名给所属销售跟建会人员推送
func SendResearchActivitiesTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail, doStatus string) (err error) {
	//如果不是公司线下调研就返回
	if activityDetail.ActivityTypeId != 4 {
		return
	}
	defer func() {
		if err != nil {
			go utils.SendEmail("线下调研活动报名给所属销售跟建会人员推送失败Err"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers)
			go utils.SendAlarmMsg(fmt.Sprint("线下调研活动报名给所属销售跟建会人员推送失败Err:", err.Error(), "活动ID：", activityDetail.ActivityId, "手机号：", user.Mobile), 2)
		}
	}()
	var mobiles []string
	//获取销售信息
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if sellerItem != nil {
		mobiles = append(mobiles, sellerItem.Mobile)
	}
	//获取建会人员信息
	admin, err := models.GetSysAdminById(activityDetail.AdminId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if admin != nil {
		mobiles = append(mobiles, admin.Mobile)
	}

	var first string
	var keyword1 string
	var keyword2 string
	var keyword3 string
	var keyword4 string
	var remark string
	var redirectUrl string

	//first = "【" + activityDetail.ActivityName + "】有客户报名"
	keyword1 = user.RealName + "——" + user.CompanyName + "(" + sellerItem.RealName + ")"
	keyword2 = user.Mobile
	keyword3 = time.Now().Format(utils.FormatDateTime)
	keyword4 = doStatus + ":" + activityDetail.ActivityName
	//remark = "点击查看报告详情"

	openIdList, e := models.GetWxOpenIdByMobileSliceList(mobiles)
	if e != nil {
		err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
		return
	}
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}
	redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityDetail.ActivityId)
	sendInfo := new(SendWxTemplate)
	sendInfo.First = first
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.Remark = remark
	sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
	sendInfo.RedirectUrl = redirectUrl
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = strconv.Itoa(activityDetail.ArticleId)
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	if err != nil {
		return
	}
	return
}

// SendActivitieSignTemplateMsg 线下调研活动扫码签到给对应销售发模班消息
func SendActivitieSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) {
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("线下调研活动扫码签到给对应销售发模班消息推送失败Err:", err.Error(), "活动ID：", activityDetail.ActivityId, "手机号：", user.Mobile), 2)
		}
	}()
	var mobiles []string
	//获取销售信息
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if sellerItem != nil {
		mobiles = append(mobiles, sellerItem.Mobile)
	}

	var first string
	var keyword1 string
	var keyword2 string
	var keyword3 string
	var keyword4 string
	var remark string
	var redirectUrl string

	first = ""
	keyword1 = activityDetail.ActivityName
	keyword2 = user.RealName + "——" + user.CompanyName
	keyword3 = time.Now().Format(utils.FormatDateTime)
	keyword4 = ""
	remark = ""

	openIdList, e := models.GetWxOpenIdByMobileSliceList(mobiles)
	if e != nil {
		err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
		return
	}
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}
	if utils.RunMode != "release" {
		redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityDetail.ActivityId)
	} else {
		redirectUrl = utils.WX_MSG_PATH_ACTIVITY_SIGN + strconv.Itoa(activityDetail.ActivityId)
	}

	sendInfo := new(SendWxTemplate)
	sendInfo.First = first
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.Remark = remark
	sendInfo.TemplateId = utils.WxMsgTemplateIdActivitySign
	sendInfo.RedirectUrl = redirectUrl
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = strconv.Itoa(activityDetail.ActivityId)
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	if err != nil {
		return
	}
	return
}

// SendActivitieCancelSignTemplateMsg 取消活动给销售和内容组四人发模版消息
func SendActivitieCancelSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) {
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID：", activityDetail.ActivityId, "手机号：", user.Mobile), 2)
		}
	}()
	//获取销售手机号
	sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	configCode := utils.TPL_MSG_NEI_RONG_ZU
	cnf, e := models.GetConfigByCode(configCode)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}
	openIdList, e := models.GetUserRecordListByMobile(4, cnf.ConfigValue+","+sellerItemQy.Mobile)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
		return
	}
	keyword1 := user.RealName + "--" + user.CompanyName + "（" + sellerItemQy.RealName + "）"
	keyword2 := user.Mobile
	keyword3 := time.Now().Format(utils.FormatDateTime)
	keyword4 := "取消报名" + "【" + activityDetail.ActivityName + "】"
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}
	sendInfo := new(SendWxTemplate)
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = user.Mobile
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	return
}

// SendNeiRongZuActivitieSignTemplateMsg 报名活动给销售和内容组四人发模版消息
func SendNeiRongZuActivitieSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) {
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID：", activityDetail.ActivityId, "手机号：", user.Mobile), 2)
		}
	}()
	//获取销售手机号
	sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	configCode := utils.TPL_MSG_NEI_RONG_ZU
	cnf, e := models.GetConfigByCode(configCode)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}
	openIdList, e := models.GetUserRecordListByMobile(4, cnf.ConfigValue+","+sellerItemQy.Mobile)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
		return
	}
	keyword1 := user.RealName + "--" + user.CompanyName + "（" + sellerItemQy.RealName + "）"
	keyword2 := user.Mobile
	keyword3 := time.Now().Format(utils.FormatDateTime)
	keyword4 := "报名" + "【" + activityDetail.ActivityName + "】"
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}
	sendInfo := new(SendWxTemplate)
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = user.Mobile
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	return
}
