完善AI助手相关接口

This commit is contained in:
李寻欢 2024-02-02 17:30:11 +08:00
parent 63c8c3a292
commit 9b3f1bb488
9 changed files with 142 additions and 2 deletions

View File

@ -0,0 +1,25 @@
package aiassistant
import (
"github.com/gin-gonic/gin"
"wechat-robot/model/param/aiassistant"
"wechat-robot/pkg/response"
aiAssistantService "wechat-robot/service/aiassistant"
)
// Save
// @description: 保存AI助手
// @param ctx
func Save(ctx *gin.Context) {
var p aiassistant.Save
if err := ctx.ShouldBind(&p); err != nil {
response.New(ctx).SetMsg("参数错误").SetError(err).Fail()
return
}
// 保存数据
if err := aiAssistantService.Save(p); err != nil {
response.New(ctx).SetMsg("保存失败").SetError(err).Fail()
return
}
response.New(ctx).SetMsg("保存成功").Success()
}

View File

@ -0,0 +1,26 @@
package aiassistant
import (
"github.com/gin-gonic/gin"
"wechat-robot/model/param/aiassistant"
"wechat-robot/pkg/response"
aiAssistantService "wechat-robot/service/aiassistant"
)
// GetAll
// @description: 获取所有AI助手
// @param ctx
func GetAll(ctx *gin.Context) {
var p aiassistant.GetAll
if err := ctx.ShouldBind(&p); err != nil {
response.New(ctx).SetMsg("参数错误").SetError(err).Fail()
return
}
records, err := aiAssistantService.GetAll(p)
if err != nil {
response.New(ctx).SetMsg("获取失败").SetError(err).Fail()
return
}
response.New(ctx).SetData(records).Success()
}

View File

@ -8,7 +8,8 @@ type AiAssistant struct {
types.BaseDbModel types.BaseDbModel
Name string `json:"name" gorm:"type:varchar(10);not null;comment:'名称'"` Name string `json:"name" gorm:"type:varchar(10);not null;comment:'名称'"`
Personality string `json:"personality" gorm:"type:varchar(999);not null;comment:'人设'"` Personality string `json:"personality" gorm:"type:varchar(999);not null;comment:'人设'"`
Model string `json:"setting" gorm:"type:varchar(50);not null;comment:'使用的模型'"` Model string `json:"model" gorm:"type:varchar(50);not null;comment:'使用的模型'"`
Enable bool `json:"enable" gorm:"type:tinyint(1);not null;default:1;comment:'是否启用'"`
} }
// TableName // TableName

View File

@ -0,0 +1,11 @@
package aiassistant
// Save
// @description: 保存AI助手入参
type Save struct {
Id string `json:"id" form:"id"` // Id
Name string `json:"name" form:"name" binding:"required"` // 名称
Personality string `json:"personality" form:"personality" binding:"required"` // 人设
Model string `json:"model" form:"model" binding:"required"` // 使用的模型
Enable bool `json:"enable" form:"enable"` // 是否启用
}

View File

@ -0,0 +1,7 @@
package aiassistant
// GetAll
// @description: 获取所有AI助手
type GetAll struct {
Keyword string `json:"keyword" form:"keyword"` // 关键字
}

View File

@ -0,0 +1,14 @@
package admin
import (
"github.com/gin-gonic/gin"
aiassistantApi "wechat-robot/api/admin/aiassistant"
)
// aiAssistant
// @description: AI助手相关
// @param g
func aiAssistant(g *gin.RouterGroup) {
g.POST("", aiassistantApi.Save) // 保存AI助手
g.GET("", aiassistantApi.GetAll) // 获取所有AI助手
}

View File

@ -13,4 +13,5 @@ func InitRoute(g *gin.RouterGroup) {
g.Use(middleware.AuthorizeToken()) g.Use(middleware.AuthorizeToken())
menu(g.Group("/menu")) // 菜单相关 menu(g.Group("/menu")) // 菜单相关
aiAssistant(g.Group("/ai-assistant")) // AI助手相关
} }

View File

@ -0,0 +1,32 @@
package aiassistant
import (
"wechat-robot/internal/database"
"wechat-robot/model/entity"
"wechat-robot/model/param/aiassistant"
)
// Save
// @description: 保存AI助手
// @param p
// @return err
func Save(p aiassistant.Save) (err error) {
if p.Id == "" {
// 新增
var ent entity.AiAssistant
ent.Name = p.Name
ent.Personality = p.Personality
ent.Model = p.Model
ent.Enable = p.Enable
err = database.Client.Create(&ent).Error
} else {
// 修改
var pm = make(map[string]any)
pm["name"] = p.Name
pm["personality"] = p.Personality
pm["model"] = p.Model
pm["enable"] = p.Enable
err = database.Client.Model(&entity.AiAssistant{}).Where("id = ?", p.Id).Updates(pm).Error
}
return
}

View File

@ -0,0 +1,23 @@
package aiassistant
import (
"wechat-robot/internal/database"
"wechat-robot/model/entity"
param "wechat-robot/model/param/aiassistant"
)
// GetAll
// @description: 获取所有AI助手
// @param p param.GetAll 查询参数
// @return records []entity.AiAssistant 查询结果
// @return err error 查询错误
func GetAll(p param.GetAll) (records []entity.AiAssistant, err error) {
tx := database.Client.Order("created_at DESC")
if p.Keyword != "" {
// 关键字模糊匹配,从名称和人设中找
tx.Where("name LIKE ? OR personality LIKE ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
}
err = tx.Find(&records).Error
return
}