package cygx

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

type CygxIndustrialArticleGroupSubject struct {
	Id                  int       `orm:"column(id);pk" description:"主键ID"`
	CygxArticleId       int       `description:"cygx_article表的主键ID"`
	ArticleId           int       `description:"cygx_article表的文章ID"`
	IndustrialSubjectId int       `description:"cygx_industrial_subject表的文章ID"`
	CreateTime          time.Time `description:"创建时间"`
}

// 新增
func AddCygxIndustrialArticleGroupSubject(item *CygxIndustrialArticleGroupSubject) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	if err != nil {
		return
	}
	return
}

func GetSubjectIds(articleId int) (subjects string, err error) {
	sql := ` SELECT
			GROUP_CONCAT( DISTINCT industrial_subject_id ORDER BY id ASC SEPARATOR ',' ) AS subjects 
			FROM
			cygx_industrial_article_group_subject WHERE article_id = ?`
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sql, articleId).QueryRow(&subjects)
	return
}

func GetArticleIdsBySubjectId(industrialSubjectId string) (articleId string, err error) {
	sql := ` SELECT
			GROUP_CONCAT( DISTINCT article_id ORDER BY id ASC SEPARATOR ',' ) AS articleId 
			FROM
			cygx_industrial_article_group_subject WHERE industrial_subject_id IN (` + industrialSubjectId + `)`
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sql).QueryRow(&articleId)
	return
}

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

type SubjectArticleGroupManagementRep struct {
	IndustrialSubjectId int    `description:"产业id"`
	SubjectName         string `description:"标的名称"`
	ArticleId           int    `description:"cygx_article表的文章ID"`
}

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

// 列表
func GetSubjectArticleGroupManagementListBysubjectIds(subjectIds []int) (items []*SubjectArticleGroupManagementRep, err error) {
	lenArr := len(subjectIds)
	if lenArr == 0 {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
  			ag.article_id
			FROM
			cygx_industrial_article_group_subject AS ag
			INNER JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = ag.industrial_subject_id 
			WHERE
			ag.industrial_subject_id  IN (` + utils.GetOrmInReplace(lenArr) + `)`
	_, err = o.Raw(sql, subjectIds).QueryRows(&items)
	return
}

type SubjectlArticleGroupResp struct {
	ArticleId              int    `description:"文章ID"`
	IndustrialManagementId int    `description:"cygx_industrial_management表的主键ID"`
	IndustrialSubjectId    int    `description:"标的id"`
	SubjectName            string `description:"标的名称"`
}

// 列表
func GetSubjectArticleGroupListByarticleIds(articleIds string) (items []*SubjectlArticleGroupResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			sg.*,
			s.subject_name,
			s.industrial_management_id 
		FROM
			cygx_industrial_article_group_subject AS sg
			INNER JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = sg.industrial_subject_id 
		WHERE
			1 = 1 
			AND sg.article_id IN (` + articleIds + `)  GROUP BY sg.article_id,sg.industrial_subject_id`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 列表
func GetSubjectArticleGroupListByarticleIdsArr(articleIds []int) (items []*SubjectlArticleGroupResp, err error) {
	lenArr := len(articleIds)
	if lenArr == 0 {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			sg.*,
			s.subject_name,
			s.industrial_management_id 
		FROM
			cygx_industrial_article_group_subject AS sg
			INNER JOIN cygx_industrial_subject AS s ON s.industrial_subject_id = sg.industrial_subject_id 
		WHERE
			1 = 1 
			AND sg.article_id IN  (` + utils.GetOrmInReplace(lenArr) + `)    GROUP BY sg.article_id,sg.industrial_subject_id`
	_, err = o.Raw(sql, articleIds).QueryRows(&items)
	return
}
