package com.goldenwater.web.controller.efinder;

import com.goldenwater.slgc.entity.efinder.DocFile;
import com.goldenwater.slgc.service.efinder.IDocFileService;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.enums.BusinessType;
import org.springframework.validation.annotation.Validated;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/efinder/file")
public class DocFileController extends BaseController {
    @Autowired
    private IDocFileService docFileService;

    /**
     * 查询文件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DocFile docFile) {
        startPage();
        List<DocFile> list = docFileService.selectList(docFile);
        return getDataTable(list);
    }

    /**
     * 获取文件详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(docFileService.selectById(id));
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response) {
        docFileService.download(id, response);
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview/{id}")
    public void preview(@PathVariable Long id, HttpServletResponse response) {
        docFileService.download(id, response);
    }

    /**
     * 新增文件
     */
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocFile docFile) {
        return toAjax(docFileService.insert(docFile));
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") org.springframework.web.multipart.MultipartFile file,
                             @RequestParam(required = false) Long pid) {
        return success(docFileService.upload(file, pid));
    }

    /**
     * 修改文件
     */
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DocFile docFile) {
        return toAjax(docFileService.update(docFile));
    }

    /**
     * 重命名文件
     */
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/rename")
    public AjaxResult rename(@RequestBody DocFile docFile) {
        return toAjax(docFileService.update(docFile));
    }

    /**
     * 移动文件
     */
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/move")
    public AjaxResult move(@RequestBody DocFile docFile) {
        return toAjax(docFileService.update(docFile));
    }

    /**
     * 删除文件
     */
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(docFileService.deleteByIds(ids));
    }

    /**
     * 查看文本文件内容
     */
    @GetMapping("/txt/view")
    public AjaxResult txtView(@RequestParam Long id,
                              @RequestParam(required = false, defaultValue = "UTF-8") String charset) {
        return success(docFileService.readTextContent(id, charset));
    }

    /**
     * 获取ZIP文件条目列表
     */
    @GetMapping("/zip/itemlist")
    public AjaxResult zipItemList(@RequestParam Long id) {
        return success(docFileService.listZipEntries(id));
    }

    /**
     * 下载ZIP文件中的单个文件
     */
    @GetMapping("/zip/download")
    public void zipDownload(@RequestParam Long id, @RequestParam String entryPath,
                            HttpServletResponse response) {
        docFileService.downloadZipEntry(id, entryPath, response);
    }

    /**
     * 流式播放媒体文件
     */
    @GetMapping("/media/{id}")
    public void media(@PathVariable Long id, HttpServletResponse response) {
        docFileService.streamMedia(id, response);
    }
}
