package com.goldenwater.web.controller.gx;

import com.goldenwater.common.annotation.Anonymous;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.gx.domain.DocDirectory;
import com.goldenwater.gx.service.IDocDirectoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/gx/directory")
public class DocDirectoryController extends BaseController {

    @Resource
    private IDocDirectoryService docDirectoryService;

    @Anonymous
    @GetMapping("/front/tree")
    public AjaxResult frontTree() {
        List<DocDirectory> tree = docDirectoryService.selectDirectoryTree();
        return AjaxResult.success(tree);
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:list')")
    @GetMapping("/tree")
    public AjaxResult tree() {
        List<DocDirectory> tree = docDirectoryService.selectDirectoryTree();
        return AjaxResult.success(tree);
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:list')")
    @GetMapping("/list/{parentId}")
    public AjaxResult list(@PathVariable Long parentId) {
        List<DocDirectory> list = docDirectoryService.selectDirectoryByParentId(parentId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(docDirectoryService.selectDirectoryById(id));
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:add')")
    @Log(title = "目录管理", businessType = com.goldenwater.common.enums.BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocDirectory directory) {
        directory.setCreateBy(getUsername());
        return toAjax(docDirectoryService.insertDirectory(directory));
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:edit')")
    @Log(title = "目录管理", businessType = com.goldenwater.common.enums.BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DocDirectory directory) {
        directory.setUpdateBy(getUsername());
        return toAjax(docDirectoryService.updateDirectory(directory));
    }

    @PreAuthorize("@ss.hasPermi('gx:directory:remove')")
    @Log(title = "目录管理", businessType = com.goldenwater.common.enums.BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(docDirectoryService.deleteDirectoryById(id));
    }
}
