package cygx

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

type CygxActivitySpecialMeetingDetail struct {
	Id          int       `orm:"column(id);pk"`
	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:"公司名称"`
	IsMeeting   int       `description:"是否到会  1.是 ，0否"`
	IsAirborne  int       `description:"是否属于空降  1.是 ，0否"`
	RealName    string    `description:"真实姓名"`
}

type CygxActivitySpecialMeetingDetailResp struct {
	Id          int    `description:"id"`
	UserId      int    `description:"用户id"`
	ActivityId  int    `description:"活动ID"`
	CreateTime  string `description:"创建时间"`
	RealName    string `description:"姓名"`
	Email       string `description:"邮箱号"`
	CompanyId   int    `description:"公司ID"`
	CompanyName string `description:"公司名称"`
	IsMeeting   int    `description:"到会类型  1.是 ，0否 ,2空降"`
	IsAirborne  int    `description:"是否属于空降  1.是 ，0否"`
	Operation   bool   `description:"操作按钮，true,到会，false 未到会"`
}

type SpecialMeetingDetailRespList struct {
	List []*CygxActivitySpecialMeetingDetailResp
}

type MeetingSpeciaDoRep struct {
	UserIds    string `description:"用户ID，多个ID用 , 隔开"`
	ActivityId int    `description:"活动ID"`
}

// 列表
func GetCygxActivitySpecialMeetingDetailListByActivity(activityId int) (items []*CygxActivitySpecialMeetingDetailResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT s.*
			FROM cygx_activity_special_meeting_detail  as s 
			WHERE 1 =1  AND s.activity_id =? `
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

// 到会操作
func MeetingDopecialMeet(meetingUids, noMeetingUids string, ActivityId int, items []*CygxActivitySpecialMeetingDetail, itemsBill []*CygxActivitySpecialTripBill) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			fmt.Println(err)
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	fmt.Println("ActivityId", ActivityId)
	//修改报名表的参会记录
	sql := `UPDATE cygx_activity_special_trip SET is_meeting = 0  WHERE activity_id =? `
	_, err = to.Raw(sql, ActivityId).Exec()
	if err != nil {
		return
	}
	sql = `UPDATE cygx_activity_special_trip SET is_meeting = 1  WHERE activity_id =? AND is_cancel = 0 AND  user_id IN  (` + meetingUids + `)`
	_, err = to.Raw(sql, ActivityId).Exec()
	if err != nil {
		return
	}
	sql = `UPDATE cygx_activity_special SET is_submit_meeting = 1  WHERE activity_id = ? `
	_, err = to.Raw(sql, ActivityId).Exec()
	if err != nil {
		return
	}
	//删除老的记录并插入新的记录
	sql = `DELETE FROM cygx_activity_special_meeting_detail   WHERE activity_id = ? `
	_, err = to.Raw(sql, ActivityId).Exec()
	if err != nil {
		return
	}
	for _, v := range items {
		_, err = to.Insert(v)
		if err != nil {
			return
		}
	}
	if len(noMeetingUids) > 0 {
		sql = `UPDATE cygx_activity_special_meeting_detail SET is_meeting = 0  WHERE activity_id =? AND user_id IN (` + noMeetingUids + `)`
		_, err = to.Raw(sql, ActivityId).Exec()
		if err != nil {
			return
		}
	}
	//添加记录表的到会信息
	sql = `UPDATE cygx_activity_special_meeting_detail SET is_meeting = 1  WHERE activity_id =? AND  user_id IN  (` + meetingUids + `)`
	_, err = to.Raw(sql, ActivityId).Exec()
	if err != nil {
		return
	}
	//添加到会扣点流水记录
	if len(itemsBill) > 0 {
		for _, v := range itemsBill {
			_, err = to.Insert(v)
			if err != nil {
				return
			}
		}
	}
	return
}

// 列表
func GetCygxActivitySpecialMeetingDetailList(condition string, pars []interface{}) (items []*CygxActivitySpecialMeetingDetail, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT s.* 
			FROM cygx_activity_special_meeting_detail  as s  WHERE 1 =1   ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetCygxActivitySpecialMeetingDetailListByActivityId(activityId int) (item []*CygxActivitySpecialMeetingDetail, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *
			FROM
			cygx_activity_special_trip  
			WHERE activity_id = ?   AND is_cancel = 0 `
	_, err = o.Raw(sql, activityId).QueryRows(&item)
	return
}

// 列表
func UpdateCygxActivitySpecialMeetingDetailRealName(realName, mobile string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	//添加记录表的到会信息
	sql := `UPDATE cygx_activity_special_meeting_detail SET real_name = ?  WHERE mobile = ? `
	_, err = o.Raw(sql, realName, mobile).Exec()
	return
}

// 添加
func AddCygxActivitySpecialMeetingDetail(item *CygxActivitySpecialMeetingDetail) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}
