package yb

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

// ComeinEventUser 进门会议用户表
type ComeinEventUser struct {
	ComeinUserId     int       `orm:"column(comein_user_id);pk" description:"自增id"`
	ComeinEventId    int       `orm:"column(comein_event_id)" description:"活动与进门会议的关系id"`
	ComeinDataId     int       `orm:"column(comein_data_id)" description:"进门会议的数据id，用作数据去重"`
	UserId           int       `description:"用户id"`
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	Name             string    `description:"姓名"`
	FirstWatchTime   time.Time `description:"首次入会时间"`
	LastWatchTime    time.Time `description:"最后退出会议时间"`
	JoinTime         int       `description:"会议参与时长"`
	AuthInfo         string    `description:"用户参与鉴权"`
	JoinType         int       `description:"参与方式 1：网络 2：电话"`
	DataType         int       `description:"数据类型，1：直播 2：回放"`
	RegisterTime     time.Time `description:"用户注册时间"`
	ViewTotal        int       `description:"报告累计阅读次数"`
	LastViewTime     time.Time `description:"报告最近一次阅读时间"`
	CompanyId        int       `description:"客户id"`
	ProductId        int       `description:"产品id"`
	CompanyName      string    `description:"客户名称"`
	Occupation       string    `description:"职位"`
	Status           string    `description:"客户产品状态"`
	SellerId         int       `description:"所属销售id"`
	SellerName       string    `description:"所属销售名称"`
	CompanyViewTotal int       `description:"客户总计阅读次数"`
	CompanyRoadTotal int       `description:"客户路演次数"`
	CreateTime       time.Time `description:"记录创建时间"`
}

// TableName 表名变更
func (comeinEventUserInfo *ComeinEventUser) TableName() string {
	return "comein_event_user"
}

// AddComeinEventUser 新增进门会议用户
func AddComeinEventUser(comeinEventUserInfo *ComeinEventUser) (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(comeinEventUserInfo)
	if err != nil {
		return
	}
	comeinEventUserInfo.ComeinUserId = int(id)
	return
}

// GetQsUserList 获取到会用户列表数据
func GetComeinUserList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*ComeinEventUser, err error) {
	o := orm.NewOrm()
	sql := "select * from comein_event_user a where 1=1 "
	sql += condition
	sql += ` order by a.comein_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
}

// GetComeinEventUserByComeinDataId 根据进门会议的数据id获取记录
func GetComeinEventUserByComeinDataId(comeinId int) (item *ComeinEventUser, err error) {
	o := orm.NewOrm()
	sql := "select * from comein_event_user where comein_data_id=? "
	err = o.Raw(sql, comeinId).QueryRow(&item)
	return
}

// GetComeinUserListForExport 获取到会用户列表数据
func GetComeinUserListForExport(condition string, pars []interface{}) (total int, list []*ComeinEventUser, err error) {
	o := orm.NewOrm()
	sql := "select * from comein_event_user a where 1=1 "
	sql += condition
	sql += ` order by a.comein_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
}
