package com.engine.custom.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.cloudstore.dev.api.bean.MessageBean;
import com.weaver.base.msgcenter.channel.IMessageReceive;
import com.weaver.general.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StringUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;

public class MessageReceiveTest implements IMessageReceive {
    public static Logger log = LoggerFactory.getLogger("Archives");
    private BaseBean bean=new BaseBean();
    @Override
    public void onMessage(String s) {
        log.error("onMessage:{}",s);
        bean.writeLog("onMessage:{}",s);
        List<MessageBean> localList = JSONArray.parseArray(s, MessageBean.class);
        for (MessageBean messageBean : localList) {
            String customContext = getCustomContext(messageBean);
            log.error("customContext:{}",customContext);
        }
    }

    @Override
    public void onStart() {
        log.error("onStartExecute");
        bean.writeLog("onStartExecute");
    }

    @Override
    public void onEnd() {
        log.error("onEndExecute");
        bean.writeLog("onEndExecute");
    }

    @Override
    public void setProp(Map<String, String> map) {
        log.error("setProp:{}",map);
        bean.writeLog("setProp:{}"+map);
    }

    @Override
    public void setList(List<IMessageReceive> list) {
        log.error("setList:{}", JSONObject.toJSONString(list));
        bean.writeLog("setList:{}"+ JSONObject.toJSONString(list));
    }

    private String getCustomContext(MessageBean messageBean) {
        log.error("onMessage:{}",messageBean);
        String customContext = "";
        if (messageBean == null ){
            bean.writeLog("messageBean==null,异常");
            return customContext;
        }
        String messageGroupTypeName = Util.null2String(messageBean.getMessageGroupTypeName());
        if (messageGroupTypeName.contains("流程")){
            RecordSet rs = new RecordSet();
            String targetId = messageBean.getTargetId();
            if (StringUtil.isEmpty(targetId)||targetId.contains("|")){
                return Util.null2String(messageBean.getContext());
            }
            rs.executeQuery("select * from WORKFLOW_REQUESTBASE where REQUESTID = ?",targetId);
            if (rs.next()){
                int creater = Util.getIntValue(rs.getString("creater"));
                User user = new User(creater);
                String createdate = Util.null2String(rs.getString("createdate"));
                String createtime = Util.null2String(rs.getString("createtime"));
                customContext = messageGroupTypeName + "-" + (StringUtil.isEmpty(messageBean.getContext()) ? messageBean.getTitle() : messageBean.getContext() )  + ",创建人:" + user.getLastname()
                               +",创建时间:"+createdate + " " + createtime + " ,请您登录到办公网进行处理。";
            }
        }else if(messageGroupTypeName.contains("邮件") || messageGroupTypeName.contains("信件")){
            // String date = messageBean.getDate();
            // String time = messageBean.getTime();
            customContext = messageGroupTypeName + "-" + (StringUtil.isEmpty(messageBean.getContext()) ? messageBean.getTitle() : messageBean.getContext() ) + " ,请您登录到办公网进行处理。";
        }else {
            customContext = Util.null2String(messageBean.getContext());
        }
        return customContext;
    }


    public static String getStringValue(JSONObject jsonObject, String key) {
        return jsonObject.containsKey(key) ? jsonObject.getString(key) : null;
    }

    public static Integer getIntegerValue(JSONObject jsonObject, String key) {
        return jsonObject.containsKey(key) ? jsonObject.getInteger(key) : null;
    }


    public static void main(String[] args) {
        // 示例 JSON 字符串
        String jsonString = "{\"name\":\"John\", \"age\":30}";

        // 将 JSON 字符串解析为 JSONObject
        JSONObject jsonObject = JSONObject.parseObject(jsonString);

        // 检查 key 是否存在并获取值
        String name = getStringValue(jsonObject, "name");
        String age = getStringValue(jsonObject, "age");
        String address = getStringValue(jsonObject, "address"); // key 不存在

        // 输出结果
        System.out.println("Name: " + name);       // 输出: Name: John
        System.out.println("Age: " + age);         // 输出: Age: 30
        System.out.println("Address: " + address); // 输出: Address: null
    }

}
