package weaver.interfaces.htsc.GlobatTools;

import com.weaver.ecology.search.model.ImageFile;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import weaver.conn.RecordSet;
import weaver.docs.qc969424.util.QC969424RequestBaseUtil;
import weaver.docs.qc969424.util.QC969424Util;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName FileGateway.java
 * @createTime 2021年11月09日 23:53:00
 */
public class FileGateway {


    public static String  getgateway(int imagefileid,String imagefilename){

        BaseBean bb = new BaseBean();
        bb.writeLog("uploadFile");
        RecordSet rs = new RecordSet();
        String fileid="";
        InputStream inputStream = null;
        CloseableHttpResponse clresponse=null;
        try{
            String upload = QC969424Util.getPV("upload");
            String systemNo = QC969424Util.getPV("systemNo");
            String url = upload+"/"+systemNo;
            bb.writeLog("url:"+url);
            bb.writeLog("imagefilename:"+imagefilename);


            QC969424RequestBaseUtil qc = new QC969424RequestBaseUtil();

            inputStream =  ImageFileManager.getInputStreamById(imagefileid);
            clresponse= qc.doUpload(url,imagefilename,inputStream);

            bb.writeLog("clresponse:"+clresponse);

            int statusCode = clresponse.getStatusLine().getStatusCode();
            bb.writeLog("statusCode:"+statusCode);
            if(statusCode == 200)
            {
                String res = EntityUtils.toString(clresponse.getEntity());
                bb.writeLog("res:"+res);
                if(!"".equals(res)){
                    fileid=res;
                    String sql = " update imagefile set fileid=? where imagefileid=?";
                    bb.writeLog(" sql:"+sql+" imagefileid:"+imagefileid);
                    rs.executeUpdate(sql,new Object[]{res,imagefileid});
                }
            }


        }catch (Exception ex){
            ex.printStackTrace();
            bb.writeLog("ex:"+ex);
        }finally {
            try {
                if(inputStream !=null){
                    inputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(clresponse !=null){
                    clresponse.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



            return fileid;

    }


}
