package com.ruoyi.common.utils;

import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Slf4j
@Component
public class FTPUtil {

    @Value("${ftp_ip}")
    private String ftpIp;
    @Value("${ftp_port}")
    private Integer ftpPort;
    @Value("${ftp_user}")
    private String ftpUser;
    @Value("${ftp_pass}")
    private String ftpPass;

    private FTPClient connectServer(String ip, int port, String user, String pwd) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            if (!ftpClient.login(user, pwd)) {
                throw new ServiceException("连接失败");
            }
            log.info("连接ftp服务器成功");
        } catch (IOException e) {
            log.error("连接ftp服务器失败", e);
        }
        return ftpClient;
    }

    public boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
        boolean upload = true;
        FileInputStream fileInputStream = null;
        // connect to ftpServer
        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
            try {
                ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for (File fileItem : fileList) {
                    fileInputStream = new FileInputStream(fileItem);
                    ftpClient.storeFile(fileItem.getName(), fileInputStream);
                }
            } catch (IOException e) {
                log.error("上传文件异常", e);
                upload = false;
            } finally {
                fileInputStream.close();
                ftpClient.disconnect();
            }
        return upload;
    }

    public boolean uploadToFtp(String remotePath, String fileName, File file) throws IOException {
        boolean upload = true;
        FileInputStream fileInputStream = null;
        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
        // connect to ftpServer
            try {
                boolean isExist = ftpClient.changeWorkingDirectory(remotePath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                if (!isExist) {
                    ftpClient.makeDirectory(remotePath);
                }
                ftpClient.changeWorkingDirectory(remotePath);
                // 上传文件 参数：上传后的文件名，输入流
                upload = ftpClient.storeFile(fileName, new FileInputStream(file));
            } catch (IOException e) {
                log.error("上传文件异常", e);
                upload = false;
            } finally {
                if (null != fileInputStream) fileInputStream.close();
                ftpClient.disconnect();
            }
        return upload;
    }

    public void downloadFile(String remotePath, String fileName, OutputStream outputStream) throws IOException {
        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
            log.info("获取文件");
            ftpClient.setBufferSize(4096);
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.setConnectTimeout(5000);  // 设置连接超时时间为5秒
            ftpClient.setDataTimeout(10000);    // 设置数据传输超时时间为10秒
            ftpClient.enterLocalPassiveMode();  // 使用被动模式
            log.info("ftp开始下载");
            ftpClient.retrieveFile(remotePath + fileName, outputStream);
            ftpClient.logout();
            ftpClient.disconnect();
    }

}
