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;

@Data
@TableName(value = "adm_qr_code",autoResultMap = true)
public class AdmQrCode extends BaseAdmDataEntity {

    private String qrCode;

    private String objId;

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

    private String remark;

    @TableField(
            exist = false
    )
    private String strLocation;

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

            }
            //location = null;
        }
    }

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

            }
            strLocation = null;
        }
    }

}