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.persagy.dmp.common.helper.SpringHelper;
import lombok.Data;

import java.io.IOException;

@Data
@TableName(value = "adm_config",autoResultMap = true)
public class AdmConfig extends BaseAdmEntity {

	private String name;

	private String code;

	@TableField(
			typeHandler = JacksonTypeHandler.class
	)
	private JsonNode data;

	@TableField(
			exist = false
	)
	private String strData;

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

			}
			//data = null;
		}
	}

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

			}
			strData = null;
		}
	}

}