package cygx

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

type CygxActivitySignupDetail 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:"公司名称"`
	FailType              int       `description:"失败原因，0，未失败，1总人数已满，2单机构超限制，3，爽约次数超限"`
	SignupType            int       `description:"报名方式，1预约外呼，2自主拨入，3我要报名"`
	DoFailType            int       `description:"失败原因，0，未失败，1总人数已满，2单机构超限制，3，爽约次数超限"`
	OutboundMobile        string    `description:"外呼手机号"`
	CountryCode           string    `description:"手机国家区号"`
	RealName              string    `description:"用户实际名称"`
	SellerName            string    `description:"所属销售"`
	FirstMeetingTime      string    `description:"首次入会时间"`
	LastMeetingTime       string    `description:"最后退出时间"`
	Duration              string    `description:"参会时长"`
	MeetingTypeStr        string    `description:"参会方式"`
	MeetingAuthentication string    `description:"参会权鉴"`
	MeetingStatusStr      string    `description:"参会状态"`
	Position              string    `description:"职位"`
	IsMeeting             int       `description:"是否到会，1到会，0未到会"`
	UseridEntity          int       `description:"参会者身份. 1:主讲人, 2:主持人, 3:嘉宾, 4:普通参会者, 5:联席主讲人, 6:会议助理"`
	ActivityTime          string    `description:"活动时间"`
	CrmCompanyMapStatusId int       `description:"转换后的对应状态信息：1=正式客户, 2=曾使用客户, 3=其他"`
}

// 获取今天报名的用户信息，存入到参会记录表中
func GetSignupDetailBySignup(condition string, pars []interface{}) (list []*CygxActivitySignupDetail, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_signup  WHERE  1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

// 获取参会记录表列表
func GetSignupDetailList(condition string, pars []interface{}) (list []*CygxActivitySignupDetail, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_signup_detail  WHERE  1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

// 添加
func AddCygxActivitySignupDetail(items []*CygxActivitySignupDetail) (err error) {
	o, err := orm.NewOrmUsingDB("hz_cygx").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	if len(items) > 0 {
		//批量添加
		_, err = o.InsertMulti(len(items), items)
		if err != nil {
			return err
		}
	}
	return
}

// UpdateActivitySignupDetailMulti 批量修改用户报名信息且报名的人
func UpdateActivitySignupDetailMulti(items []*CygxActivitySignupDetail) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	p, err := o.Raw(` UPDATE cygx_activity_signup_detail SET 
							first_meeting_time = ?,
							last_meeting_time = ?,
							duration = ?,
							meeting_type_str = ?,
							meeting_authentication = ?,
							meeting_status_str = ?,
							position = ?,
							is_meeting = ?,
							userid_entity = ?,
							activity_time = ?,
							crm_company_map_status_id = ?
							WHERE mobile = ? AND activity_id = ? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.FirstMeetingTime,
			v.LastMeetingTime,
			v.Duration,
			v.MeetingTypeStr,
			v.MeetingAuthentication,
			v.MeetingStatusStr,
			v.Position,
			v.IsMeeting,
			v.UseridEntity,
			v.ActivityTime,
			v.CrmCompanyMapStatusId,
			v.Mobile, v.ActivityId)
		if err != nil {
			return
		}
	}
	return
}
