package smart_report

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

type SmartReportResource struct {
	ResourceId int       `orm:"column(resource_id);pk" description:"智能研报资源ID"`
	ImgUrl     string    // 图片链接
	Style      string    // 版图样式
	ImgName    string    // 图片名称
	Type       int       // 类型 1-版头 2-版尾
	CreateTime time.Time // 创建时间
}

func (m *SmartReportResource) TableName() string {
	return "smart_report_resource"
}

func (m *SmartReportResource) PrimaryId() string {
	return "resource_id"
}

func (m *SmartReportResource) Create() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.ResourceId = int(id)
	return
}

func (m *SmartReportResource) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(m, cols...)
	return
}

func (m *SmartReportResource) Del() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.ResourceId).Exec()
	return
}

type SmartReportResourceItem struct {
	ResourceId int    `orm:"column(resource_id);pk" description:"智能研报资源ID"`
	ImgUrl     string // 图片链接
	ImgName    string // 图片名称
	Style      string // 版图样式
	Type       int    // 类型 1-版头 2-版尾
	CreateTime string // 创建时间
}

// SmartReportResourceListResp 智能研报资源库
type SmartReportResourceListResp struct {
	List   []*SmartReportResourceItem
	Paging *paging.PagingItem `description:"分页数据"`
}

func (m *SmartReportResource) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *SmartReportResource) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, startSize, pageSize int) (items []*SmartReportResourceItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ` ORDER BY create_time DESC`

	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// SmartReportResourceEditReq 智能研报资源编辑请求体
type SmartReportResourceEditReq struct {
	ResourceId int    `description:"资源ID"`
	ImgName    string `description:"图片名称"`
	Style      string `description:"版图样式"`
}

func (m *SmartReportResource) GetItemById(id int) (item *SmartReportResource, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

// SmartReportResourceRemoveReq 删除智能研报资源请求体
type SmartReportResourceRemoveReq struct {
	ResourceIds string `description:"资源IDs"`
}

// SmartReportResourceAddReq 新增智能研报资源请求体
type SmartReportResourceAddReq struct {
	Type    int    `description:"类型 1-版头 2-版尾"`
	ImgUrl  string `description:"图片链接"`
	ImgName string `description:"图片名称"`
	Style   string `description:"版图样式"`
}

func GetResourceItemById(id int) (item *SmartReportResource, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT * FROM smart_report_resource WHERE resource_id = ? LIMIT 1`)
	err = o.Raw(sql, id).QueryRow(&item)
	return
}
