<?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.kinggrid);
            importPackage(Packages.com.hand.hl);
            importPackage(Packages.leaf.plugin.word2pdf);
            importPackage(Packages.java.io);
            importPackage(java.io);
            //获取日期目录
            var datePath = getDate();
            var tranfer_dir= new File(String(datePath));
                if(!tranfer_dir.exists()){//如果文件夹不存在,创建文件夹
                       tranfer_dir.mkdirs();//创建文件夹
                                    }
            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;
                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 sys_file_path_bm=$bm('cont.CON4000.get_sys_url');
                var parameter_code='TEMPORARY_FILES';
                var SysurlQuery = sys_url_bm.queryAsMap({
                                    parameter_code: parameter_code
                                });

                var sys_file_path = SysurlQuery.getChildren()[0].parameter_value;
                var f_fileDir = sys_file_path;
                var filePath = (f_fileDir + y + "\/" + m + "\/").toString();
                //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);

                //excel 转pdf
                var to_file_path = filePath + file_name;
                var pdf_file_path= filePath + $ctx.parameter.file_name + y + m + d + h + mi + ".pdf";
                println('to_file_path  = ' + to_file_path);
				println('pdf_file_path  = ' + pdf_file_path);
                var outputfilepath = wordToPdf(to_file_path,pdf_file_path);

                file_size=get_file_length(outputfilepath);
                pdf_path =(pdf_file_path).toString()
                println('p_file_name = '+ $ctx.parameter.file_name + y + m + d + h + mi + ".pdf");
                println('contract_id  = ' + $ctx.parameter.contract_id);
                word_to_pdf_sign($ctx.parameter.contract_id,pdf_file_path,$ctx.parameter.file_name);

                if(result_flag=true){
                   var business_daily_email=$bm('cont.CON301N.insert_into_attachment_bmp').update({
                        p_file_path     :(datePath + $ctx.parameter.file_name + y + m + d + h + mi + ".pdf").toString(),
                        p_file_name     :($ctx.parameter.file_name + y + m + d + h + mi + ".pdf").toString(),
                        p_file_size     :file_size,
                        p_user_id       :$ctx.session.user_id,
                        p_contract_id   :$ctx.parameter.contract_id,
                        p_file_type     :'PDF',
                        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 ;
                }
            }


            function wordToPdf(word_file_path,pdf_file_path) {
            // excel转pdf
            //var pdf_file_path_new = pdf_file_path + '.pdf';
            var pdf_file_path_new = pdf_file_path;
            var wordToPdf = new WordToPdf($instance('uncertain.ocm.IObjectRegistry'), "jacob-1.19-x64.dll");
            wordToPdf.exceltopdf(word_file_path, pdf_file_path_new);
            //删除word文件
            //deleteFile(word_file_path);
            return pdf_file_path_new;
        }
           function getDate() {
                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 path = file_path + y + "/" + m + "/";
                return path;
            }
        function word_to_pdf_sign(content_id,pdf_file_path,file_name) {
                importPackage(Packages.com.hl.signatures);
                importPackage(Packages.java.io);
                importPackage(Packages.java.util);

                var org_flag = '';
                var qf_flag = '';
                var param = new ArrayList();
                var paramdetail;
                var attachment_update = $bm('cont.CON620.con_contract_save_pdf_atm');
                var file_name = file_name;
                    var file_path = pdf_file_path;
                    if (file_name.substring(file_name.length - 4) == '.doc') {
                        file_name = file_name.substring(0, file_name.length - 4);
                    } else if (file_name.substring(file_name.length - 5) == '.docx') {
                        file_name = file_name.substring(0, file_name.length - 5);
                    }
                    var outputfilepath = pdf_file_path;// 由于wordToPdf转换后pdf文件已经带有.pdf
                    var wordToPdf = new WordToPdf($instance('uncertain.ocm.IObjectRegistry'), "jacob-1.19-x64.dll");
                    //wordToPdf.word2pdf(file_path, outputfilepath);

                    var con_signature_info = $bm('cont.CON620.get_con_signature_info').queryAsMap();
                    var con_signature_data = con_signature_info.getChildren();
                    var con_signature_record;

                    for (var i = 0;i < con_signature_data.length;i++) {

                        con_signature_record = con_signature_data[i];

                        println(con_signature_record.signature_code);
                        if (con_signature_record.signature_code == 'gz_flag'){
                             paramdetail = '[{ "code":"' + con_signature_record.code +
                                    '", "seal_code":"' + con_signature_record.seal_code +
                                    '", "seal_width":' + con_signature_record.seal_width +
                                    ', "fontname":"' + con_signature_record.fontname +
                                    '", "fontsize":"' + con_signature_record.fontsize +
                                    '", "fontcolor":"' + con_signature_record.fontcolor +
                                    '", "pageno":"' + con_signature_record.pageno +
                                    '", "text":"' + con_signature_record.word_code + '"}]';
                        }
                        param.add(paramdetail);
                    }

                    var con_signature_info = $bm('cont.CON620.get_kinggrid_info').queryAsMap();
                    var con_kinggrid_data = con_signature_info.getChildren()[0];
                    var server_url = con_kinggrid_data.server_url;
                    var app_id = con_kinggrid_data.app_id;
                    var app_security = con_kinggrid_data.app_security;
                    var status = new com.hand.kinggrid.ContractSignaturesNew.hl_contract_signatures(outputfilepath, 1,param.toArray(),server_url,app_id,app_security,datePath);

<!--                    attachment_update.update({-->
<!--                        content_id: content_id,-->
<!--                        table_name: 'HLS_DOC_FILE_CONTENT'-->
<!--                    });-->
            }


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