package weaver.interfaces.htsc.GlobatTools;

import com.engine.odoc.util.DocUtil;
import com.sun.star.corba.ObjectKey;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.docs.docs.*;
import weaver.file.AESCoder;
import weaver.file.FileUpload;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.StringUtil;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.gwlc.WrokflowToOAUtil;
import weaver.interfaces.htsc.util.UploadDocUtil;
import weaver.system.SystemComInfo;
import weaver.wechat.util.Utils;
import weaver.workflow.request.RequestDoc;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * @ClassName XcTools
 * @Author K1810015
 * @Date 2022/3/5 16:52
 * @Remark 信创环境下工具类
 * Version 1.0
 */
public class XcTools {

    public static void main(String[] args) {
       SystemComInfo var4 = new SystemComInfo();
        String var5 = FileUpload.getCreateDir(var4.getFilesystem());
        String a="!23";
    }

    /**
     * @title getdocid
     * @author K1810015
     * @updateTime 2022/3/11 14:22
     * @Remark 获取信创环境
     * @throws
     */
    public static int getdocid(Map<String,Object> map,User user){
        PropBean pb=new PropBean();
        int maincategoryid = 0;
        int subcategoryid = 0;
        int seccategoryid=0;
        int filesize = 0;
        String secid = pb.getPropName("htsc_yyfhj");
        if(!"".equals(secid)){
            seccategoryid = Util.getIntValue(secid);
        }
        maincategoryid = Util.getIntValue(Util.null2String(map.get("maincategory")));
        subcategoryid = Util.getIntValue(Util.null2String(map.get("subcategory")));
        String docextendname = Util.null2String(map.get("docextendname"));
        String imagefilename = Util.null2String(map.get("imagefilename"));
        String docsubject = Util.null2String(map.get("docsubject"));
        if(!StringUtils.isBlank(docsubject) && !StringUtils.isBlank(imagefilename)){
           if(imagefilename.split(",").length>1){
               imagefilename=docsubject+imagefilename.substring(imagefilename.lastIndexOf("."),imagefilename.length());
           }

        }

        String docfiletype = Util.null2String(map.get("docfiletype"));
        String imagefiletype = Util.null2String(map.get("imagefiletype"));
        String doccreaterid = Util.null2String(map.get("doccreaterid"));
        String doctype = Util.null2String(map.get("doctype"));
        filesize = Util.getIntValue(Util.null2String(map.get("filesize")));
        String pubtime = TimeUtil.getCurrentDateString();
        String operatedate =TimeUtil.getCurrentDateString();
        String operatetime =TimeUtil.getOnlyCurrentTimeString();
        String path=Util.null2String(map.get("filerealpath"));
        String imagefileused=Util.null2String(map.get("imagefileused"));
        String filerealpath=Util.null2String(map.get("filerealpath"));
        String iszip=Util.null2String(map.get("iszip"));
        String tokenkey=Util.null2String(map.get("tokenkey"));
        String aescode=Util.null2String(map.get("aescode"));
        String ftpconfigid=Util.null2String(map.get("ftpconfigid"));
        String isaesencrypt=Util.null2String(map.get("isaesencrypt"));
        String imagefileidold=Util.null2String(map.get("imagefileid"));
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
         try {

             int imagefileid=-1;
             imagefileid=geturldocid(imagefileidold,imagefilename);
              if(imagefileid<=0){
                  imagefileid= getinputStream(filerealpath,imagefilename,iszip,isaesencrypt,aescode);
                  if(imagefileid<=0){
                     return -1;
                 }

             }
             Map<String,Object> detailmap=new HashMap<>();
             detailmap.put("filename",imagefilename);
             detailmap.put("content","");
             detailmap.put("doccreaterid",doccreaterid);
             detailmap.put("pubtime",pubtime);
             detailmap.put("maincategoryid",maincategoryid);
             detailmap.put("seccategoryid",seccategoryid);
             detailmap.put("doctype",Util.getIntValue(doctype));
             detailmap.put("docextendname",docextendname);
             int docid= NewDocUtil.createdocdetail(detailmap);

//             /**开始创建imagefile表 */
//             Map<String,Object> imagefilemap=new HashMap<>();
//             VersionIdUpdate versionIdUpdate = new VersionIdUpdate();
//             int versionid = versionIdUpdate.getVersionNewId();
//             ImageFileIdUpdate im = new ImageFileIdUpdate();
//             int imagefileid = im.getImageFileNewId();
//             imagefilemap.put("imagefileid", imagefileid);
//             imagefilemap.put("imagefilename", imagefilename);
//             imagefilemap.put("imagefiletype", imagefiletype);
//             imagefilemap.put("imagefileused",imagefileused);
//             imagefilemap.put("filerealpath",filerealpath);
//             imagefilemap.put("iszip", iszip);
//             imagefilemap.put("fileSize", filesize);
//             imagefilemap.put("tokenkey",tokenkey);
//             imagefilemap.put("aescode",aescode);
//             imagefilemap.put("ftpconfigid",ftpconfigid);
//             imagefilemap.put("isaesencrypt",isaesencrypt);
//             NewDocUtil.createimagefile(imagefilemap);

             /**
              *开始创建docimagefile表
              * */
             VersionIdUpdate versionIdUpdate = new VersionIdUpdate();
             int versionid = versionIdUpdate.getVersionNewId();
             Map<String,Object> docimagefilemap=new HashMap<>();
             docimagefilemap.put("docid",docid);
             docimagefilemap.put("imagefileid",imagefileid);
             docimagefilemap.put("imagefilename",imagefilename);
             docimagefilemap.put("docextendname",docextendname);
             docimagefilemap.put("versionid",versionid);
             docimagefilemap.put("operateruserid",user.getUID());
             docimagefilemap.put("operatedate",operatedate);
             docimagefilemap.put("operatetime",operatetime);

             NewDocUtil.createdocimagefile(docimagefilemap);
             return docid;
         }catch (Exception e){
             return -1;
        }

    }

