package com.yem.wm.sys.opplugin.op;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;


//import com.yem.wm.utils.AttachmentUtil;
import com.yem.wm.es.encasement.util.AttachmentUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.ksql.util.StringUtil;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.mq.MessageAcker;
import kd.bos.mq.MessageConsumer;

/**
 * mq消费者类
 * @description: 附件面板文件处理： 源单中文件同步到目标单的附件面板上
 * @author lib
 * @date 2023年8月28日
 */
public class FileSynchronizationConsumer implements MessageConsumer {

    Log log = LogFactory.getLog(getClass());

    @Override
    public void onMessage(Object msg, String messageId, boolean resend, MessageAcker acker) {

        log.info("自定义FileSynchronizationConsumer开始消费");
        try {
            @SuppressWarnings("unchecked")
            Map<String, DynamicObject> objMap = (Map<String, DynamicObject>) msg;
            if (objMap != null && objMap.size() > 0) {
                DynamicObject formObj = objMap.get("from");
                DynamicObject toObj = objMap.get("to");
                AttachmentUtil.uploadAttachmentPanel(formObj, toObj);

                // begin add by zhangh 20230916 附件存储内容逻辑处理
                String formattachmentcon = formObj.getString("tpv_attachmentcon_tag");
                JSONObject fromjson = new JSONObject();
                if (!StringUtil.isEmpty(formattachmentcon)) {
                    fromjson = JSONObject.parseObject(formattachmentcon);
                }

                String toattachmentcon = toObj.getString("tpv_attachmentcon_tag");
                JSONObject tojson = new JSONObject();
                if (!StringUtil.isEmpty(toattachmentcon)) {
                    tojson = JSONObject.parseObject(toattachmentcon);
                }

                for (String key : fromjson.keySet()) {
                    if (!tojson.containsKey(key)) {
                        tojson.put(key, fromjson.get(key));
                    }
                }
                toObj.set("tpv_attachmentcon_tag", tojson.toJSONString());

                String fromattachmentcontent = formObj.getString("tpv_rpasynchronizations_tag");
                JSONObject fromjsoncompar = new JSONObject();
                if (!StringUtil.isEmpty(fromattachmentcontent)) {
                    fromjsoncompar = JSONObject.parseObject(fromattachmentcontent);
                }

                String toattachmentcontent = toObj.getString("tpv_rpasynchronizations_tag");
                JSONObject tojsoncompar = new JSONObject();
                if (!StringUtil.isEmpty(toattachmentcontent)) {
                    tojsoncompar = JSONObject.parseObject(toattachmentcontent);
                }

                for (String key : fromjsoncompar.keySet()) {
                    if (!tojsoncompar.containsKey(key)) {
                        tojsoncompar.put(key, true);
                    }
                }
                toObj.set("tpv_rpasynchronizations_tag", tojsoncompar.toJSONString());

                // end add by zhangh 20230916

            }
            acker.ack(messageId);
        } catch (Exception e) {
            acker.discard(messageId);// 废弃
            log.error(e);
        }

    }

}
