Răsfoiți Sursa

fix:add agent/data/batchDelete API

jimmyyem 3 luni în urmă
părinte
comite
120b515edc

+ 8 - 0
desc/wechat/agent.api

@@ -280,6 +280,10 @@ type (
 	UploadDataReq {
 		AgentId *string `form:"agentId"`
 	}
+
+	BatchDeleteIds {
+		Ids []string `json:"ids"`
+	}
 )
 
 @server(
@@ -289,6 +293,10 @@ type (
 )
 
 service Wechat {
+	// 批量删除fastgpt中数据
+	@handler batchDeleteAgentData
+	post /agent/data/batchDelete (BatchDeleteIds) returns (BaseDataInfo)
+
 	// upload agent data | 上传data
 	@handler uploadAgentData
 	post /agent/data/upload () returns (BaseDataInfo)

+ 44 - 0
internal/handler/agent/batch_delete_agent_data_handler.go

@@ -0,0 +1,44 @@
+package agent
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/agent"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /agent/data/batchDelete agent BatchDeleteAgentData
+//
+
+//
+
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: BatchDeleteIds
+//
+// Responses:
+//  200: BaseDataInfo
+
+func BatchDeleteAgentDataHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.BatchDeleteIds
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := agent.NewBatchDeleteAgentDataLogic(r.Context(), svcCtx)
+		resp, err := l.BatchDeleteAgentData(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

@@ -142,6 +142,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 			[]rest.Route{
 				{
 					Method:  http.MethodPost,
+					Path:    "/agent/data/batchDelete",
+					Handler: agent.BatchDeleteAgentDataHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
 					Path:    "/agent/data/upload",
 					Handler: agent.UploadAgentDataHandler(serverCtx),
 				},

+ 47 - 0
internal/logic/agent/batch_delete_agent_data_logic.go

@@ -0,0 +1,47 @@
+package agent
+
+import (
+	"context"
+	"fmt"
+	"wechat-api/hook/fastgpt"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type BatchDeleteAgentDataLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewBatchDeleteAgentDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchDeleteAgentDataLogic {
+	return &BatchDeleteAgentDataLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *BatchDeleteAgentDataLogic) BatchDeleteAgentData(req *types.BatchDeleteIds) (*types.BaseDataInfo, error) {
+	var success, fail int
+
+	if len(req.Ids) > 0 {
+		for _, sid := range req.Ids {
+			response, err := fastgpt.DeleteData(sid)
+			fmt.Printf("id=%s, response=%+v\n", sid, response)
+			if err != nil {
+				fail++
+				fmt.Printf("delete fastgpt data failed:%v\n", err)
+			}
+			if response != nil && response.Code == 200 {
+				success++
+			}
+		}
+	}
+
+	resp := types.BaseDataInfo{}
+	resp.Msg = fmt.Sprintf("succeed %d rows, fail %d rows", success, fail)
+	return &resp, nil
+}

+ 4 - 0
internal/types/types.go

@@ -712,6 +712,10 @@ type UploadDataReq struct {
 	AgentId *string `form:"agentId"`
 }
 
+type BatchDeleteIds struct {
+	Ids []string `json:"ids"`
+}
+
 // ContactLabelList | Contact标签列表
 type ContactLabelList struct {
 	// label