package cygx_activity

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

//活动详情
type CygxActivity struct {
	ActivityId           int       `orm:"column(activity_id);pk";description:"活动ID 等于0新增活动，大于0修改活动"`
	ActivityTypeId       int       `description:"活动类型id"`
	ActivityTypeName     string    `description:"活动类型名称"`
	ChartPermissionId    int       `description:"行业id"`
	ChartPermissionName  string    `description:"行业名称"`
	ChartPermissionNames string    `description:"行业名称辅助字段，区分研选子分类"`
	Body                 string    `description:"内容"`
	CreateTime           time.Time `description:"创建时间"`
	IsLimitPeople        int       `description:"是否限制人数 1是，0否"`
	LimitPeopleNum       int       `description:"限制的人数数量"`
	CustomerTypeIds      string    `description:"活动可见的客户类型，多个ID用 , 隔开"`
	PublishStatus        int       `description:"发布状态 1已发布，0未发布"`
	LastUpdatedTime      time.Time `description:"更新时间"`
	ActivityTime         string    `description:"活动时间"`
	ActivityTimeText     string    `description:"活动时间带文字"`
	DistinguishedGuest   string    `description:"嘉宾"`
	Host                 string    `description:"主持人"`
	MainlandTell         string    `description:"大陆拨入号"`
	HongKongTell         string    `description:"香港拨入号"`
	TaiwanTell           string    `description:"台湾拨入号"`
	AmericaTell          string    `description:"美国拨入号"`
	ParticipationCode    string    `description:"参会密码"`
	Theme                string    `description:"主题"`
	Expert               string    `description:"专家"`
	ActivityName         string    `description:"活动名称"`
	ActivityNameTask     string    `description:"活动名称定时任务同步的时候使用"`
	OnlineParticipation  string    `description:"网络参会"`
	ReportLink           string    `description:"报告链接"`
	City                 string    `description:"城市"`
	Address              string    `description:"活动地址"`
	Highlights           string    `description:"活动亮点"`
	Remarks              string    `description:"备注"`
	Speaker              string    `description:"主讲人"`
	ArticleId            int       `description:"关联报告id"`
	Label                string    `description:"标签"`
	LinkParticipants     string    `description:"链接参会"`
	AppAttendance        string    `description:"App参会"`
	ConferencePassword   string    `description:"会议密码"`
	TemporaryLabel       string    `description:"临时标签"`
	IsMakerShow          int       `description:"是否仅决策人可见 0，否 、1，是"`
	VisibleRange         int       `description:"可见范围 1，仅本组可见 、2，全部客户可见"`
	Scale                string    `description:"管理规模，空不填，1：:50亿以下，2：50~100亿，3：100亿以上。多个用, 隔开"`
	IsShowSubjectName    int       `description:"小程序内是否展示标的名称 1是 ，0 否 默认0 "`
	IsHideAppointment    int       `description:"是否隐藏预约纪要按钮 1是 ，0 否 默认0 "`
	AdminId              int       `description:"销售/管理员ID"`
	AdminName            string    `description:"销售/管理员姓名"`
}

type CygxActivitySignup struct {
	Id                  int       `orm:"column(id)" description:"自增Id"`
	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:"公司名称"`
	OutboundMobile      string    `description:"外呼手机号"`
	CountryCode         string    `description:"手机国家区号"`
	Source              int       `description:"来源，1小程序，2后台添加"`
	SignupType          int       `description:"报名方式，1预约外呼，2自主拨入，3我要报名"`
	RealName            string    `description:"用户实际名称"`
	SellerName          string    `description:"所属销售"`
	YidongExamineStatus int       `description:"易董活动报名审核状态0审核中，1：审核通过、2审核不通过"`
}

type CygxYidongActivityExamineStatus struct {
	Id                  int       `orm:"column(id)" description:"自增Id"`
	ActivityId          int       `description:"活动ID"`
	YidongActivityId    string    `description:"易董活动ID"`
	CreateTime          time.Time `description:"创建时间"`
	ExamineTime         string    `description:"审核时间"`
	Mobile              string    `description:"手机号"`
	YidongExamineStatus int       `description:"易董活动报名审核状态0审核中，1：审核通过、2审核不通过"`
}

//通过纪要ID获取活动详情
func GetAddActivityInfoById(activityIdYiDong string) (item *CygxActivity, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			a.*
		FROM
			cygx_activity  as a
		WHERE
			yidong_activity_id =?`
	err = o.Raw(sql, activityIdYiDong).QueryRow(&item)
	return
}

