package models

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

// ReportSendThsConfig 产品推送给同花顺的配置的表结构体
type ReportSendThsConfig struct {
	ConfigId     int       `orm:"column(config_id);pk" description:"配置Id"`
	ConfigType   string    `description:"产品类型：研报、视频社区、线上路演"`
	ClassifyId   int       `description:"分类id（该产品类型对应的所属分类id）"`
	ClassifyName string    `description:"分类名称（该产品类型对应的所属分类名称）"`
	Level        int       `description:"等级，1：紧急，2：优先，3：普通"`
	Time         int       `description:"间隔时间，单位：分"`
	SysUserId    int       `description:"操作人用户id"`
	SysRealName  string    `description:"操作人用户名称"`
	ModifyTime   time.Time `description:"最近修改时间"`
	CreateTime   time.Time `description:"创建时间"`
}

// Update 更新产品推送给同花顺的配置
func (item *ReportSendThsConfig) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

// GetReportSendThsConfigByConfigId 根据配置id获取对应的配置信息
func GetReportSendThsConfigByConfigId(configId int) (item *ReportSendThsConfig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_send_ths_config WHERE config_id=? order by config_id desc`
	err = o.Raw(sql, configId).QueryRow(&item)
	return
}

type ReportSendThsConfigListResp struct {
	Paging *paging.PagingItem
	List   []*ReportSendThsConfigListItem
}

type ReportSendThsConfigListItem struct {
	ConfigId     int    `orm:"column(config_id);pk" description:"配置Id"`
	ConfigType   string `description:"产品类型：研报、视频社区、线上路演"`
	ClassifyId   int    `description:"分类id（该产品类型对应的所属分类id）"`
	ClassifyName string `description:"分类名称（该产品类型对应的所属分类名称）"`
	Level        int    `description:"等级，1：紧急，2：优先，3：普通"`
	ModifyTime   string `description:"最近修改时间"`
	CreateTime   string `description:"创建时间"`
}

// AddReportSendThsConfig 新增产品推送给同花顺的配置的记录
func AddReportSendThsConfig(item *ReportSendThsConfig) (err error) {
	o := orm.NewOrm()
	lastId, err := o.Insert(item)
	if err == nil {
		item.ConfigId = int(lastId)
	}
	return
}

// GetReportSendThsConfigByClassifyId 根据分类id获取对应的配置信息
func GetReportSendThsConfigByClassifyId(configType string, classifyId int) (item *ReportSendThsConfig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_send_ths_config WHERE classify_id=? AND config_type=? order by config_id desc`
	err = o.Raw(sql, classifyId, configType).QueryRow(&item)
	return
}

// GetCountReportSendThsConfigByClassifyId 根据分类id查询是否添加对应的配置
func GetCountReportSendThsConfigByClassifyId(configType string, classifyId int) (total int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT count(1) total FROM report_send_ths_config WHERE classify_id=? AND config_type=? `
	err = o.Raw(sql, classifyId, configType).QueryRow(&total)
	return
}

// GetReportSendThsConfigList 获取同花顺推送配置列表
func GetReportSendThsConfigList(condition string, pars []interface{}, startSize, pageSize int, orderBy string) (items []*ReportSendThsConfig, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM report_send_ths_config WHERE 1 = 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY ` + orderBy
	sql += `  limit ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// GetReportSendThsConfigCount 获取同花顺推送配置数量
func GetReportSendThsConfigCount(condition string, pars []interface{}) (count int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM report_send_ths_config WHERE 1 = 1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// DeleteReportSendThsConfigByConfigId 删除配置id获取对应的配置信息
func DeleteReportSendThsConfigByConfigId(configId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE from report_send_ths_config WHERE config_id=? `
	_, err = o.Raw(sql, configId).Exec()
	return
}
