package com.awspaas.user.apps.donenow_ctt.event;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;

public class noticeTaskAfterCreated extends ExecuteListener implements ExecuteListenerInterface {

    /**
     *执行
     * @param processExecutionContext
     * @throws Exception
     */
    @Override
    public void execute(ProcessExecutionContext processExecutionContext) throws Exception {
        String processInstId = processExecutionContext.getProcessInstance().getId();
        /*
        String kk = DBSql.getString("SELECT TEXTVALUE FROM wfc_var WHERE PROCESSINSTID=? AND VARNAME=?", new Object[]{processInstId, ""});
        //邮件接收人，多个用;隔开
        String to = DBSql.getString("SELECT TEXTVALUE FROM wfc_var WHERE PROCESSINSTID=? AND VARNAME=?", new Object[]{processInstId, ""});
        to = SDK.getRuleAPI().executeAtScript(to, processExecutionContext);
        */
        String to = "";
        String templateId = "579991a2-298f-4678-b641-dc501d990ca1";

        ResponseObject res = SDK.getNotificationAPI().getEmailTemplate(templateId);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(res.getData()));

        String email_content = data.getString("email_content");
        String email_title = data.getString("email_title");


        email_content = SDK.getRuleAPI().executeAtScript(email_content, processExecutionContext);
        email_title = SDK.getRuleAPI().executeAtScript(email_title, processExecutionContext);

        System.out.println("===============================================================================================================");
        System.out.println("流程标题：" + processExecutionContext.getProcessInstance().getTitle());
        System.out.println("流程实例ID：" + processInstId);
        System.out.println("发送邮件");
        System.out.println("接收人：" + to);
        System.out.println("邮件标题：" + email_title);
        System.out.println("邮件内容：" + email_content);

        //发送邮件
        SDK.getNotificationAPI().sendEMailAsync(null, to, "", email_title, email_content);


    }
}
