package semantic_analysis

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

type SaCompare struct {
	SaCompareId  int       `orm:"column(sa_compare_id);pk" description:"比对ID"`
	ClassifyId   int       `description:"比对分类ID"`
	ClassifyName string    `description:"比对分类名称"`
	Title        string    `description:"标题"`
	ResultImg    string    `description:"比对结果图片"`
	SysAdminId   int       `description:"创建人ID"`
	SysAdminName string    `description:"创建人姓名"`
	Sort         int       `description:"排序"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

var SaCompareColumns = struct {
	SaCompareId  string
	ClassifyId   string
	ClassifyName string
	Title        string
	ResultImg    string
	SysAdminId   string
	SysAdminName string
	Sort         string
	CreateTime   string
	ModifyTime   string
}{
	SaCompareId:  "sa_compare_id",
	ClassifyId:   "classify_id",
	ClassifyName: "classify_name",
	Title:        "title",
	ResultImg:    "result_img",
	SysAdminId:   "sys_admin_id",
	SysAdminName: "sys_admin_name",
	Sort:         "sort",
	CreateTime:   "create_time",
	ModifyTime:   "modify_time",
}

func (m *SaCompare) TableName() string {
	return "sa_compare"
}

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

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

func (m *SaCompare) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM sa_compare WHERE sa_compare_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.SaCompareId).Exec()
	return
}

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

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

func (m *SaCompare) 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 *SaCompare) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompare, 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 *SaCompare) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompare, 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 SaCompareItem struct {
	SaCompareId  int    `description:"比对ID"`
	ClassifyId   int    `description:"比对分类ID"`
	ClassifyName string `description:"比对分类名称"`
	Title        string `description:"标题"`
	ResultImg    string `description:"比对结果图片"`
	SysAdminId   int    `description:"创建人ID"`
	SysAdminName string `description:"创建人姓名"`
	CreateTime   string `description:"创建时间"`
}

type SaCompareElastic struct {
	SaCompareId  int    `description:"比对ID"`
	ClassifyId   int    `description:"比对分类ID"`
	ClassifyName string `description:"比对分类名称"`
	Title        string `description:"标题"`
	ResultImg    string `description:"比对结果图片"`
	SysAdminId   int    `description:"创建人ID"`
	SysAdminName string `description:"创建人姓名"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"修改时间"`
}

// CompareListByEsResp 文档对比Es搜索返回
type CompareListByEsResp struct {
	Paging *paging.PagingItem
	List   []*SaCompareElastic
}
