package models

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

type CygxActivitySignup 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:"所属销售"`
}

type SignupStatus struct {
	SignupStatus      string `description:"返回状态：人数已满：FullStarffed、单机构超过两人：TwoPeople、爽约次数过多：BreakPromise、超时：Overtime 、成功：Success"`
	GoFollow          bool   `description:"是否去关注"`
	SignupType        int    `description:"报名方式，1预约外呼，2我要报名"`
	ActivityId        int    `description:"活动ID"`
	HaqveJurisdiction bool   `description:"是否有权限"`
	OperationMode     string `description:"操作方式 Apply:立即申请、Call:拨号 为空则为有权限"`
	HasPermission     int    `description:"操作方式，1:有该行业权限,正常展示，2：无该行业权限，3：潜在客户，未提交过申请，4：潜在客户,已提交过申请"`
	PopupMsg          string `description:"权限弹窗信息"`
	SellerMobile      string `description:"销售电话"`
	MsgType           string `description:"Type ： 类型 , Industry ： 行业"`
	SellerName        string `description:"销售姓名"`
	Mobile            string `description:"外呼手机号"`
	CountryCode       string `description:"外呼手机号区号"`
	GoOutboundMobile  bool   `description:"是否去绑定手机号"`
	GoBindEmail       bool   `description:"是否去绑定邮箱"`
	IsResearch        bool   `description:"是否属于研选"`
	IsResearchSpecial bool   `description:"是否属于特殊的研选"`
	ActivityTypeName  string `description:"活动类型名称"`
	ActivityTime      string `description:"活动时间"`
	ActivityType      int    `description:"活动线上线下类型 1线上，0 线下"`
}

// 我的日程
type CygxMySchedule 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:"公司名称"`
}

// 报名记录日志
type CygxActivitySignupLog 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:"公司名称"`
	Type        int       `description:"操作方式，1报名，2取消报名"`
}

// 添加报名信息
func AddActivitySignup(item *CygxActivitySignup) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	var count int
	var countMySchedule int
	sql := `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countMySchedule)
	if err != nil {
		return
	}
	if countMySchedule == 0 {
		itemMy := new(CygxMySchedule)
		itemMy.UserId = item.UserId
		itemMy.ActivityId = item.ActivityId
		itemMy.CreateTime = time.Now()
		itemMy.Mobile = item.Mobile
		itemMy.Email = item.Email
		itemMy.CompanyId = item.CompanyId
		itemMy.CompanyName = item.CompanyName
		lastId, err = o.Insert(itemMy)
		if err != nil {
			return
		}
	}
	sql = `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
	if err != nil {
		return
	}
	if count > 0 {
		sql := `UPDATE cygx_activity_signup SET is_cancel = 0 ,do_fail_type = 0, signup_type=? WHERE user_id=?  AND activity_id=? `
		_, err = o.Raw(sql, item.SignupType, item.UserId, item.ActivityId).Exec()
	} else {
		lastId, err = o.Insert(item)
	}
	if err != nil {
		return
	}
	itemLog := new(CygxActivitySignupLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 1
	lastId, err = o.Insert(itemLog)
	return
}

// 仅绑定邮箱的用户修改报名后，添加报名信息
func AddActivitySignupFromEmail(item *CygxActivitySignup) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	var count int
	var countMySchedule int
	ow := orm.NewOrmUsingDB("weekly_report")
	sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ? ,is_msg_outbound_mobile = 1   WHERE user_id=? `
	_, err = ow.Raw(sql, item.OutboundMobile, item.CountryCode, item.UserId).Exec()
	if err != nil {
		return
	}
	sql = `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countMySchedule)
	if err != nil {
		return
	}
	if countMySchedule == 0 {
		itemMy := new(CygxMySchedule)
		itemMy.UserId = item.UserId
		itemMy.ActivityId = item.ActivityId
		itemMy.CreateTime = time.Now()
		itemMy.Mobile = item.Mobile
		itemMy.Email = item.Email
		itemMy.CompanyId = item.CompanyId
		itemMy.CompanyName = item.CompanyName
		lastId, err = o.Insert(itemMy)
		if err != nil {
			return
		}
	}
	sql = `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
	if err != nil {
		return
	}
	if count > 0 {
		sql := `UPDATE cygx_activity_signup SET is_cancel = 0 ,do_fail_type = 0, signup_type=? WHERE user_id=?  AND activity_id=? `
		_, err = o.Raw(sql, item.SignupType, item.UserId, item.ActivityId).Exec()
	} else {
		lastId, err = o.Insert(item)
	}
	if err != nil {
		return
	}
	itemLog := new(CygxActivitySignupLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 1
	lastId, err = o.Insert(itemLog)
	return
}

