<?xml version="1.0" encoding="UTF-8"?>
<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 import="app/app_login_check.js"><![CDATA[
    importPackage(java.io);
    importPackage(java.util.zip);
    //系统参数获取
    function info_query(parameter_code) {
        var info = new Object();
        var data = null;
        var parameter_map = $bm('wx.WX100.sys_parameter_value').queryAsMap({
            parameter_code:parameter_code
        });
        var data = parameter_map.getChildren();
        info.code_value = data[0].code_value;
        return info;
    }
    //打包文件路径
    var file_dir = info_query('HL_ELE_PACKAGE_PATH').code_value;

    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();
    }

    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 send_email() {
        try {
            if($ctx.parameter.email && $ctx.parameter.contractNo && $ctx.parameter.idNo) {
                var ele_file_email_bm = $bm('wx.WX100.wx_ele_file_email');
                ele_file_email_bm.insert({
                    mail_to: $ctx.parameter.email,
                    contract_id:$ctx.parameter.contractNo,
                    id_no:$ctx.parameter.idNo
                });
                var mail_list_id = $ctx.current_parameter.mail_list_id;
                var email_atm_file_bm = $bm('wx.WX100.wx_email_atm_file');
                var mail_file_result = email_atm_file_bm.queryAsMap({
                    mailing_list_id: mail_list_id
                }).getChildren();
                //时间戳作为压缩文件夹名称
                var date_str = getdate();
                var con_info_bm = $bm('wx.WX100.wx_email_attch_file');
                var con_info = con_info_bm.queryAsMap({
                    contract_id: $ctx.parameter.contractNo
                });
                var contract_number = con_info.getChildren()[0].contract_number;
                var doc_code = '合同签约附件';
                var zip_filename = doc_code + '-'+ contract_number  + '-' + date_str + ".zip";
                var tranfer_dir= new File(String(file_dir));
                if(!tranfer_dir.exists()){
                    tranfer_dir.mkdirs();//创建文件夹
                }
                var tranfer_file_path = file_dir + "/" + zip_filename;
                var zipFile= new File(String(tranfer_file_path));
                if(!zipFile.exists()){//如果文件夹不存在,创建年月文件夹
                    zipFile.createNewFile();
                }
                var zos = new ZipOutputStream(new FileOutputStream(zipFile));
                for (var i = 0; i < mail_file_result.length; i++) {
                    var f = mail_file_result[i];
                    if (f.file_path ) {
                        writeFile(zos, f.file_name, f.file_path);
                    }
                }
                zos.close();
                var file_url = tranfer_file_path.toString();
                var file_name = zip_filename.toString();
                con_info_bm.update({
                    attch_file_name: file_name,
                    attch_file_url:file_url,
                    mailing_list_id: mail_list_id
                });
                $ctx.parameter.return_status = '0';
                $ctx.parameter.return_message = '执行成功';
            }
        } catch (e) {
            $ctx.success = "true";
            $ctx.parameter.return_status = '1';
            $ctx.parameter.return_message = String(e);
        }
        var result = {
            code: $ctx.parameter.return_status,
            message: $ctx.parameter.return_message
        };
        $ctx.parameter.json = JSON.stringify(result);

    }
        send_email();
]]></s:server-script>
</a:init-procedure>
<a:service-output output="/parameter/@json"/>
    </a:service>