package rag

import (
	"database/sql"
	"eta/eta_api/global"
	"eta/eta_api/utils"
	"fmt"
	"time"
)

// RagEtaReport eta报告
type RagEtaReport struct {
	RagEtaReportId  int       `gorm:"primaryKey;column:rag_eta_report_id" `
	ReportId        int       `gorm:"column:report_id" description:"报告id"`
	ReportChapterId int       `gorm:"column:report_chapter_id" description:"报告章节id"`
	Title           string    `gorm:"column:title" description:"报告标题(完整标题,含期数)"`
	Author          string    `gorm:"column:author" description:"作者"`
	TextContent     string    `gorm:"column:text_content" description:"报告内容(去除html)"`
	VectorKey       string    `gorm:"column:vector_key" description:"向量库的key"`
	IsDeleted       int       `gorm:"column:is_deleted;type:tinyint(4);comment:是否删除,0:未删除,1:已删除;default:0;" description:"否删除,0:未删除,1:已删除"`
	PublishTime     time.Time `gorm:"column:publish_time" description:"发布时间"`
	ModifyTime      time.Time `gorm:"column:modify_time" description:"修改时间"`
	CreateTime      time.Time `gorm:"column:create_time" description:"新增时间"`
}

// TableName get sql table name.获取数据库表名
func (m *RagEtaReport) TableName() string {
	return "rag_eta_report"
}

// RagEtaReportColumns get sql column name.获取数据库列名
var RagEtaReportColumns = struct {
	RagEtaReportID  string
	ReportID        string
	ReportChapterID string
	Title           string
	Author          string
	TextContent     string
	VectorKey       string
	IsDeleted       string
	PublishTime     string
	ModifyTime      string
	CreateTime      string
}{
	RagEtaReportID:  "rag_eta_report_id",
	ReportID:        "report_id",
	ReportChapterID: "report_chapter_id",
	Title:           "title",
	Author:          "author",
	TextContent:     "text_content",
	VectorKey:       "vector_key",
	IsDeleted:       "is_deleted",
	PublishTime:     "publish_time",
	ModifyTime:      "modify_time",
	CreateTime:      "create_time",
}

type RagEtaReportView struct {
	RagEtaReportId  int    `gorm:"primaryKey;column:rag_eta_report_id" `
	ReportId        int    `gorm:"column:report_id" description:"报告id"`
	ReportChapterId int    `gorm:"column:report_chapter_id" description:"报告章节id"`
	Title           string `gorm:"column:title" description:"报告标题(完整标题,含期数)"`
	Author          string `gorm:"column:author" description:"作者"`
	Content         string `gorm:"column:content" description:"报告内容(包含html)"`
	TextContent     string `gorm:"column:text_content" description:"报告内容(去除html)"`
	VectorKey       string `gorm:"column:vector_key" description:"向量库的key"`
	IsDeleted       int    `gorm:"column:is_deleted;type:tinyint(4);comment:是否删除,0:未删除,1:已删除;default:0;" description:"否删除,0:未删除,1:已删除"`
	PublishTime     string `gorm:"column:publish_time" description:"发布时间"`
	ModifyTime      string `gorm:"column:modify_time" description:"修改时间"`
	CreateTime      string `gorm:"column:create_time" description:"新增时间"`
}

func (m *RagEtaReport) ToView() RagEtaReportView {
	var publishTime, modifyTime, createTime string

	if !m.PublishTime.IsZero() {
		publishTime = m.PublishTime.Format(utils.FormatDateTime)
	}
	if !m.CreateTime.IsZero() {
		createTime = m.CreateTime.Format(utils.FormatDateTime)
	}
	if !m.ModifyTime.IsZero() {
		modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
	}
	return RagEtaReportView{
		RagEtaReportId:  m.RagEtaReportId,
		ReportId:        m.ReportId,
		ReportChapterId: m.ReportChapterId,
		Title:           m.Title,
		Author:          m.Author,
		TextContent:     m.TextContent,
		VectorKey:       m.VectorKey,
		PublishTime:     publishTime,
		ModifyTime:      modifyTime,
		CreateTime:      createTime,
	}
}

func (m *RagEtaReport) ListToViewList(list []*RagEtaReport) (RagEtaReportViewList []RagEtaReportView) {
	RagEtaReportViewList = make([]RagEtaReportView, 0)

	for _, v := range list {
		RagEtaReportViewList = append(RagEtaReportViewList, v.ToView())
	}
	return
}

func (m *RagEtaReport) Create() (err error) {
	err = global.DbMap[utils.DbNameAI].Create(&m).Error

	return
}

func (m *RagEtaReport) Update(updateCols []string) (err error) {
	err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error

	return
}

func (m *RagEtaReport) GetById(id int) (item *RagEtaReport, err error) {
	err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", RagEtaReportColumns.RagEtaReportID), id).First(&item).Error

	return
}

func (m *RagEtaReport) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*RagEtaReport, err error) {
	if field == "" {
		field = "*"
	}
	sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE is_deleted=0 %s  order by publish_time desc,report_id desc,report_chapter_id desc LIMIT ?,?`, field, m.TableName(), condition)
	pars = append(pars, startSize, pageSize)
	err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error

	return
}

func (m *RagEtaReport) GetCountByCondition(condition string, pars []interface{}) (total int, err error) {
	var intNull sql.NullInt64
	sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s WHERE 1=1 AND is_deleted=0 %s`, m.TableName(), condition)
	err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
	if err == nil && intNull.Valid {
		total = int(intNull.Int64)
	}

	return
}

func (m *RagEtaReport) GetPageListByCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*RagEtaReport, err error) {

	total, err = m.GetCountByCondition(condition, pars)
	if err != nil {
		return
	}
	if total > 0 {
		items, err = m.GetListByCondition(``, condition, pars, startSize, pageSize)
	}

	return
}