package semantic_analysis

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

type SaCompareDoc struct {
	SaCompareDocId int `orm:"column(sa_compare_doc_id);pk"`
	CompareId      int `description:"比对ID"`
	DocId          int `description:"文档ID"`
}

var SaCompareDocColumns = struct {
	SaCompareDocId string
	CompareId      string
	DocId          string
}{
	SaCompareDocId: "sa_compare_doc_id",
	CompareId:      "compare_id",
	DocId:          "doc_id",
}

func (m *SaCompareDoc) TableName() string {
	return "sa_compare_doc"
}

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

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

func (m *SaCompareDoc) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM sa_compare_doc WHERE sa_compare_doc_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.SaCompareDocId).Exec()
	return
}

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

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

func (m *SaCompareDoc) 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 *SaCompareDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompareDoc, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	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 *SaCompareDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompareDoc, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	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
}

// SaCompareDocSection 比对文档段落信息
type SaCompareDocSection struct {
	DocId        int    `description:"文档ID"`
	Title        string `description:"文档标题"`
	Theme        string `description:"文档主题"`
	ClassifyName string `description:"分类名称"`
	SectionId    int    `description:"段落ID"`
	Content      string `description:"段落内容"`
	Sort         int    `description:"段落排序"`
}

// GetSaCompareDocAndSections 获取比对关联的文档段落
func GetSaCompareDocAndSections(compareId int) (items []*SaCompareDocSection, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				a.doc_id,
				b.title,
				b.theme,
				b.classify_name,
				c.sa_doc_section_id AS section_id,
				c.content,
				c.sort
			FROM
				sa_compare_doc AS a
			JOIN sa_doc AS b ON a.doc_id = b.sa_doc_id
			JOIN sa_doc_section AS c ON a.doc_id = c.doc_id
			WHERE
				a.compare_id = ?
			ORDER BY
				a.doc_id,
				c.sort ASC`
	_, err = o.Raw(sql, compareId).QueryRows(&items)
	return
}