package com.jeeplus.common.utils;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class CommonUtils {

    /**
     * 判断文件名是否带盘符，重新处理
     *
     * @param fileName
     * @return
     */
    public static String getFileName(String fileName) {
        //判断是否带有盘符信息
        // Check for Unix-style path
        int unixSep = fileName.lastIndexOf ( '/' );
        // Check for Windows-style path
        int winSep = fileName.lastIndexOf ( '\\' );
        // Cut off at latest possible point
        int pos = (winSep > unixSep ? winSep : unixSep);
        if ( pos != -1 ) {
            // Any sort of path separator found...
            fileName = fileName.substring ( pos + 1 );
        }
        //替换上传文件名字的特殊字符
        fileName = fileName.replace ( "=", "" ).replace ( ",", "" ).replace ( "&", "" ).replace ( " ", "" );
        return fileName;
    }

    /**
     * 获取服务的根路径
     *
     * @return
     */
    public static String getContextPath() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes ( );
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest ( );
        ServletContext context = request.getSession ( ).getServletContext ( );
        return context.getRealPath ( "/" );
    }

    /**
     * 计算时间差
     * 注：参数小于或等于当前时间
     */
    public static String timeDef(Date time) {
        Long def = new Date().getTime() - time.getTime();
        // 计算天数
        long day = def / (1000 * 60 * 60 * 24);
        if (day != 0) {
            return day + "天前";
        }
        // 计算小时
        long house = def / (1000 * 60 * 60);
        if (house != 0) {
            return house + "小时前";
        }
        // 计算分钟
        long min = def / (1000 * 60);
        return min + "分钟前";
    }

}
