package com.huatek.busi.init;

import com.huatek.busi.common.util.sftp.SFTPTool;
import com.huatek.busi.entity.SftpConnectConfig;
import com.huatek.busi.mapper.SftpConnectConfigMapper;
import com.jcraft.jsch.ChannelSftp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;

@Slf4j
@Component
public class SftpInit {

    @Resource
    private SftpConnectConfigMapper sftpConnectConfigService;

    private ChannelSftp initSftp(){
        SftpConnectConfig sftpConnectConfig = sftpConnectConfigService.findOne();

        // 主机IP
        String host = sftpConnectConfig.getHost();
        // 端口
        Integer port = sftpConnectConfig.getPort();
        // 用户名
        String userName = sftpConnectConfig.getUserName();
        // 密码
        String password = sftpConnectConfig.getPassword();
        return SFTPTool.getChannelSftp(host, port, userName, password);
    }

    public String download(String filePath, String remoteFile, String localTempPath, String localFile){
        return SFTPTool.download(filePath, remoteFile, localTempPath, localFile, initSftp());
    }

    public String upload(String rootPath, String destDirectory, InputStream inputStream, String fileName, int count, String status){
        return SFTPTool.upload(rootPath, destDirectory, inputStream, fileName, count, status, initSftp());
    }

    public String upload(String suffixFilePath, String finalLocalTempPath, String fileName, String status){
        return SFTPTool.upload(suffixFilePath, finalLocalTempPath, fileName, status, initSftp());
    }

    public void delete(String finalFolderPath, String fileName){
        SFTPTool.delete(finalFolderPath, fileName, initSftp());
    }
}
