package com.yushu.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yushu.common.annotation.Log;
import com.yushu.common.core.controller.BaseController;
import com.yushu.common.core.domain.AjaxResult;
import com.yushu.common.enums.BusinessType;
import com.yushu.system.domain.SysFolder;
import com.yushu.system.service.ISysFolderService;
import com.yushu.common.utils.poi.ExcelUtil;
import com.yushu.common.core.page.TableDataInfo;

/**
 * 文件夹Controller
 * 
 * @author yushu
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/system/folder")
public class SysFolderController extends BaseController
{
    @Autowired
    private ISysFolderService sysFolderService;

    /**
     * 查询文件夹列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFolder sysFolder)
    {
        startPage();
        List<SysFolder> list = sysFolderService.selectSysFolderList(sysFolder);
        return getDataTable(list);
    }


    /**
     * 获取文件夹详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{folderId}")
    public AjaxResult getInfo(@PathVariable("folderId") Long folderId)
    {
        return success(sysFolderService.selectSysFolderByFolderId(folderId));
    }

    /**
     * 新增文件夹
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件夹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFolder sysFolder)
    {
        int result = sysFolderService.insertSysFolder(sysFolder);
        if (result > 0)
        {
            // 返回创建的文件夹对象，包含生成的ID
            return success(sysFolder);
        }
        return error("新增文件夹失败");
    }

    /**
     * 修改文件夹
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件夹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFolder sysFolder)
    {
        return toAjax(sysFolderService.updateSysFolder(sysFolder));
    }

    /**
     * 删除文件夹
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件夹", businessType = BusinessType.DELETE)
	@DeleteMapping("/{folderIds}")
    public AjaxResult remove(@PathVariable Long[] folderIds)
    {
        return toAjax(sysFolderService.deleteSysFolderByFolderIds(folderIds));
    }
    
    /**
     * 复制文件夹
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "复制文件夹", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@RequestParam Long folderId, @RequestParam Long targetFolderId)
    {
        return toAjax(sysFolderService.copyFolder(folderId, targetFolderId));
    }
    
    /**
     * 获取文件夹树
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/tree")
    public AjaxResult tree(SysFolder sysFolder)
    {
        List<SysFolder> folders = sysFolderService.selectSysFolderList(sysFolder);
        return success(sysFolderService.buildFolderTree(folders));
    }
}
