package yb

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

type Pdf struct {
	PdfId        int       `orm:"column(pdf_id);pk"`
	PdfName      string    `description:"PDF名称"`
	PdfUrl       string    `description:"PDF地址"`
	OriginName   string    `description:"文件原始名称"`
	ShareUrl     string    `description:"分享地址"`
	IsDeleted    int       `description:"删除状态: 0-正常; 1-已删除"`
	SysAdminId   int       `description:"创建人ID"`
	SysAdminName string    `description:"创建人名称"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

var PdfColumns = struct {
	PdfId        string
	PdfName      string
	OriginName   string
	PdfUrl       string
	ShareUrl     string
	IsDeleted    string
	SysAdminId   string
	SysAdminName string
	CreateTime   string
	ModifyTime   string
}{
	PdfId:        "pdf_id",
	PdfName:      "pdf_name",
	OriginName:   "origin_name",
	PdfUrl:       "pdf_url",
	ShareUrl:     "share_url",
	IsDeleted:    "is_deleted",
	SysAdminId:   "sys_admin_id",
	SysAdminName: "sys_admin_name",
	CreateTime:   "create_time",
	ModifyTime:   "modify_time",
}

func (m *Pdf) TableName() string {
	return "yb_pdf"
}

func (m *Pdf) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.PdfId = int(id)
	return
}

func (m *Pdf) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *Pdf) Del() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`UPDATE %s SET is_deleted = 1 WHERE pdf_id = ? LIMIT 1`, m.TableName())
	_, err = o.Raw(sql, m.PdfId).Exec()
	return
}

func (m *Pdf) GetItemById(id int) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE pdf_id = ? AND is_deleted = 0 LIMIT 1`, m.TableName())
	err = o.Raw(sql, id).QueryRow(&m)
	return
}

func (m *Pdf) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE is_deleted = 0 `, m.TableName())
	sql += condition
	sql += ` LIMIT 1`
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

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

func (m *Pdf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Pdf, err error) {
	o := orm.NewOrm()
	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 %s WHERE is_deleted = 0 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *Pdf) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*Pdf, err error) {
	o := orm.NewOrm()
	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 %s WHERE is_deleted = 0 %s %s`, fields, m.TableName(), condition, order)
	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type PdfAddReq struct {
	PdfName    string `description:"PDF名称"`
	PdfUrl     string `description:"PDF地址"`
	OriginName string `description:"文件原始名称"`
}

type PdfEditReq struct {
	PdfId int
	PdfAddReq
}

type PdfDelReq struct {
	PdfId int
}

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

type PdfItem struct {
	PdfId        int
	PdfName      string `description:"PDF名称"`
	OriginName   string `description:"文件原始名称"`
	PdfUrl       string `description:"PDF地址"`
	ShareUrl     string `description:"分享地址"`
	SysAdminId   int    `description:"创建人ID"`
	SysAdminName string `description:"创建人名称"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"修改时间"`
}
