package llm

import (
	"eta/eta_api/global"
	"eta/eta_api/utils"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"time"
)

// UserChatRecord 定义用户聊天记录结构体
type UserChatRecord struct {
	Id           int       `gorm:"primaryKey;autoIncrement;comment:主键"`
	ChatId       int       `gorm:"chat_id;comment:会话id"`
	ChatUserType string    `gorm:"type:enum('user','assistant');comment:用户方"`
	Content      string    `gorm:"content:内容"`
	SendTime     time.Time `gorm:"comment:发送时间"`
	CreatedTime  time.Time `gorm:"comment:创建时间"`
	UpdateTime   time.Time `gorm:"autoUpdateTime;comment:更新时间"`
}
type UserChatRecordRedis struct {
	Id           int
	ChatId       int
	ChatUserType string
	Content      string
	SendTime     string
}

func (u *UserChatRecord) TableName() string {
	return "user_chat_record"
}

func BatchInsertRecords(list []*UserChatRecord) (err error) {
	o := global.DbMap[utils.DbNameAI]
	err = o.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "chat_id"}, {Name: "chat_user_type"}, {Name: "send_time"}},
		DoUpdates: clause.Assignments(map[string]interface{}{"update_time": gorm.Expr("VALUES(update_time)")}),
	}).CreateInBatches(list, utils.MultiAddNum).Error
	return
}