75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package dao
|
|
|
|
import (
|
|
"StuAcaWorksAI/proto"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AIStreamMsg struct {
|
|
gorm.Model
|
|
Type string `gorm:"column:type"`
|
|
ModelID uint `gorm:"column:model_id index:idx_covering,unique"`
|
|
UserID int `gorm:"column:user_id"` //用户id
|
|
SessionID int `gorm:"column:session_id"` //会话id
|
|
MSG string `gorm:"column:msg"` //消息内容
|
|
}
|
|
|
|
func InsertAIStreamMsgToDB(userID, sessionID int, modelID uint, msg, tp string) uint {
|
|
aiStreamMsg := AIStreamMsg{UserID: userID, ModelID: modelID, MSG: msg, Type: tp, SessionID: sessionID}
|
|
DB.Create(&aiStreamMsg)
|
|
if aiStreamMsg.ID == 0 {
|
|
return 0
|
|
}
|
|
return aiStreamMsg.ID
|
|
}
|
|
|
|
func FindAIStreamMsgByUserID(userID int) []AIStreamMsg {
|
|
var aiStreamMsgs []AIStreamMsg
|
|
DB.Where("user_id = ?", userID).Find(&aiStreamMsgs)
|
|
return aiStreamMsgs
|
|
}
|
|
|
|
func FindAIStreamMsgByModelID(modelID uint) []AIStreamMsg {
|
|
var aiStreamMsgs []AIStreamMsg
|
|
DB.Where("model_id = ?", modelID).Find(&aiStreamMsgs)
|
|
return aiStreamMsgs
|
|
}
|
|
|
|
func FindAIStreamMsgByID(id uint) (error, AIStreamMsg) {
|
|
var aiStreamMsg AIStreamMsg
|
|
err := DB.Where("id = ?", id).First(&aiStreamMsg).Error
|
|
return err, aiStreamMsg
|
|
}
|
|
|
|
type AIStreamMsgModelIDCount struct {
|
|
ModelID uint `gorm:"column:model_id" json:"model_id"`
|
|
ModelName string `gorm:"column:model_name" json:"name"`
|
|
Count int `gorm:"column:count" json:"value"`
|
|
}
|
|
|
|
// 获取模型消息数量,统计数据
|
|
func GetAIStreamModelMsgCount() ([]AIStreamMsgModelIDCount, error) {
|
|
var aiStreamMsgModelIDCounts []AIStreamMsgModelIDCount
|
|
var db2 *gorm.DB
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
db2 = DB.Debug()
|
|
} else {
|
|
db2 = DB
|
|
}
|
|
db2.Model(&AIStreamMsg{}).Select("model_id, count(*) as count").Group("model_id").Find(&aiStreamMsgModelIDCounts)
|
|
if db2.Error != nil {
|
|
return nil, db2.Error
|
|
}
|
|
//获取模型名称
|
|
for i, _ := range aiStreamMsgModelIDCounts {
|
|
modelName := FindModelByIDV2(int(aiStreamMsgModelIDCounts[i].ModelID))
|
|
if modelName == nil || len(modelName) == 0 {
|
|
aiStreamMsgModelIDCounts[i].ModelName = "未知模型"
|
|
} else {
|
|
aiStreamMsgModelIDCounts[i].ModelName = modelName[0].Description
|
|
}
|
|
|
|
}
|
|
return aiStreamMsgModelIDCounts, nil
|
|
}
|