lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
src/main/java/com/whyc/util/CommonUtil.java
@@ -1,16 +1,157 @@
package com.whyc.util;
import com.whyc.pojo.User;
import com.whyc.constant.YamlProperties;
import com.whyc.pojo.UserInf;
import com.whyc.service.UserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
 * 通用工具列
 */
@Component
public class CommonUtil {
    /**获取当前Session中的属性user*/
    public static User getUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute("user");
    private static UserLogService userLogService;
    @Autowired
    public void setUserLogService(UserLogService userLogService) {
        CommonUtil.userLogService = userLogService;
    }
    /**获取当前Session中的属性user*/
    public static UserInf getUser(HttpServletRequest request) {
        return (UserInf) request.getSession().getAttribute("user");
    }
    public static String classesPath(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        return jarFile.toString();
    }
    public static String getRootFile(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        String baseDirPath;
        if(YamlProperties.runModel == 1) {
            //开发路径
            baseDirPath = jarFile.getParentFile().toString()+File.separator+"fg_file"+File.separator;
        }else {
            //打包路径
            baseDirPath = jarFile.toString()+File.separator+"fg_file"+File.separator;
        }
        return baseDirPath;
    }
    /**
     * 获取项目所在文件夹路径
     * @return 获取项目所在文件夹路径
     */
    public static String getProjectDir(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        String baseDirPath;
        if (YamlProperties.runModel == 1) {
            //开发路径
            baseDirPath = jarFile.getParentFile().toString();
        } else {
            //打包路径
            baseDirPath = jarFile.toString();
        }
        return baseDirPath;
    }
    /**
     * 手动记录特定日志
     */
    public static void record(long uId, int operationType, String msg, String msgEnUs) {
        userLogService.record(uId, operationType, msg, msgEnUs);
    }
    /**
     * 手动记录特定日志
     */
    public static void record(long uId, int operationType, String msg, String msgDetail, String msgEnUs, String msgDetailEnUs) {
        userLogService.record(uId, operationType, msg, msgDetail, msgEnUs, msgDetailEnUs);
    }
    /**
     * 手动记录特定日志
     */
    public static void record2(HttpServletRequest request, long uId, int operationType, String msg, String msgDetail, String msgEnUs, String msgDetailEnUs) {
        userLogService.record2(request, uId, operationType, msg, msgDetail, msgEnUs, msgDetailEnUs);
    }
    /**
     * 在分表的情况下使用,按年分表,查询需要切割查询使用
     * <p>
     * 根据传入的起止时间,按照年切割成不同时间段
     *
     * @param startTimeStr 2021-01-01 10:00:00
     * @param endTimeStr   2023-05-01 10:10:10
     * @return 示例[2021, 2022, 2023]
     * 使用方法:
     * 根据返回的第一个数据,如果存在表,则查询筛选条件为>=输入的起始时间;不存在则弃用输入的起始时间
     * 根据返回的最后个数据,如果表存在,则查询筛选条件为<=输入的终止时间;不存在则弃用输入的终止时间
     * 返回的非第一最后数据,查询全表
     */
    public static List<Integer> getYearList(String startTimeStr,String endTimeStr) throws ParseException {
        Integer startYear = Integer.valueOf(startTimeStr.split("-")[0]);
        Integer endYear = Integer.valueOf(endTimeStr.split("-")[0]);
        List<Integer> yearList = new LinkedList<>();
        while (startYear<=endYear){
            yearList.add(startYear);
            startYear++;
        }
        return yearList;
    }
    /**
     *
     * 在分表的情况下使用,按月分表,查询需要切割查询使用
     *
     * 根据传入的起止时间,按照月切割成不同时间段
     * @param startTimeStr 2022-11-01 10:00:00
     * @param endTimeStr 2023-05-01 10:10:10
     * @return  示例[2022_12,2023_1,2023_2,2023_3,2023_4,2023_5]
     * 使用方法:
     *   根据返回的第一个数据,如果存在表,则查询筛选条件为>=输入的起始时间;不存在则弃用输入的起始时间
     *   根据返回的最后个数据,如果表存在,则查询筛选条件为<=输入的终止时间;不存在则弃用输入的终止时间
     *   返回的非第一最后数据,查询全表
     */
    public static List<String> getMonthList(String startTimeStr,String endTimeStr) throws ParseException {
        Calendar startTimeCalendar = Calendar.getInstance();
        Date startTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(startTimeStr);
        startTimeCalendar.setTime(startTime);
        Calendar endTimeCalendar = Calendar.getInstance();
        Date endTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(endTimeStr);
        endTimeCalendar.setTime(endTime);
        /*String[] startTimeSplit = startTimeStr.split("-");
        int startYear = Integer.parseInt(startTimeSplit[0]);
        int startMonth = Integer.parseInt(startTimeSplit[1]);
        String[] endTimeSplit = endTimeStr.split("-");
        int endYear = Integer.parseInt(endTimeSplit[0]);
        int endMonth = Integer.parseInt(endTimeSplit[1]);*/
        List<String> yearMonthList = new LinkedList<>();
        while (!startTimeCalendar.after(endTimeCalendar)){ //起始时间大于终止时间则停止
            yearMonthList.add(startTimeCalendar.get(Calendar.YEAR)+"_"+(startTimeCalendar.get(Calendar.MONTH)+1));
            startTimeCalendar.add(Calendar.MONTH,1);
        }
        return yearMonthList;
    }
}