package report_chapter_type

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

func GetEffectTypeID() (chapterIds []int, err error) {
	var chapterList []*ReportChapterType
	err = global.DEFAULT_MYSQL.Model(ReportChapterType{}).Select("report_chapter_type_id ").Where("is_show=1 AND enabled = 1").Scan(&chapterList).Error
	if err != nil {
		if err == utils.ErrNoRow {
			err = nil
		}
		return
	}
	if len(chapterList) > 0 {
		for _, v := range chapterList {
			chapterIds = append(chapterIds, int(v.ReportChapterTypeId))
		}
	}
	return
}

func GetEffectTypes() (list []*ReportChapterType, err error) {
	err = global.DEFAULT_MYSQL.Model(ReportChapterType{}).Where("is_show=1 AND enabled = 1").Scan(&list).Error
	return
}

// GetTypeIDByReportChapterTypeName 根据章节类型查找章节ID
func GetTypeIDByReportChapterTypeName(name string) (chapterIds []int, err error) {
	var chapterList []*ReportChapterType
	err = global.DEFAULT_MYSQL.Model(ReportChapterType{}).Select("report_chapter_type_id ").Where("is_show=1 AND enabled = 1 and report_chapter_type_name =?", name).Scan(&chapterList).Error
	if err != nil {
		if err == utils.ErrNoRow {
			err = nil
		}
		return
	}
	if len(chapterList) > 0 {
		for _, v := range chapterList {
			chapterIds = append(chapterIds, int(v.ReportChapterTypeId))
		}
	}
	return
}

// GetTickerTitleByTypeId 根据类型获取指标标题
func GetTickerTitleByTypeId(id int) (chapterType *ReportChapterType, err error) {
	err = global.DEFAULT_MYSQL.Model(ReportChapterType{}).Select("ticker_title, report_chapter_type_id, report_chapter_type_name, report_chapter_type_key").Where("report_chapter_type_id=?", id).First(&chapterType).Error
	if err != nil {
		if err == utils.ErrNoRow {
			err = nil
		}
		return
	}
	return
}
