package semantic_analysis

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

type SaDoc struct {
	SaDocId      int       `orm:"column(sa_doc_id);pk" description:"文档ID"`
	ClassifyId   int       `description:"文档分类ID"`
	ClassifyName string    `description:"分类名称"`
	Title        string    `description:"标题"`
	Theme        string    `description:"主题"`
	CoverImg     string    `description:"封面图"`
	ContentMd5   string    `description:"内容md5, 用于内容去重"`
	SysAdminId   int       `description:"创建人ID"`
	SysAdminName string    `description:"创建人姓名"`
	Sort         int       `description:"排序"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

var SaDocColumns = struct {
	SaDocId      string
	ClassifyId   string
	ClassifyName string
	Title        string
	Theme        string
	CoverImg     string
	ContentMd5   string
	SysAdminId   string
	SysAdminName string
	Sort         string
	CreateTime   string
	ModifyTime   string
}{
	SaDocId:      "sa_doc_id",
	ClassifyId:   "classify_id",
	ClassifyName: "classify_name",
	Title:        "title",
	Theme:        "theme",
	CoverImg:     "cover_img",
	ContentMd5:   "content_md5",
	SysAdminId:   "sys_admin_id",
	SysAdminName: "sys_admin_name",
	Sort:         "sort",
	CreateTime:   "create_time",
	ModifyTime:   "modify_time",
}

func (m *SaDoc) TableName() string {
	return "sa_doc"
}

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

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

func (m *SaDoc) Del() (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
	_, err = o.Raw(sql, m.SaDocId).Exec()
	return
}

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

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

func (m *SaDoc) 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 *SaDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDoc, 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 *SaDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDoc, 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
}

// InsertSaDocAndSections 新增文档及段落
func InsertSaDocAndSections(doc *SaDoc, sections []*SaDocSection) (err error) {
	o := orm.NewOrm()
	tx, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()

	lastId, err := tx.Insert(doc)
	if err != nil {
		return
	}
	doc.SaDocId = int(lastId)

	if len(sections) > 0 {
		for i := range sections {
			sections[i].DocId = int(lastId)
			secId, e := tx.Insert(sections[i])
			if e != nil {
				err = e
				return
			}
			sections[i].SaDocSectionId = int(secId)
		}
	}
	return
}

// DelSaDocAndSections 删除文档和段落
func DelSaDocAndSections(docId int) (err error) {
	o := orm.NewOrm()
	tx, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()

	sql := `DELETE FROM sa_doc WHERE sa_doc_id = ? LIMIT 1`
	_, err = tx.Raw(sql, docId).Exec()
	if err != nil {
		return
	}
	sql = `DELETE FROM sa_doc_section WHERE doc_id = ?`
	_, err = tx.Raw(sql, docId).Exec()
	return
}

type SaDocAddReq struct {
	Title      string `description:"标题"`
	Theme      string `description:"主题"`
	ClassifyId int    `description:"文档分类ID"`
	Content    string `description:"文档内容"`
}

type SaDocEditReq struct {
	SaDocId     int             `description:"文档ID"`
	Title       string          `description:"标题"`
	Theme       string          `description:"主题"`
	ClassifyId  int             `description:"文档分类ID"`
	SectionList []*SaDocSection `description:"段落列表"`
}

type SaDocDelReq struct {
	SaDocId int `description:"文档ID"`
}

type SaDocMoveReq struct {
	SaDocClassifyId int `description:"文档分类ID"`
	SaDocId         int `description:"文档ID"`
	PrevSaDocId     int `description:"上一个文档ID"`
	NextSaDocId     int `description:"下一个文档ID"`
}

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

type SaDocItem struct {
	SaDocId      int    `description:"文档ID"`
	ClassifyId   int    `description:"文档分类ID"`
	ClassifyName string `description:"分类名称"`
	Title        string `description:"标题"`
	Theme        string `description:"主题"`
	CoverImg     string `description:"封面图"`
	SysAdminId   int    `description:"创建人ID"`
	SysAdminName string `description:"创建人姓名"`
	Sort         int    `description:"排序"`
	CreateTime   string `description:"创建时间"`
	UseNum       int    `description:"引用数"`
}

type SaDocDetail struct {
	SaDocItem
	SectionList []*SaDocSectionItem
}

// UpdateSaDocClassifyByClassifyId 更新文档分类信息
func UpdateSaDocClassifyByClassifyId(classifyId int, classifyName string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE sa_doc SET classify_name = ? WHERE classify_id = ?`
	_, err = o.Raw(sql, classifyName, classifyId).Exec()
	return
}

// UpdateSaDocAndSections 更新文档和段落
func UpdateSaDocAndSections(docItem *SaDoc, insertSecs, updateSecs []*SaDocSection, delSecIds []int, updateCols, updateSecCols []string) (err error) {
	o := orm.NewOrm()
	tx, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()

	// 更新文档
	if _, err = tx.Update(docItem, updateCols...); err != nil {
		return
	}
	// 段落-新增/更新/删除
	if len(insertSecs) > 0 {
		if _, err = tx.InsertMulti(len(insertSecs), insertSecs); err != nil {
			return
		}
	}
	for _, s := range updateSecs {
		if _, err = tx.Update(s, updateSecCols...); err != nil {
			return
		}
	}
	if len(delSecIds) > 0 {
		sql := `DELETE FROM sa_doc_section WHERE doc_id = ? AND sa_doc_section_id IN (%s)`
		sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(delSecIds)))
		_, err = tx.Raw(sql, docItem.SaDocId, delSecIds).Exec()
	}
	return
}

// ElasticSaDoc ES-语义分析文档
type ElasticSaDoc struct {
	SaDocId        int    `description:"文档ID"`
	SaDocSectionId int    `description:"段落ID"`
	ClassifyId     int    `description:"分类ID"`
	ClassifyName   string `description:"分类名称"`
	Title          string `description:"标题"`
	Theme          string `description:"主题"`
	BodyContent    string `description:"内容"`
	Author         string `description:"创建人"`
	CoverImg       string `description:"封面图"`
	CreateTime     string `description:"创建时间"`
}

// GetSaDocsWithUseNumByCondition 获取文档及引用数
func GetSaDocsWithUseNumByCondition(condition string, pars []interface{}) (items []*SaDocItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT
				a.*, COUNT(b.sa_compare_label_id) AS use_num
			FROM
				sa_doc AS a
			LEFT JOIN sa_compare_label AS b ON a.sa_doc_id = b.doc_id
			WHERE
				1 = 1 %s
			GROUP BY
				a.sa_doc_id
			ORDER BY
			    a.sort ASC, a.create_time DESC, a.sa_doc_id ASC`
	sql = fmt.Sprintf(sql, condition)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// UpdateSaDocSort 根据分类ID更新排序
func UpdateSaDocSort(classifyId, nowSort int, prevId int, updateSort string) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE sa_doc SET sort = ` + updateSort + ` WHERE classify_id = ? AND `
	if prevId > 0 {
		sql += ` ( sort > ? or ( sa_doc_id > ` + fmt.Sprint(prevId) + ` and sort = ` + fmt.Sprint(nowSort) + ` )) `
	}
	_, err = o.Raw(sql, classifyId, nowSort).Exec()
	return
}

// GetFirstSortSaDoc 获取排序最前的文档
func GetFirstSortSaDoc(classifyId int) (item *SaDoc, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM sa_doc WHERE classify_id = ? ORDER BY sort ASC,sa_doc_id ASC LIMIT 1`
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}
