batch_delete_agent_data_logic.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package agent
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "wechat-api/hook/fastgpt"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type BatchDeleteAgentDataLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewBatchDeleteAgentDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchDeleteAgentDataLogic {
  17. return &BatchDeleteAgentDataLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx}
  21. }
  22. func (l *BatchDeleteAgentDataLogic) BatchDeleteAgentData(req *types.BatchDeleteIds) (*types.BaseDataInfo, error) {
  23. var success, fail int
  24. if len(req.Ids) > 0 {
  25. var wg sync.WaitGroup
  26. for _, sid := range req.Ids {
  27. wg.Add(1)
  28. go func(sid string) {
  29. defer wg.Done()
  30. response, err := fastgpt.DeleteData(sid)
  31. fmt.Printf("id=%s, response=%+v\n", sid, response)
  32. if err != nil {
  33. fail++
  34. fmt.Printf("delete fastgpt data failed:%v\n", err)
  35. }
  36. if response != nil && response.Code == 200 {
  37. success++
  38. }
  39. }(sid)
  40. }
  41. wg.Wait()
  42. }
  43. resp := types.BaseDataInfo{}
  44. resp.Msg = fmt.Sprintf("succeed %d rows, fail %d rows", success, fail)
  45. return &resp, nil
  46. }