package com.zksy.commonFile.controller;

import com.zksy.common.config.zksyConfig;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.utils.StringUtils;
import com.zksy.common.utils.file.FileUtils;
import com.zksy.commonFile.service.ManageCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author sy
 * @date 2022/4/1 16:56
 */
@Slf4j
@RequestMapping("/commonFile")
@RestController
@Api(tags = "文件管理通用方法", description = "文件管理通用方法desc")
public class ManageCommonController {

    @Autowired
    private ManageCommonService service;


    /**
     * 文件上传（单个）
     */
    @PostMapping("/uploadFile")
    @ApiOperation("文件上传（单个）")
    public AjaxResult uploadFile(@ApiParam(value = "文件",required = true) @RequestParam(value = "file")MultipartFile multipartFile,
                                 @ApiParam(value = "父id") @RequestParam(value = "fid",defaultValue = "0") Long fid,
                                 @ApiParam(value = "模块名称",required = true) @RequestParam(value = "moduleName") String moduleName) {
        return service.uploadFile(multipartFile,fid,moduleName);
    }

    /**
     * 文件上传（返回文件地址）
     */
    @PostMapping("/uploadFilePath")
    @ApiOperation("文件上传（返回文件地址）")
    public String uploadFilePath(@ApiParam(value = "文件",required = true) @RequestParam(value = "file")MultipartFile multipartFile) {
        return service.uploadFilePath(multipartFile);
    }

    /**
     * 文件上传（多个）
     */
    @PostMapping("/uploadFiles")
    @ApiOperation("文件上传（多个）")
    public AjaxResult uploadFiles(@ApiParam(value = "文件",required = true) @RequestParam(value = "file") List<MultipartFile> multipartFiles,
                                  @ApiParam(value = "父id",required = false) @RequestParam(value = "fid",defaultValue = "0") Long fid,
                                  @ApiParam(value = "模块名称",required = true) @RequestParam(value = "moduleName") String moduleName) {
        return service.uploadListFile(multipartFiles,fid,moduleName);
    }
    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     */
    @GetMapping("/download")
    @ApiOperation(value = "通用下载请求", notes = "通用下载请求")
    public void fileDownload(String fileName, HttpServletResponse response) {
        try {
            if (!FileUtils.checkAllowDownload(fileName)) {
                throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", fileName));
            }
            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            String filePath = zksyConfig.getDownloadPath() + fileName;

            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
    /**
     * 删除信息
     */
    @GetMapping("/deleteFileById")
    @ApiOperation("根据id删除文件信息")
    public AjaxResult deleteFileById(@ApiParam(value = "文件id",required = true) @RequestParam(value = "id") String id) {
        return service.deleteFile(id);
    }
}
