package com.microservices.file.controller;

import com.microservices.common.core.domain.R;
import com.microservices.file.service.ISysFileInfoService;
import com.microservices.system.api.domain.SysFileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公开接口
 *
 * @author otto
 */
@RestController()
@RequestMapping("/open")
@Api(tags = "文件相关公开接口")
public class OpenController {
    private static final Logger log = LoggerFactory.getLogger(OpenController.class);
    /**
     * 域名或本机访问地址
     */
    @Value("${file.domain}")
    public String domain;
    /**
     * 允许文件类型
     */
    @Value("${file.allowedExtension}")
    public String allowedExtension;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    @Autowired
    private ISysFileInfoService sysFileInfoService;

    /**
     * 根据文件标识下载
     *
     * @param fileIdentifier 文件标识
     */
    @GetMapping("/downloadByIdentifier/{fileIdentifier}")
    @ApiOperation("根据文件标识下载文件")
    public void downloadByIdentifier(@PathVariable("fileIdentifier") String fileIdentifier, HttpServletResponse response, HttpServletRequest request) {
        sysFileInfoService.fileDownloadByIdentifier(fileIdentifier, response, request);
    }

    /**
     * 根据文件id下载
     *
     * @param fileId 文件Id
     */
    @GetMapping("/download/{fileId}")
    @ApiOperation("根据文件id下载文件")
    public void fileDownloadById(@PathVariable("fileId") Long fileId, HttpServletResponse response, HttpServletRequest request) {
        sysFileInfoService.fileDownloadById(fileId, response, request);
    }

    /**
     * 根据文件Id列表获取文件列表
     */
    @GetMapping("/getFileList/{fileIds}")
    @ApiOperation(value = "根据文件Id列表获取文件列表")
    public R<List<SysFileInfo>> getFileList(@PathVariable("fileIds") String fileIds) {
        List<SysFileInfo> sysFileInfoList = sysFileInfoService.getFileListByFileIds(fileIds);
        sysFileInfoList.forEach(x -> {
            //隐藏存储路径，防止暴露文件存储细节
            x.setFilePath("");
        });
        return R.ok(sysFileInfoList, "操作成功");
    }

    /**
     * 根据文件标识列表获取文件列表
     */
    @GetMapping("/getFileListByIdentifier/{fileIdentifiers}")
    @ApiOperation(value = "根据文件标识列表获取文件列表")
    public R<List<SysFileInfo>> getFileListByIdentifier(@PathVariable("fileIdentifiers") String fileIdentifiers) {
        List<SysFileInfo> sysFileInfoList = sysFileInfoService.getFileListByIdentifier(fileIdentifiers);
        sysFileInfoList.forEach(x -> {
            //隐藏存储路径，防止暴露文件存储细节
            x.setFilePath("");
        });
        return R.ok(sysFileInfoList, "操作成功");
    }

    /**
     * 根据文件Id获取文件信息
     */
    @GetMapping("/getFile/{fileId}")
    @ApiOperation(value = "根据文件Id获取文件信息")
    public R<SysFileInfo> getFile(@PathVariable("fileId") Long fileId) {
        SysFileInfo sysFileInfo = sysFileInfoService.selectSysFileInfoByFileId(fileId);
        //隐藏存储路径，防止暴露文件存储细节
        sysFileInfo.setFilePath("");
        return R.ok(sysFileInfo);
    }
}
