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/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/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 AIConfigAnyList(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); 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.AIConfigService.FindPageByCnd(params.NewPagedSqlCnd(ctx,
|
2026-04-09 10:01:23 +08:00
|
|
|
params.QueryFilter{ParamName: "status"},
|
|
|
|
|
params.QueryFilter{ParamName: "provider"},
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "model_type"},
|
2026-04-09 10:01:23 +08:00
|
|
|
params.QueryFilter{ParamName: "name", Op: params.Like},
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "model_name", Op: params.Like},
|
2026-05-24 19:57:36 +08:00
|
|
|
).Asc("sort_no").Desc("id"))
|
2026-04-09 10:01:23 +08:00
|
|
|
results := make([]response.AIConfigResponse, 0, len(list))
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
results = append(results, response.BuildAIConfigResponse(&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:22:18 +08:00
|
|
|
func AIConfigAnyList_all(ctx *gin.Context) {
|
|
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); 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.AIConfigService.Find(params.NewSqlCnd(ctx,
|
2026-08-28 22:23:13 +08:00
|
|
|
params.QueryFilter{ParamName: "model_type"},
|
2026-04-09 10:01:23 +08:00
|
|
|
).Eq("status", enums.StatusOk).Desc("sort_no").Desc("id"))
|
|
|
|
|
|
|
|
|
|
results := make([]response.AIConfigResponse, 0, len(list))
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
results = append(results, response.BuildAIConfigResponse(&item))
|
|
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, results)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:28:21 +08:00
|
|
|
func AIConfigGetBy(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.PermissionAIConfigView); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item := services.AIConfigService.Get(id)
|
|
|
|
|
if item == nil || item.Status == enums.StatusDeleted {
|
2026-06-02 20:51:13 +08:00
|
|
|
httpx.WriteJSON(ctx, httpx.JsonErrorMsg(ctx, "error.e0012"))
|
2026-05-23 22:22:18 +08:00
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
httpx.WriteJSON(ctx, response.BuildAIConfigDetailResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AIConfigPostCreate(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigCreate)
|
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.CreateAIConfigRequest{}
|
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.AIConfigService.CreateAIConfig(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, response.BuildAIConfigResponse(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AIConfigPostUpdate(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate)
|
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.UpdateAIConfigRequest{}
|
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.AIConfigService.UpdateAIConfig(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-05-23 22:22:18 +08:00
|
|
|
func AIConfigPostDelete(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigDelete)
|
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.DeleteAIConfigRequest{}
|
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.AIConfigService.DeleteAIConfig(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
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func AIConfigPostUpdate_status(ctx *gin.Context) {
|
|
|
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate)
|
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.UpdateAIConfigStatusRequest{}
|
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.AIConfigService.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-24 19:57:36 +08:00
|
|
|
func AIConfigPostUpdateSort(ctx *gin.Context) {
|
2026-05-23 22:22:18 +08:00
|
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ids []int64
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &ids); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if err := services.AIConfigService.UpdateSort(ids); 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
|
|
|
}
|