type ExamineStatusReq struct {
	ActivityId    int    `description:"活动ID"`
	Mobile        string `description:"手机号" json:"mobile"`
	ExamineStatus int    `description:"审核状态 1，通过、2：未通过" json:"examine_status"`
}

// UpdateActivitySignupNumMulti 批量修改易董报名用户审核状态
func UpdateActivitySignupNumMulti(items []*ExamineStatusReq) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE cygx_activity_signup SET yidong_examine_status = ? WHERE mobile = ? AND activity_id = ? ").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	if len(items) > 0 {
		for _, v := range items {
			_, err = p.Exec(v.ExamineStatus, v.Mobile, v.ActivityId)
			if err != nil {
				return
			}
		}
	}

	return
}

// AddActivitySignupNumMulti 批量新增易董报名用户审核状态
func AddActivitySignupNumMulti(items []*CygxActivitySignup) (err error) {
	o := orm.NewOrm()
	if len(items) > 0 {
		//批量新增易董报名用户审核状态
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

//GetActivitySignupListByActivity获取已经报名了的用户列表
func GetActivitySignupListByActivity(activityId int) (items []*CygxActivitySignup, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_signup WHERE 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:"外呼手机号区号"`
	SellerName          string `description:"所属销售"`
	RealName            string `description:"真实姓名"`
}

func GetUserListByMobile(mobile string) (items []*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 u.mobile IN (` + mobile + `)`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CygxYidongActivitySignupLog struct {
	Id               int       `orm:"column(id)" description:"自增Id"`
	ActivityId       int       `description:"活动ID"`
	YidongActivityId string    `description:"易董活动ID"`
	CreateTime       time.Time `description:"创建时间"`
	Mobile           string    `description:"手机号"`
}

// AddCygxYidongActivitySignup 记录易董传过来的报名信息
func AddCygxYidongActivitySignupLog(item *CygxYidongActivitySignupLog) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

type CygxYidongActivitySignupReq struct {
	RealName    string `description:"真实姓名" json:"real_name"`
	Mobile      string `description:"手机号" json:"mobile"`
	CountryCode string `description:"手机国家区号" json:"country_code"`
	Email       string `description:"手机号" json:"email"`
	CompanyName string `description:"公司名称" json:"company_name"`
	Job         string `description:"职务" json:"job"`
}

//通过纪要ID获取活动详情
func GetAddActivityInfoByMobile(mobile string) (item *CygxYidongActivitySignupReq, 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 u.mobile  = ?`
	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// AddActivityItemsLogNumMulti 记录审核日志
func AddActivityItemsLogNumMulti(items []*CygxYidongActivityExamineStatus) (err error) {
	o := orm.NewOrm()
	if len(items) > 0 {
		//批量添加记录审核日志
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

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

//GetCygxMyScheduleList 获取我的日程数据
func GetCygxMyScheduleList(activityId int) (items []*CygxMySchedule, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_my_schedule WHERE activity_id = ?`
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// AddCygxMyScheduleLNumMulti 添加我的日程
func AddCygxMyScheduleLNumMulti(items []*CygxMySchedule) (err error) {
	o := orm.NewOrm()
	if len(items) > 0 {
		//添加我的日程
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

//DelCygxMyScheduleList 删除我的日程
func DelCygxMyScheduleList(mobile string, activityId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE  FROM cygx_my_schedule   WHERE mobile IN (` + mobile + `)  AND activity_id=?`
	_, err = o.Raw(sql, activityId).Exec()
	return
}

//DelCygxcActivitySignup 删除我的报名信息
func DelCygxcActivitySignup(mobile string, activityId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE  FROM cygx_activity_signup   WHERE mobile IN (` + mobile + `)  AND activity_id=?`
	_, err = o.Raw(sql, activityId).Exec()
	return
}
