package services

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

// 获取研选类型的文章分类Id
func GetYanXuanArticleTypeIds() (articleTypeIds string, err error) {
	var condition string
	condition = " AND is_show_yanx  = 1 "
	listType, e := models.GetCygxArticleTypeListCondition(condition)
	if e != nil {
		err = errors.New("GetCygxArticleTypeListCondition, Err: " + e.Error())
		return
	}
	for _, v := range listType {
		articleTypeIds += strconv.Itoa(v.ArticleTypeId) + ","
	}
	articleTypeIds = strings.TrimRight(articleTypeIds, ",")
	if articleTypeIds == "" {
		err = errors.New("研选分类ID不能为空")
		return
	}
	return
}

// 处理研选关联的新标签
func GetYanXuanIndustrialManagementIdNewMap(articleTypeIds string) (respMap map[int]bool, err error) {
	var condition string
	if articleTypeIds == "" {
		return
	}
	condition = ` AND a.article_type_id IN (` + articleTypeIds + `)  `
	list, e := models.GetIndustrialManagementNewList(condition)
	if e != nil {
		err = errors.New("GetIndustrialManagementNewList, Err: " + e.Error())
		return
	}
	newMap := make(map[int]bool)
	for _, v := range list {
		newMap[v.IndustrialManagementId] = true
	}
	respMap = newMap
	return
}

// 处理研选关联的hot标签
func GetYanXuanIndustrialManagementIdHotMap(articleTypeIds string) (respMap map[int]bool, err error) {
	var condition string
	var conditionOrder string
	if articleTypeIds == "" {
		return
	}
	condition = ` AND a.article_type_id IN (` + articleTypeIds + `)  `
	conditionOrder = ` ORDER BY sum_num DESC `
	listHot, e := models.GetThemeHeatList(0, condition, conditionOrder, 0, 3)
	if e != nil {
		err = errors.New("GetIndustrialManagementNewList, Err: " + e.Error())
		return
	}
	hotMap := make(map[int]bool)
	for _, v := range listHot {
		hotMap[v.IndustrialManagementId] = true
	}
	respMap = hotMap
	return
}

// GetArticleSubjectName 获取文章关联的多个标的名称
func GetArticleSubjectName(articleIds []int) (respMap, subjectMap map[int]string, err error) {
	lenarticleIds := len(articleIds)
	if lenarticleIds == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND ag.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
	pars = append(pars, articleIds)
	list, e := models.GetSubjectArticlGroupList(condition, pars)
	if e != nil {
		err = errors.New("GetIndustrialManagementNewList, Err: " + e.Error())
		return
	}
	artMap := make(map[int]string)
	subMap := make(map[int]string)
	for _, v := range list {
		artMap[v.ArticleId] += v.SubjectName + "/"
		subMap[v.IndustrialSubjectId] = v.SubjectName
	}
	for _, v := range list {
		artMap[v.ArticleId] = strings.TrimRight(artMap[v.ArticleId], "/")
	}
	respMap = artMap
	subjectMap = subMap
	return
}
