package models

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

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

// 获取标的列表
func GetCygxIndustrialSubjectList(subjectName string) (items []*CygxIndustrialSubject, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				s.*
			FROM
				cygx_industrial_subject as s 
				INNER JOIN  cygx_industrial_management as m ON  m.industrial_management_id = s.industrial_management_id
			WHERE
				subject_name = ? `
	_, err = o.Raw(sql, subjectName).QueryRows(&items)
	return
}

// AddCygxActiuvityGroupMulti 批量添加
func AddCygxActiuvityGroupMulti(items []*CygxIndustrialActivityGroupManagement, itemsSubject []*CygxIndustrialActivityGroupSubject) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	if len(items) > 0 {
		//批量添加关联的产业
		_, err = o.InsertMulti(len(items), items)
	}
	if len(itemsSubject) > 0 {
		//批量添加关联的标的
		_, err = o.InsertMulti(len(itemsSubject), itemsSubject)
	}
	return
}

// GetActivityIndustryRelationList 获取活动与产业关联列表
func GetActivityIndustryRelationList(condition string, pars []interface{}) (list []*CygxIndustrialActivityGroupManagement, err error) {
	sql := `SELECT
				a.activity_id,
				b.industrial_management_id
			FROM
				cygx_activity AS a
			JOIN cygx_industrial_activity_group_management AS b ON a.activity_id = b.activity_id
			WHERE
				1 = 1   AND b.source = 1  `
	if condition != `` {
		sql += condition
	}
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}

// 获取数量
func GetCygxIndustrialActivityGroupManagementCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industrial_activity_group_management as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// CygxIndustrialActivityGroupManagement 获取活动与产业关联列表
func GetCygxIndustrialActivityGroupManagementList(condition string, pars []interface{}) (list []*CygxIndustrialActivityGroupManagement, err error) {
	sql := `SELECT
			*
			FROM
			cygx_industrial_activity_group_management
			WHERE
				1 = 1 `
	if condition != `` {
		sql += condition
	}
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}

// 列表
func GetIndustrialActivityGroupManagementListByActivityId(activityId int) (items []*IndustrialActivityGroupManagementRep, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.industry_name 
			FROM
			cygx_industrial_management AS m
			INNER JOIN cygx_industrial_activity_group_management AS am ON m.industrial_management_id = am.industrial_management_id
			WHERE
			am.activity_id = ? AND m.source = 1 `
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 列表
func GetIndustrialActivityGroupManagementListByArticleIds(activityId []int) (items []*IndustrialActivityGroupManagementRep, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.industry_name,
			am.activity_id
			FROM cygx_industrial_activity_group_management  AS am
			INNER JOIN  cygx_industrial_management AS m  ON m.industrial_management_id = am.industrial_management_id
			WHERE
			am.activity_id IN  (` + utils.GetOrmInReplace(len(activityId)) + `) AND m.source = 1 `
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}
