package models

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:"总和"`
}

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

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

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:"内容"`
}

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

type SpecialBillInitBig struct {
	ApprovalTime time.Time
	CompanyId    int
	CompanyName  string
}

func GetActivitySecialTirpBillForInit(packageType int) (item []*SpecialBillInitBig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT
	a.approve_time,d.company_id,c.company_name 
FROM
	company_approval a
	INNER JOIN company_contract b ON a.company_contract_id = b.company_contract_id
	INNER JOIN company c ON a.company_id = c.company_id
	INNER JOIN cygx_company_user_type d ON a.company_id = d.company_id
WHERE
	a.apply_method = 1 
	AND a.product_id = 2 
	AND a.approve_status = '已审批' 
	AND b.product_id = 2 
	AND d.package_type = ?
	GROUP BY d.company_id `
	_, err = o.Raw(sql, packageType).QueryRows(&item)
	return
}

type SpecialBillInitNotBig struct {
	ApprovalTime       time.Time
	CompanyId          int
	CompanyName        string
	ChartPermissionIds string
}

func GetActivitySecialTirpBillForInitNotBig() (item []*SpecialBillInitNotBig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT
	a.approve_time,d.company_id,d.company_name,GROUP_CONCAT(DISTINCT c.chart_permission_id) AS chart_permission_ids
FROM
	company_approval a
	INNER JOIN company_contract b ON a.company_contract_id = b.company_contract_id
	INNER JOIN company_report_permission c ON a.company_id = c.company_id 
	INNER JOIN company d ON a.company_id = d.company_id
WHERE
	a.apply_method = 1 
	AND a.product_id = 2 
	AND a.approve_status = '已审批' 
	AND b.product_id = 2 
	AND c.product_id = 2
	AND c.is_upgrade = 1
	GROUP BY d.company_id `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

// 批量添加
func AddCygxActivitySpecialTripBillMulti(items []*CygxActivitySpecialTripBill) (err error) {
	o := orm.NewOrm()
	if len(items) > 0 {
		//批量添加
		_, err = o.InsertMulti(len(items), items)
	}
	return
}

type SpecialBillInitReduce struct {
	CreateTime  time.Time
	CompanyId   int
	CompanyName string
}

func GetActivitySecialTirpBillForReduce() (item []*SpecialBillInitReduce, err error) {
	o := orm.NewOrm()
	sql := ` SELECT
	a.create_time,a.company_id,c.company_name 
FROM
	company_operation_record a
	INNER JOIN company c ON a.company_id = c.company_id
	INNER JOIN company_report_permission e ON a.company_id = e.company_id 
WHERE
	a.operation = 'try_out' 
	AND a.product_id = 2 
	AND e.status NOT IN ('正式','永续')
	GROUP BY a.company_id `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

func GetCygxActivitySpecialTripBillForInit() (item []*CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_activity_special_trip_bill GROUP BY company_id `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

func GetCygxActivitySpecialTripBillByCompanyIdForInit(companyId int) (item []*CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_activity_special_trip_bill WHERE company_id = ? ORDER BY create_time DESC  `
	_, err = o.Raw(sql, companyId).QueryRows(&item)
	return
}

func UpdateCygxActivitySpecialTripBillByCompanyIdForInit(total string, id int) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE cygx_activity_special_trip_bill SET total=? WHERE id = ?  `
	_, err = o.Raw(sql, total, id).Exec()
	return
}

// GetCygxActivitySpecialTripBillLastDetialByActivityId  根据活动ID获取最后一条扣点明细
func GetCygxActivitySpecialTripBillLastDetialByActivityId(activityId, userId int) (item *CygxActivitySpecialTripBill, err error) {
	o := orm.NewOrm()
	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
}
