package cygx

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

type CygxCompanyUserType struct {
	Id             int `orm:"column(id);pk" description:"分析师id"`
	CompanyId      int
	CustomerTypeId int       `description:"用户身份ID"`
	PackageType    int       `description:"1：70万 2：45w"`
	CreateTime     time.Time `description:"创建时间"`
	ModifyTime     time.Time `description:"创建时间"`
}

// 新增权益客户身份类型
func AddCygxCompanyUserType(item *CygxCompanyUserType) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

func GetCygxCompanyUserTypeByCompanyId(companyId int) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_company_user_type AS a WHERE a.company_id=?  `
	err = o.Raw(sql, companyId).QueryRow(&count)
	return
}

// CygxCompanyUserType 更新权益客户身份类型
func UpdateCygxCompanyUserType(companyId, useType int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_company_user_type SET customer_type_id=? WHERE company_id=? `
	_, err = o.Raw(sql, useType, companyId).Exec()
	return
}

// 通过活动ID获取详情
func GetCygxCompanyUserType(companyId int) (item *CygxCompanyUserType, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_company_user_type AS a WHERE a.company_id=?  `
	err = o.Raw(sql, companyId).QueryRow(&item)
	return
}

// 获取大套餐客户列表
func GetCygxCompanyUserTypeList() (items []*CygxCompanyUserType, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_company_user_type AS a WHERE a.customer_type_id=2  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// CygxCompanyUserType 更新权益客户身份类型
func UpdateCygxCompanyUserTypeAndPackageType(companyId, useType, packageType int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_company_user_type SET customer_type_id=?, package_type = ? WHERE company_id=? `
	_, err = o.Raw(sql, useType, packageType, companyId).Exec()
	return
}
