feat(skill): implement restore functionality for Skill Definitions
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
|||||||
MoreHorizontalIcon,
|
MoreHorizontalIcon,
|
||||||
PlusIcon,
|
PlusIcon,
|
||||||
RefreshCwIcon,
|
RefreshCwIcon,
|
||||||
|
RotateCcwIcon,
|
||||||
SearchIcon,
|
SearchIcon,
|
||||||
Trash2Icon,
|
Trash2Icon,
|
||||||
} from "lucide-react"
|
} from "lucide-react"
|
||||||
@@ -37,6 +38,7 @@ import {
|
|||||||
createSkillDefinition,
|
createSkillDefinition,
|
||||||
deleteSkillDefinition,
|
deleteSkillDefinition,
|
||||||
fetchSkillDefinitions,
|
fetchSkillDefinitions,
|
||||||
|
restoreSkillDefinition,
|
||||||
updateSkillDefinition,
|
updateSkillDefinition,
|
||||||
updateSkillDefinitionStatus,
|
updateSkillDefinitionStatus,
|
||||||
type CreateSkillDefinitionPayload,
|
type CreateSkillDefinitionPayload,
|
||||||
@@ -64,6 +66,7 @@ type SkillRowProps = {
|
|||||||
openDebugDialog: (item: SkillDefinition) => void
|
openDebugDialog: (item: SkillDefinition) => void
|
||||||
handleToggleStatus: (item: SkillDefinition) => void
|
handleToggleStatus: (item: SkillDefinition) => void
|
||||||
handleDelete: (item: SkillDefinition) => void
|
handleDelete: (item: SkillDefinition) => void
|
||||||
|
handleRestore: (item: SkillDefinition) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
function SkillRow({
|
function SkillRow({
|
||||||
@@ -73,10 +76,17 @@ function SkillRow({
|
|||||||
openDebugDialog,
|
openDebugDialog,
|
||||||
handleToggleStatus,
|
handleToggleStatus,
|
||||||
handleDelete,
|
handleDelete,
|
||||||
|
handleRestore,
|
||||||
}: SkillRowProps) {
|
}: SkillRowProps) {
|
||||||
|
const isDeleted = item.status === Status.Deleted
|
||||||
|
const statusBadgeVariant = isDeleted
|
||||||
|
? "destructive"
|
||||||
|
: item.status === Status.Ok
|
||||||
|
? "default"
|
||||||
|
: "outline"
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableRow>
|
<TableRow className={isDeleted ? "bg-destructive/5" : undefined}>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<div className="flex items-start gap-3">
|
<div className="flex items-start gap-3">
|
||||||
<div className="mt-0.5 flex size-10 items-center justify-center rounded-2xl bg-muted text-muted-foreground">
|
<div className="mt-0.5 flex size-10 items-center justify-center rounded-2xl bg-muted text-muted-foreground">
|
||||||
@@ -113,11 +123,11 @@ function SkillRow({
|
|||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<Switch
|
<Switch
|
||||||
checked={item.status === Status.Ok}
|
checked={item.status === Status.Ok}
|
||||||
disabled={actionLoadingId === item.id}
|
disabled={actionLoadingId === item.id || isDeleted}
|
||||||
onCheckedChange={() => void handleToggleStatus(item)}
|
onCheckedChange={() => void handleToggleStatus(item)}
|
||||||
aria-label={`${item.name} 状态切换`}
|
aria-label={`${item.name} 状态切换`}
|
||||||
/>
|
/>
|
||||||
<Badge variant={item.status === Status.Ok ? "default" : "outline"}>
|
<Badge variant={statusBadgeVariant}>
|
||||||
{getEnumLabel(StatusLabels, item.status as keyof typeof StatusLabels)}
|
{getEnumLabel(StatusLabels, item.status as keyof typeof StatusLabels)}
|
||||||
</Badge>
|
</Badge>
|
||||||
</div>
|
</div>
|
||||||
@@ -147,14 +157,24 @@ function SkillRow({
|
|||||||
<MoreHorizontalIcon />
|
<MoreHorizontalIcon />
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent align="end" className="w-40 min-w-40">
|
<DropdownMenuContent align="end" className="w-40 min-w-40">
|
||||||
<DropdownMenuItem
|
{isDeleted ? (
|
||||||
disabled={actionLoadingId === item.id}
|
<DropdownMenuItem
|
||||||
onClick={() => void handleDelete(item)}
|
disabled={actionLoadingId === item.id}
|
||||||
className="text-destructive focus:text-destructive"
|
onClick={() => void handleRestore(item)}
|
||||||
>
|
>
|
||||||
<Trash2Icon />
|
<RotateCcwIcon />
|
||||||
{actionLoadingId === item.id ? "删除中..." : "删除"}
|
{actionLoadingId === item.id ? "恢复中..." : "恢复"}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
) : (
|
||||||
|
<DropdownMenuItem
|
||||||
|
disabled={actionLoadingId === item.id}
|
||||||
|
onClick={() => void handleDelete(item)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2Icon />
|
||||||
|
{actionLoadingId === item.id ? "删除中..." : "删除"}
|
||||||
|
</DropdownMenuItem>
|
||||||
|
)}
|
||||||
</DropdownMenuContent>
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
@@ -288,6 +308,10 @@ export default function DashboardSkillsPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleToggleStatus(item: SkillDefinition) {
|
async function handleToggleStatus(item: SkillDefinition) {
|
||||||
|
if (item.status === Status.Deleted) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const nextStatus = item.status === Status.Ok ? Status.Disabled : Status.Ok
|
const nextStatus = item.status === Status.Ok ? Status.Disabled : Status.Ok
|
||||||
|
|
||||||
setActionLoadingId(item.id)
|
setActionLoadingId(item.id)
|
||||||
@@ -303,6 +327,10 @@ export default function DashboardSkillsPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleDelete(item: SkillDefinition) {
|
async function handleDelete(item: SkillDefinition) {
|
||||||
|
if (item.status === Status.Deleted) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setActionLoadingId(item.id)
|
setActionLoadingId(item.id)
|
||||||
try {
|
try {
|
||||||
await deleteSkillDefinition(item.id)
|
await deleteSkillDefinition(item.id)
|
||||||
@@ -315,6 +343,23 @@ export default function DashboardSkillsPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleRestore(item: SkillDefinition) {
|
||||||
|
if (item.status !== Status.Deleted) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setActionLoadingId(item.id)
|
||||||
|
try {
|
||||||
|
await restoreSkillDefinition(item.id)
|
||||||
|
toast.success(`已恢复 Skill:${item.name}`)
|
||||||
|
await loadData()
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(error instanceof Error ? error.message : "恢复 Skill 失败")
|
||||||
|
} finally {
|
||||||
|
setActionLoadingId(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex flex-1 flex-col gap-6 p-4 lg:p-6">
|
<div className="flex flex-1 flex-col gap-6 p-4 lg:p-6">
|
||||||
@@ -388,6 +433,7 @@ export default function DashboardSkillsPage() {
|
|||||||
openDebugDialog={openDebugDialog}
|
openDebugDialog={openDebugDialog}
|
||||||
handleToggleStatus={handleToggleStatus}
|
handleToggleStatus={handleToggleStatus}
|
||||||
handleDelete={handleDelete}
|
handleDelete={handleDelete}
|
||||||
|
handleRestore={handleRestore}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
|
|||||||
@@ -913,6 +913,13 @@ export function deleteSkillDefinition(id: number) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function restoreSkillDefinition(id: number) {
|
||||||
|
return request<void>("/api/dashboard/skill-definition/restore", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ id }),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function debugRunSkillDefinition(payload: SkillDebugRunPayload) {
|
export function debugRunSkillDefinition(payload: SkillDebugRunPayload) {
|
||||||
return request<SkillDebugRunResult>("/api/dashboard/skill-definition/debug_run", {
|
return request<SkillDebugRunResult>("/api/dashboard/skill-definition/debug_run", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package dashboard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cs-agent/internal/builders"
|
"cs-agent/internal/builders"
|
||||||
@@ -30,6 +31,9 @@ func (c *SkillDefinitionController) AnyList() *web.JsonResult {
|
|||||||
params.QueryFilter{ParamName: "name", Op: params.Like},
|
params.QueryFilter{ParamName: "name", Op: params.Like},
|
||||||
params.QueryFilter{ParamName: "code", Op: params.Like},
|
params.QueryFilter{ParamName: "code", Op: params.Like},
|
||||||
).Desc("id")
|
).Desc("id")
|
||||||
|
if _, ok := params.Get(c.Ctx, "status"); !ok {
|
||||||
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
||||||
|
}
|
||||||
list, paging := services.SkillDefinitionService.FindPageByCnd(cnd)
|
list, paging := services.SkillDefinitionService.FindPageByCnd(cnd)
|
||||||
results := make([]response.SkillDefinitionResponse, 0, len(list))
|
results := make([]response.SkillDefinitionResponse, 0, len(list))
|
||||||
for _, item := range list {
|
for _, item := range list {
|
||||||
@@ -43,9 +47,13 @@ func (c *SkillDefinitionController) GetList_all() *web.JsonResult {
|
|||||||
return web.JsonError(err)
|
return web.JsonError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
list := services.SkillDefinitionService.Find(params.NewSqlCnd(c.Ctx,
|
cnd := params.NewSqlCnd(c.Ctx,
|
||||||
params.QueryFilter{ParamName: "status"},
|
params.QueryFilter{ParamName: "status"},
|
||||||
).Desc("id"))
|
).Desc("id")
|
||||||
|
if status, ok := params.Get(c.Ctx, "status"); !ok || strings.TrimSpace(status) == "" {
|
||||||
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
||||||
|
}
|
||||||
|
list := services.SkillDefinitionService.Find(cnd)
|
||||||
results := make([]response.SkillDefinitionResponse, 0, len(list))
|
results := make([]response.SkillDefinitionResponse, 0, len(list))
|
||||||
for _, item := range list {
|
for _, item := range list {
|
||||||
results = append(results, builders.BuildSkillDefinitionResponse(&item))
|
results = append(results, builders.BuildSkillDefinitionResponse(&item))
|
||||||
@@ -114,9 +122,16 @@ func (c *SkillDefinitionController) PostUpdate_status() *web.JsonResult {
|
|||||||
if !enums.IsValidStatus(req.Status) {
|
if !enums.IsValidStatus(req.Status) {
|
||||||
return web.JsonErrorMsg("状态值不合法")
|
return web.JsonErrorMsg("状态值不合法")
|
||||||
}
|
}
|
||||||
if services.SkillDefinitionService.Get(req.ID) == nil {
|
item := services.SkillDefinitionService.Get(req.ID)
|
||||||
|
if item == nil {
|
||||||
return web.JsonErrorMsg("Skill 不存在")
|
return web.JsonErrorMsg("Skill 不存在")
|
||||||
}
|
}
|
||||||
|
if item.Status == enums.StatusDeleted {
|
||||||
|
return web.JsonErrorMsg("已删除的 Skill 不能直接修改状态,请先恢复")
|
||||||
|
}
|
||||||
|
if req.Status == int(enums.StatusDeleted) {
|
||||||
|
return web.JsonErrorMsg("请使用删除接口处理删除状态")
|
||||||
|
}
|
||||||
|
|
||||||
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
||||||
"status": req.Status,
|
"status": req.Status,
|
||||||
@@ -156,6 +171,39 @@ func (c *SkillDefinitionController) PostDelete() *web.JsonResult {
|
|||||||
return web.JsonSuccess()
|
return web.JsonSuccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *SkillDefinitionController) PostRestore() *web.JsonResult {
|
||||||
|
operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionDelete)
|
||||||
|
if err != nil {
|
||||||
|
return web.JsonError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := request.RestoreSkillDefinitionRequest{}
|
||||||
|
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||||
|
return web.JsonError(err)
|
||||||
|
}
|
||||||
|
if req.ID <= 0 {
|
||||||
|
return web.JsonErrorMsg("Skill ID 不合法")
|
||||||
|
}
|
||||||
|
|
||||||
|
item := services.SkillDefinitionService.Get(req.ID)
|
||||||
|
if item == nil {
|
||||||
|
return web.JsonErrorMsg("Skill 不存在")
|
||||||
|
}
|
||||||
|
if item.Status != enums.StatusDeleted {
|
||||||
|
return web.JsonErrorMsg("仅已删除的 Skill 支持恢复")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{
|
||||||
|
"status": enums.StatusDisabled,
|
||||||
|
"update_user_id": operator.UserID,
|
||||||
|
"update_user_name": operator.Username,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
}); err != nil {
|
||||||
|
return web.JsonError(err)
|
||||||
|
}
|
||||||
|
return web.JsonSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *SkillDefinitionController) PostDebug_run() *web.JsonResult {
|
func (c *SkillDefinitionController) PostDebug_run() *web.JsonResult {
|
||||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil {
|
||||||
return web.JsonError(err)
|
return web.JsonError(err)
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ type DeleteSkillDefinitionRequest struct {
|
|||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RestoreSkillDefinitionRequest struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
type UpdateSkillDefinitionStatusRequest struct {
|
type UpdateSkillDefinitionStatusRequest struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
|
|||||||
Reference in New Issue
Block a user