package com.weaver.seconddev.chapanda.qunjie.esb;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.eteams.file.client.file.FileData;
import com.weaver.eteams.file.client.file.FileObj;
import com.weaver.file.ud.api.FileDownloadService;
import com.weaver.seconddev.chapanda.prop.util.ParamConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Service("Esb2QunjieTestAttachmentCron")
public class Esb2QunjieTestAttachmentCron implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2QunjieTestAttachmentCron.class);

    @Autowired
    private FileDownloadService fileDownloadService;

    @Autowired
    private ParamConfigUtil paramConfigUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("Esb2QunjieTestAttachmentCron");

        String value = paramConfigUtil.queryEnvironmentValue("rzlc.esgine.test");
        log.error("value:{}",value);


        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            String ip = inetAddress.getHostAddress();
            log.error("ip:{}",ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }


        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 过滤掉loopback接口（通常是127.0.0.1）
                if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    log.error("IP Address: " + addr.getHostAddress());
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }

//        String fileId = String.valueOf(params.get("fileId"));
//        log.error("fileId:{}",fileId);
//        // 获取文件的输入流
//        InputStream inputStream = null;
//        log.error("22222");
//        try{
//
//            FileData fileData = fileDownloadService.downloadFile(Long.valueOf(fileId));
//            log.error("33333");
//            FileObj fileObj = fileData.getFileObj();
//            Long docid = fileObj.getDocId();
//            String name = fileObj.getName();
//            String loadUrl = fileData.getLoadUrl();
//
//            log.error("docid:{}",docid);
//            log.error("name:{}",name);
//            log.error("loadUrl:{}",loadUrl);
//
//            inputStream = fileData.getInputStream();
//            log.error("inputStream:{}",inputStream.available());
//            if(inputStream.available() >0 ){
//
//            }
//
//            inputStream.close();
//        }catch (IOException e) {
//            log.error("e:{}",e);
//        }finally {
//            if(inputStream !=null){
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }


        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","Esb2QunjieQuerySealBaseCron");

        return WeaResult.success(actionMap);
    }

}
