package models

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

type CygxUserYanxuanPermission struct {
	UserYanxuanPermissionId int64     `orm:"column(user_yanxuan_permission_id);pk"`
	UserId                  int       `description:"用户ID"`
	CompanyId               int       `description:"权限开始日期"`
	StartDate               string    `description:"权限开始日期"`
	EndDate                 string    `description:"权限结束日期"`
	Status                  string    `description:"'试用','未开通'"`
	Mobile                  string    `description:"手机号"`
	Email                   string    `description:"邮箱"`
	RealName                string    `description:"用户实际名称"`
	CompanyName             string    `description:"公司名称"`
	AdminId                 int       `description:"后台添加人员ID"`
	AdminRealName           string    `description:"后台添加人员姓名"`
	CreatedTime             time.Time `description:"创建时间"`
	LastUpdatedTime         time.Time `description:"修改时间根据时间戳自动更新"`
	ModifyTime              time.Time `description:"修改时间"`
}

type CygxUserYanxuanPermissionLog struct {
	UserYanxuanPermissionId int64     `orm:"column(user_yanxuan_permission_log_id);pk"`
	UserId                  int       `description:"用户ID"`
	CompanyId               int       `description:"权限开始日期"`
	StartDate               string    `description:"权限开始日期"`
	EndDate                 string    `description:"权限结束日期"`
	Status                  string    `description:"'试用','未开通'"`
	Mobile                  string    `description:"手机号"`
	Email                   string    `description:"邮箱"`
	RealName                string    `description:"用户实际名称"`
	CompanyName             string    `description:"公司名称"`
	AdminId                 int       `description:"后台添加人员ID"`
	AdminRealName           string    `description:"后台添加人员姓名"`
	CreatedTime             time.Time `description:"创建时间"`
	LastUpdatedTime         time.Time `description:"修改时间根据时间戳自动更新"`
	ModifyTime              time.Time `description:"修改时间"`
}

// 根据用户ID获取是否开通过研选权限
func GetCygxUserYanxuanPermissionCountByUserId(userId int) (count int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT COUNT(1) FROM cygx_user_yanxuan_permission WHERE  status = '试用'  AND user_id = ?  `
	err = o.Raw(sql, userId).QueryRow(&count)
	return
}

// 列表
func GetCygxUserYanxuanPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxUserYanxuanPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_user_yanxuan_permission WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 根据用户ID获取研选开通详情
func GetCygxUserYanxuanPermissionDetailByUserId(userId int) (item *CygxUserYanxuanPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_user_yanxuan_permission  WHERE status = '试用' AND user_id = ? `
	err = o.Raw(sql, userId).QueryRow(&item)
	return
}

// UpdateCygxUserYanxuanPermissionToClose 把权限到期的状态进行更新
func UpdateCygxUserYanxuanPermissionToClose() (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_user_yanxuan_permission SET status = '未开通' WHERE  status = '试用'  AND end_date < ? `
	_, err = o.Raw(sql, time.Now().Format(utils.FormatDate)).Exec()
	return
}
