package cygx

import (
	"github.com/beego/beego/v2/client/orm"
	//"time"
)

type CygxReportMapping struct {
	SubCategoryName string `description:"主题"`
}

type CygxReportMappingListRep struct {
	List []*CygxReportMapping
}

// 主题列表
func CygxReportMappingist(chartPermissionId int) (items []*CygxReportMapping, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT sub_category_name FROM cygx_report_mapping WHERE  chart_permission_id = ? GROUP BY sub_category_name`
	_, err = o.Raw(sql, chartPermissionId).QueryRows(&items)
	return
}

type CygxReportMappingMatchType struct {
	MatchTypeName string `description:"匹配类型"`
}

type CygxReportMappingMatchTypeRep struct {
	List []*CygxReportMappingMatchType
}

// 主题列表
func CygxReportMappingMatchTypeList() (items []*CygxReportMappingMatchType, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT match_type_name FROM cygx_report_mapping WHERE report_type = 2  GROUP BY match_type_name`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 主题列表所有
func CygxReportMappingMatchTypeListAll() (items []*CygxReportMappingMatchType, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT match_type_name FROM cygx_report_mapping WHERE match_type_name  != ''  GROUP BY match_type_name`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 主题列表
func CygxReportMappingMatchTypeListExcel() (items []*CygxReportMappingMatchType, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT match_type_name FROM cygx_report_mapping WHERE report_type = 2 AND chart_permission_id != 23 GROUP BY match_type_name`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取产业关联的文章数量
func GetReportCount(chartPermissionId, industrialManagementId int, matchTypeName string) (recount int, err error) {
	sqlCount := `SELECT COUNT(1) as recount FROM
	cygx_industrial_article_group_management AS man_g
	INNER JOIN  cygx_article as art on art.article_id = man_g.article_id
	INNER JOIN cygx_report_mapping as re ON re.category_id = art.category_id
	WHERE re.chart_permission_id = ? 
    AND man_g.industrial_management_id = ?
	AND re.match_type_name = ` + "'" + matchTypeName + "'"
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, chartPermissionId, industrialManagementId).QueryRow(&recount)
	return
}

type ReportMappingRep struct {
	MatchTypeName string `description:"匹配类型"`
	ReportType    int    `description:"报告类型，2产业报告，1行业报告"`
	IsSummary     int    `description:"是否是纪要库，1是，0否"`
	IsReport      int    `description:"是否是报告，1是，0否"`
	CategoryId    int    `description:"分类ID"`
}

func GetMatchTypeNameById(Id int) (item *ReportMappingRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_report_mapping WHERE id = ? `
	err = o.Raw(sql, Id).QueryRow(&item)
	return
}

func GetMatchTypeNameByPermissionId(permissionId int) (item *ReportMappingRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT category_id FROM cygx_report_mapping WHERE chart_permission_id = ? AND category_id IN (1004,1005,1006,1007) AND report_type = 2 LIMIT 1 `
	err = o.Raw(sql, permissionId).QueryRow(&item)
	return
}

func GetMatchTypeNameByKeyword(keyWord string) (items []*CygxReportMapping, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			* 
		FROM
			(
			SELECT
				match_type_name AS sub_category_name 
			FROM
				cygx_report_mapping_cygx UNION
			SELECT
				"晨会精华" AS sub_category_name UNION
			SELECT
				"路演精华" AS sub_category_name UNION
			SELECT
				"重点公司" AS sub_category_name UNION
			SELECT
				"本周研究汇总" AS sub_category_name UNION
			SELECT
				"上周纪要汇总" AS sub_category_name UNION
			SELECT
				article_type_name AS sub_category_name 
			FROM
			cygx_article_type 
			) AS a
		WHERE a.sub_category_name LIKE '%` + keyWord + `%' `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
