//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.weaver.seconddev.htpoex;

import com.alibaba.fastjson.JSONArray;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.openapi.pojo.message.params.MessageVo;
import com.weaver.openapi.pojo.message.params.vo.MessageReciver;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.hrm.resource.ResourceComInfo;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * eip消息推送 门户消息推送
 * WeaReceiveManager 消息推送类 加载ECOLOGY_MESSAGE_SUBSCRIBE 表的实现类 WeaReceiveAQ调 队列调用实现类的
 * onMessage方法
 */
public class msgRecv   {

    public static final org.slf4j.Logger logger_d01caf55 = LoggerFactory.getLogger(msgRecv.class);


    @Autowired
    private ConfigProp configProp;
    public msgRecv() {
    }

    public void onMessage(String paramString) {
        BaseBean log = new BaseBean();
        logger_d01caf55.error(String.valueOf(">---onMessage---\n"));
        logger_d01caf55.error(String.valueOf(paramString + "\n"));
        List<MessageVo> localList = JSONArray.parseArray(paramString, MessageVo.class);
        if (localList != null) {
            Iterator var4 = localList.iterator();
            while (var4.hasNext()) {
                MessageVo messageBean = (MessageVo) var4.next();
                this.sendMsg(messageBean, log);
            }
        }
    }

    public void onStart() {
    }

    public void onEnd() {
    }

    public void setProp(Map<String, String> paramMap) {
    }

//    public void setList(List<IMessageReceive> paramList) {
//    }

