package models

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

type CygxActivitySpecialSignup 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:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	SellerName  string    `description:"所属销售"`
}

type SignupSpecialStatus struct {
	ActivityId    int    `description:"活动ID"`
	HasPermission int    `description:"操作方式，1:有该行业权限,正常展示，2：无该行业权限，3：潜在客户，未提交过申请，4：潜在客户,已提交过申请"`
	PopupMsg      string `description:"权限弹窗信息"`
	PopupMsg2     string `description:"权限弹窗信息"`
	Status        int    `description:"返回类型，1:添加，2：取消"`
	SellerMobile  string `description:"销售电话"`
	SellerName    string `description:"销售姓名"`
	SignupStatus  int    `description:"返回状态：1：成功 、2 ：人数已满 、3：调研次数已用完、 4：超时"`
}

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

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

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

// 列表
func GetActivityListSpecialAll(activityId int) (items []*CygxActivitySpecialSignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT art.*  FROM cygx_activity_special_signup as art WHERE 1= 1 AND  activity_id = ?  GROUP BY company_id`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 列表
func GetActivityListSpecialByActivityId(activityId int) (items []*CygxActivitySpecialSignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT art.*  FROM cygx_activity_special_signup as art WHERE 1= 1 AND  activity_id = ? `
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

type CygxActivitySpecialSignupResp 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:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	SellerName  string    `description:"所属销售"`
	Count       string    `description:"所属销售"`
}

// 列表
func GetActivityListSpecialGroupByMobile(condition string, pars []interface{}) (items []*CygxActivitySpecialSignupResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT s.*,	COUNT( 1 ) AS count   FROM cygx_activity_special_signup as s INNER JOIN cygx_activity_special AS a ON a.activity_id = s.activity_id
			` + condition + `  GROUP BY s.mobile`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 列表
func GetActivityListSpecialGroupByCompanyId(condition string, pars []interface{}) (items []*CygxActivitySpecialSignupResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT *,	COUNT( 1 ) AS count   FROM cygx_activity_special_signup   as s INNER JOIN cygx_activity_special AS a ON a.activity_id = s.activity_id
			 ` + condition + `  GROUP BY s.company_id`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// UpdateActivitySpecialSignupNumMulti 批量修改专项调研感兴趣的人数排名
func UpdateActivitySpecialSignupNumMulti(items []*CygxActivitySpecialSignupResp) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_activity_special_signup SET user_num = ? WHERE mobile = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.Count, v.Mobile)
		if err != nil {
			return
		}
	}
	return
}

// UpdateActivitySpecialSignupCompanyIdMulti 批量修改专项调研感兴趣的用户的对应公司ID
func UpdateActivitySpecialSignupCompanyIdMulti(items []*WxUser) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_activity_special_signup SET company_id = ? WHERE mobile = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.CompanyId, v.Mobile)
		if err != nil {
			return
		}
	}
	return
}

// UpdateActivitySpecialSignupCompanyNumMulti 批量修改专项调研感兴趣的公司对应的数量
func UpdateActivitySpecialSignupCompanyNumMulti(items []*CygxActivitySpecialSignupResp) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_activity_special_signup SET company_num = ? WHERE company_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(v.Count, v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}
