package com.unis.crk.mq.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.unis.crk.mq.dto.BaseDTO;
import com.unis.crk.mq.dto.UploadDTO;
import org.apache.commons.lang.StringUtils;

/**
 * @author ybw
 * @version V1.0
 * @className JsonUtils
 * @date 2024/5/9
 **/
public class JsonUtils {


    /**
     * mq消息转换为对象
     *
     * @param bytes
     * @methodName: mqMessageToObj
     * @return: com.unis.crk.mq.dto.BaseDTO<T>
     * @author: ybw
     * @date: 2024/5/9
     **/
    public static <T> BaseDTO<T> mqMessageToObj(byte[] bytes) {
        //1、转换为字符串
        String receivedMessage = new String(bytes);
        //2、使用正则表达式去除两端的双引号
        receivedMessage = receivedMessage.replaceAll("^\"|\"$", "");
        //3、将转义字符替换为对应的字符
        receivedMessage = receivedMessage.replace("\\", "");
        //4、转换为对象
        return JSON.parseObject(receivedMessage, new TypeReference<BaseDTO<T>>() {
        });
    }

    public static String mqMessageToObj1(String receivedMessage) {
        //2、使用正则表达式去除两端的双引号
        receivedMessage = receivedMessage.replaceAll("^\"|\"$", "");
        //3、将转义字符替换为对应的字符
        receivedMessage = receivedMessage.replace("\\", "");
        //处理字段下划线问题
        receivedMessage = StringUtils.replace(receivedMessage, "\"isUpload\"","\"is_upload\"");
        receivedMessage = StringUtils.replace(receivedMessage, "\"loadDate\"","\"load_date\"");
        receivedMessage = StringUtils.replace(receivedMessage, "\"depDwdm\"","\"depDwdm\"");
        receivedMessage = StringUtils.replace(receivedMessage, "\"depDqdm\"","\"depDqdm\"");
        //4、转换为对象
        return receivedMessage;
    }
}
