longz
2026-06-29 9d4cbff7464ebfd7803c2ce53ded49d0f73fe011
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.sinata.appointment.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinata.appointment.constants.ApprovalInstanceStatus;
import com.sinata.appointment.constants.MiniUserType;
import com.sinata.appointment.dto.DashboardAppointmentStatsVo;
import com.sinata.appointment.dto.DashboardDailyTotalVo;
import com.sinata.appointment.dto.DashboardOverviewVo;
import com.sinata.appointment.dto.DashboardTrendPointVo;
import com.sinata.appointment.entity.AppointmentOrder;
import com.sinata.appointment.entity.ApprovalInstance;
import com.sinata.appointment.entity.MpUser;
import com.sinata.appointment.entity.SysDept;
import com.sinata.appointment.mapper.AppointmentOrderMapper;
import com.sinata.appointment.mapper.ApprovalInstanceMapper;
import com.sinata.appointment.mapper.MpUserMapper;
import com.sinata.appointment.mapper.SysDeptMapper;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
@Service
@RequiredArgsConstructor
public class DashboardApplicationService {
 
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;
    private static final DateTimeFormatter LABEL_FORMATTER = DateTimeFormatter.ofPattern("M-d");
 
    private final AppointmentOrderMapper appointmentOrderMapper;
    private final ApprovalInstanceMapper approvalInstanceMapper;
    private final MpUserMapper mpUserMapper;
    private final SysDeptMapper sysDeptMapper;
 
    public DashboardOverviewVo getOverview(LocalDate startDate, LocalDate endDate) {
        DateRange range = resolveRange(startDate, endDate);
        LocalDate dailyDate = resolveDailyDate(range);
 
        DashboardOverviewVo vo = new DashboardOverviewVo();
        vo.setStartDate(format(range.startDate()));
        vo.setEndDate(format(range.endDate()));
        vo.setDailyDate(format(dailyDate));
        vo.setDailyAppointmentCount(countAppointments(dailyDate, dailyDate, null));
        vo.setTotalAppointmentCount(countAppointments(range.startDate(), range.endDate(), null));
        vo.setRegisteredUserCount(countRegisteredUsers(range.startDate(), range.endDate()));
        vo.setEmployeeCount(countEmployees(range.startDate(), range.endDate()));
        return vo;
    }
 
    public DashboardAppointmentStatsVo getAppointmentStats(
            LocalDate startDate, LocalDate endDate, Long visitedDeptId) {
        DateRange range = resolveRange(startDate, endDate);
        DashboardAppointmentStatsVo vo = new DashboardAppointmentStatsVo();
        vo.setStartDate(format(range.startDate()));
        vo.setEndDate(format(range.endDate()));
        vo.setVisitedDeptId(visitedDeptId);
        vo.setAppointmentCount(countAppointments(range.startDate(), range.endDate(), visitedDeptId));
        vo.setInProgressCount(
                countApprovalInstancesByCreateTime(
                        range.startDate(),
                        range.endDate(),
                        visitedDeptId,
                        ApprovalInstanceStatus.IN_PROGRESS));
        vo.setRejectedCount(
                countApprovalInstancesByCreateTime(
                        range.startDate(),
                        range.endDate(),
                        visitedDeptId,
                        ApprovalInstanceStatus.REJECTED));
        vo.setCancelledCount(
                countApprovalInstancesByCreateTime(
                        range.startDate(),
                        range.endDate(),
                        visitedDeptId,
                        ApprovalInstanceStatus.CANCELLED));
        vo.setTrend(buildTrend(range, visitedDeptId));
        return vo;
    }
 
