package com.persagy.adm.server.custom.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.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_pipe",autoResultMap = true)
public class AdmPipe extends BaseAdmDataEntity {

    private String objFrom;

    private String typeFrom;

    private String objTo;

    private String typeTo;

    private Integer direction;

    private String pipeType;

    private String pipeSubType;

    @TableField(
            typeHandler = JacksonTypeHandler.class
    )
    private List<Object> route;

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

    @TableField(
            exist = false
    )
    private String strRoute;

    @TableField(
            exist = false
    )
    private String strInfos;

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

            }
            //route = null;
        }
        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(strRoute)) {
            ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
            try {
                route = objectMapper.readValue(strRoute, List.class);
            } catch (IOException e) {

            }
            strRoute = null;
        }
        if(StrUtil.isNotBlank(strInfos)) {
            ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
            try {
                infos = objectMapper.readValue(strInfos, ObjectNode.class);
            } catch (IOException e) {

            }
            strInfos = null;
        }
    }

}