package cygx

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

type ChartPermission struct {
	ChartPermissionId int    `description:"权限id"`
	PermissionName    string `description:"权限名称"`
	IcoLink           string `description:"图标链接"`
	List              []*CygxIndustrialManagementNumCheckName
}

type ChartPermissionRepMore struct {
	PermissionName string `description:"名称"`
}
type ChartPermissionResp struct {
	List []*ChartPermission
}

type ChartPermissionRepMoreList struct {
	List []*ChartPermissionRepMore
}

func GetChartPermissionAll() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE product_id=2  AND is_report=1 AND is_other = 0  AND permission_type != 2  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取带有ICo的产业
func GetChartPermissionIco(condition string) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission  WHERE parent_id>0 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取带有ICo的产业
func GetChartPermissionIcoNew(condition string) (items []*CygxRSlChartPermissionIcoTmp, err error) {
	o := orm.NewOrm()
	sql := `SELECT chart_permission_id ,chart_permission_name,image_url as ico_link FROM chart_permission  WHERE parent_id>0 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取带有ICo的产业
func GetChartPermissionIcoDetail(condition string) (items []*CygxRSlChartPermissionIco, err error) {
	o := orm.NewOrm()
	sql := `SELECT chart_permission_id ,chart_permission_name,image_url as ico_link FROM chart_permission  WHERE parent_id>0 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取带有ICo的产业
func GetChartPermissionDetail(condition string) (items []*CygxReportSelectionChart, err error) {
	o := orm.NewOrm()
	sql := `SELECT chart_permission_id ,chart_permission_name,image_url as ico_link FROM chart_permission  WHERE parent_id>0 `
	if condition != "" {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 没有策略的顶级分类
func GetChartPermissionAllNoTactics() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND show_type=1 AND is_report=1 AND chart_permission_id != 23 AND is_other = 0 AND permission_type != 2 AND parent_id>0  ORDER BY sort ASC   `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetChartPermissionOtherAll() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND show_type=1 AND is_report=1  AND permission_type != 2   OR is_other = 1 AND parent_id>0  ORDER BY sort ASC`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取产业数量
func GetChartPermissionCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM chart_permission WHERE parent_id>0 `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 通过ID获取分类详情
func GetCategoryInfoById(chartPermissionId int) (item *ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE chart_permission_id=?`
	err = o.Raw(sql, chartPermissionId).QueryRow(&item)
	return
}

// 通过名称获取分类详情
func GetCategoryInfoByName(name string) (item *ChartPermission, err error) {
	//if name == "宏观" {
	//	name = "宏观经济"
	//}
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE permission_name=? AND parent_id>0`
	err = o.Raw(sql, name).QueryRow(&item)
	return
}

// 报告分类映射类型
type ReportMapping struct {
	CategoryId        int    `description:"匹配ID"`
	MatchTypeName     string `description:"匹配类型"`
	SubCategoryName   string `description:"二级分类"`
	ChartPermissionId int    `description:"权限id"`
}

func GetReportMapping() (item []*ReportMapping, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT *  FROM	cygx_report_mapping WHERE	match_type_name != '' `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

func GetReportMappingDetail(condition string, pars []interface{}) (item *ReportMapping, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT *  FROM	cygx_report_mapping WHERE 1=1 ` + condition + ` LIMIT 1 `
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}
