package com.izouma.nineth.converter;

import com.alibaba.fastjson.JSON;
import com.izouma.nineth.domain.InvitePrize;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class InvitePrizeConverter implements AttributeConverter<InvitePrize, String> {
    @Override
    public String convertToDatabaseColumn(InvitePrize invitePrize) {
        if (invitePrize != null  )
            return JSON.toJSONString(invitePrize);
        return null;
    }

    @Override
    public InvitePrize convertToEntityAttribute(String s) {
        try {
            if (StringUtils.isNotEmpty(s)) {
                return JSON.parseObject(s, InvitePrize.class);
            }
            return null;
        }catch (Exception e){
            return null;
        }
    }
}
