2026-04-15 17:12:30 +08:00
|
|
|
package dashboard
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
import (
|
2026-05-31 18:43:48 +08:00
|
|
|
"agent-desk/internal/models"
|
|
|
|
|
"agent-desk/internal/pkg/constants"
|
|
|
|
|
"agent-desk/internal/pkg/dto/request"
|
|
|
|
|
"agent-desk/internal/pkg/dto/response"
|
|
|
|
|
"agent-desk/internal/pkg/enums"
|
|
|
|
|
"agent-desk/internal/pkg/httpx"
|
|
|
|
|
"agent-desk/internal/services"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
2026-05-31 18:43:48 +08:00
|
|
|
"agent-desk/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 ChannelAnyList(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); 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.ChannelService.FindPageByCnd(params.NewPagedSqlCnd(ctx,
|
2026-04-09 10:01:23 +08:00
|
|
|
params.QueryFilter{ParamName: "status"},
|
|
|
|
|
params.QueryFilter{ParamName: "name", Op: params.Like},
|
|
|
|
|
params.QueryFilter{ParamName: "channelType"},
|
|
|
|
|
params.QueryFilter{ParamName: "channelId", Op: params.Like},
|
|
|
|
|
).Where("status <> ?", enums.StatusDeleted).Desc("id"))
|
|
|
|
|
results := make([]response.ChannelResponse, 0, len(list))
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
results = append(results, buildChannelResponse(&item))
|
|
|
|
|
}
|
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:28:21 +08:00
|
|
|
func ChannelGetBy(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.PermissionChannelView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
item := services.ChannelService.Get(id)
|
|
|
|
|
if item == nil || item.Status == enums.StatusDeleted {
|
2026-06-02 20:51:13 +08:00
|
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0062"))
|
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, buildChannelResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func ChannelAnyWxworkKfAccounts(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-27 11:43:34 +08:00
|
|
|
}
|
|
|
|
|
list, err := services.ChannelService.ListWxWorkKFAccounts()
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-27 11:43:34 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, list)
|
2026-04-27 11:43:34 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func ChannelPostCreate(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelCreate)
|
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.CreateChannelRequest{}
|
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.ChannelService.CreateChannel(req, operator)
|
|
|
|
|
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, buildChannelResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func ChannelPostUpdate(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate)
|
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.UpdateChannelRequest{}
|
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.ChannelService.UpdateChannel(req, operator); 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-07-25 12:04:06 +08:00
|
|
|
func ChannelPostRollback_ai_agent_rollout(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req := request.RollbackChannelAIAgentRolloutRequest{}
|
|
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := services.ChannelService.RollbackChannelAIAgentRollout(req.ID, operator); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func ChannelPostUpdate_status(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate)
|
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.UpdateChannelStatusRequest{}
|
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.ChannelService.UpdateStatus(req.ID, req.Status, operator); 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 ChannelPostReset_user_token_secret(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate)
|
2026-04-28 10:15:15 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-28 10:15:15 +08:00
|
|
|
}
|
|
|
|
|
req := request.ResetChannelUserTokenSecretRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-28 10:15:15 +08:00
|
|
|
}
|
|
|
|
|
secret, err := services.ChannelService.ResetUserTokenSecret(req.ID, operator)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-28 10:15:15 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, map[string]string{"userTokenSecret": secret})
|
2026-04-28 10:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func ChannelPostDelete(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelDelete)
|
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.DeleteChannelRequest{}
|
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.ChannelService.DeleteChannel(req.ID, operator); 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildChannelResponse(item *models.Channel) response.ChannelResponse {
|
|
|
|
|
ret := response.BuildChannelResponse(item)
|
|
|
|
|
if item == nil {
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
if aiAgent := services.AIAgentService.Get(item.AIAgentID); aiAgent != nil {
|
|
|
|
|
ret.AIAgentName = aiAgent.Name
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|