package cygx

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

// 报告匹配类型映射
func GetCygxReportMappingCygxListMap() (mapResp map[int]string) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("报告匹配类型映射，信息失败，Err："+err.Error(), 3)
		}
	}()
	var condition string
	var pars []interface{}
	list, err := cygx.GetCygxReportMappingCygxList(condition, pars, 0, 0)
	if err != nil {
		return
	}
	mapResp = make(map[int]string, 0)
	for _, v := range list {
		mapResp[v.Id] = v.MatchTypeName
	}
	return
}

// 根据文章ID获取文章行业分类
func GetCygxArticlePerssionNameListMap(articleIdArr []int) (mapResp map[int]string) {
	lenArr := len(articleIdArr)
	if lenArr == 0 {
		return
	}
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("根据文章ID获取文章行业分类，信息失败，GetCygxArticlePerssionNameListMap Err："+err.Error(), 3)
		}
	}()
	var condition string
	var pars []interface{}
	//获取分类名称
	perssionNameList, e := cygx.GetCygxReportMappingList(condition, pars)
	if e != nil {
		err = errors.New("GetCygxReportMappingCelueList, Err: " + e.Error())
		return
	}
	mapName := make(map[int]string)
	for _, v := range perssionNameList {
		mapName[v.CategoryId] = v.ChartPermissionName
	}
	pars = make([]interface{}, 0)
	condition = ` AND  article_id IN (` + utils.GetOrmInReplace(lenArr) + `)`
	pars = append(pars, articleIdArr)
	listArticle, e := cygx.GetArticlList(condition, pars, 0, 999)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("GetArticlList, Err: " + e.Error())
		return
	}
	mapResp = make(map[int]string, 0)
	for _, v := range listArticle {
		if v.CategoryId == 0 {
			mapResp[v.ArticleId] = utils.CHART_PERMISSION_NAME_MF_YANXUAN
		} else {
			mapResp[v.ArticleId] = mapName[v.CategoryId]
		}
	}
	return
}
