package cygx

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"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:"公司名称"`
	Source      int       `description:"来源,1小程序,2后台添加"`
}

// 记录日志
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取消报名"`
}

type AddMeetingReminderReq struct {
	List        []*AddUserList
	ActivityIds string `description:"活动ID,多个用,隔开"`
}

type AddUserList struct {
	UserId        int `description:"用户ID"`
	IsAppointment int `description:"是否预约纪要,1:是,0:否"`
	SignupType    int `description:"报名方式,1预约外呼,2自主拨入,3我要报名"`
}

// 取消会议提醒
func CancelActivityMeetingReminder(item *CygxActivityMeetingReminder) (lastId int64, err error) {
	o, err := orm.NewOrmUsingDB("hz_cygx").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()

	return
}

// 会议提醒的人数列表
func GetCygxActivityMeetingReminderList(activityId int, sqlStr string) (item []*CygxAppointmentList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT s.*,a.activity_time
			FROM
			cygx_activity_meeting_reminder AS s
			LEFT JOIN cygx_activity AS a ON a.activity_id = s.activity_id
			WHERE a.activity_id = ?  ` + sqlStr + `  GROUP BY s.user_id   ORDER BY s.create_time DESC `
	_, err = o.Raw(sql, activityId).QueryRows(&item)
	return
}

// 会议提醒的人数数量
func GetCygxActivityMeetingReminderCount(activityId int, sqlStr string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(*) FROM
			cygx_activity_meeting_reminder AS s
			LEFT JOIN cygx_activity AS a ON a.activity_id = s.activity_id
			WHERE
			s.activity_id = ? ` + sqlStr + `
            GROUP BY s.user_id)`
	sql = `SELECT COUNT(*)  as count FROM (` + sql + ` a`
	err = o.Raw(sql, activityId).QueryRow(&count)
	return
}

// 通过ID获取会议提醒详情
func GetCygxActivityMeetingReminderInfoById(id int) (item *CygxActivityMeetingReminder, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_meeting_reminder  WHERE id=?`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

// 添加会议提醒
func AddActivityMeetingReminderUser(items []*CygxActivityAppointment, itemsReminder []*CygxActivityMeetingReminder) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	//添加预约纪要部分
	if len(items) > 0 {
		for _, item := range items {
			var count int
			sql := `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE user_id=? AND activity_id=? `
			err = to.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
			if err != nil {
				return
			}
			if count == 0 {
				_, err = to.Insert(item)
				if err != nil {
					return
				}
			}
		}
	}
	//添加会议提醒
	for _, item := range itemsReminder {
		var count int
		sql := `SELECT COUNT(1) AS count FROM cygx_my_schedule WHERE user_id=? AND activity_id=? `
		err = to.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
		if err != nil {
			return
		}
		if count == 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 = to.Insert(itemMy)
			if err != nil {
				return
			}
		}
		sql = `SELECT COUNT(1) AS count FROM cygx_activity_appointment WHERE user_id=? AND activity_id=? `
		err = to.Raw(sql, item.UserId, item.ActivityId).QueryRow(&count)
		if err != nil {
			return
		}
		if count == 0 {
			_, err = to.Insert(item)
			if err != nil {
				return
			}
		}
	}
	return
}