package semantic_analysis

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

type SaLabel struct {
	SaLabelId    int       `orm:"column(sa_label_id);pk" description:"标签ID"`
	LabelName    string    `description:"标签名称"`
	SysAdminId   int       `description:"创建人ID"`
	SysAdminName string    `description:"创建人名称"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

var SaLabelColumns = struct {
	SaLabelId    string
	LabelName    string
	SysAdminId   string
	SysAdminName string
	CreateTime   string
	ModifyTime   string
}{
	SaLabelId:    "sa_label_id",
	LabelName:    "label_name",
	SysAdminId:   "sys_admin_id",
	SysAdminName: "sys_admin_name",
	CreateTime:   "create_time",
	ModifyTime:   "modify_time",
}

func (m *SaLabel) TableName() string {
	return "sa_label"
}

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

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

func (m *SaLabel) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM sa_label WHERE sa_label_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.SaLabelId).Exec()
	return
}

func (m *SaLabel) GetItemById(id int) (err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sa_label WHERE sa_label_id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&m)
	return
}

func (m *SaLabel) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sa_label WHERE 1=1 `
	sql += condition
	sql += ` LIMIT 1`
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

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

func (m *SaLabel) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaLabel, 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 1=1 %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 SaLabelAddReq struct {
	LabelName string `description:"标签名称"`
}

type SaLabelEditReq struct {
	SaLabelId int `description:"标签ID"`
	SaLabelAddReq
}

type SaLabelDelReq struct {
	SaLabelId int `description:"标签ID"`
}

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

type SaLabelItem struct {
	SaLabelId    int    `description:"标签ID"`
	LabelName    string `description:"标签名称"`
	UserNum      int    `description:"引用数量"`
	SysAdminId   int    `description:"创建人ID"`
	SysAdminName string `description:"创建人姓名"`
	CreateTime   string `description:"创建时间"`
}
