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/models"
|
|
|
|
|
"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"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/errorsx"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
2026-08-02 14:03:23 +08:00
|
|
|
"strings"
|
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 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},
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "channel_type"},
|
|
|
|
|
params.QueryFilter{ParamName: "channel_id", Op: params.Like},
|
2026-04-09 10:01:23 +08:00
|
|
|
).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-08-02 14:03:23 +08:00
|
|
|
func ChannelAnyWxworkOutboxFailedList(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionWxWorkOutboxView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cnd := params.NewPagedSqlCnd(ctx,
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "conversation_id"},
|
|
|
|
|
params.QueryFilter{ParamName: "message_id"},
|
2026-08-02 14:03:23 +08:00
|
|
|
).Eq("channel_type", enums.ChannelTypeWxWorkKF)
|
2026-08-28 22:23:13 +08:00
|
|
|
status := strings.TrimSpace(params.FormValue(ctx, "send_status"))
|
2026-08-02 14:03:23 +08:00
|
|
|
switch status {
|
|
|
|
|
case "":
|
|
|
|
|
cnd.Eq("send_status", string(enums.ChannelMessageOutboxStatusFailed))
|
|
|
|
|
case "all":
|
|
|
|
|
cnd.In("send_status", []string{
|
|
|
|
|
string(enums.ChannelMessageOutboxStatusFailed),
|
|
|
|
|
string(enums.ChannelMessageOutboxStatusIgnored),
|
|
|
|
|
})
|
|
|
|
|
case string(enums.ChannelMessageOutboxStatusFailed), string(enums.ChannelMessageOutboxStatusIgnored):
|
|
|
|
|
cnd.Eq("send_status", status)
|
|
|
|
|
default:
|
|
|
|
|
httpx.WriteJSON(ctx, errorsx.InvalidParam("invalid sendStatus"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
list, paging := services.ChannelMessageOutboxService.FindPageByCnd(cnd.Desc("id"))
|
|
|
|
|
results := make([]response.ChannelMessageOutboxResponse, 0, len(list))
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
results = append(results, response.BuildChannelMessageOutboxResponse(&item))
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ChannelPostWxworkOutboxRetry(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionWxWorkOutboxUpdate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req := request.ChannelMessageOutboxActionRequest{}
|
|
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := services.ChannelMessageOutboxService.RetryWxWorkFailure(req.ID, operator); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ChannelPostWxworkOutboxIgnore(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionWxWorkOutboxUpdate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req := request.ChannelMessageOutboxActionRequest{}
|
|
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := services.ChannelMessageOutboxService.IgnoreWxWorkFailure(req.ID, operator); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, nil)
|
|
|
|
|
}
|
|
|
|
|
|
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 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
|
|
|
|
|
}
|