package report_chapter_type_permission

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

// GetTypeIdsByChartPermissionId 根据权限查询对应的章节
func GetTypeIdsByChartPermissionId(chartPermissionId int) (typeIds []int, err error) {
	var list []*ReportChapterTypePermission
	query := global.DEFAULT_MYSQL.
		Table("report_chapter_type_permission AS a").
		Select("a.*").
		Joins("JOIN report_chapter_type AS b ON a.report_chapter_type_id = b.report_chapter_type_id").
		Where("b.is_show = 1 AND b.enabled = 1 AND a.chart_permission_id = ?", chartPermissionId)
	err = query.Scan(&list).Error
	//err = global.DEFAULT_MYSQL.Model(ReportChapterTypePermission{}).Where("chart_permission_id = ?", chartPermissionId).Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	if err != nil {
		return
	}
	for _, v := range list {
		typeIds = append(typeIds, int(v.ReportChapterTypeId))
	}
	return
}
