package models

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

type CygxActivitySpecialTrip 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:"公司名称"`
	RealName       string    `description:"用户实际名称"`
	SellerName     string    `description:"所属销售"`
	AdminId        int       `description:"销售/管理员ID"`
	Source         int       `description:"来源，1小程序，2后台添加"`
	OutboundMobile string    `description:"外呼手机号"`
	CountryCode    string    `description:"手机国家区号"`
	IsCancel       string    `description:"是否取消，1是，0否"`
	IsValid        int       `description:"参会报名是否有效 1：是，0"`
}

type CygxActivitySpecialTripResp struct {
	Id                  int    `description:"ID"`
	UserId              int    `description:"用户id"`
	ActivityId          int    `description:"活动ID"`
	ActivityTime        string `description:"活动时间"`
	CreateTime          string `description:"创建时间"`
	Mobile              string `description:"手机号"`
	Email               string `description:"邮箱号"`
	CompanyId           int    `description:"公司ID"`
	CompanyName         string `description:"公司名称"`
	RealName            string `description:"用户实际名称"`
	SellerName          string `description:"所属销售"`
	OutboundMobile      string `description:"外呼手机号"`
	CountryCode         string `description:"手机国家区号"`
	ResearchTheme       string `description:"调研主题"`
	ChartPermissionId   int    `description:"行业id"`
	ChartPermissionName string `description:"行业名称"`
	IsMeeting           int    `description:"是否到会，1到会，0未到会"`
}

func GetCygxActivitySpecialTripList(condition string, pars []interface{}) (item []*CygxActivitySpecialTripResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT *
			FROM
			cygx_activity_special_trip  
			WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

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

// 获取某一活动的报名的数量
func GetActivitySpecialTripCountByActivityId(condition string, pars []interface{}) (count int, err error) {
	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_special_trip as t  WHERE    1 = 1 ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取某一活动的报名的数量 (同时关联活动类型进行获取)
func GetActivitySpecialTripCountByActivitySpecial(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count
		FROM
			cygx_activity_special_trip AS t
			INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
		WHERE
			 1= 1 ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取空降的公司报名的记录
func GetActivitySpecialTripAirborneCountByActivitySpecial(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count
		FROM
			cygx_activity_special_meeting_detail AS t
			INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
		WHERE
			 1= 1  	AND YEAR ( t.create_time )= YEAR (NOW()) ` + condition
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 添加
func AddCygxActivitySpecialTrip(item *CygxActivitySpecialTrip) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	_, err = o.Insert(item)
	if err != nil {
		return
	}
	//itemBill := new(CygxActivitySpecialTripBill)
	//itemBill.UserId = item.UserId
	//itemBill.ActivityId = item.ActivityId
	//itemBill.CreateTime = time.Now()
	//itemBill.Mobile = item.Mobile
	//itemBill.Email = item.Email
	//itemBill.CompanyId = item.CompanyId
	//itemBill.CompanyName = item.CompanyName
	//itemBill.RealName = item.RealName
	//itemBill.Source = 1
	//itemBill.BillDetailed = -1 // 流水减一
	//itemBill.DoType = 1
	//itemBill.RegisterPlatform = 1
	//itemBill.ChartPermissionId = itemActivity.ChartPermissionId
	//
	//_, err = o.Insert(itemBill)
	//if err != nil {
	//	return
	//}
	return
}

// 取消
func CancelActivitySpecialTrip(uid int, item *CygxActivitySpecialDetail) (err error) {
	o := orm.NewOrm()
	sql := `DELETE  FROM cygx_activity_special_trip   WHERE user_id=?  AND activity_id=? `
	_, err = o.Raw(sql, uid, item.ActivityId).Exec()
	return
}

// CancelActivitySpecialTripIsValid  处理活动报名是否有效
func CancelActivitySpecialTripIsValid(isValid, activityId, userId int) (err error) {
	sql := ` UPDATE cygx_activity_special_trip SET  is_valid= ?,is_cancel = 1  WHERE activity_id = ? AND  user_id = ? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, isValid, activityId, userId).Exec()
	return
}

type CygxActivitySpecialTripInit 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:"公司名称"`
	RealName          string    `description:"用户实际名称"`
	SellerName        string    `description:"所属销售"`
	AdminId           int       `description:"销售/管理员ID"`
	Source            int       `description:"来源，1小程序，2后台添加"`
	OutboundMobile    string    `description:"外呼手机号"`
	CountryCode       string    `description:"手机国家区号"`
	IsCancel          string    `description:"是否取消，1是，0否"`
	IsValid           int       `description:"参会报名是否有效 1：是，0"`
	ChartPermissionId int       `description:"行业Id"`
}

func GetCygxActivitySpecialTripListinit(condition string, pars []interface{}) (item []*CygxActivitySpecialTripInit, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	t.*,
	a.chart_permission_id 
FROM
	cygx_activity_special_trip AS t
	INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
WHERE
	1 = 1 
	AND is_valid = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

// 获取今天报名的用户信息，存入到参会记录表中
func GetCygxActivitySpecialMeetingDetailList(condition string, pars []interface{}) (list []*CygxActivitySpecialTripResp, err error) {
	sql := `SELECT
			art.activity_id,
			art.user_id,
			art.real_name,
			art.mobile,
			art.email,
			art.company_id,
			art.company_name,
			a.activity_time,
			a.chart_permission_id,
			a.chart_permission_name,
			a.research_theme
		FROM
			cygx_activity_special_meeting_detail AS art
			INNER JOIN cygx_activity_special AS a ON a.activity_id = art.activity_id
		WHERE
			1 = 1  `
	if condition != "" {
		sql += condition
	}
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&list)
	return
}
