2026-04-15 17:12:30 +08:00
|
|
|
package dashboard
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
import (
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/builders"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/constants"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto/request"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto/response"
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx/params"
|
2026-05-23 22:22:18 +08:00
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-04-09 10:01:23 +08:00
|
|
|
"github.com/mlogclub/simple/web"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AgentAnyList(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
list, paging := services.AgentProfileService.FindPageByCnd(params.NewPagedSqlCnd(ctx,
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "user_id"},
|
|
|
|
|
params.QueryFilter{ParamName: "team_id"},
|
|
|
|
|
params.QueryFilter{ParamName: "service_status"},
|
|
|
|
|
params.QueryFilter{ParamName: "agent_code", Op: params.Like},
|
|
|
|
|
params.QueryFilter{ParamName: "display_name", Op: params.Like},
|
2026-04-09 10:01:23 +08:00
|
|
|
).Desc("id"))
|
|
|
|
|
results := builders.BuildAgentProfileList(list)
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AgentGetList_all(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
list := services.AgentProfileService.Find(params.NewPagedSqlCnd(ctx,
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "user_id"},
|
|
|
|
|
params.QueryFilter{ParamName: "team_id"},
|
|
|
|
|
params.QueryFilter{ParamName: "service_status"},
|
|
|
|
|
params.QueryFilter{ParamName: "agent_code", Op: params.Like},
|
|
|
|
|
).Eq("status", enums.StatusOk).Desc("id"))
|
2026-04-09 10:01:23 +08:00
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildAgentProfileList(list))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
func AgentGetUser_options(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
users := services.UserService.Find(ctx.Query("keyword"))
|
|
|
|
|
results := make([]response.AgentUserOptionResponse, 0, len(users))
|
|
|
|
|
for _, user := range users {
|
|
|
|
|
results = append(results, response.AgentUserOptionResponse{
|
|
|
|
|
ID: user.ID, Username: user.Username, Nickname: user.Nickname, Avatar: user.Avatar,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, results)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:28:21 +08:00
|
|
|
func AgentGetBy(ctx *gin.Context) {
|
|
|
|
|
id, ok := httpx.GetPathInt64(ctx, "id")
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
item := services.AgentProfileService.Get(id)
|
|
|
|
|
if item == nil {
|
2026-06-02 20:51:13 +08:00
|
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0164"))
|
2026-05-23 22:22:18 +08:00
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildAgentProfileResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AgentPostCreate(ctx *gin.Context) {
|
|
|
|
|
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentCreate)
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
req := request.CreateAgentProfileRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
item, err := services.AgentProfileService.CreateAgentProfile(req, user)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildAgentProfileResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AgentPostUpdate(ctx *gin.Context) {
|
|
|
|
|
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentUpdate)
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
req := request.UpdateAgentProfileRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if err := services.AgentProfileService.UpdateAgentProfile(req, user); err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, nil)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AgentPostDelete(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentDelete); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
req := request.DeleteAgentProfileRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if err := services.AgentProfileService.DeleteAgentProfile(req.ID); err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, nil)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|