package services

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

// Kp互动提醒
func SendCygxTemplateRecordSource(log models.WxUserRaiLabelRedis) (err error) {
	userId := log.UserId
	sourceId := log.SourceId
	sourceType := log.SourceType

	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("Kp互动提醒失败,SendCygxTemplateRecordSource err", err.Error()), 2)
		}
	}()

	var keyword1 string
	var keyword2 string
	var keyword3 string
	var keyword4 string
	var redirectUrl string

	wxUser, e := models.GetWxUserItemByUserId(userId)
	if e != nil {
		err = errors.New("GetWxUserItemByUserId" + e.Error())
		return
	}

	if wxUser.IsMaker == 0 {
		return
	}
	companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(wxUser.CompanyId, utils.COMPANY_PRODUCT_RAI_ID)
	if e != nil {
		err = errors.New("GetCompanyProductByCompanyIdAndProductId, Err:" + e.Error())
		return
	}
	var adminIds []int

	adminIds = append(adminIds, companyProduct.SellerId, companyProduct.ShareSellerId)
	listAdmin, e := models.GetAdminByAdminIds(adminIds)
	if e != nil {
		err = errors.New("GetAdminByAdminIds, Err: " + e.Error())
		return
	}

	var mobiles []string

	for _, v := range listAdmin {
		mobiles = append(mobiles, v.Mobile)
	}
	//fmt.Println(mobiles)
	keyword1 = "KP互动：" + wxUser.RealName + "-" + wxUser.CompanyName
	var source string
	switch sourceType {
	case 1:
		detail, e := models.GetArticleDetailById(sourceId)
		if e != nil {
			err = errors.New("GetArticleDetailById, Err: " + e.Error())
			return
		}
		keyword2 = "阅读报告：" + detail.Title
		redirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(sourceId)
		source = utils.CYGX_OBJ_ARTICLE
	case 4:
		activityDetail, e := models.GetAddActivityDetailByActivityId(sourceId)
		if e != nil {
			err = errors.New("GetAddActivityDetailByActivityId" + e.Error())
			return
		}
		keyword2 = "参与线上活动：" + activityDetail.ActivityName
		redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(sourceId)
		source = utils.CYGX_OBJ_ACTIVITY
	case 9:
		activityDetail, e := models.GetAddActivityDetailByActivityId(sourceId)
		if e != nil {
			err = errors.New("GetAddActivityDetailByActivityId" + e.Error())
			return
		}
		keyword2 = "参与线上活动：" + activityDetail.ActivityName
		redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(sourceId)
		source = utils.CYGX_OBJ_ACTIVITYVIDEO
	case 10:
		chartDetail, e := models.GetChartDetailByChartId(sourceId)
		if e != nil {
			err = errors.New("GetChartDetailByChartId, Err: " + e.Error())
			return
		}
		keyword2 = "阅读图表：" + chartDetail.Title
		redirectUrl = utils.WX_MSG_PATH_CHART_DETAIL + strconv.Itoa(sourceId)
		source = utils.CYGX_OBJ_NEWCHART
	default:
		return
	}

	var startDate, endDate string
	startDate = time.Now().Format(utils.FormatDate)
	startDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
	tatal, e := models.GetCygxTemplateRecordSourceCount(userId, sourceId, source, startDate, endDate)
	if e != nil {
		err = errors.New("GetCygxTemplateRecordSourceCount, Err: " + e.Error())
		return
	}
	if tatal > 0 {
		return // 一天只推送一次
	}

	item := new(models.CygxTemplateRecordSource)
	item.UserId = userId
	item.Source = source
	item.SourceId = sourceId
	item.CreateTime = time.Now()
	e = models.AddCygxTemplateRecordSource(item)
	if e != nil {
		err = errors.New("AddCygxTemplateRecordSource, Err: " + e.Error())
		return
	}

	openIdList, e := models.GetMfyxWxOpenIdByMobileList(strings.Join(mobiles, ","))
	if e != nil {
		err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error())
		return
	}
	if len(openIdList) == 0 {
		return
	}
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}
	//redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1"

	sendInfo := new(SendWxTemplate)
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
	sendInfo.RedirectUrl = redirectUrl
	sendInfo.RedirectTarget = 3
	sendInfo.Resource = strconv.Itoa(sourceId)
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	return
}
