package models

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

type CygxUserFreeeButton struct {
	Id            int       `orm:"column(id);pk"`
	UserId        int       `description:"用户id"`
	CompanyId     int       `description:"公司id"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	EffectiveTime string    `description:"有效隐藏时间"`
}

//添加历史信息
func AddCygxUserFreeeButton(item *CygxUserFreeeButton) (lastId int64, err error) {
	o := orm.NewOrm()
	item.ModifyTime = time.Now()
	lastId, err = o.Insert(item)
	return
}

//更新当天隐藏时间
func UpdateCygxUserFreeeButton(userId int) (err error) {
	o := orm.NewOrm()
	msql := ` UPDATE cygx_user_freee_button SET effective_time = NOW() ,modify_time = NOW() WHERE user_id = ? `
	_, err = o.Raw(msql, userId).Exec()
	return
}

func CountCygxUserFreeeButton(userId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM cygx_user_freee_button WHERE user_id = ? AND  effective_time = date_format(now(),'%Y-%m-%d')`
	err = o.Raw(sql, userId).QueryRow(&count)
	return
}

func CountCygxUserFreeeButtonByUser(userId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM cygx_user_freee_button WHERE user_id = ? `
	err = o.Raw(sql, userId).QueryRow(&count)
	return
}
