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.core.type.TypeReference;
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_problem_arch",autoResultMap = true)
public class AdmProblemArch extends BaseAdmDataEntity {

    private String workId;

    private String geomType;

    @TableField(
            typeHandler = JacksonTypeHandler.class
    )
    private List<ObjectNode> geom;

    private String problemType;

    private String message;

    private Integer problemState;

    @TableField(
            exist = false
    )
    private String strGeom;

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

            }
            //geom = null;
        }
    }

    @Override
    public void deserializeJsonProp() {
        if(StrUtil.isNotBlank(strGeom)) {
            ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
            try {
                geom = objectMapper.readValue(strGeom, new TypeReference<List<ObjectNode>>(){});
            } catch (IOException e) {

            }
            strGeom = null;
        }
    }

}