package eta

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/models"
)

const (
	Columns                 = "chart_permission_id,permission_name,parent_id"
	FirstPermissionsColumns = "DISTINCT parent_id"
)

// ChartPermission represents the structure of the chart_permission table.
type ChartPermission struct {
	ChartPermissionID int    `gorm:"primaryKey;autoIncrement;column:chart_permission_id;comment:主键"`
	PermissionName    string `gorm:"size:50;default:'';column:permission_name;comment:权限名"`
	ParentID          int    `gorm:"size:11;default:0;column:parent_id;comment:父级权限id"`
}

// TableName sets the insert table name for this struct type
func (c *ChartPermission) TableName() string {
	return "chart_permission"
}

func GetChartPermissionList() (chartPermissionList []ChartPermission, err error) {
	db := models.ETA()
	err = db.Select(Columns).Where("product_id =? and enabled =?", 1, true).Order("parent_id asc").Order("sort asc").Find(&chartPermissionList).Error
	if err == nil && chartPermissionList == nil {
		chartPermissionList = []ChartPermission{}
	}
	return
}
func GetPermissionNamesByClassifyID(ids []int) (chartPermissionNames []string, err error) {
	sql := "select chart_permission_id, permission_name from chart_permission WHERE chart_permission_id in ?"
	err = doSql(sql, &chartPermissionNames, ids)
	return
}
func GetPermissionNamesByPermissionIds(ids []int) (chartPermissionNames []string, err error) {
	sql := "select permission_name from chart_permission WHERE chart_permission_id in ?"
	err = doSql(sql, &chartPermissionNames, ids)
	return
}
func GetFirstPermissionsByClassifyID(classifyID int) (chartPermissionList []ChartPermission, err error) {
	sql := "select chart_permission_id, permission_name,parent_id from chart_permission WHERE chart_permission_id in( select DISTINCT cp.parent_id from chart_permission_search_key_word_mapping cpskwp left JOIN chart_permission cp on cp.chart_permission_id =cpskwp.chart_permission_id where cpskwp.classify_id=? order by cp.parent_id)"
	err = doSql(sql, &chartPermissionList, classifyID)
	return
}

func GetSecondPermissionsByClassifyID(classifyID int) (chartPermissionList []ChartPermission, err error) {
	sql := "select chart_permission_id, permission_name from chart_permission WHERE chart_permission_id in( select DISTINCT cpskwp.chart_permission_id from chart_permission_search_key_word_mapping cpskwp  where cpskwp.classify_id=? order by cpskwp.chart_permission_id)"
	err = doSql(sql, &chartPermissionList, classifyID)
	return
}
func GetFirstPermissionsByIds(ids []int) (chartPermissionList []ChartPermission, err error) {
	sql := "select chart_permission_id, permission_name from chart_permission where chart_permission_id in (select parent_id from chart_permission WHERE chart_permission_id in ?)"
	err = doSql(sql, &chartPermissionList, ids)
	return
}

func GetPermissionIdByName(name string) (id int, err error) {
	sql := "select chart_permission_id from chart_permission where permission_name =? "
	err = doSql(sql, &id, name)
	return
}
func doSql(sql string, result interface{}, values ...interface{}) (err error) {
	db := models.ETA()
	err = db.Raw(sql, values...).Find(result).Error
	if err != nil {
		logger.Error("执行sql[%v]失败:%v", sql, err)
	}
	return
}