    private List<DashboardTrendPointVo> buildTrend(DateRange range, Long visitedDeptId) {
        List<Long> deptIds = resolveDeptIds(visitedDeptId);
        List<DashboardTrendPointVo> orderRows =
                appointmentOrderMapper.listDashboardTrend(range.startDate(), range.endDate(), deptIds);
        Map<LocalDate, DashboardTrendPointVo> orderRowMap = new LinkedHashMap<>();
        if (orderRows != null) {
            for (DashboardTrendPointVo row : orderRows) {
                if (row == null || row.getStatDate() == null) {
                    continue;
                }
                orderRowMap.put(row.getStatDate(), row);
            }
        }
        Map<LocalDate, Long> inProgressMap = buildInProgressTrendMap(range, deptIds);
        Map<LocalDate, Long> rejectedMap =
                buildFinishedTrendMap(range, deptIds, ApprovalInstanceStatus.REJECTED);
        Map<LocalDate, Long> cancelledMap =
                buildFinishedTrendMap(range, deptIds, ApprovalInstanceStatus.CANCELLED);
 
        List<DashboardTrendPointVo> result = new ArrayList<>();
        LocalDate cursor = range.startDate();
        while (!cursor.isAfter(range.endDate())) {
            DashboardTrendPointVo row = orderRowMap.get(cursor);
            DashboardTrendPointVo point = new DashboardTrendPointVo();
            point.setStatDate(cursor);
            point.setLabel(cursor.format(LABEL_FORMATTER));
            point.setAppointmentCount(row == null ? 0L : defaultLong(row.getAppointmentCount()));
            point.setInProgressCount(inProgressMap.getOrDefault(cursor, 0L));
            point.setRejectedCount(rejectedMap.getOrDefault(cursor, 0L));
            point.setCancelledCount(cancelledMap.getOrDefault(cursor, 0L));
            result.add(point);
            cursor = cursor.plusDays(1);
        }
        return result;
    }
 
    private long countAppointments(LocalDate startDate, LocalDate endDate, Long visitedDeptId) {
        return appointmentOrderMapper.selectCount(
                appointmentRangeWrapper(startDate, endDate, resolveDeptIds(visitedDeptId), null));
    }
 
    private LambdaQueryWrapper<AppointmentOrder> appointmentRangeWrapper(
            LocalDate startDate, LocalDate endDate, List<Long> deptIds, String status) {
        LambdaQueryWrapper<AppointmentOrder> wrapper =
                new LambdaQueryWrapper<AppointmentOrder>()
                        .eq(AppointmentOrder::getDelFlag, 0)
                        .ge(AppointmentOrder::getCreateTime, atStartOfDay(startDate))
                        .lt(AppointmentOrder::getCreateTime, atStartOfNextDay(endDate));
        if (deptIds != null && !deptIds.isEmpty()) {
            wrapper.in(AppointmentOrder::getVisitedDeptId, deptIds);
        }
        if (status != null && !status.isBlank()) {
            wrapper.eq(AppointmentOrder::getStatus, status);
        }
        return wrapper;
    }
 
    private long countApprovalInstancesByCreateTime(
            LocalDate startDate, LocalDate endDate, Long visitedDeptId, String status) {
        Long count =
                approvalInstanceMapper.countDashboardByCreateTime(
                        startDate, endDate, resolveDeptIds(visitedDeptId), status);
        return defaultLong(count);
    }
 
    private Map<LocalDate, Long> buildFinishedTrendMap(
            DateRange range, List<Long> deptIds, String status) {
        List<DashboardDailyTotalVo> rows =
                approvalInstanceMapper.listDashboardFinishedTrend(
                        range.startDate(), range.endDate(), deptIds, status);
        Map<LocalDate, Long> result = new HashMap<>();
        if (rows == null) {
            return result;
        }
        for (DashboardDailyTotalVo row : rows) {
            if (row == null || row.getStatDate() == null) {
                continue;
            }
            result.put(row.getStatDate(), defaultLong(row.getTotalCount()));
        }
        return result;
    }
 
    private Map<LocalDate, Long> buildInProgressTrendMap(DateRange range, List<Long> deptIds) {
        List<ApprovalInstance> rows =
                approvalInstanceMapper.listDashboardInProgressInstances(
                        range.startDate(), range.endDate(), deptIds);
        Map<LocalDate, Long> result = new HashMap<>();
        if (rows == null || rows.isEmpty()) {
            return result;
        }
        for (ApprovalInstance row : rows) {
            if (row.getCreateTime() == null) {
                continue;
            }
            LocalDate start = row.getCreateTime().toLocalDate();
            LocalDate end =
                    row.getFinishedAt() == null
                            ? range.endDate()
                            : row.getFinishedAt().toLocalDate();
            if (end.isBefore(range.startDate()) || start.isAfter(range.endDate())) {
                continue;
            }
            LocalDate cursor = start.isBefore(range.startDate()) ? range.startDate() : start;
            LocalDate limit = end.isAfter(range.endDate()) ? range.endDate() : end;
            while (!cursor.isAfter(limit)) {
                result.merge(cursor, 1L, Long::sum);
                cursor = cursor.plusDays(1);
            }
        }
        return result;
    }
 
