package cygx

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

type CygxReportSelectionLog struct {
	ArticleSunId           int       `orm:"column(article_sun_id);pk"description:"子级报告id"`
	ArticleId              int       `description:"父级报告Id"`
	ChartPermissionId      int       `description:"行业ID"`
	CreateTime             time.Time `description:"创建时间"`
	Body                   string    `description:"内容"`
	IndustrialSubjectId    string    `description:"标的ID"`
	SubjectName            string    `description:"标的名称"`
	IndustrialManagementId string    `description:"产业资源包Id  多个用 , 隔开"`
	CompanyLabel           string    `description:"公司标签多个用{|}隔开"`
	OverviewArticleId      int       `description:"关联的综述报告ID"`
	IsNew                  int       `description:"是否为New标签"`
	IsShowOverviewArticle  int       `description:"是否展示综述报告 1展示，0隐藏"`
}

type CygxReportSelectionLogRep struct {
	Sort                string `description:"排序"`
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	ChartPermissionSort string `description:"行业排序"`
	Body                string `description:"内容"`
	ReportLink          string `description:"报告链接"`
	IcoLink             string `description:"图标链接"`
}

type AddCygxReportSelectionLog struct {
	ChartPermissionId        int      `description:"行业ID"`
	IndustrialSubjectId      string   `description:"标的ID"`
	SubjectName              string   `description:"标的名称"`
	IndustrialSubjectName    string   `description:"标的ID"`
	Body                     string   `description:"内容"`
	IndustrialManagementId   string   `description:"产业资源包Id  多个用 , 隔开"`
	IndustrialManagementName string   `description:"产业资源包Id  多个用 , 隔开"`
	CompanyLabel             []string `description:"公司标签"`
	Label                    string   `description:"公司标签"`
	OverviewArticleId        int      `description:"综述报告Id"`
	IsNew                    int      `description:"是否为New标签"`
	IsShowOverviewArticle    int      `description:"是否展示综述报告 1展示，0隐藏"`
}

type CygxReportSelectionLogResp struct {
	ChartPermissionId        int      `description:"行业ID"`
	IndustrialSubjectId      string   `description:"标的ID"`
	SubjectName              string   `description:"标的名称"`
	IndustrialSubjectName    string   `description:"标的ID"`
	Body                     string   `description:"内容"`
	IndustrialManagementId   string   `description:"产业资源包Id  多个用 , 隔开"`
	IndustrialManagementName string   `description:"产业资源包Id  多个用 , 隔开"`
	CompanyLabel             []string `description:"公司标签"`
	Label                    string   `description:"公司标签"`
	OverviewArticleId        int      `description:"综述报告Id"`
	OverviewArticleTitle     string   `description:"综述报告标题"`
	IsNew                    int      `description:"是否为New标签"`
	IsShowOverviewArticle    int      `description:"是否展示综述报告 1展示，0隐藏"`
	ChartPermissionSort      int      `description:"品种排序"`
}

type CygxReportSelectionChart struct {
	ChartPermissionId   int                           `description:"行业ID"`
	ChartPermissionName string                        `description:"行业名称"`
	List                []*CygxReportSelectionLogResp `description:"列表内容’"`
	BodyChartSummary    string                        `description:"行业核心逻辑汇总"`
}

func GetCygxReportSelectionLogRepListSon(articleId, chartPermissionId int) (items []*CygxReportSelectionLogResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
	l.*, l.company_label as label,
	l.subject_name as industrial_subject_name,
	( SELECT GROUP_CONCAT( DISTINCT m.industry_name SEPARATOR ',' ) AS sellerName FROM cygx_industrial_management AS m WHERE m.industrial_management_id IN ( l.industrial_management_id ) ) AS industrial_management_name 
FROM
	cygx_report_selection_log AS l
	LEFT JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = l.industrial_subject_id 
			WHERE  1= 1 AND l.article_id=?  AND l.chart_permission_id =?  `
	_, err = o.Raw(sql, articleId, chartPermissionId).QueryRows(&items)
	return
}

func GetCygxReportSelectionSjdpList(articleId int, typeStr string) (items []*CygxReportSelectionLogRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT l.* FROM cygx_research_summary_log AS l
			WHERE  1= 1 AND l.article_id=? AND l.type =?  `
	_, err = o.Raw(sql, articleId, typeStr).QueryRows(&items)
	return
}

type CygxReportSelectionLoglist struct {
	ArticleSunId           int       `description:"子级报告id"`
	ArticleId              int       `description:"父级报告Id"`
	ChartPermissionId      int       `description:"行业ID"`
	PermissionName         string    `description:"行业ID"`
	CreateTime             time.Time `description:"创建时间"`
	Body                   string    `description:"内容"`
	IndustrialSubjectId    string    `description:"标的ID"`
	IndustrialManagementId string    `description:"产业资源包Id  多个用 , 隔开"`
	SubjectName            string    `description:"标的名称"`
	IcoLink                string    `orm:"column(image_url)"description:"图标链接"`
	OverviewArticleId      int       `description:"关联的综述报告ID"`
	IsNew                  int       `description:"是否为New标签"`
}

// 列表
func GetReportSelectionlogListAll(articleId int) (items []*CygxReportSelectionLoglist, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT  l.* 
			FROM
			cygx_report_selection_log AS l
			INNER JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = l.industrial_subject_id 
			WHERE l.article_id = ? `
	_, err = o.Raw(sql, articleId).QueryRows(&items)
	return
}
