package cygx

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/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:"创建时间"`
}

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

type IndustryAndSubjectName struct {
	IndustryNames  string `description:"产业名称"`
	SubjectNames   string `description:"标的名称"`
	TemporaryLabel string `description:"临时标签"`
}

// 列表
func GetIndustrialActivityGroupManagementList(activityId, source int) (items []*IndustrialActivityGroupManagementRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			m.chart_permission_id,
			m.industrial_management_id,
			m.industry_name 
			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 = ? AND am.source = ?`
	_, err = o.Raw(sql, activityId, source).QueryRows(&items)
	return
}

// 根据产业ID获取关联的对应活动
func GetIndustrialActivityGroupManagementListByIndustriaId(industrialId, source int) (items []*IndustrialActivityGroupManagementRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			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.industrial_management_id = ? AND am.source = ?`
	_, err = o.Raw(sql, industrialId, source).QueryRows(&items)
	return
}

// 获取产业关联的活动数量
func GetIndustrialManagementGroupActCount(industrialManagementId int) (count int, err error) {
	sqlCount := `SELECT
				COUNT( 1 ) AS count 
				FROM
				cygx_industrial_activity_group_management AS am
				INNER JOIN cygx_activity AS a ON a.activity_id = am.activity_id 
				INNER JOIN cygx_industrial_management as m ON m.industrial_management_id = am.industrial_management_id
				WHERE
				am.industrial_management_id =? 
				AND a.publish_status = 1 `
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, industrialManagementId).QueryRow(&count)
	return
}

func GetActivityIdsByIndustrialManagementId(industrialManagementId string) (activityId string, err error) {
	sql := ` SELECT
			GROUP_CONCAT( DISTINCT activity_id ORDER BY id ASC SEPARATOR ',' ) AS activityId 
			FROM
			cygx_industrial_activity_group_management WHERE industrial_management_id IN (` + industrialManagementId + `)`
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sql).QueryRow(&activityId)
	return
}

type IndustrialActivityGroupResp struct {
	ActivityId             int    `description:"活动ID"`
	IndustrialManagementId int    `description:"cygx_industrial_management表的主键ID"`
	IndustryName           string `description:"产业名称"`
}

// 列表
func GetIndustrialActivityGroupListByactivityIds(activityIds string) (items []*IndustrialActivityGroupResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT  mg.*,m.industry_name  FROM
	        cygx_industrial_activity_group_management AS mg
	      	 INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = mg.industrial_management_id 
			WHERE 1 = 1 
			AND mg.activity_id IN (` + activityIds + `) AND mg.source = 1 GROUP BY  mg.activity_id,mg.industrial_management_id`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 列表
func GetIndustrialActivityGroupListByactivityIdsArr(activityIds []int, source int) (items []*IndustrialActivityGroupResp, err error) {
	lenArr := len(activityIds)
	if lenArr == 0 {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT  mg.*,m.industry_name  FROM
	        cygx_industrial_activity_group_management AS mg
	      	 INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = mg.industrial_management_id 
			WHERE 1 = 1 
			AND mg.activity_id IN (` + utils.GetOrmInReplace(lenArr) + `) AND mg.source = ? GROUP BY  mg.activity_id,mg.industrial_management_id`
	_, err = o.Raw(sql, activityIds, source).QueryRows(&items)
	return
}

type IndustryActCountGroupByType struct {
	ActivityType  string `json:"activity_type" description:"活动类型名称"`
	ActivityCount int    `json:"activity_count" description:"关联的活动数"`
}

// GetIndustryActCountGroupByType 获取产业关联的活动数-根据活动类型分组
func GetIndustryActCountGroupByType(industryId int) (list []*IndustryActCountGroupByType, err error) {
	sqlCount := `SELECT
					COUNT(1) AS activity_count,
					CASE
						WHEN a.chart_permission_name LIKE "%研选%" THEN
							"研选活动"
						ELSE
							"弘则活动"
						END AS activity_type
				FROM
					cygx_industrial_activity_group_management AS am
				INNER JOIN cygx_activity AS a ON a.activity_id = am.activity_id
				INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = am.industrial_management_id
				WHERE
					am.industrial_management_id = ? AND a.publish_status = 1
				GROUP BY
					activity_type `
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sqlCount, industryId).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.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

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