    private long countRegisteredUsers(LocalDate startDate, LocalDate endDate) {
        return mpUserMapper.selectCount(
                new LambdaQueryWrapper<MpUser>()
                        .eq(MpUser::getDelFlag, 0)
                        .eq(MpUser::getUserType, MiniUserType.VISITOR)
                        .ge(MpUser::getCreateTime, atStartOfDay(startDate))
                        .lt(MpUser::getCreateTime, atStartOfNextDay(endDate)));
    }
 
    private long countEmployees(LocalDate startDate, LocalDate endDate) {
        return mpUserMapper.selectCount(
                new LambdaQueryWrapper<MpUser>()
                        .eq(MpUser::getDelFlag, 0)
                        .eq(MpUser::getUserType, MiniUserType.EMPLOYEE)
                        .ge(MpUser::getCreateTime, atStartOfDay(startDate))
                        .lt(MpUser::getCreateTime, atStartOfNextDay(endDate)));
    }
 
    private DateRange resolveRange(LocalDate startDate, LocalDate endDate) {
        LocalDate today = LocalDate.now();
        LocalDate defaultStart = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate defaultEnd = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
 
        LocalDate resolvedStart = startDate == null ? defaultStart : startDate;
        LocalDate resolvedEnd = endDate == null ? defaultEnd : endDate;
        if (resolvedStart.isAfter(resolvedEnd)) {
            return new DateRange(resolvedEnd, resolvedStart);
        }
        return new DateRange(resolvedStart, resolvedEnd);
    }
 
    private LocalDate resolveDailyDate(DateRange range) {
        LocalDate today = LocalDate.now();
        if (!today.isBefore(range.startDate()) && !today.isAfter(range.endDate())) {
            return today;
        }
        return range.endDate();
    }
 
    private LocalDateTime atStartOfDay(LocalDate date) {
        return date.atStartOfDay();
    }
 
    private LocalDateTime atStartOfNextDay(LocalDate date) {
        return LocalDateTime.of(date.plusDays(1), LocalTime.MIN);
    }
 
    private String format(LocalDate date) {
        return date == null ? null : date.format(DATE_FORMATTER);
    }
 
    private List<Long> resolveDeptIds(Long visitedDeptId) {
        if (visitedDeptId == null) {
            return List.of();
        }
        List<SysDept> depts =
                sysDeptMapper.selectList(
                        new LambdaQueryWrapper<SysDept>()
                                .eq(SysDept::getDelFlag, 0)
                                .orderByAsc(SysDept::getId));
        if (depts == null || depts.isEmpty()) {
            return List.of(visitedDeptId);
        }
        Map<Long, List<Long>> childrenMap = new HashMap<>();
        Set<Long> existingIds = new HashSet<>();
        for (SysDept dept : depts) {
            if (dept.getId() == null) {
                continue;
            }
            existingIds.add(dept.getId());
            Long parentId = dept.getParentId() == null ? 0L : dept.getParentId();
            childrenMap.computeIfAbsent(parentId, key -> new ArrayList<>()).add(dept.getId());
        }
        if (!existingIds.contains(visitedDeptId)) {
            return List.of(visitedDeptId);
        }
        LinkedHashSet<Long> result = new LinkedHashSet<>();
        ArrayList<Long> stack = new ArrayList<>();
        stack.add(visitedDeptId);
        while (!stack.isEmpty()) {
            Long current = stack.remove(stack.size() - 1);
            if (current == null || !result.add(current)) {
                continue;
            }
            for (Long childId : childrenMap.getOrDefault(current, List.of())) {
                if (Objects.nonNull(childId)) {
                    stack.add(childId);
                }
            }
        }
        return new ArrayList<>(result);
    }
 
    private long defaultLong(Long value) {
        return value == null ? 0L : value;
    }
 
    private record DateRange(LocalDate startDate, LocalDate endDate) {}
}