package ai_summary

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

type AiSummary struct {
	AiSummaryId     int       `orm:"column(ai_summary_id);pk"` // ai纪要id
	SaDocId         int       // 文档ID
	OriginContent   string    // 原始内容
	SummaryContent  string    // 纪要内容
	ClassifyId      int       // 分类id
	SysUserId       int       // 创建人ID
	SysUserRealName string    // 创建人姓名
	Title           string    // 文档标题
	CreateTime      time.Time // 创建时间
	ModifyTime      time.Time // 更新时间
	OpenaiFileName  string    // 文件名称
	OpenaiFilePath  string    // 文件路径
	OriginTitle     string    // 原文标题
	Sort            int       // 排序字段,越小越靠前,默认值:10
}

type AiSummaryItems struct {
	AiSummaryId     int    `orm:"column(ai_summary_id);pk"` // ai纪要id
	SaDocId         int    // 文档ID
	OriginContent   string // 原始内容
	OriginTitle     string // 原文标题
	SummaryContent  string // 纪要内容
	ClassifyId      int    // 分类id
	SysUserId       int    // 创建人ID
	SysUserRealName string // 创建人姓名
	Title           string // 文档标题
	CreateTime      string // 创建时间
	ModifyTime      string // 更新时间
	OpenaiFileName  string // 文件名称
	OpenaiFilePath  string // 文件路径
	Sort            int    // 排序字段,越小越靠前,默认值:10
	ParentIds       string
}

func GetAiChatTopicList(sysUserId int) (item []*AiSummary, err error) {
	sql := ` SELECT * FROM ai_summary WHERE classify_id=? ORDER BY create_time DESC `
	o := orm.NewOrm()
	_, err = o.Raw(sql, sysUserId).QueryRows(&item)
	return
}

// GetAiSummaryById 根据纪要id获取详情
func GetAiSummaryById(aiSummaryId int) (sandboxInfo *AiSummary, err error) {
	o := orm.NewOrm()
	sql := `select * from ai_summary where ai_summary_id = ? `
	err = o.Raw(sql, aiSummaryId).QueryRow(&sandboxInfo)
	return
}

// GetAiSummaryById 根据纪要id获取详情
func GetAiSummaryItemById(aiSummaryId int) (sandboxInfo *AiSummaryItems, err error) {
	o := orm.NewOrm()
	sql := `select * from ai_summary where ai_summary_id = ? `
	err = o.Raw(sql, aiSummaryId).QueryRow(&sandboxInfo)
	return
}

// DelAiSummaryById 根据纪要id删除纪要
func DelAiSummaryById(aiSummaryId int) (err error) {
	o := orm.NewOrm()
	sql := `delete from ai_summary where ai_summary_id = ? `
	_, err = o.Raw(sql, aiSummaryId).Exec()
	return
}

func MoveAiSummary(aiSummaryId, classifyId int) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE  ai_summary
			SET
			  classify_id = ?
			WHERE ai_summary_id = ?`
	_, err = o.Raw(sql, classifyId, aiSummaryId).Exec()
	return
}

// GetFirstAiSummaryByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
func GetFirstAiSummaryByClassifyId(classifyId int) (item *AiSummary, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM ai_summary WHERE classify_id=? order by sort asc,ai_summary_id asc limit 1`
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

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

// UpdateSandboxSortByClassifyId 根据纪要id更新排序
func UpdateAiSummarySortByClassifyId(classifyId, nowSort, prevAiSummaryId int, updateSort string) (err error) {
	o := orm.NewOrm()
	sql := ` update ai_summary set sort = ` + updateSort + ` WHERE classify_id=?  AND `
	if prevAiSummaryId > 0 {
		sql += ` (sort > ? or (ai_summary_id > ` + fmt.Sprint(prevAiSummaryId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
	}
	_, err = o.Raw(sql, classifyId, nowSort).Exec()
	return
}

func GetAiSummaryListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*AiSummaryItems, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM ai_summary WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += " ORDER BY create_time DESC LIMIT ?,? "
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
	return
}

func GetAiSummaryListCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT COUNT(1) AS count FROM ai_summary WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func AddAiSummary(item *AiSummary) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type AddAiSummaryReq struct {
	SaDocId        int    // 文档ID
	OriginContent  string // 原始内容
	SummaryContent string // 纪要内容
	ClassifyId     int    // 分类id
	Title          string // 文档标题
	OpenaiFileName string // 文件名称
	OpenaiFilePath string // 文件路径
	OriginTitle    string // 原文标题
}

type GenerateAiSummaryReq struct {
	AiChatTopicId int      `description:"主题id"`
	OriginContent string   // 原始内容
	OpenaiFileId  []string `description:"openai返回的文件id"`
	Model         string   `description:"模型名称"`
	Prompt        string   `description:"提示词"`
	SaDocId       int      // 文档ID
}

// GetAiSummaryMaxSort 获取ai纪要下最大的排序数
func GetAiSummaryMaxSort(classifyId int) (sort int, err error) {
	o := orm.NewOrm()
	sql := `SELECT Max(sort) AS sort FROM ai_summary WHERE parent_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&sort)
	return
}

type AiSummaryAddResp struct {
	AiSummaryId int
	UniqueCode  string // 唯一编码
	ParentId    int
}

type AiSummaryDetailResp struct {
	*AiSummaryItems
	SaDocTitle      string `description:"文档库标题"`
	SaDocClassifyId int    `description:"文档库分类id"`
}