package chart_permission_search_key_word_mapping

import (
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
)

// GetChartPermissionIdsByKeyWord 查询二级分类中的权限
func GetChartPermissionIdsByKeyWord(classifyId int) (ids []int, err error) {
	var list []*ChartPermissionSearchKeyWordMapping
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("classify_id = ? and `from` = 'rddp' ", classifyId).Scan(&list).Error
	if err != nil && err != utils.ErrNoRow {
		return
	}
	if len(list) > 0 {
		for _, item := range list {
			ids = append(ids, item.ChartPermissionId)
		}
	}
	return
}

// GetChartPermissionByFrom 查询二级分类中的权限
func GetChartPermissionByFrom(from string) (list []*ChartPermissionSearchKeyWordMapping, err error) {
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("`from` = ? and chart_permission_id != 33", from).Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetClassifyIdsByChartPermissionId 根据权限ID， 查询二级分类IDs
func GetClassifyIdsByChartPermissionId(charPermissionId int, from string) (classifyIds []int, err error) {
	var list []*ChartPermissionSearchKeyWordMapping
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("chart_permission_id = ? and `from` = ? ", charPermissionId, from).Scan(&list).Error
	if err != nil && err != utils.ErrNoRow {
		return
	}
	if len(list) > 0 {
		exists := make(map[int]bool)
		for _, item := range list {
			if exists[item.ClassifyId] {
				continue
			}
			classifyIds = append(classifyIds, item.ClassifyId)
			exists[item.ClassifyId] = true
		}
	}
	return
}

// GetKeyWordsByChartPermissionIdOnly 根据权限ID， 查询二级分类名称
func GetKeyWordsByChartPermissionIdOnly(charPermissionId int) (keyWords []string, err error) {
	var list []*ChartPermissionSearchKeyWordMapping
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("chart_permission_id = ? ", charPermissionId).Scan(&list).Error
	if err != nil && err != utils.ErrNoRow {
		return
	}
	if len(list) > 0 {
		for _, item := range list {
			keyWords = append(keyWords, item.KeyWord)
		}
	}
	return
}

// GetKeyWordsByChartPermissionByIds 根据权限ID， 查询二级分类名称
func GetKeyWordsByChartPermissionByIds(charPermissionIds []int) (keyWords []string, err error) {
	var list []*ChartPermissionSearchKeyWordMapping
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("chart_permission_id IN (?) ", charPermissionIds).Scan(&list).Error
	if err != nil && err != utils.ErrNoRow {
		return
	}
	if len(list) > 0 {
		for _, item := range list {
			keyWords = append(keyWords, item.KeyWord)
		}
	}
	return
}
