yxh
8 天以前 edbfa6b1a3a45e79cb17b06f3798535c3b1ba395
ruoyi-project/src/main/java/com/ruoyi/project/common/AttachmentNameValidator.java
@@ -8,14 +8,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * 报销单附件文件名规范校验(2026-10-15)。
 * 报销单附件文件名规范校验(2026-11-03 调整)。
 *
 * <p>业务口径(业务方确认):
 * <p>业务口径:
 * <ol>
 *   <li>必须包含中文(至少 1 个汉字)——"为方便查验附件,请用中文规范命名附件!"</li>
 *   <li>去掉扩展名后的主干,按字符数计算不能超过 20 个字——"为方便查验附件,附件名字长度不能超过20个字!"</li>
 *   <li>允许文件名不含中文;含中文/纯英文/数字均可</li>
 *   <li>去掉扩展名后的主干,按字符数计算不能超过 20 个字</li>
 *   <li>不能包含空格及 {@code / \ : * ? " < > | # %} 等特殊字符</li>
 *   <li>同一明细行内不能重名(比较时忽略扩展名大小写与首尾空格)</li>
 *   <li>只校验新上传/新保存的附件;历史附件由 existingNames 放行</li>
 * </ol>
 *
 * <p>与前端 {@code opo-web/src/utils/fileNameCheck.js} <b>同口径</b>:
@@ -34,18 +35,21 @@
    /** 主干(去扩展名)最大字符数 */
    public static final int MAX_MAIN_LENGTH = 20;
    public static final String MSG_NO_CHINESE = "为方便查验附件,请用中文规范命名附件!";
    public static final String MSG_EMPTY_NAME = "为方便查验附件,附件名不能为空!";
    public static final String MSG_TOO_LONG = "为方便查验附件,附件名字长度不能超过20个字!";
    public static final String MSG_ILLEGAL = "为方便查验附件,附件名不能包含空格及 / \\ : * ? \" < > | # % 等特殊字符!";
    public static final String MSG_DUPLICATE = "为方便查验附件,同一行内不能有重名的附件!";
    /** 2026-11-17:非专家费/伦理专家费的报销单,附件名必须含中文 */
    public static final String MSG_NEED_CHINESE =
            "附件名不符合医院报销单审批要求:名称须反映发票主要内容(需包含中文),如\"2026年10月住院费\"";
    /** 附件项里取不到任何文件名(既没有 name 也没有 fileName)时的提示 */
    public static final String MSG_NO_NAME = "为方便查验附件,附件必须带文件名,请重新上传!";
    /** 汉字 */
    private static final Pattern CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]");
    /** 非法字符:空格(含各类空白)+ / \ : * ? " < > | # % */
    private static final Pattern ILLEGAL = Pattern.compile("[\\s/\\\\:*?\"<>|#%]");
    /** 中文匹配(主干是否含中文) */
    private static final Pattern CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]");
    private static final ObjectMapper MAPPER = new ObjectMapper();
