package yb

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

// ActivityRegister 活动报名表
type ActivityRegister struct {
	Id            int       `orm:"column(activity_register_id);pk" description:"活动报名ID"`
	ActivityId    int       `description:"活动ID"`
	UserId        int       `description:"微信用户表ID"`
	CompanyId     int       `description:"客户id"`
	CompanyName   string    `description:"客户名称"`
	SellerName    string    `description:"所属销售名称"`
	RealName      string    `description:"用户实际名称"`
	Mobile        string    `description:"用户手机号"`
	RegisterState int8      `description:"报名状态 0-取消报名 1-已报名"`
	CreateTime    time.Time `description:"创建时间"`
}

// TableName 表名变更
func (activity *ActivityRegister) TableName() string {
	return "yb_activity_register"
}

type ActivityRegisterList struct {
	ActivityId        int       `orm:"column(activity_id);pk" description:"活动ID"`
	ActivityTypeId    int       `description:"活动类型ID"`
	ActivityTypeName  string    `description:"活动类型名称"`
	ActivityName      string    `description:"活动标题"`
	StartTime         time.Time `description:"活动开始时间"`
	EndTime           time.Time `description:"活动结束时间"`
	IsLimitPeople     int8      `description:"是否限制人数 1是，0否"`
	LimitPeopleNum    int       `description:"限制人数数量"`
	RegisterPeopleNum int       `description:"已报名人数数量"`
}

// GetActivityRegisterList 获取活动报名列表数据
func GetActivityRegisterList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*ActivityRegisterList, err error) {
	o := orm.NewOrm()
	sql := "select * from yb_activity where 1=1 AND is_delete = 0 AND publish_status = 1"
	sql += condition
	sql += ` order by start_time asc ` //按照开始时间正序

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// Update 更新活动报名
func (activityRegister *ActivityRegister) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(activityRegister, cols...)
	return
}

// AddActivityRegister 新增活动报名
func AddActivityRegister(activityRegister *ActivityRegister) (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(activityRegister)
	if err != nil {
		return
	}
	activityRegister.Id = int(id)
	return
}

type ActivityRegisterTotal struct {
	Total      int `description:"报名总人数"`
	ActivityId int `description:"活动id"`
}

// GetActivityRegisterTotalListByActivityIds 根据活动id集合获取报名总人数列表
func GetActivityRegisterTotalListByActivityIds(activityIds string) (list []*ActivityRegisterTotal, err error) {
	if activityIds == "" {
		return
	}
	o := orm.NewOrm()
	sql := `select count(*) total,activity_id from yb_activity_register where activity_id in (` + activityIds + `) and register_state = 1  group by activity_id`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetActivityRegisterListByActivityId 获取已经报名活动的用户列表数据
func GetActivityRegisterListByActivityId(activityId, startSize, pageSize int) (total int, list []*ActivityRegister, err error) {
	o := orm.NewOrm()
	sql := "select * from yb_activity_register where register_state = 1 and activity_id = ? "
	sql += ` order by activity_register_id desc `

	totalSql := `select count(1) total from (` + sql + `) z `
	err = o.Raw(totalSql, activityId).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, activityId, startSize, pageSize).QueryRows(&list)
	return
}

// GetActivityRegister 获取活动报名用户详情
func GetActivityRegister(activityId, userId int) (item *ActivityRegister, err error) {
	o := orm.NewOrm()
	sql := "select * from yb_activity_register where activity_id = ? and user_id = ? order by activity_register_id desc"
	err = o.Raw(sql, activityId, userId).QueryRow(&item)
	return
}

// GetAllActivityRegisterListByActivityId 根据活动id获取所有的线下报名用户
func GetAllActivityRegisterListByActivityId(activityId int) (list []*ActivityRegister, err error) {
	o := orm.NewOrm()
	sql := "select * from yb_activity_register where register_state = 1 and activity_id = ? "
	sql += ` order by activity_register_id asc ` //按照预约时间正序
	_, err = o.Raw(sql, activityId).QueryRows(&list)
	return
}

// CancelActivityRegisters 根据活动ID取消所有报名
func CancelActivityRegisters(activityId int) (err error) {
	o := orm.NewOrm()
	sql := "UPDATE yb_activity_register SET register_state = 0 WHERE activity_id = ?"
	_, err = o.Raw(sql, activityId).Exec()
	return
}
