package yb

import (
	"eta/eta_api/global"
	"eta/eta_api/utils"
	"fmt"
	"strings"
	"time"
)

// YbPosterResource 研报小程序海报
type YbPosterResource struct {
	Id         int       `orm:"column(id);pk" gorm:"primaryKey" description:"价格驱动ID"`
	Path       string    `description:"请求路径"`
	ImgUrl     string    `description:"图片地址"`
	Type       string    `description:"类型 poster-海报; qrcode-太阳码"`
	Version    string    `description:"版本号"`
	CreateTime time.Time `description:"创建时间"`
}

func (m *YbPosterResource) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*YbPosterResource, err error) {
	o := global.DbMap[utils.DbNameWeekly]
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM yb_poster_resource WHERE 1=1 %s %s`, fields, condition, order)
	err = o.Raw(sql, pars...).Find(&items).Error
	return
}

func (m *YbPosterResource) RemovePosters(ids []int) (err error) {
	num := len(ids)
	if num == 0 {
		return
	}
	o := global.DbMap[utils.DbNameWeekly]
	sql := fmt.Sprintf(`DELETE FROM yb_poster_resource WHERE id IN (%s) LIMIT %d`, utils.GetOrmInReplace(num), num)
	err = o.Exec(sql, ids).Error
	return
}