@@ -62,22 +66,34 @@
    }
    /**
     * 校验单个文件名。
     * 校验单个文件名(不要求含中文,兼容旧调用)。
     *
     * @param fileName 原始文件名
     * @return {@code null}=通过;否则返回不合格原因
     */
    public static String checkName(String fileName) {
        return checkName(fileName, false);
    }
    /**
     * 校验单个文件名。
     *
     * @param fileName       原始文件名
     * @param requireChinese {@code true}=主干必须含中文(非专家费/伦理专家费的报销单)
     * @return {@code null}=通过;否则返回不合格原因
     */
    public static String checkName(String fileName, boolean requireChinese) {
        String name = fileName == null ? "" : fileName.trim();
        if (name.isEmpty()) {
            return MSG_NO_CHINESE;
            return MSG_EMPTY_NAME;
        }
        if (ILLEGAL.matcher(name).find()) {
            return MSG_ILLEGAL;
        }
        String main = mainName(name);
        if (!CHINESE.matcher(main).find()) {
            return MSG_NO_CHINESE;
        // 2026-11-17:非专家费/伦理专家费的报销单,附件名主干必须含中文
        if (requireChinese && !CHINESE.matcher(main).find()) {
            return MSG_NEED_CHINESE;
        }
        // 按字符数计(码点计数:中文/字母/数字/全角各计 1)
        if (main.codePointCount(0, main.length()) > MAX_MAIN_LENGTH) {
@@ -93,7 +109,18 @@
     * @return {@code null}=通过;否则返回不合格原因(含文件名)
     */
    public static String checkJson(String json) {
        return checkJson(json, null, true);
        return checkJson(json, null, true, false);
    }
    /**
     * 校验一个附件列表 JSON(可指定是否必须含中文)。
     *
     * @param json           附件字段内容(annexfiles / invoicefiles),可为空
     * @param requireChinese {@code true}=文件名主干必须含中文
     * @return {@code null}=通过;否则返回不合格原因(含文件名)
     */
    public static String checkJson(String json, boolean requireChinese) {
        return checkJson(json, null, true, requireChinese);
    }
    /**
@@ -106,7 +133,14 @@
     *                      需为小写去空白形式;为空/为 null 表示按全新单据处理)
     */
    public static String checkJsonForUpdate(String json, Set<String> existingNames) {
        return checkJson(json, existingNames, false);
        return checkJson(json, existingNames, false, false);
    }
    /**
     * 修改保存时使用(可指定是否必须含中文),忽略"保存前已存在"的历史附件名。
     */
    public static String checkJsonForUpdate(String json, Set<String> existingNames, boolean requireChinese) {
        return checkJson(json, existingNames, false, requireChinese);
    }
    /**
@@ -139,8 +173,10 @@
    /**
     * @param existingNames 历史附件名(小写),命中则跳过校验;{@code null} 表示不忽略任何名字
     * @param strictNoName  {@code true}=取不到文件名时拦截(全新保存);{@code false}=放行(修改历史单据)
     * @param requireChinese {@code true}=文件名主干必须含中文(非专家费/伦理专家费的报销单)
     */
    private static String checkJson(String json, Set<String> existingNames, boolean strictNoName) {
    private static String checkJson(String json, Set<String> existingNames, boolean strictNoName,
                                    boolean requireChinese) {
        if (json == null || json.trim().isEmpty()) {
            return null;
        }
@@ -179,7 +215,7 @@
                // 历史附件(保存前已存在)不参与校验:业务确认"只校验本次变更的附件",避免卡住老单据
                continue;
            }
            String reason = checkName(name);
            String reason = checkName(name, requireChinese);
            if (reason != null) {
                return "【" + name + "】" + reason;
            }
@@ -218,11 +254,24 @@
     * @param jsons 附件字段内容,可为 null/空
     * @return {@code null}=通过;否则返回不合格原因
     */
    public static String checkJsonList(String... jsons) {        if (jsons == null) {
    public static String checkJsonList(String... jsons) {
        return checkJsonList(false, jsons);
    }
    /**
     * 校验同一明细行下的多个附件列表(可指定是否必须含中文),逐个列表独立校验:
     * 每个列表内部查重名(与前端 checkDetails 的口径一致)。
     *
     * @param requireChinese {@code true}=文件名主干必须含中文
     * @param jsons          附件字段内容,可为 null/空
     * @return {@code null}=通过;否则返回不合格原因
     */
    public static String checkJsonList(boolean requireChinese, String... jsons) {
        if (jsons == null) {
            return null;
        }
        for (String json : jsons) {
            String reason = checkJson(json);
            String reason = checkJson(json, requireChinese);
            if (reason != null) {
                return reason;
            }
@@ -232,11 +281,18 @@
    /** 修改保存时使用:忽略历史附件名,逐个列表校验(供"多个字段一起校验"的调用点) */
    public static String checkJsonListForUpdate(Set<String> existingNames, String... jsons) {
        return checkJsonListForUpdate(existingNames, false, jsons);
    }
    /**
     * 修改保存时使用(可指定是否必须含中文):忽略历史附件名,逐个列表校验。
     */
    public static String checkJsonListForUpdate(Set<String> existingNames, boolean requireChinese, String... jsons) {
        if (jsons == null) {
            return null;
        }
        for (String json : jsons) {
            String reason = checkJsonForUpdate(json, existingNames);
            String reason = checkJsonForUpdate(json, existingNames, requireChinese);
            if (reason != null) {
                return reason;
            }