package com.izouma.nineth.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.izouma.nineth.dto.BuildingPosDTO;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.List;

@Converter
public class BuildingPosListConverter implements AttributeConverter<List<BuildingPosDTO>, String> {

    @Override
    public String convertToDatabaseColumn(List<BuildingPosDTO> buildingPosDTOList) {
        if (buildingPosDTOList == null) {
            return null;
        }
        return JSON.toJSONString(buildingPosDTOList);
    }

    @Override
    public List<BuildingPosDTO> convertToEntityAttribute(String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        try {
            return JSONArray.parseArray(s, BuildingPosDTO.class);
        } catch (Exception ignored) {
        }
        return null;
    }
}