package models

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

type CygxActivityMeetingReminder 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:"公司名称"`
}

// 记录日志
type CygxActivityMeetingReminderLog 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:"公司名称"`
	Type        int       `description:"操作方式，1报名，2取消报名"`
}

// 添加会议提醒信息
func AddActivityMeetingReminder(item *CygxActivityMeetingReminder) (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 count int
	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
		lastId, err = o.Insert(itemMy)
		if err != nil {
			return
		}
	}
	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(&count)
	if err != nil {
		return
	}
	if count > 0 {
		sql := `UPDATE cygx_activity_meeting_reminder SET is_cancel = 0  WHERE user_id=?  AND activity_id=? `
		_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
	} else {
		lastId, err = o.Insert(item)
	}
	itemLog := new(CygxActivityMeetingReminderLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 1
	lastId, err = o.Insert(itemLog)
	return
}

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

// 取消会议提醒
func CancelActivityMeetingReminder(item *CygxActivityMeetingReminder) (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 countSignup int
	var countAppointment 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(&countSignup)

	sql = `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE  user_id=? AND activity_id=? `
	err = o.Raw(sql, item.UserId, item.ActivityId).QueryRow(&countAppointment)
	if err != nil {
		return
	}
	if countSignup == 0 && countAppointment == 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_meeting_reminder   WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, item.UserId, item.ActivityId).Exec()
	if err != nil {
		return
	}
	itemLog := new(CygxActivityMeetingReminderLog)
	itemLog.UserId = item.UserId
	itemLog.ActivityId = item.ActivityId
	itemLog.CreateTime = time.Now()
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.Type = 2
	lastId, err = o.Insert(itemLog)
	return
}

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

// GetCygxReminderListByUser 获取设置会议提醒的人
func GetCygxReminderListByUser(condition string, pars []interface{}) (item []*CygxActivityMeetingReminder, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM
			cygx_activity_meeting_reminder  
			WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

// 修改是否推送消息状态
func UpdateSendedMsgMeetingSendWxMsg(activityIds []int) (err error) {
	lenarr := len(activityIds)
	if lenarr == 0 {
		return
	}
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	var condition string
	var pars []interface{}
	condition = ` AND activity_id IN (` + utils.GetOrmInReplace(lenarr) + `)`
	pars = append(pars, activityIds)
	//修改设置消息提醒微信模版消息推送状态
	sql := ` UPDATE cygx_activity_meeting_reminder SET  is_send_wx_msg= 1  WHERE  1=1   ` + condition
	_, err = o.Raw(sql, pars).Exec()
	if err != nil {
		return
	}

	//修改设置预约外呼微信模版消息推送状态
	sql = ` UPDATE cygx_activity_signup SET  is_send_wx_msg= 1  WHERE  1=1   ` + condition
	_, err = o.Raw(sql, pars).Exec()
	return
}
