package models

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

type Banner struct {
	Id           int       `orm:"column(id);pk"`
	ClassifyId   int       `description:"分类id"`
	ImageUrl     string    `description:"图片路径"`
	BannerType   int       `description:"类型 1:轮播图，2:头部海报"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
	ClassifyName string    `description:"分类名称"`
	JumpUrl      string    `description:"跳转地址"`
}

type BannerAddReq struct {
	ImgUrl       string `description:"图片路径"`
	BannerType   int    `description:"类型"`
	ClassifyId   int    `description:"分类id"`
	ClassifyName string `description:"分类名称"`
	JumpUrl      string `description:"跳转地址"`
}

//添加banner
func AddBanner(item *Banner) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Insert(item)
	return
}

type BannerEditReq struct {
	BannerId     int    `description:"BannerId"`
	ImgUrl       string `description:"图片路径"`
	BannerType   int    `description:"类型"`
	ClassifyId   int    `description:"分类id"`
	ClassifyName string `description:"分类名称"`
	JumpUrl      string `description:"跳转地址"`
}

//编辑banner
func EditBanner(item *BannerEditReq) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := " UPDATE banner SET classify_id = ?, image_url = ?, banner_type = ?,classify_name=?,jump_url=?, modify_time = NOW() WHERE id = ? "
	o.Raw(sql, item.ClassifyId, item.ImgUrl, item.BannerType, item.ClassifyName, item.JumpUrl, item.BannerId).Exec()
	return
}

type BannerDeleteReq struct {
	BannerId int `description:"BannerId"`
}

func DeleteBanner(bannerId int) (err error) {
	sql := `DELETE FROM banner WHERE id=? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, bannerId).Exec()
	return
}

func GetClassify() (items []*Classify, err error) {
	sql := `SELECT * FROM classify WHERE parent_id>0 ORDER BY id DESC `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type BannerList struct {
	Id           int       `orm:"column(id);pk"`
	ClassifyId   int       `description:"分类id"`
	ImageUrl     string    `description:"图片路径"`
	BannerType   int       `description:"类型 1:轮播图，2:头部海报"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
	ClassifyName string    `description:"分类名称"`
	JumpUrl      string    `description:"跳转地址"`
}

//获取轮播图列表
func GetBannerList(condition string, pars []interface{}, startSize, pageSize int) (items []*BannerList, err error) {
	sql := ` SELECT * FROM banner WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  id DESC LIMIT ?,? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetBannerListCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count FROM banner WHERE 1=1 `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

type BannerListResp struct {
	List   []*BannerList
	Paging *paging.PagingItem `description:"分页数据"`
}
