package com.persagy.bdtp.adm.entity.db;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.common.helper.SpringHelper;
import lombok.Data;

import java.io.IOException;
import java.util.List;

@Data
@TableName(value = "adm_object",autoResultMap = true)
public class AdmObject extends BaseDeliveryEntity {

	private String name;

	private String localId;

	private String localName;

	private String objType;

	private String classCode;

	private String groupCode;

	@TableField("`grouping`")
	private Integer grouping;

	@TableField(
			typeHandler = JacksonTypeHandler.class
	)
	private List<String> virtualCodes;

	private String createApp;

	private String updateApp;

	private String containerId;

	@TableField(
			typeHandler = JacksonTypeHandler.class
	)
	private ObjectNode infos;

	@TableField(
			exist = false
	)
	private String strInfos;

	public JsonNode getInfo(String infoCode){
		ObjectNode node = infos;
		if(node != null)
			return node.get(infoCode);
		return null;
	}

	@Override
	public void serializeJsonProp() {
		if(infos != null) {
			ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
			try {
				strInfos = objectMapper.writeValueAsString(infos);
			} catch (JsonProcessingException e) {

			}
			//infos = null;
		}
	}

	@Override
	public void deserializeJsonProp() {
		if(StrUtil.isNotBlank(strInfos)) {
			ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
			try {
				infos = objectMapper.readValue(strInfos, ObjectNode.class);
			} catch (IOException e) {

			}
			strInfos = null;
		}
	}

}
