package org.dromara.system.controller;

import java.io.IOException;
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.FileInfoVo;
import org.dromara.system.domain.bo.FileInfoBo;
import org.dromara.system.service.IFileInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 文件信息
 *
 * @author Lion Li
 * @date 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/file/info")
public class FileInfoController extends BaseController {

    private final IFileInfoService fileInfoService;

    /**
     * 查询文件信息列表
     */
    @SaCheckPermission("file:info:list")
    @GetMapping("/list")
    public TableDataInfo<FileInfoVo> list(FileInfoBo bo, PageQuery pageQuery) {
        // 添加调试日志
        System.out.println("FileInfoController.list - bo.getCategoryType(): " + bo.getCategoryType());
        System.out.println("FileInfoController.list - bo.getCategoryId(): " + bo.getCategoryId());
        return fileInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询文件信息列表
     */
    @SaCheckPermission("file:info:list")
    @GetMapping("/listAll")
    public R<List<FileInfoVo>> listAll(FileInfoBo bo) {
        List<FileInfoVo> list = fileInfoService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 根据分类ID查询文件列表
     */
    @SaCheckPermission("file:info:list")
    @GetMapping("/category/{categoryId}")
    public R<List<FileInfoVo>> listByCategoryId(@PathVariable Long categoryId) {
        List<FileInfoVo> list = fileInfoService.queryByCategoryId(categoryId);
        return R.ok(list);
    }

    /**
     * 根据文件类型查询文件列表
     */
    @SaCheckPermission("file:info:list")
    @GetMapping("/type/{type}")
    public R<List<FileInfoVo>> listByType(@PathVariable String type) {
        List<FileInfoVo> list = fileInfoService.queryByType(type);
        return R.ok(list);
    }

    /**
     * 导出文件信息列表
     */
    @SaCheckPermission("file:info:export")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FileInfoBo bo, HttpServletResponse response) {
        List<FileInfoVo> list = fileInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "文件信息", FileInfoVo.class, response);
    }

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

    /**
     * 新增文件信息
     */
    @SaCheckPermission("file:info:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FileInfoBo bo) {
        return toAjax(fileInfoService.insertByBo(bo));
    }

    /**
     * 修改文件信息
     */
    @SaCheckPermission("file:info:edit")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FileInfoBo bo) {
        return toAjax(fileInfoService.updateByBo(bo));
    }

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

    /**
     * 更新文件下载次数
     */
    @SaCheckPermission("file:info:update")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping("/download/{id}")
    public R<Void> updateDownloadCount(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return toAjax(fileInfoService.updateDownloadCount(id));
    }

    /**
     * 更新文件查看次数
     */
    @SaCheckPermission("file:info:update")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping("/view/{id}")
    public R<Void> updateViewCount(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return toAjax(fileInfoService.updateViewCount(id));
    }

    /**
     * 下载文件
     *
     * @param id 文件ID
     */
    @SaCheckPermission("file:info:download")
    @GetMapping("/download/{id}")
    public void downloadFile(@NotNull(message = "文件ID不能为空")
                            @PathVariable Long id,
                            HttpServletResponse response) throws IOException {
        fileInfoService.downloadFile(id, response);
    }
}
