package cygx

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

type CygxIndustryFllow struct {
	Id                     int       `orm:"column(id);pk"`
	IndustrialManagementId int       `description:"产业D"`
	UserId                 int       `description:"用户ID"`
	Mobile                 string    `description:"手机号"`
	Email                  string    `description:"邮箱"`
	CompanyId              int       `description:"公司id"`
	CompanyName            string    `description:"公司名称"`
	Type                   int       `description:"操作方式，1报名，2取消报名"`
	CreateTime             time.Time `description:"创建时间"`
	ModifyTime             time.Time `description:"更新时间"`
	RealName               string    `description:"用户实际名称"`
	Source                 int       `description:"来源1查研观向，2查研观向小助手,3勾选全部赛道的用户进行自动关注"`
}

// 列表
func GetCygxIndustryFllowList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxIndustryFllow, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_industry_fllow WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	if startSize+pageSize > 0 {
		sql += ` LIMIT ?,?  `
		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	} else {
		_, err = o.Raw(sql, pars).QueryRows(&items)
	}
	return
}

// 关注产业列表
func GetCygxIndustryFllowByUser(uid, startSize, pageSize int, condition string) (items []*UserInteraction, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			a.industry_name,
			f.modify_time as create_time,
			( SELECT GROUP_CONCAT( DISTINCT s.subject_name SEPARATOR '/' ) FROM cygx_industrial_subject AS s WHERE s.industrial_management_id = f.industrial_management_id ) AS subject_name_str 
		FROM
			cygx_industry_fllow AS f
			INNER JOIN cygx_industrial_management AS a ON a.industrial_management_id = f.industrial_management_id 
		WHERE
			f.user_id = ? 
			AND f.type = 1 
			AND f.source  IN (0,1,2) ` + condition +
		`ORDER BY
			f.create_time DESC  LIMIT ?,? `
	_, err = o.Raw(sql, uid, startSize, pageSize).QueryRows(&items)
	return
}

// 关注产业数量
func GetCygxIndustryFllowCount(uid int, condition string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industry_fllow  as  f
                           INNER JOIN   cygx_industrial_management  as   a  ON a.industrial_management_id  = f.industrial_management_id 
                           WHERE   f.user_id = ?  AND f.type = 1 AND  f.source   IN (0,1,2)  ` + condition
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// 判断用户是否关注这些产业ID
func GetCygxIndustryFllowCountByUser(uid int, industrialManagementd string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industry_fllow  WHERE   user_id = ?  AND type = 1 AND  industrial_management_id IN (` + industrialManagementd + `)`
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// GetUserFllowIndustrialListByUserIdAndIndustrial 通过用户ID 跟产业ID获取用户关注的产业列表
func GetUserFllowIndustrialListByUserIdAndIndustrial(userIds, industrials string) (items []*CygxIndustryFllow, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_industry_fllow 
			WHERE 1 = 1 
		AND user_id IN ( ` + userIds + ` ) 
		AND industrial_management_id IN ( ` + industrials + `) `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 批量添加
func AddCygxIndustryFllowMulti(items []*CygxIndustryFllow) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	if len(items) > 0 {
		//批量添加新的关注记录
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// 获取所有关注了该产业永续客户的openid
func GetCygxForeverUserIndustryFllowOpneidByActivityIds(activityId int) (items []*OpenIdList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
				f.* 
			FROM
				cygx_industrial_activity_group_management AS agm
				INNER JOIN cygx_industry_fllow AS f ON f.industrial_management_id = agm.industrial_management_id 
			WHERE
				agm.activity_id = ?`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 获取所有关注了该产业用户的companyIds
func GetCygxIndustryFollowCompanyIdsByActivityId(activityId int) (item *string, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT
	GROUP_CONCAT( DISTINCT f.company_id SEPARATOR ',' ) AS company_ids 
FROM
	cygx_industrial_activity_group_management AS agm
	INNER JOIN cygx_industry_fllow AS f ON f.industrial_management_id = agm.industrial_management_id 
WHERE
	agm.activity_id = ? `
	err = o.Raw(sql, activityId).QueryRow(&item)
	return
}
