//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.kingdee.bos.workflow.event;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.service.message.Message;
import com.kingdee.bos.workflow.ProcessInstInfo;
import com.kingdee.bos.workflow.WfException;
import com.kingdee.bos.workflow.define.ProcessType;
import com.kingdee.bos.workflow.define.Resource;
import com.kingdee.bos.workflow.enactment.WfEngine;
import com.kingdee.bos.workflow.enactment.message.WfMessage;
import com.kingdee.bos.workflow.util.ApplicationUtil;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.mobile.msg.IMobileAppMsgFacade;
import com.kingdee.eas.mobile.msg.MobileAppMsgFacadeFactory;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;

public class WfEvent {
    private static final Logger logger = Logger.getLogger(WfEvent.class);
    public static final String EVENT_MESSAGE_DOMAIN = "kingdee.bos.workflow.event";
    public static final String HEADER_NAME_PROCESSDEFID = "processDefId";
    public static final String HEADER_NAME_PROCESSDEFVERSION = "processDefVersion";
    public static final String HEADER_NAME_ACTDEFID = "actDefId";
    public static final String HEADER_NAME_PROCESSID = "processId";
    public static final String HEADER_NAME_ACTIVITYID = "activityId";
    public static final String HEADER_NAME_EVENTTYPE = "eventType";
    private int eventId;
    private String processDefId;
    private int processDefVersion;
    private String actDefId;
    private String processId;
    private Resource processName;
    private String activityId;
    private WfEventType eventType;
    private HashMap parameters;
    private String receiverList;
    private String sourceids;
    private WfEngine engine;
    private ProcessType wfType;

    public WfEvent(WfEngine engine) {
        this.engine = engine;
    }

    public int getEventId() {
        return this.eventId;
    }

    public Map getParameters() {
        return (Map)this.parameters.clone();
    }

    public void setParameters(HashMap map) {
        this.parameters = map;
    }

    public Object getParameters(Object key) {
        return this.parameters.get(key);
    }

    public WfEventType getEventType() {
        return this.eventType;
    }

    public void setEventType(WfEventType type) {
        this.eventType = type;
    }

    public String getActDefId() {
        return this.actDefId;
    }

    public void setActDefId(String actDefId) {
        this.actDefId = actDefId;
    }

    public String getActivityId() {
        return this.activityId;
    }

    public void setActivityId(String activityId) {
        this.activityId = activityId;
    }

    public String getProcessDefId() {
        return this.processDefId;
    }

    public void setProcessDefId(String processDefId) {
        this.processDefId = processDefId;
    }

    public int getProcessDefVersion() {
        return this.processDefVersion;
    }

    public void setProcessDefVersion(int processDefVersion) {
        this.processDefVersion = processDefVersion;
    }

    public String getProcessId() {
        return this.processId;
    }

    public void setProcessId(String processId) {
        this.processId = processId;
    }

    public void setWfType(ProcessType wfType) {
        this.wfType = wfType;
    }

    public ProcessType getWfType() {
        return this.wfType;
    }

    private String getEventName(Locale locale) {
        if (locale == null) {
            return this.eventType.getName();
        } else if (!ProcessType.BACKGROUNDJOB.equals(this.wfType)) {
            return this.eventType.getName(locale);
        } else {
            if ("l2".equalsIgnoreCase(locale.toString())) {
                if (this.eventType.equals(WfEventType.PROCESS_STARTED)) {
                    return "后台事务已经启动";
                }

                if (this.eventType.equals(WfEventType.PROCESS_COMPLETED)) {
                    return "后台事务已经完成";
                }

                if (this.eventType.equals(WfEventType.DEADLINE)) {
                    return "后台事务已经到达期限";
                }

                if (this.eventType.equals(WfEventType.PROCESS_SUSPENDED)) {
                    return "后台事务已经挂起";
                }
            }

            if ("l3".equalsIgnoreCase(locale.toString())) {
                if (this.eventType.equals(WfEventType.PROCESS_STARTED)) {
                    return "後臺事務已經啟動";
                }

                if (this.eventType.equals(WfEventType.PROCESS_COMPLETED)) {
                    return "後臺事務已經完成";
                }

                if (this.eventType.equals(WfEventType.DEADLINE)) {
                    return "後臺事務已經到達期限";
                }

                if (this.eventType.equals(WfEventType.PROCESS_SUSPENDED)) {
                    return "後臺事務已經掛起";
                }
            }

            return this.eventType.getName();
        }
    }

