diff --git a/api/admin/aiassistant/save.go b/api/admin/aiassistant/save.go new file mode 100644 index 0000000..80fb21d --- /dev/null +++ b/api/admin/aiassistant/save.go @@ -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() +} diff --git a/api/admin/aiassistant/select.go b/api/admin/aiassistant/select.go new file mode 100644 index 0000000..a461b6d --- /dev/null +++ b/api/admin/aiassistant/select.go @@ -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() +} diff --git a/model/entity/aiassistant.go b/model/entity/aiassistant.go index e1fb78e..402ab5c 100644 --- a/model/entity/aiassistant.go +++ b/model/entity/aiassistant.go @@ -8,7 +8,8 @@ type AiAssistant struct { types.BaseDbModel Name string `json:"name" gorm:"type:varchar(10);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 diff --git a/model/param/aiassistant/save.go b/model/param/aiassistant/save.go new file mode 100644 index 0000000..b8218d2 --- /dev/null +++ b/model/param/aiassistant/save.go @@ -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"` // 是否启用 +} diff --git a/model/param/aiassistant/select.go b/model/param/aiassistant/select.go new file mode 100644 index 0000000..2146fd0 --- /dev/null +++ b/model/param/aiassistant/select.go @@ -0,0 +1,7 @@ +package aiassistant + +// GetAll +// @description: 获取所有AI助手 +type GetAll struct { + Keyword string `json:"keyword" form:"keyword"` // 关键字 +} diff --git a/router/admin/aiassistant.go b/router/admin/aiassistant.go new file mode 100644 index 0000000..e2df8a4 --- /dev/null +++ b/router/admin/aiassistant.go @@ -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助手 +} diff --git a/router/admin/route.go b/router/admin/route.go index c7cacf5..8c2f93f 100644 --- a/router/admin/route.go +++ b/router/admin/route.go @@ -12,5 +12,6 @@ func InitRoute(g *gin.RouterGroup) { login(g) // 登录相关路由 g.Use(middleware.AuthorizeToken()) - menu(g.Group("/menu")) // 菜单相关 + menu(g.Group("/menu")) // 菜单相关 + aiAssistant(g.Group("/ai-assistant")) // AI助手相关 } diff --git a/service/aiassistant/save.go b/service/aiassistant/save.go new file mode 100644 index 0000000..b756f4a --- /dev/null +++ b/service/aiassistant/save.go @@ -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 +} diff --git a/service/aiassistant/select.go b/service/aiassistant/select.go new file mode 100644 index 0000000..32e8a7d --- /dev/null +++ b/service/aiassistant/select.go @@ -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 +}