<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:s="leaf.plugin.script" xmlns:dr="leaf.plugin.excelreport" xmlns:mail="leaf.plugin.mail" xmlns:msg="leaf.application.features.msg" xmlns:t="uncertain.composite.transform" xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:excel="leaf.application.task.excel" trace="true">
    <a:init-procedure>
        <a:model-update model="psr.PSR1010.psr_check_send_mail"/>
        <s:server-script><![CDATA[
            importPackage(Packages.leaf.plugin.mail);
            importPackage(Packages.uncertain.proc);
            importPackage(Packages.leaf.plugin.script.scriptobject);
            
           // 更新发送信息   
            function after_send_mail(p_error_message) {
                var send_mail_update_bm = $bm('sys.sys_send_mail');
                 send_mail_update_bm.update({
                    mailing_list_id: $ctx.parameter.mailing_list_id,
                    error_message:p_error_message
                });
            }
              
            var mailing_list_id = $ctx.parameter.mailing_list_id;
            var error_message;
            try {
                var sm = new SendMail($instance('uncertain.ocm.IObjectRegistry'));
                // 获取附件
                var attachment_batch_dl = $bm('sys.sys_send_mail'); //获取附件的bm
                var result = attachment_batch_dl.queryAsMap({ // 参数
                    mailing_list_id: $ctx.parameter.mailing_list_id
                });
            
                var attachments = [];
                var j = 0;
                var arr = result.getChildren();
                if (arr.length > 0) {
                    if (arr[0].body) { // 邮件内容
                        sm.setContent(arr[0].body);
                    }
                    if (arr[0].subject) { //邮件主题
                        sm.setTitle(arr[0].subject);
                    }
                    if (arr[0].mail_to) { // 收件人
                        sm.setTto(arr[0].mail_to);
                    }
                    if (arr[0].mail_cc) { // 抄送
                        sm.setCto(arr[0].mail_cc);
                    }
            
                    for (var i = 0;i < arr.length;i++) {
                        var f = arr[i];
                        var attachment = new Attachment();
                        if (f.file_path && f.file_name) {
                            attachment.setPath(f.file_path);
                            attachment.setName(f.file_name);
                            attachments[j] = attachment;
                            j++;
     	                   }
                    }
                } else {
                    var mail_info = $bm('sys.zj_sys_mailing_list');
                    var result = mail_info.queryAsMap({ // 参数
                        mailing_list_id: $ctx.parameter.mailing_list_id
                    });
                    var arr_info = result.getChildren();
                    if (arr_info.length > 0) {
                        if (arr_info[0].body) { // 邮件内容
                            sm.setContent(arr_info[0].body);
                        }
                        if (arr_info[0].subject) { //邮件主题
                            sm.setTitle(arr_info[0].subject);
                        }
                        if (arr_info[0].mail_to) { // 收件人
                            sm.setTto(arr_info[0].mail_to);
                        }
                        if (arr_info[0].mail_cc) { // 抄送
                            sm.setCto(arr_info[0].mail_cc);
                        }
                    }
                }
                if (attachments) {
                    sm.setAttachments(attachments);
                }
                var procedureRunner = ScriptUtil.getProcedureRunner();
                //发送邮件
                sm.run(procedureRunner);
                error_message = null;
            } catch (e) {
                 // raise_app_error(e);
                 error_message = e.message;
            }    
         // 处理发送状态 
       // after_send_mail(error_message);
            
        ]]></s:server-script>
    </a:init-procedure>
    <a:service-output/>
</a:service>