package cygx

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

type CygxActivitySpecialTripBill 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:"用户实际名称"`
	AdminId             int       `description:"销售/管理员ID"`
	Source              int       `description:"来源，1小程序，2后台添加, 3开发人员手动添加"`
	BillDetailed        int       `description:"流水明细，判断是进账还是出账"`
	DoType              int       `description:"操作方式，1报名，2取消报名"`
	RegisterPlatform    int       `description:"来源 1小程序，2：网页"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	Way                 int       `description:"1报名，取消报名。2到会取消到会 3转正或清零 4取消活动"`
	Content             string    `description:"内容"`
	Total               string    `description:"总和"`
}

type CygxActivitySpecialTripBillList 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:"用户实际名称"`
	AdminId             int       `description:"销售/管理员ID"`
	Source              int       `description:"来源，1小程序，2后台添加, 3开发人员手动添加"`
	BillDetailed        int       `description:"流水明细，判断是进账还是出账"`
	DoType              int       `description:"操作方式，1报名，2取消报名"`
	RegisterPlatform    int       `description:"来源 1小程序，2：网页"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	Way                 int       `description:"1报名，取消报名。2到会取消到会 3转正或清零 4取消活动"`
	Content             string    `description:"内容"`
	Total               string    `description:"总和"`
}

// 添加
func AddCygxActivitySpecialTripBill(item *CygxActivitySpecialTripBill) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	if err != nil {
		return
	}
	return
}

func GetCygxActivitySpecialTripBill(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *
			FROM
			cygx_activity_special_trip_bill  
			WHERE 1 = 1 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetCygxActivitySpecialTripBillList(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBillList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			b.*
		FROM
			cygx_activity_special_trip_bill AS b
		WHERE
			1 = 1` + condition
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

type AirborneCount struct {
	Count             int
	ChartPermissionId int ` description:"品种权限ID"`
}

// 获取空降的公司报名的记录
func GetActivitySpecialTripAirborneListByActivitySpecial(condition string, pars []interface{}) (items []*AirborneCount, err error) {
	sqlCount := ` SELECT chart_permission_id,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 + `GROUP BY chart_permission_id`
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sqlCount, pars).QueryRows(&items)
	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.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

type CygxActivitySpecialTripBillDetailList struct {
	Id                  int       `orm:"column(id);pk"`
	UserId              int       `description:"用户id,多个用,隔开"`
	ActivityId          int       `description:"活动ID"`
	ResearchTheme       string    `description:"调研主题"`
	CreateTime          time.Time `description:"创建时间"`
	Mobile              string    `description:"手机号"`
	Email               string    `description:"邮箱号"`
	CompanyId           int       `description:"公司ID"`
	CompanyName         string    `description:"公司名称"`
	RealName            string    `description:"用户实际名称"`
	AdminId             int       `description:"销售/管理员ID"`
	Source              int       `description:"来源，1小程序，2后台添加, 3开发人员手动添加"`
	BillDetailed        int       `description:"流水明细，判断是进账还是出账"`
	DoType              int       `description:"操作方式，1报名，2取消报名"`
	RegisterPlatform    int       `description:"来源 1小程序，2：网页"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	Content             string    `description:"内容"`
	Way                 int       `description:"1报名，取消报名。2到会取消到会 3转正或清零 4取消活动"`
	Total               string    `description:"总和"`
}

//func GetCygxActivitySpecialTripBillDetailList(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBillDetailList, err error) {
//	o := orm.NewOrm()
//	sql := `SELECT
//			b.*,
//			a.research_theme,
//			c.chart_permission_name
//		FROM
//			cygx_activity_special_trip_bill AS b
//			INNER JOIN chart_permission AS c ON c.chart_permission_id = b.chart_permission_id
//			INNER JOIN cygx_activity_special AS a ON a.activity_id = b.activity_id
//		WHERE
//			1 = 1` + condition
//	_, err = o.Raw(sql, pars).QueryRows(&item)
//	return
//}

type CygxActivitySpecialPointsBillRespItem struct {
	Id                  int    `gorm:"column:id;primary_key;AUTO_INCREMENT"`
	Content             string `gorm:"column:content" `                                 // 内容说明
	Total               string `gorm:"column:points;default:0;NOT NULL" `               // 合计
	CreateTime          string `gorm:"column:create_time" `                             // 创建时间
	CompanyId           int    `gorm:"column:company_id;default:0" `                    // 公司ID
	CompanyName         string `gorm:"column:company_name" `                            // 公司名称
	RealName            string `gorm:"column:real_name"`                                // 用户实际名称
	BillDetailed        int    `gorm:"column:bill_detailed;default:0;NOT NULL" json:""` // 流水明细，判断是进账还是出账
	ActivityName        string `description:"活动标题"`
	ChartPermissionId   int    `description:"行业id"`
	ChartPermissionName string `description:"行业名称"`
}

type CygxActivitySpecialPointsBillResp struct {
	List []*CygxActivitySpecialPointsBillRespItem
}

func GetCygxActivitySpecialTripBillDetailListAll(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBillDetailList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			b.*,
			a.research_theme
		FROM
			cygx_activity_special_trip_bill AS b
			LEFT JOIN cygx_activity_special AS a ON a.activity_id = b.activity_id
		WHERE
			1 = 1` + condition

	sql += ` ORDER BY b.create_time DESC`
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetCygxActivitySpecialTripBillByCompanyId(companyId int) (item *CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			*
		FROM
			cygx_activity_special_trip_bill WHERE company_id = ? ORDER BY create_time DESC LIMIT 1
		`
	err = o.Raw(sql, companyId).QueryRow(&item)
	return
}

// GetCygxActivitySpecialTripBillLastDetialByActivityId  根据活动ID获取最后一条扣点明细
func GetCygxActivitySpecialTripBillLastDetialByActivityId(activityId, userId int) (item *CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *
			FROM
			cygx_activity_special_trip_bill  
			WHERE 1 = 1  AND activity_id =?  AND  user_id = ?  ORDER BY id  DESC LIMIT 1  `
	err = o.Raw(sql, activityId, userId).QueryRow(&item)
	return
}

func GetBestNewBillList() (item []*CygxActivitySpecialTripBillList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
*  
FROM
	cygx_activity_special_trip_bill 
WHERE
	1 = 1
AND id  IN (SELECT
	MAX(id) as  mid 
FROM
	cygx_activity_special_trip_bill 
WHERE
	1 = 1 
GROUP BY
	company_id)`
	_, err = o.Raw(sql).QueryRows(&item)
	return
}
