package cygx

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

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.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// 批量添加
func AddCygxActivityRestrictSignupList(items []*CygxActivityRestrictSignup) (lastId int64, err error) {
	lenitems := len(items)
	if lenitems == 0 {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.InsertMulti(1, items)
	return
}

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

// 删除
func DeleteCygxActivityRestrictSignupByUserIds(userIdDelArr []int) (err error) {
	if len(userIdDelArr) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_activity_restrict_signup  WHERE  1= 1  AND user_id IN (` + utils.GetOrmInReplace(len(userIdDelArr)) + `)`
	_, err = o.Raw(sql, userIdDelArr).Exec()
	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.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, mobile).QueryRow(&count)
	return
}

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

// 列表
func GetCygxActivityRestrictSignupList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityRestrictSignup, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_restrict_signup as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}
