<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script import="seal_util.js;jacob/jacob.js"><![CDATA[
            importPackage(java.util.zip);
            importPackage(java.io);
            importPackage(Packages.hls.plugin.docx4j);
            importPackage(Packages.hls.plugin.jacob.engine);
            //importPackage(org.apache.tools.zip); /*可以传入参数*/
            //importPackage(java.util.zip);
            
            function writeFile(zos, fn, fp) {
                var ze = new ZipEntry(fn);
                //zos.setEncoding("UTF-8");//如果是org.apache.tools.zip需要追加字符集
                zos.putNextEntry(ze);
                var fis = new FileInputStream(fp);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    zos.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function transfer(file, os) {
                var fis = new FileInputStream(file);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function getdate() {
                var now = new Date()
                y = now.getFullYear()
                m = now.getMonth() + 1
                d = now.getDate()
                m = m < 10 ? "0" + m : m
                d = d < 10 ? "0" + d : d
                return y + "" + m + "" + d
            }
            $ctx["__request_type__"] = 'file'; //to indicate this request is not a JSON_REQUEST
            var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
            resp.setHeader("Pragma", "No-cache");
            resp.setHeader("Cache-Control", "no-cache, must-revalidate");
            var date_str = getdate();
            var doc_code = '合同文本附件'
            var type = $ctx.parameter.type;
            var zip_filename = doc_code + '-' + date_str + ".zip";
            var file_path;
            var fnd_atm_flag = $ctx.parameter.fnd_atm_flag;
            //
            resp.setDateHeader("Expires", 0);
            resp.setContentType("application/x-msdownload");
            
            try {
                var attachment_batch_dl = $bm('cont.CON761.con_contract_content_download_all');
                var result = attachment_batch_dl.queryAsMap({
                    session_id: $ctx.parameter.session_id,
                });
                var arr = result.getChildren();
                if (arr.length >= 1) {
                        //打包下载
                        zip_filename = "合同结清文件.zip";
                        resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(zip_filename, 'UTF-8'));
                        var zos = new ZipOutputStream(resp.getOutputStream());
            
                        //zos.setEncoding("GBK"); //如果是org.apache.tools.zip需要追加字符集
            
                        for (var i = 0;i < arr.length;i++) {
                            var f = arr[i];
                            var temp_file_name = f.file_name2,
                                temp_file_path;
                            if (fnd_atm_flag == 'Y') {
                                temp_file_path = f.file_path;
                            } else {
                                temp_file_path = f.file_path + temp_file_name;
                            }
                            // if (temp_file_name.substring(temp_file_name.length - 4) == '.doc') {
                                // temp_file_name = temp_file_name.substring(0, temp_file_name.length - 4);
                            // } else if (temp_file_name.substring(temp_file_name.length - 5) == '.docx') {
                                // temp_file_name = temp_file_name.substring(0, temp_file_name.length - 5);
                            // }
                            // var brwt = new ConvertOutPDF();
                            // var outputfilepath = brwt.convertPdf(temp_file_path);
                            // var outputfilepath = temp_file_path + '_pdf';
                            // var file_type = new File(outputfilepath);
	            			// if (file_type.exists()&&file_type.length()>0){
	            			     // null;
	            			// }else{
	            				// var pe = new PdfConvertEngine($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
	                            // var attachment_id = f.attachment_id;
	                            // var server = jacob_config.jacobUrl + attachment_id;
	                            // var target_path = jacob_config.target_path;
	                            // var url = jacob_config.jacobserviceUrl + '?target_url=' + server + '&target_path=' + target_path;
	                            // try {
	                                // pe.run(url);
	                            // } catch (e) {
	                                // raise_app_error('转化PDF失败:' + e);
	                            // }     
	            			// }
                            
                            // var sign_path = null;
                            // var file_sign = null;
                            // $ctx.parameter.sign_status = 'N';
                            /*电子签章先不做*/
                            /* if (f.templet_code == 'HY_RISK' || f.templet_code == 'HY_RES_RELEVANCE' || f.templet_code == 'GH_WITHHOLD_AGREE') {
                                sign_path = outputfilepath;
                            } else {
                                sign_path = seal(file_name, outputfilepath);
                            } */
                            //sign_path = outputfilepath;
                            // if (f.templet_code == 'CON_LEASE'||f.templet_code == 'CON_LEASE_ORG'||f.templet_code == 'CON_LEASE1'||f.templet_code == 'CON_LEASE2' ){
	                            // //盖骑缝章包含签章
	                            // sign_path = outputfilepath+'_sign';
	                            // file_sign = new File(sign_path);
	                            // if (file_sign.exists()&&file_sign.length()>0){
	                                // null
	                            // }else{
	                            	// sign_path = seal(file_name, outputfilepath,'Y');    
	                            // }
	                        // }else if(f.templet_code == 'CON_ITEM_INFORMATION'||f.templet_code == 'CON_PAY_CCB'||f.templet_code=='PAY_PLAN1'||f.templet_code=='PAY_PLAN'||
	                        // f.templet_code=='CON_PAY_BOC'||f.templet_code=='CON_PAY_PSBC'){
	                            // //盖签章
	                            // sign_path = outputfilepath+'_sign';
	                            // file_sign = new File(sign_path);
	                            // if (file_sign.exists()&&file_sign.length()>0){
	                                // null
	                            // }else{
	                            	// sign_path = seal(file_name, outputfilepath,'N');
	                            // }
	                        // }
	                        // else{
	                        	// sign_path = outputfilepath;    
	                        // }
                            if (temp_file_path && temp_file_name) {
                                writeFile(zos, temp_file_name , temp_file_path);
                            }
                        }
                        zos.close();
                }
            } catch (e) {
                println(e);
                var logger = $logger("server-script");
                logger.severe(e.message);
            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>