package com.izouma.nineth.converter;

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

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

public class InvitePrizeListConverter implements AttributeConverter<List<InvitePrize>, String> {

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

    @Override
    public List<InvitePrize> convertToEntityAttribute(String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        try {
            return JSONArray.parseArray(s, InvitePrize.class);
        } catch (Exception ignored) {
            System.out.println("转换错误");
        }
        return null;
    }

    public static void main(String[] args) {
        String str ="{\"id\":\"10061\",\"del\":false,\"inviteStartdatetime\":\"2022-05-17 00:00:00\",\"inviteEnddatetime\":\"2022-05-18 00:00:00\",\"byInvitePrize\":{\"type\":\"asset\",\"inviteCollection\":\"11574\",\"inviteNum\":0},\"prl\":\"https://cosmoscdn.9space.vip/image/2022-05-16-18-35-04hBeHjIqJ.jpg\",\"content\":\"<p>1:活动时间的钱的期望的委屈的</p >\\n<p>2:的委屈的委屈的我确定委屈的委屈的我确定我</p >\\n<p>3：的委屈的委屈的期望的期望的期望的委屈的 的我确定</p >\\n<p>4:分废物废物废物废物<span style=\\\"color: #e03e2d;\\\">粉为废物 </span>分额外废物粉</p >\\n<p>5:违法额外废物废物废物<span style=\\\"color: #3598db;\\\">废物废物粉</span>额外废物分万分额外分</p >\",\"name\":\"测试提交邀请有礼活动\",\"poster\":\"https://cosmoscdn.9space.vip/image/2022-05-16-18-35-00jfyZqufU.jpg\",\"projectId\":1,\"createdAt\":\"2022-05-16 18:36:51\",\"InvitePrizeList\":[{\"type\":\"asset\",\"inviteCollection\":\"11578\",\"inviteNum\":3}]}";
        ActivityInvite ll = JSONArray.parseObject(str, ActivityInvite.class);
        System.out.println( ll);
    }
}
