package company

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

type CompanyContractPermission struct {
	Id                int       `orm:"column(id);pk" description:"客户合同id"`
	CompanyId         int       `description:"客户id"`
	CompanyContractId int       `description:"合同id"`
	ChartPermissionId int       `description:"权限id"`
	IsUpgrade         int       `description:"是否升级，1是，0否"`
	ExpensiveYx       int       `description:"权益研选: 0-3w; 1-5w"`
	StartDate         string    `description:"开始日期"`
	EndDate           string    `description:"结束日期"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
	Points            float64   `description:"研选扣点包点数"`
}

type CompanyContract struct {
	CompanyContractId int       `orm:"column(company_contract_id);pk" description:"客户合同id"`
	ContractType      string    `description:"合同类型：枚举值：'新签合同','续约合同','补充协议'"`
	ProductId         int       `description:"产品id"`
	ProductName       string    `description:"产品名称"`
	CompanyId         int       `description:"客户id"`
	CompanyProductId  int       `description:"客户产品id"`
	ContractCode      string    `description:"合同编码"`
	StartDate         string    `description:"合同开始时间"`
	EndDate           string    `description:"合同结束时间"`
	Money             float64   `description:"合同金额"`
	PayMethod         string    `description:"支付方式"`
	PayChannel        string    `description:"支付渠道"`
	ImgUrl            string    `description:"合同图片，多个用#隔开"`
	CreateTime        time.Time `description:"合同创建时间"`
	ModifyTime        time.Time `description:"合同修改时间"`
	Status            int       `description:"状态"`
	Source            string    `description:"合同来源，枚举值：上传附件、系统合同，默认上传附件"`
	PackageType       int       `description:"套餐类型，0：无，1：大套餐，2：小套餐"`
	RaiPackageType    int       `description:"权益套餐类型: 0-无; 1-70w大套餐; 2-45w大套餐"`
	RaiContractType   string    `description:"权益合同类型：枚举值：'新签合同','续约合同','补充协议'"`
}

func GetCompanyContractPermissionCheck(companyContractId, permissionId int) (count int, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT COUNT(1) AS count FROM company_contract_permission AS a WHERE a.company_contract_id=? AND a.chart_permission_id=? `
	err = o.Raw(sql, companyContractId, permissionId).QueryRow(&count)
	return
}

// 通过查询条件获取详情
func GetCompanyContractPermissionDetail(condition string, pars []interface{}) (item *CompanyContractPermission, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM company_contract_permission  WHERE 1 = 1  ` + condition
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

// 获取合同对应的权限列表
func GetCompanyContractPermissionList(condition string, pars []interface{}) (items []*CompanyContractPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM company_contract_permission WHERE 1 = 1  `

	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 通过查询条件获取详情
func GetCompanyContractList(condition string, pars []interface{}) (items []*CompanyContract, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM company_contract  WHERE 1 = 1  ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetCompanyContractById(companyContractId int) (item *CompanyContract, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM company_contract WHERE company_contract_id=? `
	err = o.Raw(sql, companyContractId).QueryRow(&item)
	return
}

// 通过查询条件获取最后一个合同
func GetCompanyContractLastList(condition string, pars []interface{}) (items []*CompanyContract, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT MAX( company_contract_id )  as company_contract_id  FROM company_contract  WHERE 1 = 1  ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 获取合同是否包含点数
func GetCompanyContractPermissionPointsCount(companyId int) (points float64, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sqlCount := `SELECT points FROM company_contract_permission  WHERE company_contract_id=?  AND points >0   LIMIT 	  1 `
	err = o.Raw(sqlCount, companyId).QueryRow(&points)
	return
}
