package models

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

type CygxActivitySignin struct {
	Id           int       `orm:"column(id);pk"`
	ActivityId   int       `description:"活动ID"`
	UserId       int       `description:"用户ID"`
	CreateTime   time.Time `description:"创建时间"`
	Mobile       string    `description:"手机号"`
	Email        string    `description:"邮箱"`
	CompanyId    int       `description:"公司id"`
	CompanyName  string    `description:"公司名称"`
	BusinessCard string    `description:"名片"`
	RealName     string    `description:"用户实际名称"`
	IsSignup     int       `description:"是否报名了"`
	CountryCode  string    `description:"手机国家区号"`
	OpenId       string    `description:"Openid"`
}

type CygxActivitySigninLog struct {
	Id           int       `orm:"column(id);pk"`
	ActivityId   int       `description:"活动ID"`
	UserId       int       `description:"用户ID"`
	CreateTime   time.Time `description:"创建时间"`
	Mobile       string    `description:"手机号"`
	Email        string    `description:"邮箱"`
	CompanyId    int       `description:"公司id"`
	CompanyName  string    `description:"公司名称"`
	BusinessCard string    `description:"名片"`
	RealName     string    `description:"用户实际名称"`
	IsSignup     int       `description:"是否报名了"`
	CountryCode  string    `description:"手机国家区号"`
	OpenId       string    `description:"Openid"`
}

type CygxActivitySigninResp struct {
	ActivityId   int    `description:"活动ID"`
	ActivityName string `description:"活动名称"`
	RealName     string `description:"用户实际名称"`
	Mobile       string `description:"手机号"`
	CompanyName  string `description:"公司名称"`
	BusinessCard string `description:"名片"`
	IsNewUser    bool   `description:"是否属于新客户"`
	IsSignup     bool   `description:"是否报名了"`
}

type CygxActivitySigninDetailResp struct {
	Detail          *CygxActivitySigninResp
	IsBindingMobile bool `description:"是否绑定手机号"`
}

// 获取数量
func GetCygxActivitySigninCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_signin as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// GetCygxActivitySigninList 获取签到列表信息
func GetCygxActivitySigninList(condition string, pars []interface{}) (items []*CygxActivitySignin, err error) {
	sql := `SELECT
			*
		FROM
			cygx_activity_signin AS art
		WHERE
			1 = 1 	` + condition
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetCygxActivitySigninDetail(condition string, pars []interface{}) (item *CygxActivitySignin, err error) {
	sql := `SELECT *  FROM cygx_activity_signin as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	err = orm.NewOrm().Raw(sql, pars).QueryRow(&item)
	return
}

// 修改创建时间，做签到时间使用
func UpdatedateCygxActivitySignin(item *CygxActivitySignin) (err error) {
	sql := ` UPDATE cygx_activity_signin SET  create_time= NOW()   WHERE   open_id = ?  AND activity_id = ? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, item.OpenId, item.ActivityId).Exec()
	return
}

// 添加
func AddCygxActivitySignin(item *CygxActivitySignin) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

// 修改
func UpdateCygxActivitySignin(item *CygxActivitySignin) (err error) {
	o := orm.NewOrm()
	updateParams := make(map[string]interface{})
	updateParams["UserId"] = item.UserId
	updateParams["Mobile"] = item.Mobile
	updateParams["Email"] = item.Email
	updateParams["CompanyId"] = item.CompanyId
	updateParams["CompanyName"] = item.CompanyName
	updateParams["BusinessCard"] = item.BusinessCard
	updateParams["RealName"] = item.RealName
	updateParams["CountryCode"] = item.CountryCode
	ptrStructOrTableName := "cygx_activity_signin"
	whereParam := map[string]interface{}{"id": item.Id}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

// 添加日志
func AddCygxActivitySigninLog(item *CygxActivitySigninLog) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

type CygxActivitySigninReq struct {
	ActivityId   int    `description:"活动id"`
	CountryCode  string `description:"手机国家区号"`
	Mobile       string `description:"手机号"`
	VCode        string `description:"验证码"`
	CompanyName  string `description:"公司名称"`
	BusinessCard string `description:"名片"`
	RealName     string `description:"用户实际名称"`
	SigninType   int    `description:"签到方式，1：填写手机号/机构名称；2：上传名片"`
}
