package models

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

type CygxActivityAppointment 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 AppointmentResp struct {
	ActivityId    int    `description:"活动ID"`
	HasPermission int    `description:"操作方式，1:有该行业权限,正常展示，2：无该行业权限，3：潜在客户，未提交过申请，4：潜在客户,已提交过申请"`
	PopupMsg      string `description:"权限弹窗信息"`
	SellerMobile  string `description:"销售电话"`
	SellerName    string `description:"销售姓名"`
	GoFollow      bool   `description:"是否去关注"`
}

//添加
func AddCygxActivityAppointment(item *CygxActivityAppointment) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	var countMySchedule int
	sql := `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countMySchedule)
	if err != nil {
		return
	}
	if countMySchedule == 0 {
		itemMy := new(CygxMySchedule)
		itemMy.UserId = item.UserId
		itemMy.ActivityId = item.ActivityId
		itemMy.CreateTime = time.Now()
		itemMy.Mobile = item.Mobile
		itemMy.Email = item.Email
		itemMy.CompanyId = item.CompanyId
		itemMy.CompanyName = item.CompanyName
		_, err = o.Insert(itemMy)
		if err != nil {
			return
		}
	}
	_, err = o.Insert(item)
	return
}

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

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

//删除
func DeleteCygxActivityAppointment(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 CancelcygxActivityAppointment(item *CygxActivityAppointment) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	//判断是否删除我的日程
	var countSingup int
	var countReminder int
	sql := `SELECT COUNT(1) AS count FROM cygx_activity_signup WHERE  is_cancel = 0  AND user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countSingup)
	sql = `SELECT COUNT(1) AS count FROM cygx_activity_meeting_reminder WHERE   user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countReminder)
	if err != nil {
		return
	}
	if countSingup == 0 && countReminder == 0 {
		sql = `DELETE  FROM cygx_my_schedule   WHERE user_id=?  AND activity_id=? `
		_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
		if err != nil {
			return
		}
	}
	//删除预约的纪要
	sql = `DELETE  FROM cygx_activity_appointment   WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
	if err != nil {
		return
	}
	return
}

type CygxAppointmentAndActivity struct {
	Mobile         string `description:"手机号"`
	ActivityName   string `description:"所属销售"`
	ActivityTypeId int    `description:"活动类型"`
}

//通过活动ID获取预约列表
func GetAppointmentListByActivityId(activityIds, activityTypeIds string) (items []*CygxAppointmentAndActivity, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.*,a.activity_name,
			a.activity_type_id
		FROM
			cygx_activity_appointment AS m
			INNER JOIN cygx_activity AS a ON a.activity_id = m.activity_id 
		WHERE
			a.activity_id IN ( ` + activityIds + ` ) 
			AND a.activity_type_id IN (` + activityTypeIds + `) 
			AND DATE_SUB( CURDATE(), INTERVAL 14 DAY ) <= date( a.activity_time ) 
			AND a.active_state = 3 	GROUP BY m.mobile`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

//通过活动ID、手机号获取预约列表
func GetAppointmentListByActivityIdAndMobile(activityIds, mobile string) (items []*CygxAppointmentAndActivity, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			m.*,a.activity_name
		FROM
			cygx_activity_appointment AS m
			INNER JOIN cygx_activity AS a ON a.activity_id = m.activity_id 
		WHERE
			a.activity_id IN ( ` + activityIds + ` ) 
			AND a.activity_type_id IN ( 1, 2, 5 ) 
			AND DATE_SUB( CURDATE(), INTERVAL 14 DAY ) <= date( a.activity_time ) 
			AND a.active_state = 3
			AND  m.mobile= ?`
	_, err = o.Raw(sql, mobile).QueryRows(&items)
	return
}

//通过活动ID获取预约纪要的人数列表
func GetCygxAppointmentSummaryListBySubjectId(subjectIds string) (item []*CygxAppointmentAndActivity, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			ap.mobile,
			ap.activity_id,
			a.activity_name 
		FROM
			cygx_activity_appointment AS ap
			INNER JOIN cygx_activity AS a ON a.activity_id = ap.activity_id
			INNER JOIN cygx_industrial_activity_group_subject AS sg ON sg.activity_id = a.activity_id 
		WHERE
			a.active_state = 3 
			AND a.chart_permission_id = 31 
			AND DATE_SUB( CURDATE(), INTERVAL 14 DAY ) <= date( a.activity_time ) 
			AND a.activity_type_id = 1 
			AND sg.industrial_subject_id IN (` + subjectIds + `) 
		GROUP BY
			ap.mobile,
			ap.activity_id `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

type CygxAppointment struct {
	Mobile         string `description:"手机号"`
	UserId         int    `description:"userId"`
	ActivityName   string `description:"所属销售"`
	ActivityTypeId int    `description:"活动类型"`
}

//通过活动ID获取预约纪要的人数列表
func GetCygxAppointmentSummaryBySubjectId(subjectIds string) (item []*CygxAppointment, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			ap.user_id,
			ap.mobile,
			ap.activity_id,
			a.activity_name 
		FROM
			cygx_activity_appointment AS ap
			INNER JOIN cygx_activity AS a ON a.activity_id = ap.activity_id
			INNER JOIN cygx_industrial_activity_group_subject AS sg ON sg.activity_id = a.activity_id 
		WHERE
			a.active_state = 3
			AND sg.industrial_subject_id IN (` + subjectIds + `) 
		GROUP BY
			ap.mobile,
			ap.activity_id `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

//GetCygxAppointmentListByUser 获取预约纪要的人
func GetCygxAppointmentListByUser(condition string, pars []interface{}) (item []*CygxActivityAppointment, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM
			cygx_activity_appointment  
			WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}
