package cygx

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

//func init() {
//	HandleTacticsTimeLinLink("https://web.hzinsights.com/charts/5577")
//}

// 处理传过来的链接，获取文章或者图表的ID详情
func HandleTacticsTimeLinLink(link string) (articleId, chartId int) {
	sliceLink := strings.Split(link, "/")
	resourceId := sliceLink[len(sliceLink)-1]
	if strings.Contains(link, "info") {
		articleId, _ = strconv.Atoi(resourceId)
	} else if strings.Contains(link, "charts") {
		chartId, _ = strconv.Atoi(resourceId)
	}
	return
}

// 获取策略时间线的阅读数据
func GetCygxTacticsTimeLineHistoryListMap(timeLineIds []int) (mapPv, mapUv map[int]int) {
	var err error
	defer func() {
		if err != nil {
			go alarm_msg.SendAlarmMsg("获取策略时间线的阅读数据，信息失败，Err："+err.Error(), 3)
		}
	}()
	lentimeLineIds := len(timeLineIds)
	if lentimeLineIds == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND time_line_id IN (` + utils.GetOrmInReplace(lentimeLineIds) + `)`
	pars = append(pars, timeLineIds)
	list, err := cygx.GetCygxTacticsTimeLineHistoryList(condition, pars)
	if err != nil {
		return
	}
	mapPv = make(map[int]int, 0)
	mapUv = make(map[int]int, 0)
	mapUvCount := make(map[string]int, 0)
	for _, v := range list {
		mapPv[v.TimeLineId]++
		if _, ok := mapUvCount[fmt.Sprint("UID_", v.UserId, "TimeLineId_", v.TimeLineId)]; !ok {
			mapUvCount[fmt.Sprint("UID_", v.UserId, "TimeLineId_", v.TimeLineId)] = v.TimeLineId
			mapUv[v.TimeLineId]++
		}
	}
	return
}

// GetArticleTitleMapByid 通过文章ID获取文章标题 map
func GetArticleTitleMapByid(articleIds []int) (mapResp map[int]string) {
	var err error
	defer func() {
		if err != nil {
			go alarm_msg.SendAlarmMsg("通过文章ID获取文章标题 map，信息失败，Err："+err.Error(), 3)
		}
	}()
	lenarticleIds := len(articleIds)
	if lenarticleIds == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = `  AND article_id IN (` + utils.GetOrmInReplace(lenarticleIds) + `)  `
	pars = append(pars, articleIds)
	articleList, err := cygx.GetCygxArticleList(condition, pars, 0, lenarticleIds)
	if err != nil {
		return
	}
	mapResp = make(map[int]string, 0)
	for _, v := range articleList {
		mapResp[v.ArticleId] = v.Title
	}
	return
}
