package eta_trial

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

type EtaTrial struct {
	EtaTrialId        int       `orm:"column(eta_trial_id);pk" description:"eta试用客户id"`
	UserName          string    `description:"客户名称"`
	CompanyName       string    `description:"客户公司姓名"`
	Position          string    `description:"职位"`
	Password          string    `json:"-"`
	Account           string    `json:"-"`
	Mobile            string    `description:"手机号"`
	Enabled           int       `description:"1:有效，0:禁用"`
	ActiveTime        int       `description:"累计活跃时长"`
	LastLoginTime     time.Time `description:"最后一次登陆时间"`
	SellerId          int       `description:"销售id"`
	Seller            string    `description:"销售员名称"`
	CreateTime        time.Time
	ModifyTime        time.Time
	LastLoginDuration int `description:"最后一次登录时长"`
}

// Update 更新用户基础信息
func (item *EtaTrial) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

func GetETATrialList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*ETATrialListRespItem, err error) {
	sql := `SELECT * FROM eta_trial WHERE 1=1  `
	if sortStr == "" {
		sortStr = "ORDER BY create_time DESC "
	}
	if condition != "" {
		sql += condition
	}

	sql += sortStr + `  LIMIT ?,? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetETATrialListCount(condition string, pars []interface{}) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM eta_trial WHERE 1 = 1 `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type ETATrialListRespItem struct {
	EtaTrialId        int    `orm:"column(eta_trial_id);pk" description:"eta试用客户id"`
	UserName          string `description:"客户名称"`
	CompanyName       string `description:"客户公司姓名"`
	Position          string `description:"职位"`
	Mobile            string `description:"手机号"`
	ActiveTime        string `description:"累计活跃时长"`
	LastLoginTime     string `description:"最后一次登陆时间"`
	SellerId          int    `description:"销售id"`
	Seller            string `description:"销售员名称"`
	InterestModule    string `description:"感兴趣模块"`
	Expiration        int    `description:"账号到期时长"`
	Enabled           int    `description:"1:有效，0:禁用"`
	IndexNum          int    `description:"累计添加指标"`
	ChartNum          int    `description:"累计添加图表"`
	LoginNum          int    `description:"累计登录次数"`
	Password          string
	Account           string
	CreateTime        string
	ModifyTime        string
	LastLoginDuration string `description:"最后一次登录时长"`
}

type ETATrialListRespList struct {
	List        []*ETATrialListRespItem
	Paging      *paging.PagingItem
	ApprovalNum int
}

type ETATrialAddItem struct {
	UserName    string `description:"客户名称"`
	CompanyName string `description:"客户公司姓名"`
	Position    string `description:"职位"`
	Mobile      string `description:"手机号"`
}

type ETATrialAddReq struct {
	List []ETATrialAddItem
}

func GetETATrialByMobile(mobile string) (item *EtaTrial, err error) {
	sql := `SELECT * FROM eta_trial WHERE mobile = ? `
	o := orm.NewOrm()

	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// 新增客户
func AddETATrial(item *EtaTrial) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

func GetETATrialByAccount(account string) (items []*EtaTrial, err error) {
	sql := `SELECT * FROM eta_trial WHERE account LIKE '%` + account + `%' `
	o := orm.NewOrm()

	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func UpdateETATrialEnable(mobile string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial SET enabled=1,modify_time=NOW()  
WHERE mobile=? `
	_, err = o.Raw(sql, mobile).Exec()
	return
}
