Ver Fonte

fix:edit alloc_agent API

jimmyyem há 3 meses atrás
pai
commit
8099c7a482

+ 16 - 0
desc/wechat/alloc_agent.api

@@ -18,6 +18,14 @@ type (
         Status  *int `json:"status,optional"`
     }
 
+	AllocAgentQuery {
+		// user_id | 前台用户ID
+		UserId  *string `json:"userId,optional"`
+
+		// organization_id | 租户ID
+		OrganizationId  *uint64 `json:"organizationId,optional"`
+	}
+
     // The response data of alloc agent list | AllocAgent列表数据
     AllocAgentListResp {
         BaseDataInfo
@@ -77,4 +85,12 @@ service Wechat {
     // Get alloc agent by ID | 通过ID获取AllocAgent
     @handler getAllocAgentById
     post /alloc_agent (IDReq) returns (AllocAgentInfoResp)
+
+	// Get alloc agent by ID | 通过ID获取AllocAgent
+	@handler queryAllocAgent
+	post /alloc_agent/query (AllocAgentQuery) returns (AllocAgentInfoResp)
+
+	// Get alloc agent by ID | 通过ID获取AllocAgent
+	@handler doAllocAgent
+	post /alloc_agent/alloc (AllocAgentInfo) returns (BaseMsgResp)
 }

+ 44 - 0
internal/handler/allocagent/do_alloc_agent_handler.go

@@ -0,0 +1,44 @@
+package allocagent
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/allocagent"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /alloc_agent/alloc allocagent DoAllocAgent
+//
+// Get alloc agent by ID | 通过ID获取AllocAgent
+//
+// Get alloc agent by ID | 通过ID获取AllocAgent
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: AllocAgentInfo
+//
+// Responses:
+//  200: BaseMsgResp
+
+func DoAllocAgentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.AllocAgentInfo
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := allocagent.NewDoAllocAgentLogic(r.Context(), svcCtx)
+		resp, err := l.DoAllocAgent(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/allocagent/query_alloc_agent_handler.go

@@ -0,0 +1,44 @@
+package allocagent
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/allocagent"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /alloc_agent/query allocagent QueryAllocAgent
+//
+// Get alloc agent by ID | 通过ID获取AllocAgent
+//
+// Get alloc agent by ID | 通过ID获取AllocAgent
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: AllocAgentQuery
+//
+// Responses:
+//  200: AllocAgentInfoResp
+
+func QueryAllocAgentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.AllocAgentQuery
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := allocagent.NewQueryAllocAgentLogic(r.Context(), svcCtx)
+		resp, err := l.QueryAllocAgent(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 10 - 0
internal/handler/routes.go

@@ -1537,6 +1537,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/alloc_agent",
 					Handler: allocagent.GetAllocAgentByIdHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/alloc_agent/query",
+					Handler: allocagent.QueryAllocAgentHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/alloc_agent/alloc",
+					Handler: allocagent.DoAllocAgentHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 74 - 0
internal/logic/allocagent/do_alloc_agent_logic.go

@@ -0,0 +1,74 @@
+package allocagent
+
+import (
+	"context"
+	"github.com/alibabacloud-go/tea/tea"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/ent"
+	"wechat-api/ent/allocagent"
+	"wechat-api/ent/predicate"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type DoAllocAgentLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewDoAllocAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DoAllocAgentLogic {
+	return &DoAllocAgentLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *DoAllocAgentLogic) DoAllocAgent(req *types.AllocAgentInfo) (*types.BaseMsgResp, error) {
+	resp := types.BaseMsgResp{}
+
+	if len(req.Agents) == 0 {
+		return nil, errorx.NewInvalidArgumentError("智能体ID不能为空")
+	}
+	if req.OrganizationId == nil && req.UserId == nil {
+		return nil, errorx.NewInvalidArgumentError("租户ID和用户ID不能都为空")
+	}
+
+	var predicates []predicate.AllocAgent
+	if req.UserId != nil && *req.UserId != "" {
+		predicates = append(predicates, allocagent.UserIDEQ(*req.UserId))
+	}
+	if req.OrganizationId != nil && *req.OrganizationId > 0 {
+		predicates = append(predicates, allocagent.OrganizationIDEQ(*req.OrganizationId))
+	}
+	_, err := l.svcCtx.DB.AllocAgent.Query().Where(predicates...).First(l.ctx)
+	if err != nil {
+		if ent.IsNotFound(err) {
+			_, err = l.svcCtx.DB.AllocAgent.Create().
+				SetNotNilUserID(req.UserId).
+				SetNotNilOrganizationID(req.OrganizationId).
+				SetNotNilAgents(req.Agents).
+				SetNotNilStatus(tea.Int(1)).
+				Save(l.ctx)
+			if err != nil {
+				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+			}
+		} else {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+	} else {
+		_, err = l.svcCtx.DB.AllocAgent.Update().
+			SetNotNilAgents(req.Agents).
+			Where(predicates...).
+			Save(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+	}
+
+	return &resp, err
+}

+ 63 - 0
internal/logic/allocagent/query_alloc_agent_logic.go

@@ -0,0 +1,63 @@
+package allocagent
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/utils/pointy"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/ent"
+	"wechat-api/ent/allocagent"
+	"wechat-api/ent/predicate"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type QueryAllocAgentLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewQueryAllocAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryAllocAgentLogic {
+	return &QueryAllocAgentLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *QueryAllocAgentLogic) QueryAllocAgent(req *types.AllocAgentQuery) (*types.AllocAgentInfoResp, error) {
+	resp := types.AllocAgentInfoResp{}
+
+	var predicates []predicate.AllocAgent
+	if req.UserId != nil && *req.UserId != "" {
+		predicates = append(predicates, allocagent.UserIDEQ(*req.UserId))
+	}
+	if req.OrganizationId != nil && *req.OrganizationId > 0 {
+		predicates = append(predicates, allocagent.OrganizationIDEQ(*req.OrganizationId))
+	}
+
+	item, err := l.svcCtx.DB.AllocAgent.Query().Where(predicates...).First(l.ctx)
+	if err != nil {
+		if ent.IsNotFound(err) {
+			return nil, errorx.NewInvalidArgumentError("item not found")
+		}
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	resp.Data = types.AllocAgentInfo{
+		BaseIDInfo: types.BaseIDInfo{
+			Id:        &item.ID,
+			CreatedAt: pointy.GetPointer(item.CreatedAt.UnixMilli()),
+			UpdatedAt: pointy.GetPointer(item.UpdatedAt.UnixMilli()),
+		},
+		UserId:         &item.UserID,
+		OrganizationId: &item.OrganizationID,
+		Agents:         item.Agents,
+		Status:         &item.Status,
+	}
+
+	return &resp, nil
+}

+ 7 - 0
internal/types/types.go

@@ -2987,6 +2987,13 @@ type AllocAgentInfo struct {
 	Status *int `json:"status,optional"`
 }
 
+type AllocAgentQuery struct {
+	// user_id | 前台用户ID
+	UserId *string `json:"userId,optional"`
+	// organization_id | 租户ID
+	OrganizationId *uint64 `json:"organizationId,optional"`
+}
+
 // The response data of alloc agent list | AllocAgent列表数据
 // swagger:model AllocAgentListResp
 type AllocAgentListResp struct {