package org.springnewfiber.dataadapter.sswj.util; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Filter; import cn.hutool.core.util.ReflectUtil; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import org.springnewfiber.dataadapter.entity.MqNodeData; import org.springnewfiber.dataadapter.entity.PtReceiveBaseModel; import org.springnewfiber.dataadapter.xf.entity.BaseXfInterfaceEntity; import org.springnewfiber.dataadapter.xf.entity.MonitorChnlDataDto; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Date; import java.util.Map; /** * @program: newfiber-data-adapter * @description: * @author: djt * @create: 2022-04-21 15:48 **/ public class RealTimeSerializer extends JsonSerializer<Object> { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Class clazz = value.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { for (Annotation annotation : field.getAnnotations()) { Class annotationType = annotation.annotationType(); System.out.println("属性【" + field + "】的注释类型有: " + annotationType); } } // ReflectUtil.get gen.writeEndObject(); } public static void main(String[] args) { // BzLive bzLive = new BzLive(); // bzLive.setDataUpStatus("1"); // bzLive.setDataUpUuid("111111"); // System.out.println(); // System.out.println(JSONObject.toJSONString(bzLive)); MonitorChnlDataDto dto = new MonitorChnlDataDto(); // dto.setWptn(new BigDecimal(2)); dto.setZ(new BigDecimal(2)); dto.setTm(new Date()); dto.setStnm("ceshi"); dto.setStcd("123456789"); PtReceiveBaseModel json = xfObjectToRealMap(dto); System.out.println(JSONObject.toJSONString(json)); } public static PtReceiveBaseModel objectToRealMap(Object object) { if (null == object) { return null; } Map<String, Object> map = BeanUtil.beanToMap(JSONObject.parse(JSONObject.toJSONString(object))); Map<String, MqNodeData> dataMap = Maps.newHashMap(); PtReceiveBaseModel model = new PtReceiveBaseModel(); CollUtil.filter(map, new Filter<Map.Entry<String, Object>>() { @Override public boolean accept(Map.Entry<String, Object> stringObjectEntry) { if (PtReceiveBaseModel.constantSet.contains(stringObjectEntry.getKey())) { return false; } return true; } }).forEach((key, value) -> { MqNodeData data = new MqNodeData(); data.setSn(key); data.setKey(key); data.setValue(value); dataMap.put(key, data); }); model.setSt(map.get(PtReceiveBaseModel.RealTimeConsstant.st).toString()); model.setTt(DateUtil.parse(map.get(PtReceiveBaseModel.RealTimeConsstant.tt).toString(), DatePattern.NORM_DATETIME_PATTERN)); model.setUt(DateUtil.parse(map.get(PtReceiveBaseModel.RealTimeConsstant.ut) == null ? DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN) : map.get(PtReceiveBaseModel.RealTimeConsstant.ut).toString(), DatePattern.NORM_DATETIME_PATTERN)); model.setDataMap(dataMap); return model; } public static PtReceiveBaseModel xfObjectToRealMap(BaseXfInterfaceEntity object) { if (null == object) { return null; } PtReceiveBaseModel model = new PtReceiveBaseModel(); Map<String, MqNodeData> dataMap = Maps.newHashMap(); ReflectUtil.getFieldMap(object.getClass()).forEach((filedName, filed) -> { try { JsonProperty annotation = filed.getAnnotation(JsonProperty.class); filed.setAccessible(true); if (!PtReceiveBaseModel.XFconstantSet.contains(filedName)) { if (filed.get(object) == null || StringUtils.isBlank(filed.get(object).toString())) { return; } if (annotation != null) { String name = annotation.value(); // System.out.println(name); MqNodeData data = new MqNodeData(); data.setSn(filedName); data.setKey(name); data.setValue(filed.get(object)); if(dataMap.containsKey(name)){ dataMap.put(filedName, data); }else { dataMap.put(name, data); } } } } catch (IllegalAccessException e) { e.printStackTrace(); } }); model.setSt(object.getStcd()); model.setTt(object.getTm()); model.setUt(new Date()); model.setDataMap(dataMap); return model; } }