package chart_permission

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

// GetListByProductId 根据产品id获取所有权限列表
func GetListByProductId(productId int64) (list []*ChartPermission, err error) {
	err = global.DEFAULT_MYSQL.Where(" product_id = ?", productId).Find(&list).Error
	return
}

// GetFiccListExceptTacticByProductId 获取ficc 除了市场策略的所有权限
func GetFiccListExceptTacticByProductId() (list []*ChartPermission, err error)  {
	err = global.DEFAULT_MYSQL.Where(" enabled = 1 AND permission_type = 0 AND product_id = 1 and classify_name != '市场策略'").Find(&list).Error
	return
}
// GetClassNameListByProductId 根据权限id获取权限分类
func GetClassNameListByProductId(productId int64) (list []*ChartPermission, err error) {
	err = global.DEFAULT_MYSQL.Where(" product_id = ?", productId).Group("classify_name").Find(&list).Error
	return
}

// GetByWhereMap 根据查询条件map获取信息
func GetByWhereMap(where map[string]interface{}) (list []*ChartPermission, err error) {
	cond, whereVal, buildErr := utils.WhereBuild(where)
	if buildErr != nil {
		err = errors.New("系统异常，生成查询语句失败")
		return
	}
	err = global.DEFAULT_MYSQL.Where(cond, whereVal...).Find(&list).Error
	return
}

// GetListByIds 通过IDs获取图表权限集合
func GetListByIds(permissionIds []int) (list []*ChartPermission, err error) {
	err = global.DEFAULT_MYSQL.Model(ChartPermission{}).Where("chart_permission_id IN (?)", permissionIds).Scan(&list).Error
	return
}

// GetListByProductIdAndClassifyName 根据product及classify_name获取集合
func GetListByProductIdAndClassifyName(productId int, classifyName string) (items []*ChartPermission, err error) {
	err = global.DEFAULT_MYSQL.Model(ChartPermission{}).Where("enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name = ?", productId, classifyName).Order("sort ASC").Scan(&items).Error
	return
}

// GetByChartPermissionId 根据chartPermissionId 查找权限基本信息
func GetByChartPermissionId(chartPermissionId int) (item *ChartPermission, err error)  {
	err = global.DEFAULT_MYSQL.Model(ChartPermission{}).Where("chart_permission_id = ?", chartPermissionId).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}