package models

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

type CygxIndustrialSubject struct {
	IndustrialSubjectId    int    `orm:"column(industrial_subject_id);pk" description:"标的id"`
	IndustrialManagementId int    `description:"产业id"`
	SubjectName            string `description:"标的名称"`
	CreateTime             string `description:"创建时间"`
	Source                 int    `description:"来源,1正常添加，2：通过文章添加，3通过活动添加(默认为1)"`
}

type IndustrialSubjectByArticle struct {
	IndustrialSubjectId    int    `description:"标的id"`
	IndustrialManagementId int    `description:"产业id"`
	SubjectName            string `description:"标的名称"`
	IndustryName           string `description:"产业名称"`
	ArticleId              int    `description:"文章ID"`
}

// 获取标的列表
func GetCygxIndustrialSubjectListCondition(condition string, pars []interface{}) (items []*CygxIndustrialSubject, err error) {
	o := orm.NewOrm()
	sql := `SELECT * 
		FROM
			cygx_industrial_subject WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 获取标的列表
func GetArticleGroupSubjectList(pars []interface{}, condition string) (items []*IndustrialSubjectByArticle, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			s.subject_name,
			s.industrial_subject_id,
			s.industrial_management_id,
			g.article_id 
		FROM
			cygx_industrial_article_group_subject AS g
			INNER JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = g.industrial_subject_id 
		WHERE
			1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetIndustrialSubjectDetailByIds(industrialSubjectIds []int) (list []*CygxIndustrialSubject, err error) {
	lenArr := len(industrialSubjectIds)
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_industrial_subject WHERE  industrial_subject_id IN (` + utils.GetOrmInReplace(lenArr) + `) `
	_, err = o.Raw(sql, industrialSubjectIds).QueryRows(&list)
	return
}