    /**
     * @title
     * @author K1810015
     * @updateTime 2022/3/11 14:19
     * @Remark 传递一个路径 重新新生成附件和imagefile表，并且返回imagefileid
     * @throws
     */
    public  static int getinputStream (String path,String filename,String iszip,String isaesencrypt, String aescode) {
        int inmagefileid=-1;
        InputStream obj = null;
        ZipInputStream zipInputStream=null;
        try {


            int isaesencryptint = Util.getIntValue(isaesencrypt, 0);
            File file = new File(path);
            if ("1".equals(iszip)) {
                 zipInputStream = new ZipInputStream(new FileInputStream(file));
                if (zipInputStream.getNextEntry() != null) {
                    obj = (InputStream)new BufferedInputStream(zipInputStream);
                }

            } else {
                obj = (InputStream)new BufferedInputStream(new FileInputStream(file));
            }
            if (isaesencryptint == 1) {
                obj = AESCoder.decrypt(obj, aescode);
            }
            if(obj!=null){
                inmagefileid= DocUtil.saveImageFile(obj, filename, "");
            }

    }catch (Exception e){
            inmagefileid =-1;
        }finally {
            try {
                if (obj != null) {
                    obj.close();
                }
                if(zipInputStream!=null){
                    zipInputStream.close();
                }
            }catch (Exception e){

            }

        }
        return inmagefileid;
    }

    /**
     * @title
     * @description
     * @author admin
     * @updateTime 2022/3/11 14:19
     * @Remark 随机获取附件路径
     * @throws
     */
    public static String createfile(){
        SystemComInfo systemComInfo = new SystemComInfo();
        String path = FileUpload.getCreateDir(systemComInfo.getFilesystem());
        return path;

    }


    public static int geturldocid(String imagefileid,String filename){
        int value=-1;
        if(StringUtils.isBlank(imagefileid) || StringUtils.isBlank(filename)){
            return  value;
        }
        String urlvalue=new PropBean().getPropName("xcoa.downloadurl");
        urlvalue+=imagefileid;
        URL url =null;
        HttpURLConnection urlcon =null;
        try {
            url = new URL(urlvalue);
            urlcon = (HttpURLConnection) url.openConnection();
            urlcon.connect();         //获取连接
            InputStream inputStream = urlcon.getInputStream();
            value= DocUtil.saveImageFile(inputStream, filename, "");
        }catch (Exception e){
            value=-1;
        }finally {

        }
        return value;

    }


}
