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 GetCygxIndustryFllowByUser(uid, startSize, pageSize int) (items []*UserInteraction, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.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 = m.industrial_management_id ) AS subject_name_str 
		FROM
			cygx_industry_fllow AS f
			INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = f.industrial_management_id 
		WHERE
			user_id = ? 
			AND type = 1 
		ORDER BY
			f.create_time DESC  LIMIT ?,? `
	_, err = o.Raw(sql, uid, startSize, pageSize).QueryRows(&items)
	return
}

//关注产业数量
func GetCygxIndustryFllowCount(uid int) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_industry_fllow  WHERE   user_id = ?  AND type = 1  `
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

//判断用户是否关注这些产业ID
func GetCygxIndustryFllowCountByUser(uid int, industrialManagementd string) (count int, err error) {
	o := orm.NewOrm()
	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.NewOrm()
	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.NewOrm()
	if len(items) > 0 {
		//批量添加新的关注记录
		_, err = o.InsertMulti(len(items), items)
	}
	return
}
