package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.rnr.fp.common.util.CTCCSftpUtil;
import com.cusc.nirvana.user.rnr.fp.dto.T1FileReqDTO;
import com.cusc.nirvana.user.rnr.fp.service.CtccT1UploadService;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @className: CtccT1UploadServiceImpl
 * @description: 电信T1上报指令
 * @author: jk
 * @date: 2022/7/26 14:36
 * @version: 1.0
 **/
@Service
@Slf4j
public class CtccT1UploadServiceImpl implements CtccT1UploadService {
    //ip地址
    @Value("${t1.CTCC.host:}")
    private String host;

    //端口号
    @Value("${t1.CTCC.port:}")
    private Integer port;

    //路径
    @Value("${t1.CTCC.phoPath:}")
    private String phoPath;

    //用户名
    @Value("${t1.CTCC.username:}")
    private String username;

    //密码
    @Value("${t1.CTCC.password:}")
    private String password;

    //session链接超时时间
    @Value("${t1.CTCC.sessionConnectTimeout:15000}")
    private Integer sessionConnectTimeout = 15000;

    //channel链接超时时间
    @Value("${t1.CTCC:15000}")
    private Integer channelConnectedTimeout = 15000;

    @Override
    public Response upload(T1FileReqDTO file) {
        CTCCSftpUtil ctccSftpUtil = new CTCCSftpUtil(host,port,phoPath,username,password,sessionConnectTimeout,channelConnectedTimeout);
        String uploadFile = null;
        try {
            uploadFile=ctccSftpUtil.uploadFile(file.getEncryptFile().getPath());
        } catch (SftpException | JSchException e) {
            e.printStackTrace();
            return Response.createError( "CmccUpload file failed, exception cause:" + e.getMessage(),file.getRequestID());
        }
        if(StringUtils.isEmpty(uploadFile)){
            return Response.createError( "指令下发失败",file.getRequestID());
        }
        return Response.createSuccess(uploadFile,file.getRequestID());
    }
}
