package semantic_analysis

import (
	"eta_gn/eta_api/global"
	"fmt"
	"strings"
)

type SaDocSection struct {
	SaDocSectionId int    `gorm:"primaryKey;column:sa_doc_section_id;type:int(10) unsigned;not null"`      // 语义分析-文档段落表
	DocId          int    `gorm:"index:idx_doc_id;column:doc_id;type:int(10) unsigned;not null;default:0"` // 文档Id
	Content        string `gorm:"column:content;type:text"`                                                // 段落内容
	Sort           int    `gorm:"column:sort;type:int(10) unsigned;not null;default:0"`                    // 排序
}

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) {
	err = global.DEFAULT_DmSQL.Create(m).Error
	return
}

func (m *SaDocSection) Update(cols []string) (err error) {
	err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
	return
}

func (m *SaDocSection) Del() (err error) {
	sql := `DELETE FROM sa_doc_section WHERE sa_doc_section_id = ? LIMIT 1`
	err = global.DEFAULT_DmSQL.Exec(sql, m.SaDocSectionId).Error
	return
}

func (m *SaDocSection) GetItemById(id int) (err error) {
	sql := `SELECT * FROM sa_doc_section WHERE sa_doc_section_id = ? LIMIT 1`
	err = global.DEFAULT_DmSQL.Raw(sql, id).First(&m).Error
	return
}

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

func (m *SaDocSection) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
	return
}

func (m *SaDocSection) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDocSection, err error) {
	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 = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
	return
}

func (m *SaDocSection) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDocSection, err error) {
	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`
	err = global.DEFAULT_DmSQL.Raw(totalSql, pars...).Scan(&total).Error
	if err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
	return
}

type SaDocSectionItem struct {
	SaDocSection
	UseNum     int `description:"整段引用数量"`
	UsePartNum int `description:"片段引用数量"`
}