package com.izouma.nineth.converter;

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

import javax.persistence.AttributeConverter;

public class MintRuleConverter implements AttributeConverter<MintActivityRule, String> {
    @Override
    public String convertToDatabaseColumn(MintActivityRule mintActivityRule) {
        if (mintActivityRule == null) {
            return null;
        } else {
            return JSON.toJSONString(mintActivityRule);
        }
    }

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