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

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

/**
 * 多选字段与逗号分隔串互转。
 */
public final class MedicalResourceMultiValueCodec
{
    private MedicalResourceMultiValueCodec()
    {
    }

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

    public static void decodeLists(BizMedicalResource row)
    {
        if (row == null)
        {
            return;
        }
        if (StringUtils.isNotEmpty(row.getConsultModes()))
        {
            row.setConsultModesList(splitInts(row.getConsultModes()));
        }
        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;
    }
}
