package com.tofly.ghrq_route.module.pipeAnalyze.domain;

import com.google.gson.Gson;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Map_Field_Converter implements PropertyConverter<List<FieldsBean>, String> {

    private static final String SPLIT = "#";

    @Override
    public List<FieldsBean> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null)
            return null;

        List<String> list_str = Arrays.asList(databaseValue.split(SPLIT));
        List<FieldsBean> fieldsBeanList = new ArrayList<>();

        for (String str : list_str){
            fieldsBeanList.add(new Gson().fromJson(str, FieldsBean.class));
        }
        return fieldsBeanList;
    }

    @Override
    public String convertToDatabaseValue(List<FieldsBean> fieldsBeans) {
        if (fieldsBeans == null){
            return null;
        }else {
            StringBuilder builder = new StringBuilder();
            for (FieldsBean fieldsBean : fieldsBeans){
                String str = new Gson().toJson(fieldsBean);
                builder.append(str);
                builder.append(SPLIT);
            }
            return builder.toString();
        }
    }
}
