package models

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)  `
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_label as art WHERE 1= 1   `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

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

// 批量添加
func CygxUserLabelMulti(items []*CygxUserLabel) (err error) {
	o := orm.NewOrm()
	if len(items) > 0 {
		//批量添加新的记录
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

// 修改是否关注
func UpdateCygxUserLabelIsFollow(isFollow, sourceId, source, userId int, label string) (err error) {
	o := orm.NewOrm()
	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.NewOrm()
	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
}

// 把所有用户标签权重设置为0
func UpdateCygxUserLabelWeightAll() (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_user_label SET weight = 0 `
	_, err = o.Raw(sql).Exec()
	return
}

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

// UpdateCygxUserLabelMulti 批量修改用户标签权重
func UpdateCygxUserLabelMulti(items []*CygxUserLabel) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw(` UPDATE cygx_user_label SET weight = ? WHERE user_id = ? AND label = ? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.Weight,
			v.UserId,
			v.Label)
		if err != nil {
			return
		}
	}
	return
}