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
}

// SaCompareSaveReq 比对保存请求体
type SaCompareSaveReq struct {
	SaCompareId int    `description:"比对ID"`
	Title       string `description:"标题"`
	ClassifyId  int    `description:"分类ID"`
	CompareList []struct {
		SaDocId     int `description:"文档ID"`
		SectionList []struct {
			SaDocSectionId int    `description:"段落ID"`
			IsPart         int    `description:"是否为片段: 0-整段; 1-片段"`
			StartIndex     int    `description:"片段开始下标"`
			EndIndex       int    `description:"片段结束下标"`
			Content        string `description:"打标签的内容"`
			LabelIds       []int  `description:"标签IDs"`
		} `description:"段落标签信息"`
	} `description:"比对列表"`
}

// SaCompareSaveResp 比对保存响应体
type SaCompareSaveResp struct {
	SaCompareId int                       `description:"比对ID"`
	TitleList   []string                  `description:"标题列表"`
	LabelList   []*SaCompareSaveRespLabel `description:"标签列表"`
	//ThemeList   []string                  `description:"主题列表"`
}

// SaCompareSaveRespLabel 比对响应标签
type SaCompareSaveRespLabel struct {
	SaLabelId int                     `description:"标签ID"`
	LabelName string                  `description:"标签名称"`
	DocList   []*SaCompareSaveRespDoc `description:"文档列表"`
}

// SaCompareSaveRespDoc 比对响应文档
type SaCompareSaveRespDoc struct {
	SaDocId     int                         `description:"文档ID"`
	Title       string                      `description:"文档标题"`
	SectionList []*SaCompareSaveRespSection `description:"段落列表"`
}

// SaCompareSaveRespSection 比对响应段落
type SaCompareSaveRespSection struct {
	SaDocSectionId int    `description:"段落ID"`
	IsPart         int    `description:"是否为片段: 0-否 1-是"`
	Content        string `description:"段落内容"`
}

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

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:"修改时间"`
}

// SaCompareUpdateResultImgReq 更新比对结果图片请求体
type SaCompareUpdateResultImgReq struct {
	SaCompareId int    `description:"比对ID"`
	ResultImg   string `description:"结果图片"`
}

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

// CreateSaCompare 新增比对、比对段落和比对标签
func CreateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel) (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(compareItem)
	if err != nil {
		return
	}
	compareId := int(lastId)
	compareItem.SaCompareId = compareId

	// 新增比对段落
	if len(compareDocs) > 0 {
		for i := range compareDocs {
			compareDocs[i].CompareId = compareId
		}
		if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
			err = e
			return
		}
	}

	// 新增比对标签
	if len(compareLabels) > 0 {
		for i := range compareLabels {
			compareLabels[i].CompareId = compareId
		}
		if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
			err = e
			return
		}
	}
	return
}

// UpdateSaCompare 保存比对、比对段落和比对标签
func UpdateSaCompare(compareItem *SaCompare, compareDocs []*SaCompareDoc, compareLabels []*SaCompareLabel, compareUpCols []string) (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_compare_doc WHERE compare_id = ?`
	_, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
	if err != nil {
		return
	}
	sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
	_, err = tx.Raw(sql, compareItem.SaCompareId).Exec()
	if err != nil {
		return
	}

	// 更新比对
	_, err = tx.Update(compareItem, compareUpCols...)
	if err != nil {
		return
	}

	// 新增比对段落
	if len(compareDocs) > 0 {
		for i := range compareDocs {
			compareDocs[i].CompareId = compareItem.SaCompareId
		}
		if _, e := tx.InsertMulti(len(compareDocs), compareDocs); e != nil {
			err = e
			return
		}
	}

	// 新增比对标签
	if len(compareLabels) > 0 {
		for i := range compareLabels {
			compareLabels[i].CompareId = compareItem.SaCompareId
		}
		if _, e := tx.InsertMulti(len(compareLabels), compareLabels); e != nil {
			err = e
			return
		}
	}
	return
}

type SaCompareDetail struct {
	SaCompareItem
	HeadLabel    []*SaCompareDetailHeadLabel `description:"标签列表"`
	DocList      []*SaCompareDetailDoc       `description:"文档列表"`
	KeywordsList []string                    `description:"历史搜索关键词"`
}

type SaCompareDetailHeadLabel struct {
	LabelId   int    `description:"标签ID"`
	LabelName string `description:"标签名称"`
	IsMine    int    `description:"是否为我的:0-否;1-是"`
}

type SaCompareDetailDoc struct {
	DocId        int                       `description:"文档ID"`
	Title        string                    `description:"文档标题"`
	Theme        string                    `description:"文档主题"`
	ClassifyName string                    `description:"分类名称"`
	SectionList  []*SaCompareDetailSection `description:"段落列表"`
}

type SaCompareDetailSection struct {
	SectionId  int                           `description:"段落ID"`
	Content    string                        `description:"段落内容"`
	Sort       int                           `description:"段落排序"`
	IsPart     int                           `description:"是否为片段: 0-否; 1-是"`
	StartIndex int                           `description:"片段开始下标"`
	EndIndex   int                           `description:"片段结束下标"`
	LabelList  []*SaCompareDetailFormatLabel `description:"段落标签"`
}

type SaCompareDetailFormatLabel struct {
	LabelId   int    `description:"标签ID"`
	LabelName string `description:"标签名称"`
	IsThis    int    `description:"是否为当前标签"`
	IsHistory int    `description:"是否为历史标签"`
	IsOther   int    `description:"是否为其他人标签"`
}

type SaCompareDelReq struct {
	SaCompareId int `description:"比对ID"`
}

// DelSaCompare 删除文档和段落
func DelSaCompare(compareId 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_compare WHERE sa_compare_id = ? LIMIT 1`
	_, err = tx.Raw(sql, compareId).Exec()
	if err != nil {
		return
	}
	sql = `DELETE FROM sa_compare_doc WHERE compare_id = ?`
	_, err = tx.Raw(sql, compareId).Exec()
	if err != nil {
		return
	}
	sql = `DELETE FROM sa_compare_label WHERE compare_id = ?`
	_, err = tx.Raw(sql, compareId).Exec()
	return
}

// SaCompareMoveReq 移动比对请求体
type SaCompareMoveReq struct {
	SaCompareClassifyId int `description:"比对分类ID"`
	SaCompareId         int `description:"比对ID"`
	PrevSaCompareId     int `description:"上一个比对ID"`
	NextSaCompareId     int `description:"下一个比对ID"`
}

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

// GetFirstSortSaCompare 获取排序最前的比对
func GetFirstSortSaCompare(classifyId int) (item *SaCompare, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM sa_compare WHERE classify_id = ? ORDER BY sort ASC,sa_compare_id ASC LIMIT 1`
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

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