lxw
2023-05-25 7b7e2cb0a59221fd0e7f77a471bd22e42ece4911
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.whyc.util;
 
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 {
 
    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;
    }
 
}