package models

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

type CygxSearchKeyWordLog struct {
	Id          int       `orm:"column(id);" description:"id"`
	KeyWord     string    `description:"搜索关键词"`
	UserId      int       `description:"用户ID"`
	CreateTime  time.Time `description:"搜索时间"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	CompanyId   int       `description:"公司id"`
	CompanyName string    `description:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	Source      int       `description:"来源；1:纪要、2：图表、3：纪要/图表、4：产业资源包、5：报告、6：活动"`
}

type CygxSearchKeyWordLogRep struct {
	KeyWord string `description:"搜索关键词"`
}

//新增搜索
func AddSearchKeyWordLog(item *CygxSearchKeyWordLog) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

//修改用户搜索的相关信息
func UpdateCygxSearchKeyWordLog(wxUser *WxUserItem) (err error) {
	o := orm.NewOrm()
	var sql string
	if wxUser.Mobile != "" {
		sql = `UPDATE cygx_search_key_word SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
		_, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
	} else if wxUser.Email != "" {
		sql = `UPDATE cygx_search_key_word SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE email=? `
		_, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
	}
	return
}
