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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.farming.domain.BizTechResource;

/**
 * 服务周日多选与逗号分隔串互转。
 */
public final class TechResourceMultiValueCodec
{
    private TechResourceMultiValueCodec()
    {
    }

    public static void encodeLists(BizTechResource row)
    {
        if (row == null)
        {
            return;
        }
        row.setServiceWeekdays(joinInts(row.getServiceWeekdaysList()));
    }

    public static void decodeLists(BizTechResource row)
    {
        if (row == null)
        {
            return;
        }
        if (StringUtils.isNotEmpty(row.getServiceWeekdays()))
        {
            row.setServiceWeekdaysList(splitInts(row.getServiceWeekdays()));
        }
    }

    public static String joinInts(List<Integer> values)
    {
        if (values == null || values.isEmpty())
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (Integer v : values)
        {
            if (v == null)
            {
                continue;
            }
            if (sb.length() > 0)
            {
                sb.append(',');
            }
            sb.append(v);
        }
        return sb.length() == 0 ? null : sb.toString();
    }

    public static List<Integer> splitInts(String text)
    {
        if (StringUtils.isEmpty(text))
        {
            return Collections.emptyList();
        }
        String[] parts = text.split(",");
        List<Integer> list = new ArrayList<>(parts.length);
        for (String part : parts)
        {
            String p = part.trim();
            if (!p.isEmpty())
            {
                list.add(Integer.valueOf(p));
            }
        }
        return list;
    }
}
