package com.ruoyi.web.modules.trading.support;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;

/**
 * 车辆号牌 JSON 与列表互转。
 */
public final class SupplierVehiclePlatesHelper
{
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    private SupplierVehiclePlatesHelper()
    {
    }

    public static String toJson(List<String> plates)
    {
        if (plates == null || plates.isEmpty())
        {
            return null;
        }
        try
        {
            return OBJECT_MAPPER.writeValueAsString(plates);
        }
        catch (Exception ex)
        {
            throw new ServiceException("车辆号牌序列化失败");
        }
    }

    public static List<String> fromJson(String json)
    {
        if (StringUtils.isEmpty(json))
        {
            return Collections.emptyList();
        }
        try
        {
            List<String> list = OBJECT_MAPPER.readValue(json, new TypeReference<List<String>>() { });
            return list == null ? Collections.emptyList() : new ArrayList<>(list);
        }
        catch (Exception ex)
        {
            throw new ServiceException("车辆号牌数据格式错误");
        }
    }
}
