package models

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

type CygxYanxuanSpecialFollow struct {
	CygxYanxuanSpecialFollowId int       `orm:"column(cygx_yanxuan_special_follow_id);pk"`
	UserId                     int       // 用户ID
	FollowUserId               int       // 被关注用户ID
	Mobile                     string    // 手机号
	Email                      string    // 邮箱
	CompanyId                  int       // 公司ID
	CompanyName                string    // 公司名称
	RealName                   string    // 用户实际名称
	SellerName                 string    // 所属销售
	CreateTime                 time.Time // 创建时间
	ModifyTime                 time.Time // 修改时间
	RegisterPlatform           int       // 来源 1小程序，2：网页
	YanxuanSpecialId           int       // cygx_yanxuan_special 表主键ID
}

type FollowCygxYanxuanSpecialReq struct {
	FollowUserId int // 被关注的用户id
	Status       int // 1关注2取消关注
	SpecialId    int // 研选专栏Id
}

func AddCygxYanxuanSpecialFollow(item *CygxYanxuanSpecialFollow) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

func DelCygxYanxuanSpecialFollow(userId, followUserId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_yanxuan_special_follow WHERE user_id=? AND follow_user_id=? `
	_, err = o.Raw(sql, userId, followUserId).Exec()
	return
}

func GetCygxYanxuanSpecialFollowOpenIdList(followUserId int) (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	cr.*,
	c.user_id 
FROM
	user_record AS c
	INNER JOIN cygx_user_record AS cr ON cr.union_id = c.union_id
	INNER JOIN cygx_yanxuan_special_follow AS cf ON cf.user_id = c.user_id 
	AND cf.follow_user_id = ? 
WHERE
	create_platform = 4 `
	_, err = o.Raw(sql, followUserId).QueryRows(&items)
	return
}

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

// 获取数量判断用户是否关注专栏
func GetCygxYanxuanSpecialFollowCountByUser(userId, followUserId int) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_yanxuan_special_follow as a WHERE user_id=? AND follow_user_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, userId, followUserId).QueryRow(&count)
	return
}
