MaterialFolderController.java 5.82 KB
package com.bs.reception.controller.materials;

import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.reception.entity.materials.MaterialFolder;
import com.bs.reception.model.materials.MaterialFolderNodeVO;
import com.bs.reception.model.materials.MaterialFolderPageQuery;
import com.bs.reception.model.materials.MaterialFolderSortRequest;
import com.bs.reception.service.IMaterialFolderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "素材文件夹管理", description = "素材文件夹接口")
@RequestMapping("/materials/folders")
public class MaterialFolderController {

    @Resource
    private IMaterialFolderService folderService;

    @PostMapping("/list")
    @Operation(summary = "文件夹列表")
    public AjaxResult list(@RequestBody MaterialFolder body) {
        try {
            List<MaterialFolder> list = folderService.getFolderList(body);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/page")
    @Operation(summary = "文件夹分页查询")
    public AjaxResult page(@RequestBody MaterialFolderPageQuery query) {
        try {
            TableDataInfo<List<MaterialFolder>> page = folderService.pageFolders(query);
            return AjaxResult.success(page);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/create")
    @Operation(summary = "创建文件夹")
    public AjaxResult create(@RequestBody MaterialFolder body) {
        try {
            MaterialFolder data = folderService.createFolder(body);
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/update")
    @Operation(summary = "更新文件夹")
    public AjaxResult update(@RequestBody MaterialFolder body) {
        try {
            boolean ok = folderService.updateFolder(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/status")
    @Operation(summary = "设置状态")
    public AjaxResult setStatus(@RequestBody MaterialFolder body) {
        try {
            boolean ok = folderService.setFolderStatus(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/sort")
    @Operation(summary = "设置排序")
    public AjaxResult setSort(@RequestBody MaterialFolder body) {
        try {
            boolean ok = folderService.setFolderSort(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/sort/list")
    @Operation(summary = "文件夹列表排序")
    public AjaxResult sortList(@RequestBody MaterialFolderSortRequest body) {
        try {
            if (body.getIds() == null || body.getIds().isEmpty()) {
                return AjaxResult.error("ID列表不能为空");
            }
            if (body.getTypeCode() == null || body.getTypeCode().isBlank()) {
                return AjaxResult.error("业务类型编码不能为空");
            }
            boolean ok = folderService.sort(body.getTypeCode(), body.getIds());
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/delete")
    @Operation(summary = "删除文件夹")
    public AjaxResult delete(@RequestBody MaterialFolder body) {
        try {
            boolean ok = folderService.deleteFolder(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/detail")
    @Operation(summary = "文件夹详情")
    public AjaxResult detail(@RequestBody MaterialFolder body) {
        try {
            MaterialFolder ok = folderService.getFolderDetail(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/tree/list")
    @Operation(summary = "文件夹树列表")
    public AjaxResult folderTree(@RequestBody MaterialFolder body) {
        // 查询文件夹树(扁平列表)
        try {
            List<MaterialFolderNodeVO> list = folderService.listFolderTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }
}