package chart_permission_search_key_word_mapping

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

// GetChartPermissionIdsByKeyWord 查询二级分类中的权限
func GetChartPermissionIdsByKeyWord(keyWord string) (ids []int, err error)  {
	var list []*ChartPermissionSearchKeyWordMapping
	err = global.DEFAULT_MYSQL.Model(ChartPermissionSearchKeyWordMapping{}).Where("key_word = ? and `from` = 'rddp' ", keyWord).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
}

// GetKeyWordsByChartPermissionId 根据权限ID， 查询二级分类名称
func GetKeyWordsByChartPermissionId(charPermissionId int, from string) (keyWords []string, 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 {
		for _, item := range list {
			keyWords = append(keyWords, item.KeyWord)
		}
	}
	return
}

// GetKeyWordsByChartPermissionId 根据权限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
}