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.CMCCSftpUtil;
import com.cusc.nirvana.user.rnr.fp.dto.T1CommonResponseDTO;
import com.cusc.nirvana.user.rnr.fp.dto.T1FileReqDTO;
import com.cusc.nirvana.user.rnr.fp.service.CmccT1UploadService;
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 CmccT1UploadServiceImpl implements CmccT1UploadService {
    //ip地址
    @Value("${t1.CMCC.host:}")
    private String host;

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

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

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

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

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

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


    @Override
    public Response upload(T1FileReqDTO file) {
        CMCCSftpUtil cmccSftpUtil = new CMCCSftpUtil(host,port,phoPath,username,password,sessionConnectTimeout,channelConnectedTimeout);
        String uploadFile = null;
        try {
            uploadFile=cmccSftpUtil.uploadFile(file);
        } 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());
    }
}
