package weaver.interfaces.htsc.hthy;

import com.api.doc.search.service.DocShareService;
import com.engine.htsc.seal.service.SendManualSignService;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.DocViewer;
import weaver.docs.docs.VersionIdUpdate;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.hrm.User;
import weaver.interfaces.htsc.util.UploadDocUtil;
import weaver.interfaces.htsc.workflow.odoc.DocConvertToPdfAction;
import weaver.wps.officed.ConvertToPdf;

public class Hthy_Util {
    public int hyDoc2Pdf(String docid, int seccategory, String userids)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        int newdocid = 0 ;
        try {
            if(!"".equals(docid))
            {
                String imageFileid = "" ;
                String imagefilename ="" ;
                String sql ="select rownum,imagefileid,versionid,imagefilename from docimagefile\n" +
                        "where docid="+docid+" order by versionid desc" ;
                rs.execute(sql);
                if(rs.next()){
                    imageFileid = Util.null2String(rs.getString("imagefileid"));
                    imagefilename = Util.null2String(rs.getString("imagefilename"));
                }
                bb.writeLog("imageFileid:"+imageFileid);
                bb.writeLog("imagefilename:"+imagefilename);

                int newImageFileid = 0;
                if(!"".equals(imageFileid)){
                    ConvertToPdf convertToPdf = new ConvertToPdf();
                    newImageFileid = convertToPdf.getDownloadPDFUrl(Util.getIntValue(imageFileid),imagefilename, false);
                    bb.writeLog("转完pdf后新imagefileid:"+newImageFileid);
                }

                //4.根据新imagefileid 关联docdetail docimageFile表
                int maincategory = -1;
                int subcategory = -1;
//                int seccategory = -1;
                String docextendname = "pdf" ;
                String imagefilenamepdf = "";
                String docfiletype = "5";
                String doccreaterid ="";
                String doctype = "1";
                String filerealpath = "";
                String iszip = "";
                String imagefilenamepdfWithSuffix = "";

                rs.executeQuery("select imagefilename,imagefiletype,filesize,filerealpath,iszip from ImageFile where imagefileid = ?",newImageFileid);
                if(rs.next()){
                    imagefilenamepdfWithSuffix = weaver.general.Util.null2String(rs.getString("imagefilename"));
                    imagefilenamepdf = imagefilenamepdfWithSuffix.substring(0, imagefilenamepdfWithSuffix.lastIndexOf("."));
                    filerealpath = weaver.general.Util.null2String(rs.getString("filerealpath"));
                    iszip = weaver.general.Util.null2String(rs.getString("iszip"));
                }
                rs.executeQuery("select maincategory,subcategory,seccategory,doccreaterid from docdetail where id=?",docid);
                if (rs.next()) {
                    maincategory = weaver.general.Util.getIntValue(rs.getString("maincategory"));
                    subcategory = weaver.general.Util.getIntValue(rs.getString("subcategory"));
                    // seccategory = weaver.general.Util.getIntValue(rs.getString("seccategory"));
                    doccreaterid = weaver.general.Util.null2String(rs.getString("doccreaterid"));
                }

                //5.docdetail生成数据
                String content = "";
                String pubtime = TimeUtil.getCurrentDateString();
                newdocid = new UploadDocUtil().createDoc(imagefilenamepdf,content, Util.getIntValue(doccreaterid),pubtime,
                                                            maincategory,subcategory,seccategory, Util.getIntValue(doctype),docextendname,"");

                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+","+newImageFileid+",'"+imagefilenamepdfWithSuffix+"','0','0','1','0','"+docfiletype+"',"+versionid+")";
                rs.executeUpdate(sql);

                //6.更新附件个数
                rs.executeUpdate("update docdetail set accessorycount = (select count(1) from DocImageFile where docid = ?) where id = ?",new  Object[]{newdocid,newdocid});
                //8.上传网关

//                DocConvertToPdfAction DocConvertToPdfAction = new DocConvertToPdfAction();
//                DocConvertToPdfAction.callbackgateway(newImageFileid,imagefilename,filerealpath,iszip);

                //9.添加相关权限
                DocShareService docShareService = new DocShareService();
                docShareService.deleteAll(String.valueOf(newdocid), true, true);
                docShareService.addShareForAll(new User(), newdocid, 1, 0, 100, true);
//                docShareService.addShareForUser(new User(), newdocid, userids, 1, true);
//                docShareService.addShareForUser(new User(), newdocid, "26342,11635,21785,16653,42033", 1, true);
                DocViewer docViewer = new DocViewer();
                docViewer.setDocShareByDoc(String.valueOf(newdocid));
            }
        }catch (Exception e){
            bb.writeLog(e);
            newdocid = -1;
        }

        return newdocid;
    }
}
