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

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityCompanion;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard;

/**
 * 药物禁配标准入参校验（主药、配伍列表、配伍结果；与项目长度口径一致）。
 */
public final class DrugIncompatibilityStandardValidation
{
    private DrugIncompatibilityStandardValidation()
    {
    }

    public static String normalizeName(String name)
    {
        return name == null ? "" : name.trim();
    }

    public static void validateMainDrugName(String mainDrugName)
    {
        String s = normalizeName(mainDrugName);
        if (s.isEmpty() || s.length() > 20)
        {
            throw new ServiceException("主药名称须为1～20个字符");
        }
    }

    public static void validateCompatResult(String compatResult)
    {
        if (compatResult == null)
        {
            return;
        }
        String s = compatResult.trim();
        if (s.isEmpty())
        {
            return;
        }
        if (s.length() > 50)
        {
            throw new ServiceException("配伍结果不能超过50个字符");
        }
    }

    public static void validateCompanionName(String companionDrugName)
    {
        String s = normalizeName(companionDrugName);
        if (s.isEmpty() || s.length() > 20)
        {
            throw new ServiceException("配伍药物名称须为1～20个字符");
        }
    }

    public static void validateCompanions(BizDrugIncompatibilityStandard row)
    {
        List<BizDrugIncompatibilityCompanion> list = row.getCompanions();
        if (list == null || list.isEmpty())
        {
            throw new ServiceException("配伍药物至少一条");
        }
        String main = normalizeName(row.getMainDrugName());
        Set<String> seen = new HashSet<>();
        for (BizDrugIncompatibilityCompanion c : list)
        {
            if (c == null)
            {
                throw new ServiceException("配伍药物数据非法");
            }
            String n = normalizeName(c.getCompanionDrugName());
            validateCompanionName(n);
            if (n.equals(main))
            {
                throw new ServiceException("配伍药物名称不能与主药名称相同");
            }
            if (!seen.add(n))
            {
                throw new ServiceException("配伍药物名称不能重复");
            }
        }
    }

    public static void trimFieldsForSave(BizDrugIncompatibilityStandard row)
    {
        row.setMainDrugName(normalizeName(row.getMainDrugName()));
        if (row.getCompatResult() != null)
        {
            String t = row.getCompatResult().trim();
            row.setCompatResult(t.isEmpty() ? null : t);
        }
        if (row.getCompanions() != null)
        {
            for (BizDrugIncompatibilityCompanion c : row.getCompanions())
            {
                if (c != null && c.getCompanionDrugName() != null)
                {
                    c.setCompanionDrugName(normalizeName(c.getCompanionDrugName()));
                }
            }
        }
    }
}
