<?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(Packages.batch_download);
            importPackage(Packages.ZipCompress);
            
            //@author zhuxianfei 20180117
            //@purpose 实现多个合同附件打包成一个压缩包下载
            
            //打包文件路径，迁移环境需要修改
            var file_dir = "E:\hls_files\dr_hls\hls_download";
            
            //下载压缩包
            
            function downLoadZip(filePath, fileName) {
                try {
                    var res = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
                    var file = new java.io.File(filePath);
                    res.setCharacterEncoding("UTF-8");
                    res.setHeader("Content-disposition", "attachment; filename=" + encodeURI(fileName, 'utf-8'));
                    var len = file.length();
                    res.setContentLength(len);
                    res.setContentType("application/zip");
                    var fis = new java.io.FileInputStream(file);
                    var buff = new java.io.BufferedInputStream(fis);
                    var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                    var k = 0;
                    var os = res.getOutputStream();
                    // start download
                    while (k < len) {
                        var j = buff.read(b, 0, 1024);
                        k += j;
                        os.write(b, 0, j);
                    }
                    os.flush();
                    buff.close();
                    // file.delete();
            
                } catch (e) {
                    var logger = $logger("server-script");
                    logger.severe(e.message)
                }
                //return null;
            }
            
            //将各个合同下的附件复制到指定的目标文件夹
            try {
                //时间戳作为压缩文件夹名称
                var folder_name = "支付表附件下载"+ (new Date()).valueOf();
            
                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 attachment_batch_dl = $bm('cont.CON309.lease_atm_batch_dl');
                    var arr = attachment_batch_dl.queryAsMap({
                        table_name: 'CON_CONTRACT',
                        table_pk_value: con.contract_id,
                        cdd_list_id: con.cdd_list_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]) {
                            var toPath = file_dir + folder_name + '/' + con.contract_number + '/' + f.file_name;
                            //copy file to folder
                            BatchDownload.copyFile(f.file_path, toPath);
            
                        } 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;
                            var toPath = file_dir + folder_name + '/' + con.contract_number + '/' + temp_exists_file_namee;
                            //copy file to folder
                            BatchDownload.copyFile(f.file_path, toPath);
                        }
                    }
            
                }
            } catch (e) {
                var logger = $logger("server-script");
                logger.severe(e.message)
            }
            //压缩文件夹
            FileCompress.compress(file_dir + folder_name + '/', file_dir + folder_name + ".zip");
            
            //下载文件
            downLoadZip(file_dir + folder_name + ".zip", folder_name + ".zip");
        ]]></s:server-script>
    </a:init-procedure>
</a:service>
