package com.engine.htsc.ccwg.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.ccwg.service.Upload2StorageGatewayService;
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.general.BaseBean;
import weaver.general.Util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

public class Upload2StorageGatewayServiceImpl extends Service implements Upload2StorageGatewayService {
    @Override
    public Map<String, Object> uploaddoc(Map<String, Object> params) {
        BaseBean bb = new BaseBean();

        Map<String,Object> apidate = new HashMap<String,Object>();
        bb.writeLog("Upload2StorageGatewayServiceImpl");
        RecordSet rs = new RecordSet();
        BufferedInputStream inputStream = null;
        CloseableHttpResponse clresponse = null;
        try{
            String imagefileid = Util.null2String(params.get("imagefileid"));
            String iszip = "";
            String imagefilename = "";
            String fileRealPath = "";
            String sql = " select t1.imageFileId,t1.imageFileName,t1.imageFileType,t1.imageFileUsed,t1.fileRealPath,t1.isZip,t1.isencrypt,t1.filesize,t1.downloads,t1.miniimgpath,t1.imgsize,t1.isFTP,t1.FTPConfigId,t1.isaesencrypt,t1.aescode,t1.tokenKey,t1.storageStatus,t1.comefrom,t2.imagefilename as realname  from ImageFile t1 left join DocImageFile t2 on t1.imagefileid = t2.imagefileid where t1.imagefileid =  " + imagefileid;
            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            if (rs.next())
            {
                imagefilename = Util.null2String(rs.getString("realname"));
                if (imagefilename.equals("")) {
                    imagefilename = Util.null2String(rs.getString("imageFileName"));
                }
                fileRealPath = Util.null2String(rs.getString("fileRealPath"));
                iszip = Util.null2String(rs.getString("isZip"));
            }
            if (fileRealPath != null && !fileRealPath.equals(""))
            {
                File file = new File(fileRealPath);
                if (iszip.equals("1")) {
                    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
                    if (zipInputStream.getNextEntry() != null) {
                        inputStream = new BufferedInputStream(zipInputStream);
                    }
                } else {
                    inputStream = new BufferedInputStream(new FileInputStream(file));
                }
            }

            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();
            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)){
                    sql = " update imagefile set fileid=? where imagefileid=?";
                    bb.writeLog(" sql:"+sql+" imagefileid:"+imagefileid);
                    boolean flag = rs.executeUpdate(sql,new Object[]{res,imagefileid});
                    if(flag){
                        apidate.put("flag","1");
                    }
                }else{
                    apidate.put("flag","-1");
                }
            }else{
                apidate.put("flag","-2");
            }

            inputStream.close();
            //释放链接
            clresponse.close();

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

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