package services

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

// 约访专家的请求
func AddCygxBannerYxSurvey(user *models.WxUserItem, content string) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("约访专家的请求失败"+err.Error(), 2)
		}
	}()
	item := new(models.CygxBannerYxSurvey)
	item.UserId = user.UserId
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.Mobile = user.Mobile
	item.Email = user.Email
	item.CompanyId = user.CompanyId
	item.CompanyName = user.CompanyName
	item.Content = content
	item.RegisterPlatform = utils.REGISTER_PLATFORM
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	item.RealName = user.RealName
	if sellerItem != nil {
		item.SellerName = sellerItem.RealName
	}
	_, e := models.AddCygxBannerYxSurvey(item)
	if e != nil {
		err = errors.New("AddCygxBannerYxSurvey, Err: " + e.Error())
		return
	}
	return
}

// 用户提交研选调研需求，给汪洋、对口销售、内容组全员发模板消息
func SendCygxBannerYxSurveyTemplateMsg(user *models.WxUserItem, content string, articleId int) (err error) {
	var msg string
	msg = fmt.Sprint(user, articleId)
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("SendCygxBannerYxSurveyTemplateMsg 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
	}
	//内容组全员手机号
	configCode := utils.TPL_MSG_NEI_RONG_ZU
	cnf, e := models.GetConfigByCode(configCode)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}

	//汪洋手机号
	cnfWangYang, e := models.GetConfigByCode(utils.TPL_MSG_WANG_YANG)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}

	mobiles = append(mobiles, sellerItem.Mobile)
	mobiles = append(mobiles, cnfWangYang.ConfigValue)
	listMobile := strings.Split(cnf.ConfigValue, ",")
	for _, v := range listMobile {
		mobiles = append(mobiles, v)
	}
	mobiles = append(mobiles, utils.MobileShenTao, utils.MobileZhangChuanXin, utils.MobileWangFang) // 添加沈涛、张传星、王芳手机号
	openIdList, e := models.GetWxOpenIdByMobileSliceList(mobiles)
	if e != nil {
		err = errors.New("GetWxOpenIdByMobileSliceList, Err: " + e.Error() + configCode)
		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
	redirectUrl = utils.WX_MSG_PATH_YX_SURVEY_DETAIL + strconv.Itoa(articleId)
	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.Resource = strconv.Itoa(articleId)
	sendInfo.OpenIdArr = openIdArr
	err = PublicSendTemplateMsg(sendInfo)
	return
}
