lckt-server/api/v1/bot/bot.go
2025-04-09 12:17:33 +08:00

194 lines
5.3 KiB
Go

package bot
import (
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/bot"
botReq "git.echol.cn/loser/lckt/model/bot/request"
"git.echol.cn/loser/lckt/model/common/response"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type BotApi struct{}
// CreateBot 创建机器人
// @Tags Bot
// @Summary 创建机器人
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body bot.Bot true "创建机器人"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /bt/createBot [post]
func (btApi *BotApi) CreateBot(c *gin.Context) {
// 创建业务用Context
ctx := c.Request.Context()
var bt bot.Bot
err := c.ShouldBindJSON(&bt)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = btService.CreateBot(ctx, &bt)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败:"+err.Error(), c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteBot 删除机器人
// @Tags Bot
// @Summary 删除机器人
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body bot.Bot true "删除机器人"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /bt/deleteBot [delete]
func (btApi *BotApi) DeleteBot(c *gin.Context) {
// 创建业务用Context
ctx := c.Request.Context()
ID := c.Query("ID")
err := btService.DeleteBot(ctx, ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败:"+err.Error(), c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteBotByIds 批量删除机器人
// @Tags Bot
// @Summary 批量删除机器人
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /bt/deleteBotByIds [delete]
func (btApi *BotApi) DeleteBotByIds(c *gin.Context) {
// 创建业务用Context
ctx := c.Request.Context()
IDs := c.QueryArray("IDs[]")
err := btService.DeleteBotByIds(ctx, IDs)
if err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败:"+err.Error(), c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateBot 更新机器人
// @Tags Bot
// @Summary 更新机器人
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body bot.Bot true "更新机器人"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /bt/updateBot [put]
func (btApi *BotApi) UpdateBot(c *gin.Context) {
// 从ctx获取标准context进行业务行为
ctx := c.Request.Context()
var bt bot.Bot
err := c.ShouldBindJSON(&bt)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = btService.UpdateBot(ctx, bt)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败:"+err.Error(), c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindBot 用id查询机器人
// @Tags Bot
// @Summary 用id查询机器人
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询机器人"
// @Success 200 {object} response.Response{data=bot.Bot,msg=string} "查询成功"
// @Router /bt/findBot [get]
func (btApi *BotApi) FindBot(c *gin.Context) {
// 创建业务用Context
ctx := c.Request.Context()
ID := c.Query("ID")
rebt, err := btService.GetBot(ctx, ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败:"+err.Error(), c)
return
}
response.OkWithData(rebt, c)
}
// GetBotList 分页获取机器人列表
// @Tags Bot
// @Summary 分页获取机器人列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query botReq.BotSearch true "分页获取机器人列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /bt/getBotList [get]
func (btApi *BotApi) GetBotList(c *gin.Context) {
// 创建业务用Context
ctx := c.Request.Context()
var pageInfo botReq.BotSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := btService.GetBotInfoList(ctx, pageInfo)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败:"+err.Error(), c)
return
}
response.OkWithDetailed(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, "获取成功", c)
}
// FindKey 不需要鉴权的机器人接口
// @Tags Bot
// @Summary 不需要鉴权的机器人接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /bt/getBotPublic [get]
func (btApi *BotApi) FindKey(c *gin.Context) {
var req botReq.FindKey
if err := c.ShouldBind(&req); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
bt, err := btService.GetBotPublic(req)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败:"+err.Error(), c)
return
}
response.OkWithDetailed(bt.Content, "获取成功", c)
}