<?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="contract_print_path.js"><![CDATA[
            importPackage(java.io);
            importPackage(Packages.hls.plugin.docx4j);

             function copyFile(fOld, fNew) {
                var fis = new java.io.FileInputStream(fOld);
                var fos = new java.io.FileOutputStream(fNew);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 4);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fis.close();
                fos.close();
            }
            //按日期创建目录
            function getDatePath() {
              set_parameter_file_path();
              var file_path = $ctx.parameter.file_path;
              var now = new Date()
              y = now.getFullYear()
              m = now.getMonth() + 1
              m = m < 10 ? "0" + m : m
              var datePath = file_path + y + "/" + m + "/";
              return datePath;
              }

         //获取文件长度
         function get_file_length(path){
            var file=new File(String(path));
            if(file.exists() && file.isFile()){
                length=file.length();
                return length;
            }else{
                 return 0 ;
            }
         }

         try {
           var datePath = getDatePath();
           FileUtils.forceMkdir(new File(datePath)); //根据日期创建目录
           //获取原文件信息
            var file_templet_get_atm_path = $bm('csh.CSH501.csh_journal_get_atm').queryAsMap({
                payment_req_id: $ctx.parameter.payment_req_id,
                user_id :$ctx.parameter.user_id
                        });
            var from_file_data = file_templet_get_atm_path.getChildren();
            for (var i = 0;i < from_file_data.length;i++) {
                var to_file_path = datePath;
				var record_data = from_file_data[i];

                var to_file_name = record_data.to_file_name;
                var from_file_path = record_data.file_path;

				var guid_file_name_path = $bm('cont.CON500.con_contract_get_guid_file_name').queryAsMap();
                var guid_file_name_tables = guid_file_name_path.getChildren();
                to_file_path = to_file_path + guid_file_name_tables[0].guid_file_name;

                copyFile(from_file_path, to_file_path);
                 //生成文件的大小
                 var file_size=get_file_length(to_file_path.toString());
				 var table_name = record_data.table_name;
				 var file_type_code=record_data.file_type_code;
				 var mime_type=record_data.mime_type;
                $bm('csh.CSH501.csh_journal_get_atm').execute({
                    table_name: table_name,
                    journal_id: $ctx.parameter.journal_id,
                    file_name: to_file_name.toString(),
                    file_path: to_file_path.toString(),
                    user_id :$ctx.parameter.user_id,
                    file_size: file_size,
                    file_type_code:file_type_code,
                    mime_type:mime_type,
                });
            }
          } catch (e) {
              raise_app_error(e);
             }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>