package models

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

type CygxActivitySpecialTrip struct {
	Id               int       `orm:"column(id);pk"`
	UserId           int       `description:"用户id,多个用,隔开"`
	ActivityId       int       `description:"活动ID"`
	CreateTime       time.Time `description:"创建时间"`
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱号"`
	CompanyId        int       `description:"公司ID"`
	CompanyName      string    `description:"公司名称"`
	RealName         string    `description:"用户实际名称"`
	SellerName       string    `description:"所属销售"`
	AdminId          int       `description:"销售/管理员ID"`
	Source           int       `description:"来源,1小程序,2后台添加"`
	OutboundMobile   string    `description:"外呼手机号"`
	CountryCode      string    `description:"手机国家区号"`
	IsCancel         string    `description:"是否取消,1是,0否"`
	IsValid          int       `description:"参会报名是否有效 1:是,0"`
	RegisterPlatform int       `description:"来源 1小程序,2:网页"`
}

type CygxActivitySpecialTripResp struct {
	Id             int    `description:"ID"`
	UserId         int    `description:"用户id"`
	ActivityId     int    `description:"活动ID"`
	CreateTime     string `description:"创建时间"`
	Mobile         string `description:"手机号"`
	Email          string `description:"邮箱号"`
	CompanyId      int    `description:"公司ID"`
	CompanyName    string `description:"公司名称"`
	RealName       string `description:"用户实际名称"`
	SellerName     string `description:"所属销售"`
	OutboundMobile string `description:"外呼手机号"`
	CountryCode    string `description:"手机国家区号"`
}

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

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

// 获取某一用户有效报名数量
func GetUserActivitySpecialTripIsValidCount(uid, activityId int) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_special_trip  WHERE  user_id=?  AND   activity_id =?  AND  is_valid = 1 `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
	return
}

// 获取某一活动的报名的数量
func GetActivitySpecialTripCountByActivityId(condition string, pars []interface{}) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_special_trip as t    WHERE    1 = 1 ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取某一活动的报名的数量 (同时关联活动类型进行获取)
func GetActivitySpecialTripCountByActivitySpecial(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count
		FROM
			cygx_activity_special_trip AS t
			INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
		WHERE
			 1= 1 ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取空降的公司报名的记录
func GetActivitySpecialTripAirborneCountByActivitySpecial(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count
		FROM
			cygx_activity_special_meeting_detail AS t
			INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
		WHERE
			 1= 1 ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

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

// 取消
func CancelActivitySpecialTrip(uid int, item *CygxActivitySpecialDetail) (err error) {
	o := orm.NewOrm()
	sql := `DELETE  FROM cygx_activity_special_trip   WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, uid, item.ActivityId).Exec()
	return
}

// CancelActivitySpecialTripIsValid  处理活动报名是否有效
func CancelActivitySpecialTripIsValid(isValid, activityId, userId int) (err error) {
	sql := ` UPDATE cygx_activity_special_trip SET  is_valid= ?,is_cancel = 1  WHERE activity_id = ? AND  user_id = ? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, isValid, activityId, userId).Exec()
	return
}