// 添加报名信息
func AddActivitySignupNoSchedule(item *CygxActivitySignup) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	lastId, err = o.Insert(item)
	if err != nil {
		return
	}
	itemLog := new(CygxActivitySignupLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 1
	lastId, err = o.Insert(itemLog)
	return
}

// 获取用户报名成功数量
func GetActivitySignupCount(uid, activityId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE is_cancel=0  AND do_fail_type = 0 AND user_id=? AND activity_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
	return
}

// 获取用户报名数量
func GetActivitySignupByUserCount(uid, activityId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE is_cancel=0 AND user_id=? AND activity_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
	return
}

// 获取除弘则以外所有成功报名的用户数量
func GetActivitySignupSuccessByUserCountNoHz(activityId int) (count int, err error) {
	sqlCount := `SELECT
	COUNT( 1 ) AS count 
FROM
	cygx_activity_signup 
WHERE
	is_cancel = 0 
	AND company_id != 16 
	AND do_fail_type = 0 
	AND activity_id =  ? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, activityId).QueryRow(&count)
	return
}

// 获取用户报名禁止数量
func GetActivitySignupByUserRestrictCount(uid, activityId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE do_fail_type=3 AND user_id=? AND activity_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
	return
}

// 获取某一活动已经报名的数量
func GetActivitySignupSuccessCount(activityId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE is_cancel=0 AND do_fail_type = 0 AND activity_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, activityId).QueryRow(&count)
	return
}

// 获取某一活动某个机构已经报名的数量
func GetActivitySignupCompanyCount(activityId, companyId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE is_cancel=0 AND fail_type = 0 AND activity_id=? AND company_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, activityId, companyId).QueryRow(&count)
	return
}

// 获取某一用户是否被限制报名
func GetUserRestrictCount(mobile string) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_restrict_signup WHERE is_restrict=1  AND mobile=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, mobile).QueryRow(&count)
	return
}

// 获取某一用户的日程数量
func GetUserMeetingReminderCount(uid int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE  user_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// 获取某一用户的报名的数量
func GetUserSignupCount(uid int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_signup_log WHERE  user_id=? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// 取消报名
func CancelActivitySignup(item *CygxActivitySignup) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	//判断是否删除我的日程
	var countReminder int
	var countAppointment int
	sql := `SELECT COUNT(1) AS count FROM cygx_activity_meeting_reminder WHERE is_cancel = 0  AND  user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countReminder)

	sql = `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE  user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countAppointment)
	if err != nil {
		return
	}
	if countReminder == 0 && countAppointment == 0 {
		sql = `DELETE  FROM cygx_my_schedule   WHERE user_id=?  AND activity_id=? `
		_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
		if err != nil {
			return
		}
	}
	sql = `DELETE  FROM cygx_activity_signup   WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
	if err != nil {
		return
	}
	itemLog := new(CygxActivitySignupLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 2
	lastId, err = o.Insert(itemLog)
	return
}

