package com.storlead.framework.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: sp-sales
 * @description: String工具类
 * @author: xiaojiawei
 * @create: 2022-09-29 11:24
 **/
public class StringUtil extends StringUtils {

    public static String trimSpace(String str, String space) {
        if (str.startsWith(space)) {
            str = str.substring(space.length());
        }
        if (str.endsWith(space)) {
            str = str.substring(0, str.length() - space.length());
        }
        return str;
    }

    public static String removePrefixAndSuffix(String str, String prefix, String suffix) {
        if (str.startsWith(prefix)) {
            str = str.substring(prefix.length());
        }
        if (str.endsWith(suffix)) {
            str = str.substring(0, str.length() - suffix.length());
        }
        return str;
    }

    /**
     * 判断两个字符串是否相等，除去html标签 两端空格和换行
     * @param s1 s1
     * @param s2 s2
     * @return: @return boolean
     * @author: xiajiawei
     * @date: 2022-09-29
     */
    public static  boolean ignoreHtmlSpaceEnterEquals(String s1,String s2){
        if(s1==null&&s2==null){
            return true;
        }
        if(s1 == null||s2==null){
            return false;
        }
        s1 = HtmlUtils.userContent(s1)
                .replace("/r", "")
                .replace("/n", "")
                .trim();
        s2 = HtmlUtils.userContent(s2)
                .replace("/r", "")
                .replace("/n", "")
                .trim();
        return Objects.equals(s1,s2);
    }

    public static List<Long> stringToLongList(String commaSeparatedString) {
        List<Long> longList = new ArrayList<>();
        if (commaSeparatedString != null && !commaSeparatedString.isEmpty()) {
            String[] strings = commaSeparatedString.split(",");
            for (String s : strings) {
                // 去除字符串两侧的空白字符，并尝试转换为Long
                Long number = Long.parseLong(s.trim());
                longList.add(number);
            }
        }
        return longList;
    }
}
