package cygx

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

type CygxUserLabelLogRedis struct {
	UserId     int       `description:"用户ID"`
	SourceId   int       `description:"资源ID"`
	SourceType int       `description:"1：文章阅读、 2产业关注、3：活动到会、4系列关注、5专项调研活动到会。"`
	IsFllow    int       `description:"1关注、0取消关注"`
	CreateTime time.Time `description:"创建时间"`
}

type CygxUserLabel struct {
	Id         int       `orm:"column(id);pk"`
	UserId     int       `description:"用户ID"`
	CompanyId  int       `description:"公司id"`
	RealName   string    `description:"用户实际名称"`
	Mobile     string    `description:"手机号"`
	Email      string    `description:"邮箱"`
	Label      string    `description:"标签内容"`
	Weight     int       `description:"权重"`
	SourceId   int       `description:"来源ID（产业ID，系列ID）"`
	Source     int       `description:"来源1:产业、2：系列"`
	IsFollow   int       `description:"是否关注，1是，0否"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"更新时间"`
}

// 获取数量
func GetCygxUserLabelCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_label as art WHERE 1= 1   AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 添加
func AddCygxUserLabel(item *CygxUserLabel) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

// 修改是否关注
func UpdateCygxUserLabelIsFollow(isFollow, sourceId, source, userId int, label string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_user_label SET is_follow=?,label = ? , modify_time=NOW()  WHERE source_id=? AND source = ? AND user_id = ? `
	_, err = o.Raw(sql, isFollow, label, sourceId, source, userId).Exec()
	return
}

// 修改对应标签权重+1
func UpdateCygxUserLabelWeight(sourceId, source, userId int, label string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_user_label SET weight = weight +1 ,label = ? , modify_time=NOW()  WHERE source_id=? AND source = ? AND  user_id = ? `
	_, err = o.Raw(sql, label, sourceId, source, userId).Exec()
	return
}

// 列表
func GetCygxUserLabelList(condition string, pars []interface{}) (items []*CygxUserLabel, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_user_label as art WHERE 1= 1   AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)	`
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