// 详情
func GetActivitySignupDetail(activityId, uid int) (item *CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_signup WHERE  activity_id = ? AND user_id =? `
	err = o.Raw(sql, activityId, uid).QueryRow(&item)
	return
}

func GetActivitySignuListByUser(condition string, pars []interface{}) (item []*CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM
			cygx_activity_signup  
			WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

// 解除报名限制之后二次报名
func AddActivitySignupByRestrict(item *CygxActivitySignup) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	itemMy := new(CygxMySchedule)
	itemMy.UserId = item.UserId
	itemMy.ActivityId = item.ActivityId
	itemMy.CreateTime = time.Now()
	itemMy.Mobile = item.Mobile
	itemMy.Email = item.Email
	itemMy.CompanyId = item.CompanyId
	itemMy.CompanyName = item.CompanyName
	lastId, err = o.Insert(itemMy)
	if err != nil {
		return
	}
	sql := `UPDATE cygx_activity_signup SET do_fail_type = 0 , fail_type=0 WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
	itemLog := new(CygxActivitySignupLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 1
	lastId, err = o.Insert(itemLog)
	return
}

// 列表
func GetActivitySignupListAll() (items []*CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_signup `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 修改是否推送消息状态
func UPdateSignup(item *CygxActivitySignup) (err error) {
	sql := ` UPDATE cygx_activity_signup SET  outbound_mobile= ? , country_code=86  WHERE id = ?`
	o := orm.NewOrm()
	_, err = o.Raw(sql, item.Mobile, item.Id).Exec()
	return
}

// 获取数量
func GetActivityCountByIdWithUid(activityId, Uid int) (count int, err error) {
	o := orm.NewOrm()
	sqlCount := `SELECT COUNT(1) AS count  FROM cygx_activity_signup WHERE activity_id = ? AND user_id = ?`
	err = o.Raw(sqlCount, activityId, Uid).QueryRow(&count)
	return
}

// 获取用户报名数量
func GetActivitySignupNomeetingCount(activityId int) (count int, err error) {
	sqlCount := `SELECT
	COUNT( 1 ) count 
FROM
	cygx_activity_signup AS s
	INNER JOIN cygx_activity AS a ON a.activity_id = s.activity_id 
WHERE
	1 = 1 
	AND a.is_limit_people > 0 
	AND s.is_meeting = 0 
	AND a.is_submit_meeting = 1
	AND a.activity_id = ?`
	o := orm.NewOrm()
	err = o.Raw(sqlCount, activityId).QueryRow(&count)
	return
}

func GetUserMeetingMobile(activityId int) (items []*CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
FROM
	cygx_activity_signup AS s 
WHERE
	s.is_meeting = 1 
	AND s.activity_id = ?`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 获取用户是否被限制报名
func GetRestrictSignupCountByUid(uid int) (count int, err error) {
	sqlCount := `SELECT COUNT( 1 ) count FROM cygx_activity_restrict_signup  WHERE user_id = ?`
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// 获取用户爽约次数
func GetActivitySignupNomeetingCountByUid(uid int) (count int, err error) {
	sqlCount := `SELECT
	COUNT( 1 ) count 
FROM
	cygx_activity_signup AS s
	INNER JOIN cygx_activity AS a ON a.activity_id = s.activity_id 
WHERE
	1 = 1 
	AND a.is_limit_people > 0 
	AND s.is_meeting = 0 
	AND a.is_submit_meeting = 1
	AND s.do_fail_type = 0
	AND s.user_id = ?`
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid).QueryRow(&count)
	return
}

// 删除
func DeleteCygxActivityRestrictSignup(uid int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM cygx_activity_restrict_signup  WHERE user_id=?`
	_, err = o.Raw(sql, uid).Exec()
	return
}

type CygxActivitySignupList struct {
	Id          int    `orm:"column(id);pk"`
	UserId      int    `description:"用户id"`
	ActivityId  int    `description:"活动ID"`
	CompanyName string `description:"公司名称"`
	RealName    string `description:"姓名"`
	CreateTime  string `description:"创建时间"`
	IsMeeting   int    `description:"是否到会 ，1是， 0否"`
	Operation   bool   `description:"操作按钮，true,到会，false 未到会"`
	Channel     int    `description:"报名渠道，0 空降、 1小程序报名"`
	SellerName  string `description:"所属销售"`
	Mobile      string `description:"手机号"`
	Email       string `description:"邮箱"`
	CompanyId   int    `description:"公司id"`
	SigninTime  string `description:"签到时间"`
}

type CygxActivitySignupResp struct {
	CompanyName string `description:"公司名称"`
	RealName    string `description:"姓名"`
	IsMeeting   int    `description:"是否到会 ，1是， 0否"`
	SigninTime  string `description:"签到时间"`
}

// 获取用户报名列表
func GetActivitySignupNomeetingCountList(activityId int) (items []*CygxActivitySignupList, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	s.*
FROM
	cygx_activity_signup AS s
	INNER JOIN cygx_activity AS a ON a.activity_id = s.activity_id 
WHERE
	1 = 1
	AND s.is_meeting = 0 
	AND a.is_submit_meeting = 1
	AND a.activity_id = ?`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

type UserAndCompanyName struct {
	UserId              int `orm:"column(user_id);pk"`
	Mobile              string
	Email               string
	CompanyId           int
	CompanyName         string `description:"公司名称"`
	CountryCode         string `description:"手机国家区号"`
	OutboundMobile      string `description:"外呼手机号"`
	OutboundCountryCode string `description:"外呼手机号区号"`
}

func GetUserAndCompanyNameList(uid int) (item *UserAndCompanyName, err error) {
	o := orm.NewOrm()
	sql := ` SELECT u.*,c.company_name 
			FROM wx_user AS u
	INNER JOIN company AS c ON c.company_id = u.company_id
	WHERE user_id =?`
	err = o.Raw(sql, uid).QueryRow(&item)
	return
}

type CygxActivityRestrictSignup struct {
	Id          int       `orm:"column(id);pk"`
	UserId      int       `description:"用户id,多个用,隔开"`
	CreateTime  time.Time `description:"创建时间"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱号"`
	CompanyId   int       `description:"公司ID"`
	CompanyName string    `description:"公司名称"`
	IsRestrict  int       `description:"是否限制报名，1是，0否"`
}

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

// 获取列表信息根据手机号分组
func GetCygxActivitySignupByMobileList(condition string) (items []*CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_signup_detail  WHERE  1= 1 ` + condition + `  GROUP BY mobile  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 修改用户报名的相关信息
func UpdateCygxActivitySignup(wxUser *WxUserItem) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_activity_signup_detail 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()
	return
}

// UpdateCygxActivitySignupisMeet 修改易董的活动，用户已到会
func UpdateCygxActivitySignupisMeet(activityId int, mobile string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_activity_signup SET is_meeting=1 WHERE activity_id=?  AND mobile = ? `
	_, err = o.Raw(sql, activityId, mobile).Exec()
	return
}

// UpdateCygxActivitySignupisMeetList 批量修改易董的活动，用户已到会
func UpdateCygxActivitySignupisMeetList(condition string, pars []interface{}) (err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrm()
	sql := `UPDATE cygx_activity_signup SET is_meeting=1 WHERE  1= 1 ` + condition
	_, err = o.Raw(sql, pars).Exec()
	return
}

// GetCygxCygxActivitySignupList 获取报名列表信息
func GetActivitySignupList(condition string, pars []interface{}) (items []*CygxActivitySignup, err error) {
	sql := `SELECT
			*
		FROM
			cygx_activity_signup AS v
		WHERE
			1 = 1 	` + condition
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// GetCygxCygxActivitySignupList 获取报名列表信息
func GetActivitySignupInnerActivityList(condition string, pars []interface{}) (items []*CygxActivitySignup, err error) {
	sql := `SELECT
			*
		FROM
			cygx_activity_signup AS v
			INNER JOIN cygx_activity as a  ON a.activity_id = v.activity_id
		WHERE
			1 = 1 	` + condition
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// UpdateCygxActivitySignupIsMeeting 批量标记用户的到会信息
func UpdateCygxActivitySignupIsMeeting(items []*CygxActivityAttendanceDetail) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw(` UPDATE cygx_activity_signup SET
							is_meeting = 1
							WHERE outbound_mobile = ? AND activity_id = ? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.Mobile,
			v.ActivityId)
		if err != nil {
			return
		}
	}
	return
}
