package models

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

type CygxIndustrialArticleGroupManagement struct {
	Id                     int       `orm:"column(id);pk" description:"主键ID"`
	CygxArticleId          int       `description:"cygx_article表id"`
	ArticleId              int       `description:"文章ID"`
	IndustrialManagementId int       `description:"cygx_industrial_management表的主键ID"`
	Source                 int       `description:"来源，1 活动，2专项调研"`
	CreateTime             time.Time `description:"创建时间"`
}

// IndustryArtCount 产业文章数
type IndustryArtCount struct {
	ArtNum                 int `json:"art_num" description:"文章数"`
	IndustrialManagementId int `json:"industrial_management_id" description:"产业ID"`
}

// GetIndustryArtCountByCondition 获取产业文章关联的文章数
func GetIndustryArtCountByCondition(condition string, pars []interface{}) (list []*IndustryArtCount, err error) {
	sql := `SELECT
				COUNT(1) AS art_num,
				agm.industrial_management_id
			FROM
				cygx_industrial_article_group_management AS agm
			JOIN cygx_industrial_management AS man ON man.industrial_management_id = agm.industrial_management_id
			WHERE
				1 = 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY agm.industrial_management_id`
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}

// IndustryArticleMinMaxPublishTime 产业文章最大最小发布时间
type IndustryArticleMinMaxPublishTime struct {
	IndustrialManagementId int       `description:"产业ID"`
	MinPublishTime         time.Time `description:"文章最小发布时间"`
	MaxPublishTime         time.Time `description:"文章最大发布时间"`
}

// GetIndustryArticleMinMaxPublishTime 获取产业文章最大最小发布时间
func GetIndustryArticleMinMaxPublishTime(condition string, pars []interface{}) (list []*IndustryArticleMinMaxPublishTime, err error) {
	sql := `SELECT
				a.industrial_management_id,
				MIN(b.publish_date) AS min_publish_time,
				MAX(b.publish_date) AS max_publish_time
			FROM
				cygx_industrial_article_group_management AS a
			JOIN cygx_article AS b ON a.article_id = b.article_id
			WHERE
				1 = 1`
	if condition != `` {
		sql += condition
	}
	sql += `GROUP BY a.industrial_management_id`
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}

// GetIndustrialArticleGroupManagementList  查询产业
func GetIndustrialArticleGroupManagementList(condition string, pars []interface{}) (list []*CygxIndustrialArticleGroupManagement, err error) {
	sql := `SELECT
				*
			FROM
				cygx_industrial_article_group_management AS a
			WHERE
				1 = 1 `
	if condition != `` {
		sql += condition
	}
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}

type IndustrialActivityGroupManagementRep struct {
	IndustrialManagementId int    `description:"cygx_industrial_management表的主键ID"`
	IndustryName           string `description:"产业名称"`
	ChartPermissionId      int    `description:"权限id"`
	PermissionName         string `description:"行业名称"`
	ActivityId             int    `description:"活动ID"`
}

// 列表
func GetIndustrialArticleGroupManagementListByArticleId(articleId int) (items []*IndustrialActivityGroupManagementRep, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.chart_permission_id,
			m.industrial_management_id,
			m.industry_name 
			FROM
			cygx_industrial_article_group_management AS am
			INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = am.industrial_management_id
			WHERE
			am.article_id = ?`
	_, err = o.Raw(sql, articleId).QueryRows(&items)
	return
}

type CygxIndustrialArticleGroupManagementResp struct {
	ArticleId              int       `description:"文章ID"`
	IndustrialManagementId int       `description:"cygx_industrial_management表的主键ID"`
	PublishDate            time.Time `description:"发布时间"`
}

// GetCygxIndustrialArticleGroupManagement  获取产业关联的所有文章
func GetCygxIndustrialArticleGroupManagement(condition string, pars []interface{}) (list []*CygxIndustrialArticleGroupManagementResp, err error) {
	sql := `SELECT
			mg.industrial_management_id,
			a.article_id,
			a.publish_date
		FROM
			cygx_industrial_article_group_management AS mg
			INNER JOIN cygx_article AS a ON a.article_id = mg.article_id 
		WHERE
			1 = 1 
			AND a.publish_status = 1 `
	if condition != `` {
		sql += condition
	}
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}
