Просмотр исходного кода

20.本地自测"统计通道日级别高峰时段过检率接口",添加补齐日期逻辑

sunpanhu недель назад: 3
Родитель
Сommit
33583de39a

+ 70 - 51
airport-common/src/main/java/com/sundot/airport/common/utils/DateUtils.java

@@ -8,16 +8,20 @@ import java.time.LocalDateTime;
8
 import java.time.LocalTime;
8
 import java.time.LocalTime;
9
 import java.time.ZoneId;
9
 import java.time.ZoneId;
10
 import java.time.ZonedDateTime;
10
 import java.time.ZonedDateTime;
11
+import java.util.Calendar;
11
 import java.util.Date;
12
 import java.util.Date;
13
+import java.util.List;
14
+import java.util.Objects;
15
+
16
+import com.google.common.collect.Lists;
12
 import org.apache.commons.lang3.time.DateFormatUtils;
17
 import org.apache.commons.lang3.time.DateFormatUtils;
13
 
18
 
14
 /**
19
 /**
15
  * 时间工具类
20
  * 时间工具类
16
- * 
21
+ *
17
  * @author ruoyi
22
  * @author ruoyi
18
  */
23
  */
19
-public class DateUtils extends org.apache.commons.lang3.time.DateUtils
20
-{
24
+public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
21
     public static String YYYY = "yyyy";
25
     public static String YYYY = "yyyy";
22
 
26
 
23
     public static String YYYY_MM = "yyyy-MM";
27
     public static String YYYY_MM = "yyyy-MM";
@@ -29,63 +33,52 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
29
     public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
33
     public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
30
 
34
 
31
     private static String[] parsePatterns = {
35
     private static String[] parsePatterns = {
32
-            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
36
+            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
33
             "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
37
             "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
34
             "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
38
             "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
35
 
39
 
36
     /**
40
     /**
37
      * 获取当前Date型日期
41
      * 获取当前Date型日期
38
-     * 
42
+     *
39
      * @return Date() 当前日期
43
      * @return Date() 当前日期
40
      */
44
      */
41
-    public static Date getNowDate()
42
-    {
45
+    public static Date getNowDate() {
43
         return new Date();
46
         return new Date();
44
     }
47
     }
45
 
48
 
46
     /**
49
     /**
47
      * 获取当前日期, 默认格式为yyyy-MM-dd
50
      * 获取当前日期, 默认格式为yyyy-MM-dd
48
-     * 
51
+     *
49
      * @return String
52
      * @return String
50
      */
53
      */
51
-    public static String getDate()
52
-    {
54
+    public static String getDate() {
53
         return dateTimeNow(YYYY_MM_DD);
55
         return dateTimeNow(YYYY_MM_DD);
54
     }
56
     }
55
 
57
 
56
-    public static final String getTime()
57
-    {
58
+    public static final String getTime() {
58
         return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
59
         return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
59
     }
60
     }
60
 
61
 
61
-    public static final String dateTimeNow()
62
-    {
62
+    public static final String dateTimeNow() {
63
         return dateTimeNow(YYYYMMDDHHMMSS);
63
         return dateTimeNow(YYYYMMDDHHMMSS);
64
     }
64
     }
65
 
65
 
66
-    public static final String dateTimeNow(final String format)
67
-    {
66
+    public static final String dateTimeNow(final String format) {
68
         return parseDateToStr(format, new Date());
67
         return parseDateToStr(format, new Date());
69
     }
68
     }
70
 
69
 
71
-    public static final String dateTime(final Date date)
72
-    {
70
+    public static final String dateTime(final Date date) {
73
         return parseDateToStr(YYYY_MM_DD, date);
71
         return parseDateToStr(YYYY_MM_DD, date);
74
     }
72
     }
75
 
73
 
76
-    public static final String parseDateToStr(final String format, final Date date)
77
-    {
74
+    public static final String parseDateToStr(final String format, final Date date) {
78
         return new SimpleDateFormat(format).format(date);
75
         return new SimpleDateFormat(format).format(date);
79
     }
76
     }
80
 
77
 
81
-    public static final Date dateTime(final String format, final String ts)
82
-    {
83
-        try
84
-        {
78
+    public static final Date dateTime(final String format, final String ts) {
79
+        try {
85
             return new SimpleDateFormat(format).parse(ts);
80
             return new SimpleDateFormat(format).parse(ts);
86
-        }
87
-        catch (ParseException e)
88
-        {
81
+        } catch (ParseException e) {
89
             throw new RuntimeException(e);
82
             throw new RuntimeException(e);
90
         }
83
         }
91
     }
84
     }
@@ -93,8 +86,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
93
     /**
86
     /**
94
      * 日期路径 即年/月/日 如2018/08/08
87
      * 日期路径 即年/月/日 如2018/08/08
95
      */
88
      */
96
-    public static final String datePath()
97
-    {
89
+    public static final String datePath() {
98
         Date now = new Date();
90
         Date now = new Date();
99
         return DateFormatUtils.format(now, "yyyy/MM/dd");
91
         return DateFormatUtils.format(now, "yyyy/MM/dd");
100
     }
92
     }
@@ -102,8 +94,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
102
     /**
94
     /**
103
      * 日期路径 即年/月/日 如20180808
95
      * 日期路径 即年/月/日 如20180808
104
      */
96
      */
105
-    public static final String dateTime()
106
-    {
97
+    public static final String dateTime() {
107
         Date now = new Date();
98
         Date now = new Date();
108
         return DateFormatUtils.format(now, "yyyyMMdd");
99
         return DateFormatUtils.format(now, "yyyyMMdd");
109
     }
100
     }
@@ -111,18 +102,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
111
     /**
102
     /**
112
      * 日期型字符串转化为日期 格式
103
      * 日期型字符串转化为日期 格式
113
      */
104
      */
114
-    public static Date parseDate(Object str)
115
-    {
116
-        if (str == null)
117
-        {
105
+    public static Date parseDate(Object str) {
106
+        if (str == null) {
118
             return null;
107
             return null;
119
         }
108
         }
120
-        try
121
-        {
109
+        try {
122
             return parseDate(str.toString(), parsePatterns);
110
             return parseDate(str.toString(), parsePatterns);
123
-        }
124
-        catch (ParseException e)
125
-        {
111
+        } catch (ParseException e) {
126
             return null;
112
             return null;
127
         }
113
         }
128
     }
114
     }
@@ -130,8 +116,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
130
     /**
116
     /**
131
      * 获取服务器启动时间
117
      * 获取服务器启动时间
132
      */
118
      */
133
-    public static Date getServerStartDate()
134
-    {
119
+    public static Date getServerStartDate() {
135
         long time = ManagementFactory.getRuntimeMXBean().getStartTime();
120
         long time = ManagementFactory.getRuntimeMXBean().getStartTime();
136
         return new Date(time);
121
         return new Date(time);
137
     }
122
     }
@@ -139,20 +124,18 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
139
     /**
124
     /**
140
      * 计算相差天数
125
      * 计算相差天数
141
      */
126
      */
142
-    public static int differentDaysByMillisecond(Date date1, Date date2)
143
-    {
127
+    public static int differentDaysByMillisecond(Date date1, Date date2) {
144
         return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
128
         return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
145
     }
129
     }
146
 
130
 
147
     /**
131
     /**
148
      * 计算时间差
132
      * 计算时间差
149
      *
133
      *
150
-     * @param endDate 最后时间
134
+     * @param endDate   最后时间
151
      * @param startTime 开始时间
135
      * @param startTime 开始时间
152
      * @return 时间差(天/小时/分钟)
136
      * @return 时间差(天/小时/分钟)
153
      */
137
      */
154
-    public static String timeDistance(Date endDate, Date startTime)
155
-    {
138
+    public static String timeDistance(Date endDate, Date startTime) {
156
         long nd = 1000 * 24 * 60 * 60;
139
         long nd = 1000 * 24 * 60 * 60;
157
         long nh = 1000 * 60 * 60;
140
         long nh = 1000 * 60 * 60;
158
         long nm = 1000 * 60;
141
         long nm = 1000 * 60;
@@ -173,8 +156,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
173
     /**
156
     /**
174
      * 增加 LocalDateTime ==> Date
157
      * 增加 LocalDateTime ==> Date
175
      */
158
      */
176
-    public static Date toDate(LocalDateTime temporalAccessor)
177
-    {
159
+    public static Date toDate(LocalDateTime temporalAccessor) {
178
         ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
160
         ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
179
         return Date.from(zdt.toInstant());
161
         return Date.from(zdt.toInstant());
180
     }
162
     }
@@ -182,10 +164,47 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
182
     /**
164
     /**
183
      * 增加 LocalDate ==> Date
165
      * 增加 LocalDate ==> Date
184
      */
166
      */
185
-    public static Date toDate(LocalDate temporalAccessor)
186
-    {
167
+    public static Date toDate(LocalDate temporalAccessor) {
187
         LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
168
         LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
188
         ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
169
         ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
189
         return Date.from(zdt.toInstant());
170
         return Date.from(zdt.toInstant());
190
     }
171
     }
172
+
173
+    /**
174
+     * 生成近30天的日期列表(包含今天)
175
+     */
176
+    public static List<Date> generateLast30Days() {
177
+        List<Date> dates = Lists.newArrayList();
178
+        Calendar calendar = Calendar.getInstance();
179
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
180
+        calendar.set(Calendar.MINUTE, 0);
181
+        calendar.set(Calendar.SECOND, 0);
182
+        calendar.set(Calendar.MILLISECOND, 0);
183
+
184
+        for (int i = 29; i >= 0; i--) {
185
+            Calendar cal = (Calendar) calendar.clone();
186
+            cal.add(Calendar.DAY_OF_MONTH, -i);
187
+            dates.add(cal.getTime());
188
+        }
189
+        return dates;
190
+    }
191
+
192
+    /**
193
+     * 格式化日期为字符串键值(yyyy-MM-dd)
194
+     */
195
+    public static String formatDateKey(Date date) {
196
+        if (Objects.isNull(date)) {
197
+            return "";
198
+        }
199
+        Calendar cal = Calendar.getInstance();
200
+        cal.setTime(date);
201
+        cal.set(Calendar.HOUR_OF_DAY, 0);
202
+        cal.set(Calendar.MINUTE, 0);
203
+        cal.set(Calendar.SECOND, 0);
204
+        cal.set(Calendar.MILLISECOND, 0);
205
+        return String.format("%04d-%02d-%02d",
206
+                cal.get(Calendar.YEAR),
207
+                cal.get(Calendar.MONTH) + 1,
208
+                cal.get(Calendar.DAY_OF_MONTH));
209
+    }
191
 }
210
 }

+ 13 - 2
airport-ledger/src/main/java/com/sundot/airport/ledger/domain/vo/LaneThroughputResVO.java

@@ -1,9 +1,11 @@
1
 package com.sundot.airport.ledger.domain.vo;
1
 package com.sundot.airport.ledger.domain.vo;
2
 
2
 
3
     import com.fasterxml.jackson.annotation.JsonFormat;
3
     import com.fasterxml.jackson.annotation.JsonFormat;
4
-import lombok.Data;
4
+    import lombok.AllArgsConstructor;
5
+    import lombok.Data;
6
+    import lombok.NoArgsConstructor;
5
 
7
 
6
-import java.io.Serializable;
8
+    import java.io.Serializable;
7
 import java.math.BigDecimal;
9
 import java.math.BigDecimal;
8
 import java.util.Date;
10
 import java.util.Date;
9
 
11
 
@@ -14,6 +16,8 @@ import java.util.Date;
14
  * @date 2026/5/19 15:27
16
  * @date 2026/5/19 15:27
15
  */
17
  */
16
 @Data
18
 @Data
19
+@NoArgsConstructor
20
+@AllArgsConstructor
17
 public class LaneThroughputResVO implements Serializable {
21
 public class LaneThroughputResVO implements Serializable {
18
     /**
22
     /**
19
      * 日期
23
      * 日期
@@ -40,4 +44,11 @@ public class LaneThroughputResVO implements Serializable {
40
      * 过检率(保留1位小数)
44
      * 过检率(保留1位小数)
41
      */
45
      */
42
     private BigDecimal throughputRate;
46
     private BigDecimal throughputRate;
47
+
48
+    public LaneThroughputResVO(Date date) {
49
+        this.recordDate = date;
50
+        this.totalThroughput = 0;
51
+        this.totalRecord = 0;
52
+        this.throughputRate = java.math.BigDecimal.ZERO;
53
+    }
43
 }
54
 }

+ 62 - 5
airport-ledger/src/main/java/com/sundot/airport/ledger/service/impl/OperationLanePeakThroughputServiceImpl.java

@@ -1,24 +1,31 @@
1
 package com.sundot.airport.ledger.service.impl;
1
 package com.sundot.airport.ledger.service.impl;
2
 
2
 
3
-import java.util.Collections;
4
-import java.util.List;
5
-import java.util.Objects;
6
-
7
 import cn.hutool.core.collection.CollUtil;
3
 import cn.hutool.core.collection.CollUtil;
8
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5
+import com.google.common.collect.Lists;
9
 import com.sundot.airport.common.utils.DateUtils;
6
 import com.sundot.airport.common.utils.DateUtils;
10
 import com.sundot.airport.ledger.domain.OperationLanePeakThroughput;
7
 import com.sundot.airport.ledger.domain.OperationLanePeakThroughput;
11
 import com.sundot.airport.ledger.domain.vo.CountQueryReqVO;
8
 import com.sundot.airport.ledger.domain.vo.CountQueryReqVO;
12
 import com.sundot.airport.ledger.domain.vo.LaneThroughputResVO;
9
 import com.sundot.airport.ledger.domain.vo.LaneThroughputResVO;
13
 import com.sundot.airport.ledger.mapper.OperationLanePeakThroughputMapper;
10
 import com.sundot.airport.ledger.mapper.OperationLanePeakThroughputMapper;
14
 import com.sundot.airport.ledger.service.IOperationLanePeakThroughputService;
11
 import com.sundot.airport.ledger.service.IOperationLanePeakThroughputService;
12
+import lombok.extern.slf4j.Slf4j;
15
 import org.springframework.beans.factory.annotation.Autowired;
13
 import org.springframework.beans.factory.annotation.Autowired;
16
 import org.springframework.stereotype.Service;
14
 import org.springframework.stereotype.Service;
17
 import org.springframework.transaction.annotation.Transactional;
15
 import org.springframework.transaction.annotation.Transactional;
18
 
16
 
17
+import java.util.Collections;
18
+import java.util.Comparator;
19
+import java.util.Date;
20
+import java.util.List;
21
+import java.util.Map;
22
+import java.util.Objects;
23
+import java.util.stream.Collectors;
24
+
19
 /**
25
 /**
20
  * 通道日别高峰时段过检Service业务层处理
26
  * 通道日别高峰时段过检Service业务层处理
21
  */
27
  */
28
+@Slf4j
22
 @Service
29
 @Service
23
 public class OperationLanePeakThroughputServiceImpl extends ServiceImpl<OperationLanePeakThroughputMapper, OperationLanePeakThroughput>
30
 public class OperationLanePeakThroughputServiceImpl extends ServiceImpl<OperationLanePeakThroughputMapper, OperationLanePeakThroughput>
24
         implements IOperationLanePeakThroughputService {
31
         implements IOperationLanePeakThroughputService {
@@ -102,7 +109,57 @@ public class OperationLanePeakThroughputServiceImpl extends ServiceImpl<Operatio
102
         if (Objects.isNull(laneThroughputQuery.getDeptId()) && Objects.isNull(laneThroughputQuery.getTeamId()) && Objects.isNull(laneThroughputQuery.getGroupId())) {
109
         if (Objects.isNull(laneThroughputQuery.getDeptId()) && Objects.isNull(laneThroughputQuery.getTeamId()) && Objects.isNull(laneThroughputQuery.getGroupId())) {
103
             return Collections.emptyList();
110
             return Collections.emptyList();
104
         }
111
         }
112
+        
113
+        // 查询数据库中存在的日期数据
105
         List<LaneThroughputResVO> laneThroughputResList = this.baseMapper.countLanePeakThroughput(laneThroughputQuery);
114
         List<LaneThroughputResVO> laneThroughputResList = this.baseMapper.countLanePeakThroughput(laneThroughputQuery);
106
-        return CollUtil.emptyIfNull(laneThroughputResList);
115
+        laneThroughputResList = CollUtil.emptyIfNull(laneThroughputResList);
116
+        
117
+        log.info("SQL查询返回数据条数: {}", laneThroughputResList.size());
118
+        
119
+        // 生成近30天的日期列表
120
+        List<Date> last30Days = DateUtils.generateLast30Days();
121
+        
122
+        // 将查询结果按日期建立映射(只比较日期部分,忽略时间)
123
+        Map<String, LaneThroughputResVO> dataMap = laneThroughputResList.stream()
124
+                .collect(Collectors.toMap(
125
+                        vo -> DateUtils.formatDateKey(vo.getRecordDate()),
126
+                        vo -> vo,
127
+                        (existing, replacement) -> existing
128
+                ));
129
+        
130
+        log.info("数据Map中的日期键: {}", dataMap.keySet());
131
+        
132
+        // 补齐缺失的日期数据
133
+        List<LaneThroughputResVO> resultList = Lists.newArrayList();
134
+        for (Date date : last30Days) {
135
+            String dateKey = DateUtils.formatDateKey(date);
136
+            if (dataMap.containsKey(dateKey)) {
137
+                // 存在的日期,使用查询结果
138
+                resultList.add(dataMap.get(dateKey));
139
+            } else {
140
+                // 缺失的日期,补充0数据
141
+                LaneThroughputResVO emptyVO = this.createEmptyVO(date, laneThroughputResList);
142
+                resultList.add(emptyVO);
143
+            }
144
+        }
145
+        
146
+        log.info("补齐后返回的总数据条数: {}", resultList.size());
147
+        
148
+        // 按日期升序排序
149
+        resultList.sort(Comparator.comparing(LaneThroughputResVO::getRecordDate));
150
+        
151
+        return resultList;
152
+    }
153
+    
154
+    /**
155
+     * 创建空的VO对象
156
+     */
157
+    private LaneThroughputResVO createEmptyVO(Date date, List<LaneThroughputResVO> existingList) {
158
+        LaneThroughputResVO emptyVO = new LaneThroughputResVO(date);
159
+        // 从已有数据中获取groupName(如果有的话)
160
+        if (CollUtil.isNotEmpty(existingList)) {
161
+            emptyVO.setGroupName(existingList.get(0).getGroupName());
162
+        }
163
+        return emptyVO;
107
     }
164
     }
108
 }
165
 }