    public Message toMessage() {
        WfMessage message = null;
        message = new WfMessage();
        ProcessInstInfo procInfo = null;
        String code = "";
        String title = "";
        String topic = "";

        try {
            procInfo = this.engine.getProcessInst(this.processId).getData();
            code = procInfo.getCode();
        } catch (WfException var10) {
            logger.error("获取procInfo报错", var10);
        }

        Locale[] locales = ApplicationUtil.getContextLocales(this.engine.context());

        String content;
        for(int i = 0; i < locales.length; ++i) {
            Locale locale = locales[i];
            topic = procInfo.getMeta().getProcInstTopic(locale);
            if (topic != null) {
                title = topic;
            } else {
                title = "process name" + this.getProcessName().getValue(locale) + this.getEventName(locale);
            }

            if ("BACKGROUNDJOB".equals(procInfo.getMeta().getProcdefType())) {
                message.addContent("请通过 “ 系统功能 -> 后台事务 -> 后台事务监控” 查看 “编码” 为{ " + code + " }的后台事务的错误日志，了解挂起原因或修复此事务。", locale);
            } else {
//                content = "";
//                if (this.eventType.equals(WfEventType.PROCESS_SUSPENDED)) {
//                    content = title + " was aborted due to an error. For details, please contact IT (itsupport@gtiit.edu.cn) for help.";
//                } else {
//                    content = title + " was aborted due to an error. For details, please contact HR（hr@gtiit.edu.cn) and IT (itsupport@gtiit.edu.cn) for help.";
//                }
                //20250506微信消息要求改
                content = title + " was aborted due to an error. For details, please contact IT (itsupport@gtiit.edu.cn) and HR(hr@gtiit.edu.cn) for help.";
                message.addContent(content, locale);
            }

            message.addSubject(title, locale);
            message.addInitiatorName(procInfo.getInitiatorName(locale), locale);
        }

        message.setInitiatorId(procInfo.getInitiatorId());
        message.setContext(this.engine.context());
        message.setReceiver(this.receiverList);
        message.setProcessDefHashValue(procInfo.getProcDefHashValue());
        message.setProcessDefId(this.getProcessDefId());
        message.setProcessInstId(this.getProcessId());
        message.setActivityDefId(this.getActDefId());
        message.setActivityInstId(this.getActivityId());
        message.setSourceIds(this.getSourceIds());
        String suspend = null;
        if (this.parameters != null) {
            suspend = (String)this.parameters.get("suspendValue");
        }

        if (suspend != null) {
            message.setIsParamSuspend(true);
        }

        if (WfEventType.PROCESS_SUSPENDED.equals(this.getEventType())) {
            String isEmail = String.valueOf(this.getParameters("suspendEmail"));
            content = String.valueOf(this.getParameters("suspendSms"));
            if ("true".equals(isEmail) && "true".equals(content)) {
                message.setContentType(7);
            } else if ("true".equals(isEmail)) {
                message.setContentType(3);
            }
        }

        message.fillMessage();
        return message.getMessage();
    }

    public void sendEmailWhenSuspend() throws EASBizException, BOSException {
        IMobileAppMsgFacade iMsg = MobileAppMsgFacadeFactory.getLocalInstance(this.engine.context());
        iMsg.mobileAppListener(this.toMessage());
    }

    public static WfEvent FromMessage(WfEngine engine, Message message) {
        WfEvent event = new WfEvent(engine);
        event.processDefId = message.getStringHeader("processDefId");
        event.processDefVersion = message.getIntHeader("processDefVersion");
        event.actDefId = message.getStringHeader("actDefId");
        event.processId = message.getStringHeader("processId");
        event.activityId = message.getStringHeader("activityId");
        event.eventType = WfEventType.getEnum(message.getStringHeader("eventType"));
        return event;
    }

    public String dump() {
        StringBuffer dest = new StringBuffer();
        dest.append("eventType = ");
        dest.append(this.eventType.getName());
        dest.append("\n");
        dest.append("processDefId = ");
        dest.append(this.processDefId);
        dest.append("\n");
        dest.append("processDefVersion = ");
        dest.append(this.processDefVersion);
        dest.append("\n");
        dest.append("actDefId = ");
        dest.append(this.actDefId);
        dest.append("\n");
        dest.append("processId = ");
        dest.append(this.processId);
        dest.append("\n");
        dest.append("activityId = ");
        dest.append(this.activityId);
        dest.append("\n");
        return dest.toString();
    }

    public String getReceiverList() {
        return this.receiverList;
    }

    public void setReceiverList(String string) {
        this.receiverList = string;
    }

    public Resource getProcessName() {
        return this.processName;
    }

    public void setProcessName(Resource resource) {
        this.processName = resource;
    }

    public String getSourceIds() {
        return this.sourceids;
    }

    public void setSourceIds(String sourceids) {
        this.sourceids = sourceids;
    }
}
