package models

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

type CygxActivitySpecialPermissionPoints struct {
	Id                  int       `orm:"column(id);pk"`
	CompanyId           int       `description:"公司ID"`
	Points              float64   `description:"剩余点数"`
	CompanyName         string    `description:"公司名称"`
	ChartPermissionId   int       `description:"行业id"`
	ChartPermissionName string    `description:"行业名称"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"更新时间"`
}

// 添加
func AddCygxActivitySpecialPermissionPoints(item *CygxActivitySpecialPermissionPoints) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

// 获取公司每个行业剩余研选点数列表
func GetCygxActivitySpecialPermissionPointsList(condition string, pars []interface{}) (items []*CygxActivitySpecialPermissionPoints, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_special_permission_points WHERE 1 = 1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 更新公司每个行业剩余研选点数
func UpdateCygxActivitySpecialPermissionPoints(points float64, companyId, chartPermissionId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_activity_special_permission_points SET points = ?  WHERE company_id=?  AND chart_permission_id=? `
	_, err = o.Raw(sql, points, companyId, chartPermissionId).Exec()
	return
}

// 获取公司剩余点数
func GetCygxActivitySpecialPermissionPoints(companyId int) (comapnyPointsNum float64, err error) {
	o := orm.NewOrm()
	sql := `SELECT points FROM cygx_activity_special_permission_points  WHERE company_id=? `
	err = o.Raw(sql, companyId).QueryRow(&comapnyPointsNum)
	return
}

// 获取数量
func GetCygxActivitySpecialPermissionPointsCountByCompanyId(companyId int) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_special_permission_points WHERE company_id=?  `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, companyId).QueryRow(&count)
	return
}
