Pārlūkot izejas kodu

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

sunpanhu 3 nedēļas atpakaļ
vecāks
revīzija
33583de39a

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

@@ -8,16 +8,20 @@ import java.time.LocalDateTime;
8 8
 import java.time.LocalTime;
9 9
 import java.time.ZoneId;
10 10
 import java.time.ZonedDateTime;
11
+import java.util.Calendar;
11 12
 import java.util.Date;
13
+import java.util.List;
14
+import java.util.Objects;
15
+
16
+import com.google.common.collect.Lists;
12 17
 import org.apache.commons.lang3.time.DateFormatUtils;
13 18
 
14 19
 /**
15 20
  * 时间工具类
16
- * 
21
+ *
17 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 25
     public static String YYYY = "yyyy";
22 26
 
23 27
     public static String YYYY_MM = "yyyy-MM";
@@ -29,63 +33,52 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
29 33
     public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
30 34
 
31 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 37
             "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
34 38
             "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
35 39
 
36 40
     /**
37 41
      * 获取当前Date型日期
38
-     * 
42
+     *
39 43
      * @return Date() 当前日期
40 44
      */
41
-    public static Date getNowDate()
42
-    {
45
+    public static Date getNowDate() {
43 46
         return new Date();
44 47
     }
45 48
 
46 49
     /**
47 50
      * 获取当前日期, 默认格式为yyyy-MM-dd
48
-     * 
51
+     *
49 52
      * @return String
50 53
      */
51
-    public static String getDate()
52
-    {
54
+    public static String getDate() {
53 55
         return dateTimeNow(YYYY_MM_DD);
54 56
     }
55 57
 
56
-    public static final String getTime()
57
-    {
58
+    public static final String getTime() {
58 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 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 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 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 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 80
             return new SimpleDateFormat(format).parse(ts);
86
-        }
87
-        catch (ParseException e)
88
-        {
81
+        } catch (ParseException e) {
89 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 87
      * 日期路径 即年/月/日 如2018/08/08
95 88
      */
96
-    public static final String datePath()
97
-    {
89
+    public static final String datePath() {
98 90
         Date now = new Date();
99 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 95
      * 日期路径 即年/月/日 如20180808
104 96
      */
105
-    public static final String dateTime()
106
-    {
97
+    public static final String dateTime() {
107 98
         Date now = new Date();
108 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 107
             return null;
119 108
         }
120
-        try
121
-        {
109
+        try {
122 110
             return parseDate(str.toString(), parsePatterns);
123
-        }
124
-        catch (ParseException e)
125
-        {
111
+        } catch (ParseException e) {
126 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 120
         long time = ManagementFactory.getRuntimeMXBean().getStartTime();
136 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 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 135
      * @param startTime 开始时间
152 136
      * @return 时间差(天/小时/分钟)
153 137
      */
154
-    public static String timeDistance(Date endDate, Date startTime)
155
-    {
138
+    public static String timeDistance(Date endDate, Date startTime) {
156 139
         long nd = 1000 * 24 * 60 * 60;
157 140
         long nh = 1000 * 60 * 60;
158 141
         long nm = 1000 * 60;
@@ -173,8 +156,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
173 156
     /**
174 157
      * 增加 LocalDateTime ==> Date
175 158
      */
176
-    public static Date toDate(LocalDateTime temporalAccessor)
177
-    {
159
+    public static Date toDate(LocalDateTime temporalAccessor) {
178 160
         ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
179 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 165
      * 增加 LocalDate ==> Date
184 166
      */
185
-    public static Date toDate(LocalDate temporalAccessor)
186
-    {
167
+    public static Date toDate(LocalDate temporalAccessor) {
187 168
         LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
188 169
         ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
189 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 1
 package com.sundot.airport.ledger.domain.vo;
2 2
 
3 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 9
 import java.math.BigDecimal;
8 10
 import java.util.Date;
9 11
 
@@ -14,6 +16,8 @@ import java.util.Date;
14 16
  * @date 2026/5/19 15:27
15 17
  */
16 18
 @Data
19
+@NoArgsConstructor
20
+@AllArgsConstructor
17 21
 public class LaneThroughputResVO implements Serializable {
18 22
     /**
19 23
      * 日期
@@ -40,4 +44,11 @@ public class LaneThroughputResVO implements Serializable {
40 44
      * 过检率(保留1位小数)
41 45
      */
42 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 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 3
 import cn.hutool.core.collection.CollUtil;
8 4
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5
+import com.google.common.collect.Lists;
9 6
 import com.sundot.airport.common.utils.DateUtils;
10 7
 import com.sundot.airport.ledger.domain.OperationLanePeakThroughput;
11 8
 import com.sundot.airport.ledger.domain.vo.CountQueryReqVO;
12 9
 import com.sundot.airport.ledger.domain.vo.LaneThroughputResVO;
13 10
 import com.sundot.airport.ledger.mapper.OperationLanePeakThroughputMapper;
14 11
 import com.sundot.airport.ledger.service.IOperationLanePeakThroughputService;
12
+import lombok.extern.slf4j.Slf4j;
15 13
 import org.springframework.beans.factory.annotation.Autowired;
16 14
 import org.springframework.stereotype.Service;
17 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 26
  * 通道日别高峰时段过检Service业务层处理
21 27
  */
28
+@Slf4j
22 29
 @Service
23 30
 public class OperationLanePeakThroughputServiceImpl extends ServiceImpl<OperationLanePeakThroughputMapper, OperationLanePeakThroughput>
24 31
         implements IOperationLanePeakThroughputService {
@@ -102,7 +109,57 @@ public class OperationLanePeakThroughputServiceImpl extends ServiceImpl<Operatio
102 109
         if (Objects.isNull(laneThroughputQuery.getDeptId()) && Objects.isNull(laneThroughputQuery.getTeamId()) && Objects.isNull(laneThroughputQuery.getGroupId())) {
103 110
             return Collections.emptyList();
104 111
         }
112
+        
113
+        // 查询数据库中存在的日期数据
105 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
 }