package services

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

// 重点公司路演申请，给吴昂迪、对口销售
func SendCygxReportSelectionLogApplyTemplateMsg(user *models.WxUserItem, content string) (err error) {
	var msg string
	msg = fmt.Sprint(user, content)
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("SendCygxReportSelectionLogApplyTemplateMsg Err"+msg+err.Error(), 2)
		}
	}()

	var mobiles []string
	//获取权益销售
	sellerItem, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if e != nil {
		err = errors.New("GetAdminByRoleName, Err: " + e.Error())
		return
	}

	mobiles = append(mobiles, sellerItem.Mobile)
	mobiles = append(mobiles, "")
	mobiles = append(mobiles, utils.MobileShenTao, utils.MobileZhangChuanXin, utils.MobileWuAngDi) // 添加沈涛、张传星、吴昂迪手机号
	openIdList, e := models.GetWxOpenIdByMobileSliceList(mobiles)
	if e != nil {
		err = errors.New("GetWxOpenIdByMobileSliceList, Err: " + e.Error())
		return
	}
	if len(openIdList) == 0 {
		return
	}
	openIdArr := make([]string, 0)
	for _, v := range openIdList {
		openIdArr = append(openIdArr, v.OpenId)
	}

	fmt.Println(openIdArr)
	var keyword1 string
	var keyword2 string
	var keyword3 string
	var keyword4 string
	keyword1 = fmt.Sprint(user.RealName, "--", user.CompanyName, "(", sellerItem.RealName, ")")
	if user.Mobile == "" {
		user.Mobile = user.Email
	}
	keyword2 = user.Mobile
	keyword3 = time.Now().Format(utils.FormatDateTime)
	keyword4 = fmt.Sprint(content, "(申请路演)")
	var redirectUrl string
	sendInfo := new(SendWxTemplate)
	sendInfo.Keyword1 = keyword1
	sendInfo.Keyword2 = keyword2
	sendInfo.Keyword3 = keyword3
	sendInfo.Keyword4 = keyword4
	sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD
	sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
	sendInfo.RedirectUrl = redirectUrl
	sendInfo.RedirectTarget = 3
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	return
}

// 记录用户在重点公司单独点了某一个标签
func GetReportSelectionLabelName(req models.AddCygxReportSelectionSubjectHistoryReq, userId int) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("SendCygxReportSelectionLogApplyTemplateMsg Err"+err.Error(), 2)
		}
	}()
	var condition string
	var pars []interface{}
	condition = ` AND article_id = ?  AND industrial_subject_id = ? AND  industrial_management_id = ? AND  third_id = ? `
	pars = append(pars, req.ArticleId, req.IndustrialSubjectId, req.IndustrialManagementId, req.ThirdId)
	listLog, e := models.GetCygxReportSelectionLog(condition, pars, 0, 9999)
	if e != nil {
		err = errors.New("GetCygxReportSelectionLog, Err: " + e.Error())
		return
	}
	var labelName string
	for _, v2 := range listLog {
		if v2.ThirdId > 0 {
			labelName = v2.ThirdName
		} else if v2.IndustrialSubjectId == 0 && v2.IndustrialManagementId != "" {
			labelName = v2.IndustrialManagementNames
		} else {
			labelName = v2.SubjectName
		}
	}
	if labelName == "" {
		return
	}
	ReportSelectionWxUserRaiLabelRedisAdd(req.ArticleId, userId, time.Now(), "cygx_report_selection", labelName)
	return
}
