<?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:service xmlns:ns1="leaf.application.action" xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script ><![CDATA[
            importPackage(Packages.java.util);
            importPackage(Packages.java.lang);
            importPackage(Packages.com.hand.hl);
            importPackage(Packages.java.io);
            importPackage(java.io);
            try{
                 var sys_url_bm=$bm('cont.CON4000.get_sys_url');
                 var parameter_code='HLS_SYSTEM_URL';
                 var SysurlQuery = sys_url_bm.queryAsMap({
                                        parameter_code: parameter_code
                                });

                var sys_url = SysurlQuery.getChildren()[0].parameter_value;
                var down_url =sys_url+'con/contract/conExportExcel?contractId=' +$ctx.parameter.contract_id;
                println(down_url);
                var now = new Date();
                y = now.getFullYear();
                m = now.getMonth() + 1;
                m = m < 10 ? "0" + m : m;
                d = now.getDate();
                d = d < 10 ? "0" + d : d;
                h = now.getHours();
                h = h < 10 ? "0" + h : h;
                mi = now.getMinutes();
                mi = mi < 10 ? "0" + mi : mi;

                var f_fileDir = $ctx.parameter.file_path;
                var filePath = (f_fileDir + y + "//" + m + "//").toString();
                var tranfer_dir= new File(String(filePath));
                if(!tranfer_dir.exists()){//如果文件夹不存在,创建文件夹
                       tranfer_dir.mkdirs();//创建文件夹
                   }
                var contract_number =  $ctx.parameter.contract_number;
                var file_name =  $ctx.parameter.file_name + y + m + d + h + mi + ".xlsx";
                var f_fileName = file_name.toString();
                var result_flag=DoloadFileByUrl.downloadFile(down_url, filePath, f_fileName);
                file_size=get_file_length(filePath+f_fileName);
                if(result_flag=true){
                var business_daily_email=$bm('cont.CON301N.insert_into_attachment_bmp').update({
                        p_file_path      :(filePath+f_fileName).toString(),
                        p_file_name     :f_fileName,
                        p_file_size     :file_size,
                        p_user_id       :$ctx.session.user_id,
                        p_contract_id   :$ctx.parameter.contract_id,
                        p_file_type     :'EXCEL',
                        p_lease_type    :'01'
                });
                }
            }catch(e){
                 raise_app_error(e.message);
            }

            function get_file_length(path){
                var file=new File(String(path));
                if(file.exists() && file.isFile()){
                    length=file.length();
                    return length;
                }else{
                     return 0 ;
                }
            }

        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>