    private void sendMsg(MessageVo msg, BaseBean log) {
        try {
            String systemId = "SYS006";
            String password = "123456";
            String moudule = "流程待办";
            String messages = "<messages>";
            logger_d01caf55.error(String.valueOf("code:" + msg.getEventId()));
            int code = msg.getEventId();
            String meetingmsg = "";
            switch(code) {
                case 19:
                case 20:
                case 21:
                case 22:
                case 23:
                case 24:
                case 25:
                    break;
                case 60:
                    moudule = "通知公告";
                    break;
                case 124:
                    moudule = "会议";
                    meetingmsg = "您有一个新的会议，会议名称：";
                    break;
                case 125:
                    moudule = "会议";
                    meetingmsg = "您有一个会议即将开始，会议名称：";
                    break;
                case 127:
                    moudule = "会议";
                    meetingmsg = "您有一个会议已取消，会议名称：";
                    break;
                case 137:
                    moudule = "会议";
                    meetingmsg = "您有一个会议已变更，会议名称：";
                    break;
                case 579:
                    moudule = "接待日历";
                    break;
                default:
                    return;
            }
            logger_d01caf55.error(String.valueOf(moudule));
            String content = msg.getText();
            String url = msg.getEntity().getPcUrl();
            String mobileurl = msg.getEntity().getH5Url();
            // TODO 获取配置文件信息发生变化,请将配置内容:htportaloaUrl.pc_url 维护到weaver-secondev-service.properties中
//            String pcurl = I18nContextUtil.getBean(Environment.class).getProperty("htportaloaUrl.pc_url") + URLEncoder.encode(url);
            String pcurl = configProp.getPc_url()+ URLEncoder.encode(url);
            // TODO 获取配置文件信息发生变化,请将配置内容:htportaloaUrl.pc_url 维护到weaver-secondev-service.properties中
//            String msgurl = I18nContextUtil.getBean(Environment.class).getProperty("htportaloaUrl.pc_url") + URLEncoder.encode(mobileurl);
            String msgurl = configProp.getPc_url()+ URLEncoder.encode(mobileurl);
            String m_msgtitle = msg.getTitle();
            String sendid = msg.getSender().getEmployeeId();
            String sendname = "系统管理员";
            ResourceComInfo rc = I18nContextUtil.getBean(ResourceComInfo.class);
            String userid = "1";
            if (sendid != "1") {
                sendname = rc.getLastname(sendid + "");
                userid = rc.getWorkcode(sendid + "");
            }
            String pre = "";
            String msgtt = m_msgtitle;
            if (m_msgtitle.contains("~`~`7")) {
                msgtt = m_msgtitle.substring(m_msgtitle.indexOf("~`~`7") + 6, m_msgtitle.indexOf("`~`8"));
            }
            if ("会议".equals(moudule)) {
                content = content.substring(content.indexOf("~`~`7") + 6, content.indexOf("`~`8")).replaceAll("<br/>", " ");
                msgtt = meetingmsg + msgtt + " " + content;
            }
            if ("通知公告".equals(moudule)) {
                content = content.substring(content.indexOf("~`~`7") + 6, content.indexOf("`~`8")).replaceAll("<br/>", " ");
                msgtt = "您有新的待阅文档：" + msgtt;
            }
            String refurl;
            if ("流程待办".equals(moudule)) {
                refurl = msg.getTitle();
                if (refurl.contains("~`~`7")) {
                    refurl = refurl.substring(refurl.indexOf("~`~`7") + 6, refurl.indexOf("`~`8"));
                    msgtt = refurl + "：" + msgtt;
                }
            }
            logger_d01caf55.error(String.valueOf(content));
            logger_d01caf55.error(String.valueOf(msgtt));
            refurl = msgurl;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String msgtime = df.format(new Date());
            String target = "2";
            String extdata = "";
//            Set<String> useridset = msg.getRecivers();
            List<MessageReciver> useridset = msg.getRecivers();
            Object[] arr = useridset.toArray();
            for (int i = 0; i < arr.length; i += 100) {
                StringBuilder sb = new StringBuilder();
                for (int j = i; j < i + 100 && j < arr.length; ++j) {
                    sb.append(rc.getWorkcode(arr[j] + "") + ",");
                }
                String user = sb.toString().substring(0, sb.length() - 1);
                String msgid = "MsgSYS006" + userid + (new Date()).getTime();
                messages = messages + "<message>";
                messages = messages + "<MSG_ID>" + msgid + "</MSG_ID><PRE_MSG_ID>" + pre + "</PRE_MSG_ID><USER_ID>" + user + "</USER_ID><MSG_TITLE>" + msgtt + "</MSG_TITLE><MSG_CONTENT>" + msgtt + "</MSG_CONTENT><SENDER_NAME>" + sendname + "</SENDER_NAME><REF_URL>" + refurl + "</REF_URL><MSG_TIME>" + msgtime + "</MSG_TIME><TARGET>" + target + "</TARGET><EXT_DATA>" + extdata + "</EXT_DATA>";
                messages = messages + "</message>";
                if (!"流程待办".equals(moudule)) {
                    String pctarget = "1";
                    pre = msgid;
                    msgid = "MsgPCSYS006" + userid + (new Date()).getTime();
                    messages = messages + "<message>";
                    messages = messages + "<MSG_ID>" + msgid + "</MSG_ID><PRE_MSG_ID>" + pre + "</PRE_MSG_ID><USER_ID>" + user + "</USER_ID><MSG_TITLE>" + msgtt + "</MSG_TITLE><MSG_CONTENT>" + msgtt + "</MSG_CONTENT><SENDER_NAME>" + sendname + "</SENDER_NAME><REF_URL>" + pcurl + "</REF_URL><MSG_TIME>" + msgtime + "</MSG_TIME><TARGET>" + pctarget + "</TARGET><EXT_DATA>" + extdata + "</EXT_DATA>";
                    messages = messages + "</message>";
                }
            }
            messages = messages + "</messages>";
            logger_d01caf55.error(String.valueOf(messages));
            String typeNamespace = "http://message.ht_portal_webservice.com";
            String endpoint = "https://sappop.htgd.com.cn:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_HTU_POP&receiverParty=&receiverService=&interface=SI_HTPortalMessage_OUT&interfaceNamespace=http%3A%2F%2Fhengtong.com%2FHTU%2FHTPortalMessage";
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTimeout(300000);
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName(new QName(typeNamespace, "message"));
            call.addParameter(new QName(typeNamespace, "systemId"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(typeNamespace, "password"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(typeNamespace, "moudule"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(typeNamespace, "messages"), XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnClass(String.class);
            call.setReturnQName(new QName(typeNamespace, "messageResponse"));
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://sap.com/xi/WebService/soap1.1");
            Hashtable<String, String> headers = (Hashtable) call.getProperty("HTTP-Request-Headers");
            if (headers == null) {
                headers = new Hashtable();
                call.setProperty("HTTP-Request-Headers", headers);
            }
            // TODO 获取配置文件信息发生变化,请将配置内容:htportaloaUrl.url_password 维护到weaver-secondev-service.properties中
//            headers.put("Authorization", I18nContextUtil.getBean(Environment.class).getProperty("htportaloaUrl.url_password"));
            headers.put("Authorization", configProp.getUrl_password());

            Object[] val = new Object[] { systemId, password, moudule, messages };
            String obj = (String) call.invoke(val);
            logger_d01caf55.error(String.valueOf(obj));
            logger_d01caf55.error(String.valueOf("----onMessage--<\n"));
        } catch (Exception var35) {
            var35.printStackTrace();
            logger_d01caf55.error(String.valueOf("推送流程消息出错：" + var35.toString()));
        }
    }
}
