   package com.goldenwater.web.controller.yjjt;

import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.yjjt.domain.YjjtFile;
import com.goldenwater.yjjt.service.IYjjtFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 引江济太文件管理Controller
 */
@RestController
@RequestMapping("/yjjt/file")
@Tag(name = "引江济太文件管理", description = "引江济太文件管理接口")
public class YjjtFileController extends BaseController {

    @Resource
    private IYjjtFileService yjjtFileService;

    /**
     * 查询引江济太文件列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询文件列表", description = "查询上传文件列表")
    @PreAuthorize("@ss.hasPermi('yjjtgl:file:list')")
    public TableDataInfo list(YjjtFile yjjtFile) {
        startPage();
        List<YjjtFile> list = yjjtFileService.selectYjjtFileList(yjjtFile);
        return getDataTable(list);
    }

    /**
     * 查询引江济太文件详细
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询文件详情", description = "查询文件详细信息")
    @PreAuthorize("@ss.hasPermi('yjjtgl:file:query')")
    public AjaxResult getInfo(@Parameter(description = "文件ID") @PathVariable("id") Long id) {
        return AjaxResult.success(yjjtFileService.selectYjjtFileById(id));
    }

    /**
     * 删除引江济太文件
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除文件", description = "删除文件记录")
    @PreAuthorize("@ss.hasPermi('yjjtgl:file:remove')")
    public AjaxResult remove(@Parameter(description = "文件ID列表") @PathVariable Long[] ids) {
        return toAjax(yjjtFileService.deleteYjjtFileByIds(ids));
    }

    /**
     * 下载引江济太文件
     */
    @GetMapping("/download/{id}")
    @Operation(summary = "下载文件", description = "下载上传的文件")
    @PreAuthorize("@ss.hasPermi('yjjtgl:file:download')")
    public ResponseEntity download(@Parameter(description = "文件ID") @PathVariable("id") Long id) {
        YjjtFile file = yjjtFileService.selectYjjtFileById(id);
        if (file == null) {
            return ResponseEntity.notFound().build();
        }

        File filePath = new File(file.getFilePath());
        if (!filePath.exists()) {
            return ResponseEntity.notFound().build();
        }

        try {
            org.springframework.core.io.Resource resource = new FileSystemResource(filePath);
            String encodedFilename = URLEncoder.encode(file.getFileViewname(), StandardCharsets.UTF_8)
                    .replace("+", "%20");

            return ResponseEntity.ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFilename + "\"")
                    .body(resource);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}