package weaver.interfaces.htsc.GlobatTools;

import com.sap.db.jdbc.Hash;
import org.apache.commons.collections.map.HashedMap;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.ImageFileIdUpdate;
import weaver.docs.docs.VersionIdUpdate;
import weaver.file.ImageFileManager;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.util.UploadDocSyncUtil;
import weaver.interfaces.htsc.util.UploadDocUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName Uploadcory.java
 * @createTime 2021年11月16日 10:01:00
 */
public class Uploadcory {

    public static Map<String,Integer> coryfile(String docid){
        String sql = "";
        int newdocid = 0;
        int imagefileid = 0;
        String filename="";
        try {
            Map<String,Object> filevalue=getdocimagefile(docid);
            if(filevalue==null || filevalue.size()<=0){
                return null;
            }
            int seccategoryid = Util.getIntValue(Util.null2String(filevalue.get("seccategory")));
            int maincategoryid = Util.getIntValue(Util.null2String(filevalue.get("maincategory")));
            int subcategoryid = Util.getIntValue(Util.null2String(filevalue.get("subcategory")));
            String docextendname = Util.null2String(filevalue.get("docextendname"));
            String imagefilename = Util.null2String(filevalue.get("imagefilename"));
            filename= Util.null2String(filevalue.get("imagefilename"));
            String docfiletype = Util.null2String(filevalue.get("docfiletype"));
            String filerealpath= Util.null2String(filevalue.get("filerealpath"));
            String imagefiletype = Util.null2String(filevalue.get("imagefiletype"));
            String doccreaterid = Util.null2String(filevalue.get("doccreaterid"));
            String doctype = Util.null2String(filevalue.get("doctype"));
            String oldimagefileid=Util.null2String(filevalue.get("imagefileid"));
            int filesize = Util.getIntValue(Util.null2String(filevalue.get("filesize")));
            String content = "";
            String pubtime = TimeUtil.getCurrentDateString();
            newdocid = new UploadDocUtil().createDoc(imagefilename,content,Util.getIntValue(doccreaterid),pubtime,maincategoryid,subcategoryid,seccategoryid,Util.getIntValue(doctype),docextendname,"");

            RecordSet rs = new RecordSet();
            String filetype=filename.substring(filename.lastIndexOf("."),filename.length());
            if(!".RXF".equals(filetype.toUpperCase())){
                ImageFileManager imageFileManager=new ImageFileManager();
                imagefileid= imageFileManager.copyImageFile(Util.getIntValue(oldimagefileid));
            }else{
            ImageFileIdUpdate im = new ImageFileIdUpdate();
            imagefileid = im.getImageFileNewId();
            sql= " insert into ImageFile (imagefileid,imagefilename,imagefiletype,imagefileused,filerealpath,iszip,isencrypt,fileSize) "+
            " values("+imagefileid+",'"+imagefilename+"','"+imagefiletype+"',1,'"+filerealpath+"',0,0,'"+filesize+"')";
            rs.execute(sql);
            }

//            sql= " insert into ImageFile (imagefileid,imagefilename,imagefiletype,imagefileused,filerealpath,iszip,isencrypt,fileSize) "+
//                    " values("+imagefileid+",'"+imagefilename+"','"+imagefiletype+"',1,'"+filerealpath+"',0,0,'"+filesize+"')";
//            rs.execute(sql);

            DocImageManager dm = new DocImageManager();
            VersionIdUpdate versionIdUpdate = new VersionIdUpdate();

            int versionid = versionIdUpdate.getVersionNewId();
            sql = " insert into DocImageFile (id,docid,imagefileid,imagefilename,imagefilewidth,imagefileheight,isextfile,imagefielsize,docfiletype,versionid)"+
                    " values("+dm.getNextDocImageFileId()+","+newdocid+","+imagefileid+",'"+imagefilename+"','0','0','1','0','"+docfiletype+"',"+versionid+")";
            rs.execute(sql);

            sql = " select count(1) as cou from DocImageFile where docid="+newdocid;
            rs.execute(sql);
            rs.next();
            int count = Util.getIntValue(Util.null2String(rs.getString("cou")));

            sql = " update docdetail set accessorycount="+count+" where id="+newdocid;

            rs.execute(sql);

        }catch (Exception e){
            return null;
        }

        new SyncFileGateway(imagefileid,filename).start();


        Map<String,Integer> map=new HashedMap();
        map.put("docid",newdocid);
        map.put("imagefileid",imagefileid);

        return map;
    }

    public static Map<String,Object> getimagefile(String imagefileid){
        String sql="select * from imagefile ";
        Map<String,String> map=new HashMap<>();
        map.put("imagefileid",imagefileid);
       Map<String,Object> returnmap= JAVATools.GetTableToMap(sql,null);
        if(returnmap==null || returnmap.size()<=0){
            return null;
        }
        return returnmap;


    }

    public static  Map<String,Object> getdocimagefile(String docid,String imagefileid){
        String sql="select * from docimagefile ";
        Map<String,String> map=new HashMap<>();
        map.put("imagefileid",imagefileid);
        map.put("docid",docid);
        Map<String,Object> returnmap= JAVATools.GetTableToMap(sql,null);
        if(returnmap==null || returnmap.size()<=0){
            return null;
        }
        return returnmap;
    }
    public static  Map<String,Object> getdocimagefile(String docid){
        String sql =" select t.maincategory,t.subcategory,t.seccategory,t.docextendname,l.imagefilename,t.doctype,p.filesize, " +
                " l.docfiletype,p.imagefiletype,p.imagefileid,p.filerealpath,t.doccreaterid " +
                " from docdetail t " +
                " inner join docimagefile l on l.docid = t.id " +
                " inner join imagefile p on p.imagefileid = l.imagefileid " +
                " where docid=? and versionid IN(select max(versionid) from DocImageFile where docid=? ) ";
        Map<String,String> map=new HashMap<>();
        return DBTools.getsqltomap(sql,docid,docid);

    }



}
