<?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><![CDATA[
            importPackage(java.util.zip);
            importPackage(java.io);
            importPackage(Packages.filetozip);
            
            
            /* importPackage(java.io);
             importPackage(org.apache.tools.zip);    */ /*可以传入参数*/
            
            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
            }
            
            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();
            }
            $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 =  $ctx.parameter.doc_code;
            var filename = doc_code + '-' + date_str + ".zip"
            resp.setHeader("Content-disposition", "attachment; filename=" + filename);
            resp.setDateHeader("Expires", 0);
            resp.setContentType("application/x-msdownload");
            var zos = new ZipOutputStream(resp.getOutputStream());
            var children = $ctx.parameter.getChildren();
            try {
                var con_batch_dl = $bm('cont.CON309.con_batch_download_dll');
                var result = con_batch_dl.queryAsMap();
                var arr_con = result.getChildren();
                for (var j = 0;j < arr_con.length;j++) {
                    var con = arr_con[j];
                    // var contract_id = con.contract_id;
                    // var resp_j = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
                    // resp_j.setHeader("Pragma", "No-cache");
                    // resp_j.setHeader("Cache-Control", "no-cache, must-revalidate");
                    // var date_str_j = getdate();
                    // var filename_j = con.contract_id + '-' + date_str_j + ".zip"
                    // resp_j.setHeader("Content-disposition", "attachment; filename=" + filename_j);
                    // resp_j.setDateHeader("Expires", 0);
                    // resp_j.setContentType("application/x-msdownload");
                    // var zos_j = new ZipOutputStream(resp_j.getOutputStream());
                    var attachment_batch_dl = $bm('batch_download.lease_atm_batch_dl');
                    var arr = attachment_batch_dl.queryAsMap({
                        table_name: 'CON_CONTRACT',
                        table_pk_value: con.contract_id
                    }).getChildren();
                    var file_exist_list = {};
                    for (var i = 0;i < arr.length;i++) {
                        var f = arr[i];
                        if (f.file_path && !file_exist_list[f.file_name]) {
                            println(f.file_path);
                            println(f.file_name);
                            println("D://zip_file/" + f.file_name);
                            var toPath = "D://zip_file/" + f.file_name;
                            println('=====================');
                            var file = new FileZip();
                            //var result = new String(FileZip.copyFile(f.file_path, toPath));
                            var result = file.copyFile(f.file_path, toPath);
                            println(result);
                            println('=====================');
                            // writeFile(zos, f.file_name, f.file_path);
                            // file_exist_list[f.file_name] = 1;
                        } else {
                            file_exist_list[f.file_name] = file_exist_list[f.file_name] * 1 + 1;
                            var last_index = f.file_name.lastIndexOf(".");
                            var temp_exists_file_name = f.file_name.substr(0, last_index);
                            var temp_exists_file_type = f.file_name.substr(last_index, f.file_name.length);
                            temp_exists_file_name = temp_exists_file_name + '-' + file_exist_list[f.file_name] + temp_exists_file_type;
                            writeFile(zos, temp_exists_file_name, f.file_path);
                            // writeFile(zos_j, f.file_name, f.file_path);
            
                        }
                    }
                    
                }
                // zos_j.close();
                // writeFile(zos, temp_exists_file_name, f.file_path);
            } catch (e) {
                var logger = $logger("server-script");
                logger.severe(e.message)
            }
            // try {
            // var attachment_batch_dl = $bm('batch_download.lease_atm_batch_dl');
            // var result = attachment_batch_dl.queryAsMap();
            // var arr = result.getChildren();
            // var file_exist_list = {};
            // for (var i = 0;i < arr.length;i++) {
            // var f = arr[i];
            // if (f.file_path && !file_exist_list[f.file_name]) {
            // writeFile(zos, f.file_name, f.file_path);
            // file_exist_list[f.file_name] = 1;
            // } else {
            // file_exist_list[f.file_name] = file_exist_list[f.file_name] * 1 + 1;
            // var last_index = f.file_name.lastIndexOf(".");
            // var temp_exists_file_name = f.file_name.substr(0, last_index);
            // var temp_exists_file_type = f.file_name.substr(last_index, f.file_name.length);
            // temp_exists_file_name = temp_exists_file_name + '-' + file_exist_list[f.file_name] + temp_exists_file_type;
            // writeFile(zos, temp_exists_file_name, f.file_path);
            // }
            // }
            // } catch (e) {
            // var logger = $logger("server-script");
            // logger.severe(e.message)
            // }
            zos.close();
        ]]></s:server-script>
    </a:init-procedure>
</a:service>