package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.FileCategoryVo;
import org.dromara.system.domain.bo.FileCategoryBo;
import org.dromara.system.service.IFileCategoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 文件分类
 *
 * @author Lion Li
 * @date 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/file/category")
public class FileCategoryController extends BaseController {

    private final IFileCategoryService fileCategoryService;

    /**
     * 查询文件分类列表
     */
    @SaCheckPermission("file:category:list")
    @GetMapping("/list")
    public TableDataInfo<FileCategoryVo> list(FileCategoryBo bo, PageQuery pageQuery) {
        return fileCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询文件分类列表
     */
    @SaCheckPermission("file:category:list")
    @GetMapping("/listAll")
    public R<List<FileCategoryVo>> listAll(FileCategoryBo bo) {
        List<FileCategoryVo> list = fileCategoryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询文件分类树结构
     */
    @SaCheckPermission("file:category:list")
    @GetMapping("/tree")
    public R<List<FileCategoryVo>> tree() {
        List<FileCategoryVo> tree = fileCategoryService.queryCategoryTree();
        return R.ok(tree);
    }

    /**
     * 根据类型查询分类列表
     */
    @SaCheckPermission("file:category:list")
    @GetMapping("/type/{type}")
    public R<List<FileCategoryVo>> listByType(@PathVariable Integer type) {
        List<FileCategoryVo> list = fileCategoryService.queryByType(type);
        return R.ok(list);
    }

    /**
     * 导出文件分类列表
     */
    @SaCheckPermission("file:category:export")
    @Log(title = "文件分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FileCategoryBo bo, HttpServletResponse response) {
        List<FileCategoryVo> list = fileCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "文件分类", FileCategoryVo.class, response);
    }

    /**
     * 获取文件分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("file:category:query")
    @GetMapping("/{id}")
    public R<FileCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(fileCategoryService.queryById(id));
    }

    /**
     * 新增文件分类
     */
    @SaCheckPermission("file:category:add")
    @Log(title = "文件分类", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FileCategoryBo bo) {
        return toAjax(fileCategoryService.insertByBo(bo));
    }

    /**
     * 修改文件分类
     */
    @SaCheckPermission("file:category:edit")
    @Log(title = "文件分类", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FileCategoryBo bo) {
        return toAjax(fileCategoryService.updateByBo(bo));
    }

    /**
     * 删除文件分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("file:category:remove")
    @Log(title = "文件分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(fileCategoryService.deleteWithValidByIds(List.of(ids), true));
    }
}
