package com.dsk.web.controller.business;

import com.dsk.common.config.RuoYiConfig;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.file.FileUploadUtils;
import com.dsk.common.utils.file.FileUtils;
import com.dsk.framework.config.ServerConfig;
import com.dsk.system.domain.BusinessIdDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author lxl
 * @Description: 项目文件管理Controller
 * @Date 2023/5/30 上午 8:44
 **/
@Slf4j
@RestController
@RequestMapping("/business/file")
public class BusinessFileController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 新建文件夹
     */
//    @PreAuthorize("@ss.hasPermi('system:file:add')")
//    @Log(title = "项目资料文档", businessType = BusinessType.INSERT)
    @GetMapping("/new/{filePath}")
    public AjaxResult newFolder(@PathVariable String filePath) {
        return FileUtils.newFolder(RuoYiConfig.getProfile() + filePath) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除某个文件或整个文件夹
     */
    @PostMapping("/remove")
    public AjaxResult removeFile(@RequestBody(required=false) BusinessIdDto folderPath) {
        return FileUtils.delFolder(RuoYiConfig.getProfile() + folderPath.getFolderPath()) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 分页查询项目的所有文件
     * 获取文件夹中所有文件
     */
    @GetMapping(value = "/list")
    public TableDataInfo getAllFiles(@RequestBody(required=false) BusinessIdDto folderPath) {
        startPage();
        List<String> allFiles = FileUtils.getAllFiles(RuoYiConfig.getProfile() + folderPath.getFolderPath());
        return getDataTable(allFiles);
    }

/*    *//**
     * 上传文件及文件夹
     * @param url
     * @param folderPath
     * @return
     *//*
    @GetMapping("/upload/{url}/{folderPath}")
    public AjaxResult uploadFolder(@PathVariable("url") String url,@PathVariable("folderPath") String folderPath) throws IOException {
        return toAjax(FileUtils.uploadFolder(url, LOCALPATH + folderPath));
    }*/

    /**
     * 上传文件及文件夹
     * @param file 文件流
     * @param request 请求头参数
     * @return
     */
    @PostMapping("/upload/")
    public AjaxResult uploadFolder(@RequestPart("file") MultipartFile file, HttpServletRequest request){
        try {
        String businessFileName = request.getParameter("filePath");
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath()+businessFileName;
        // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 下载文件
     * @param url
     * @param targetFolder
     * @return
     */
    @GetMapping("/download/{url}/{targetFolder}")
    public AjaxResult downloadFolder(@PathVariable("url") String url,@PathVariable("targetFolder") String targetFolder) throws IOException {
        return toAjax(FileUtils.downloadFolder(url, targetFolder));
    }
}
