package cygx

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

type CygxBanner struct {
	BannerId   int       `orm:"column(banner_id);pk"`
	ImgId      int       `description:"cygx_banner_img主键ID"`
	ListType   string    `description:"ABC哪一列"`
	BannerType int       `description:"1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
	Title      string    `description:"标题"`
	Subtitle   string    `description:"副标题"`
	Sort       int       `description:"排序"`
	Link       string    `description:"链接地址"`
	AdminId    int       `description:"管理员ID"`
	Status     int       `description:"0:未发布，1:已发布"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"更改时间"`
}

type CygxBannerReq struct {
	BannerId   int    `description:"BannerId等于0新增，大于零修改"`
	ImgId      int    `description:"cygx_banner_img主键ID"`
	ListType   string `description:"ABC哪一列"`
	BannerType int    `description:"1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
	Title      string `description:"标题"`
	Subtitle   string `description:"副标题"`
	Sort       int    `description:"排序"`
	Link       string `description:"链接地址"`
}

// 添加
func AddCygxBanner(item *CygxBanner) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// 修改
func UpdateCygxBanner(item *CygxBanner) (err error) {
	to := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["ImgId"] = item.ImgId
	updateParams["ListType"] = item.ListType
	updateParams["BannerType"] = item.BannerType
	updateParams["Title"] = item.Title
	updateParams["Subtitle"] = item.Subtitle
	updateParams["Sort"] = item.Sort
	updateParams["Link"] = item.Link
	updateParams["Status"] = item.Status
	updateParams["ModifyTime"] = time.Now()
	ptrStructOrTableName := "cygx_banner"
	whereParam := map[string]interface{}{"banner_id": item.BannerId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

type CygxBannerResp struct {
	BannerId       int    `description:"BannerId等于0新增，大于零修改"`
	ImgId          int    `description:"cygx_banner_img主键ID"`
	IndexImg       string `description:"小程序封面图"`
	ListType       string `description:"ABC哪一列"`
	BannerType     int    `description:"添加类型，1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
	BannerTypeName string `description:"添加类型名称"`
	Title          string `description:"标题"`
	Subtitle       string `description:"副标题"`
	Sort           int    `description:"排序"`
	Status         int    `description:"0:未发布，1:已发布"`
	Link           string `description:"链接地址"`
	ModifyTime     string `description:"更改时间"`
	Pv             int    `description:"Pv"`
	Uv             int    `description:"Uv"`
}

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

type CygxBannerMoveReq struct {
	BannerId         int    `description:"BannerId"`
	PreviousBannerId int    `description:"上一个BannerId"`
	NextBannerId     int    `description:"下一个BannerId"`
	ListType         string `description:"ABC哪一列"`
}

type GetCygxBannerImgRespDetailResp struct {
	Detail *CygxBannerResp
}

// 通过ID获取详情
func GetCygxBannerDetail(banneId int) (item *CygxBannerResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_banner  WHERE banner_id=? `
	err = o.Raw(sql, banneId).QueryRow(&item)
	return
}

// 获取某一列，最大的排序值
func GetCygxBannerDetailByListTypeMaxSort(listType string) (item *CygxBanner, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT  * FROM cygx_banner  WHERE  list_type = ? ORDER BY sort DESC   LIMIT 1  `
	err = o.Raw(sql, listType).QueryRow(&item)
	return
}

// UpdateCygxBannerRespMulti 批量修改banner排序
func UpdateCygxBannerMulti(items []*CygxBanner) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	p, err := o.Raw(` UPDATE cygx_banner SET sort = ? WHERE banner_id = ?  `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.Sort,
			v.BannerId)
		if err != nil {
			return
		}
	}
	return
}

// 修改是否展示
func EditCygxBannerStatus(status, banneId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_banner SET status=?, modify_time=NOW()   WHERE banner_id=? `
	_, err = o.Raw(sql, status, banneId).Exec()
	return
}

// 获取数量
func GetCygxBannerCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_banner as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxBannerList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxBannerResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_banner as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

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

// 移动列数并修改排序
func EditCygxBannerMove(sort, banneId int, listType string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	//更改上一个节点的排序
	sql := `UPDATE cygx_banner SET sort = sort +1  WHERE   status = 1 AND sort <=?   AND list_type =?   `
	_, err = to.Raw(sql, sort, listType).Exec()
	if err != nil {
		return
	}

	//更改下一个节点的排序
	sql = `UPDATE cygx_banner SET sort = sort +3  WHERE   status = 1 AND sort >?   AND list_type =?   `
	_, err = to.Raw(sql, sort, listType).Exec()
	if err != nil {
		return
	}

	//更改移动对象的排序
	sql = `UPDATE cygx_banner SET sort = ?,  list_type =?, modify_time=NOW()  WHERE banner_id=?   `
	_, err = to.Raw(sql, sort+2, listType, banneId).Exec()
	if err != nil {
		return
	}

	return
}

// 移动列数并修改排序
func UpdateCygxBannerlistType(sort, banneId int, listType string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_banner SET sort = ?,  list_type =?, modify_time=NOW()  WHERE banner_id=?   `
	_, err = o.Raw(sql, sort, listType, banneId).Exec()

	return
}
