package yb

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

// QsEventUser 全时会议用户表
type QsEventUser struct {
	QsUserId         int       `orm:"column(qs_user_id);pk" description:"自增id"`
	QsId             int       `orm:"column(qs_id)" description:"活动与全时会议的关系id"`
	UserId           int       `description:"用户id"`
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	Name             string    `description:"姓名"`
	RegisterTime     time.Time `description:"用户注册时间"`
	ViewTotal        int       `description:"报告累计阅读次数"`
	LastViewTime     time.Time `description:"报告最近一次阅读时间"`
	CompanyId        int       `description:"客户id"`
	ProductId        int       `description:"产品id"`
	CompanyName      string    `description:"客户名称"`
	Status           string    `description:"客户产品状态"`
	SellerId         int       `description:"所属销售id"`
	SellerName       string    `description:"所属销售名称"`
	CompanyViewTotal int       `description:"客户总计阅读次数"`
	CompanyRoadTotal int       `description:"客户路演次数"`
	CreateTime       time.Time `description:"记录创建时间"`
}

// TableName 表名变更
func (qsEventUserInfo *QsEventUser) TableName() string {
	return "qs_event_user"
}

// AddQsEventUser 新增全时会议用户
func AddQsEventUser(qsEventUser *QsEventUser) (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(qsEventUser)
	if err != nil {
		return
	}
	qsEventUser.QsUserId = int(id)
	return
}

// GetQsUserList 获取到会用户列表数据
func GetQsUserList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*QsEventUser, err error) {
	o := orm.NewOrm()
	sql := "select * from qs_event_user a where 1=1 "
	sql += condition
	sql += ` order by a.qs_user_id desc`

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// GetQsUserListForExport 获取到会用户列表数据
func GetQsUserListForExport(condition string, pars []interface{}) (total int, list []*QsEventUser, err error) {
	o := orm.NewOrm()
	sql := "select * from qs_event_user a where 1=1 "
	sql += condition
	sql += ` order by a.qs_user_id desc`

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}