package com.infoepoch.pms.agent.common.utils;

import com.infoepoch.pms.agent.domain.baisc.user.User;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonTool {

    /**
     * 获取职位
     *
     * @param duty
     * @return
     */
    public static String getPosition(String duty) {
        if (StringUtils.isBlank(duty))
            return null;
        if (duty.contains("-")) {
            String[] arr = duty.split("-");
            return arr[arr.length - 1];
        } else {
            return duty;
        }
    }

    /**
     * 部门名称裁剪
     *
     * @param departmentName
     * @return
     */
    public static String departmentNameCutOut(String departmentName) {
        return departmentNameCutOut(departmentName, "江苏\\");
    }


    /**
     * 部门名称裁剪
     *
     * @param departmentName
     * @param cutOutStr
     * @return
     */
    public static String departmentNameCutOut(String departmentName, String cutOutStr) {
        return departmentNameCutOut(departmentName, cutOutStr, "");
    }

    /**
     * 部门名称裁剪
     *
     * @param departmentName
     * @param cutOutStr
     * @param defaultStr
     * @return
     */
    public static String departmentNameCutOut(String departmentName, String cutOutStr, String defaultStr) {
        if (StringUtils.isBlank(departmentName))
            return defaultStr;
        if (StringUtils.isBlank(cutOutStr))
            return departmentName;
        if (departmentName.startsWith(cutOutStr)) {
            return departmentName.substring(cutOutStr.length());
        }
        return departmentName.replace(cutOutStr, "");
    }

    /**
     * 过滤掉管理员
     *
     * @param userList
     */
    public static void adminFilter(List<User> userList) {
        if(CollectionUtils.isEmpty(userList))
            return;
        userList.removeIf(user -> StringUtils.isNotBlank(user.getUsername()) && user.getUsername().equals("admin"));
    }
}
