package semantic_analysis

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
)

type SaDocSection struct {
	SaDocSectionId int    `orm:"column(sa_doc_section_id);pk" description:"段落ID"`
	DocId          int    `description:"文档ID"`
	Content        string `description:"段落内容"`
	Sort           int    `description:"排序"`
}

var SaDocSectionColumns = struct {
	SaDocSectionId string
	DocId          string
	Content        string
	Sort           string
}{
	SaDocSectionId: "sa_doc_section_id",
	DocId:          "doc_id",
	Content:        "content",
	Sort:           "sort",
}

func (m *SaDocSection) TableName() string {
	return "sa_doc_section"
}

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

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

func (m *SaDocSection) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM sa_doc_section WHERE sa_doc_section_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.SaDocSectionId).Exec()
	return
}

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

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

func (m *SaDocSection) 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 *SaDocSection) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDocSection, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY sort ASC`
	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 *SaDocSection) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDocSection, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY sort ASC`
	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 SaDocSectionItem struct {
	SaDocSection
	UseNum     int `description:"整段引用数量"`
	UsePartNum int `description:"片段引用数量"`
}
