zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package com.tcm.training.admin.app.patient.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcm.training.admin.app.auth.service.AppAuthSessionService;
import com.tcm.training.admin.app.patient.mapper.AppMyPatientQueryMapper;
import com.tcm.training.admin.app.patient.row.AppMyPatientExchangeRow;
import com.tcm.training.admin.app.patient.row.AppMyPatientExchangeOrderItemRow;
import com.tcm.training.admin.app.patient.row.AppMyPatientLiveRow;
import com.tcm.training.admin.app.patient.row.AppMyPatientRow;
import com.tcm.training.admin.app.patient.vo.AppMyPatientExchangeItemVO;
import com.tcm.training.admin.app.patient.vo.AppMyPatientExchangeOrderItemVO;
import com.tcm.training.admin.app.patient.vo.AppMyPatientItemVO;
import com.tcm.training.admin.app.patient.vo.AppMyPatientLiveItemVO;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.member.entity.AppUser;
import com.tcm.training.admin.member.mapper.UserMapper;
import com.tcm.training.admin.points.entity.PointsExchangeOrder;
import com.tcm.training.admin.points.entity.PointsExchangeOrderItem;
import com.tcm.training.admin.points.mapper.PointsExchangeOrderItemMapper;
import com.tcm.training.admin.points.mapper.PointsExchangeOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * APP 我的患者聚合服务。
 */
@Service
public class AppMyPatientService {
 
    private static final int USER_TYPE_PERSONAL = 0;
    private static final int USER_TYPE_DOCTOR = 1;
    private static final int USER_STATUS_ENABLED = 1;
    private static final String BUSINESS_TYPE_LIVE_WATCH = "LIVE_WATCH";
    private static final String INCOME_EXPENSE_TYPE_INCOME = "INCOME";
    private static final int EXCHANGE_STATUS_PENDING = 0;
    private static final int EXCHANGE_STATUS_SHIPPED = 1;
    private static final int EXCHANGE_STATUS_COMPLETED = 2;
    private static final int EXCHANGE_ITEM_STATUS_PENDING = 0;
    private static final int EXCHANGE_ITEM_STATUS_PICKUP = 1;
    private static final int EXCHANGE_ITEM_STATUS_COMPLETED = 2;
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    private final AppAuthSessionService appAuthSessionService;
    private final UserMapper userMapper;
    private final AppMyPatientQueryMapper appMyPatientQueryMapper;
    private final PointsExchangeOrderMapper pointsExchangeOrderMapper;
    private final PointsExchangeOrderItemMapper pointsExchangeOrderItemMapper;
 
    /**
     * 注入 APP 我的患者聚合所需依赖。
     */
    public AppMyPatientService(AppAuthSessionService appAuthSessionService,
                               UserMapper userMapper,
                               AppMyPatientQueryMapper appMyPatientQueryMapper,
                               PointsExchangeOrderMapper pointsExchangeOrderMapper,
                               PointsExchangeOrderItemMapper pointsExchangeOrderItemMapper) {
        this.appAuthSessionService = appAuthSessionService;
        this.userMapper = userMapper;
        this.appMyPatientQueryMapper = appMyPatientQueryMapper;
        this.pointsExchangeOrderMapper = pointsExchangeOrderMapper;
        this.pointsExchangeOrderItemMapper = pointsExchangeOrderItemMapper;
    }
 
    /**
     * 分页查询直属患者列表。
     */
    public PageResponse<AppMyPatientItemVO> pagePatients(int page, int pageSize, String keyword) {
        Long doctorUserId = requireDoctorUserId();
        IPage<AppMyPatientRow> result = appMyPatientQueryMapper.selectPatientPage(
                new Page<AppMyPatientRow>(normalizePage(page), normalizePageSize(pageSize)),
                doctorUserId,
                trimToNull(keyword)
        );
        return PageResponse.of(toPatientItems(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    /**
     * 分页查询直属患者直播积分记录。
     */
    public PageResponse<AppMyPatientLiveItemVO> pageLiveRecords(int page,
                                                                int pageSize,
                                                                String keyword,
                                                                String startDate,
                                                                String endDate) {
        Long doctorUserId = requireDoctorUserId();
        IPage<AppMyPatientLiveRow> result = appMyPatientQueryMapper.selectLiveRecordPage(
                new Page<AppMyPatientLiveRow>(normalizePage(page), normalizePageSize(pageSize)),
                doctorUserId,
                trimToNull(keyword),
                parseStartDate(startDate),
                parseEndDate(endDate),
                BUSINESS_TYPE_LIVE_WATCH,
                INCOME_EXPENSE_TYPE_INCOME
        );
        return PageResponse.of(toLiveItems(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    /**
     * 分页查询直属患者兑换记录。
     */
    public PageResponse<AppMyPatientExchangeItemVO> pageExchangeRecords(int page,
                                                                        int pageSize,
                                                                        String keyword,
                                                                        String status) {
        Long doctorUserId = requireDoctorUserId();
        IPage<AppMyPatientExchangeRow> result = appMyPatientQueryMapper.selectExchangeRecordPage(
                new Page<AppMyPatientExchangeRow>(normalizePage(page), normalizePageSize(pageSize)),
                doctorUserId,
                trimToNull(keyword),
                normalizeExchangeStatus(status)
        );
        return PageResponse.of(toExchangeItems(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    /**
     * 分页查询直属患者所属主订单下的子订单列表。
     */
    public PageResponse<AppMyPatientExchangeOrderItemVO> pageExchangeOrderItems(Long orderId, int page, int pageSize) {
        Long doctorUserId = requireDoctorUserId();
        requireDoctorOrder(orderId, doctorUserId);
        IPage<AppMyPatientExchangeOrderItemRow> result = appMyPatientQueryMapper.selectExchangeOrderItemPage(
                new Page<AppMyPatientExchangeOrderItemRow>(normalizePage(page), normalizePageSize(pageSize)),
                doctorUserId,
                orderId
        );
        return PageResponse.of(toExchangeOrderItemList(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    /**
     * 确认当前医生可查看的兑换主订单已收货。
     */
    @Transactional
    public void confirmExchangeOrderReceived(Long orderId) {
        Long doctorUserId = requireDoctorUserId();
        PointsExchangeOrder order = requireDoctorOrder(orderId, doctorUserId);
        if (order.getStatus() == null || order.getStatus().intValue() != EXCHANGE_STATUS_SHIPPED) {
            throw new IllegalArgumentException("当前订单不允许确认收货");
        }
        LocalDateTime now = LocalDateTime.now();
        order.setStatus(EXCHANGE_STATUS_COMPLETED);
        order.setCompletedAt(now);
        pointsExchangeOrderMapper.updateById(order);
 
        List<PointsExchangeOrderItem> items = pointsExchangeOrderItemMapper.selectList(new LambdaQueryWrapper<PointsExchangeOrderItem>()
                .eq(PointsExchangeOrderItem::getDeleted, 0)
                .eq(PointsExchangeOrderItem::getOrderId, orderId)
                .isNull(PointsExchangeOrderItem::getCancelledAt));
        for (PointsExchangeOrderItem item : items) {
            if (item.getStatus() == null || item.getStatus().intValue() == EXCHANGE_ITEM_STATUS_COMPLETED) {
                continue;
            }
            item.setStatus(EXCHANGE_ITEM_STATUS_PICKUP);
            pointsExchangeOrderItemMapper.updateById(item);
        }
    }
 
    /**
     * 校验当前登录用户是启用中的医生账号。
     */
    private Long requireDoctorUserId() {
        Long userId = appAuthSessionService.requireLoginUserId();
        AppUser user = userMapper.selectById(userId);
        if (user == null || user.getDeleted() == null || user.getDeleted().intValue() != 0) {
            throw new IllegalArgumentException("用户不存在");
        }
        if (user.getStatus() == null || user.getStatus().intValue() != USER_STATUS_ENABLED) {
            throw new IllegalArgumentException("用户已禁用");
        }
        if (user.getType() == null || user.getType().intValue() != USER_TYPE_DOCTOR) {
            throw new IllegalArgumentException("当前用户无权查看患者数据");
        }
        return userId;
    }
 
    /**
     * 将患者列表查询结果转换为 APP 页面展示结构。
     */
    private List<AppMyPatientItemVO> toPatientItems(List<AppMyPatientRow> rows) {
        List<AppMyPatientItemVO> items = new ArrayList<AppMyPatientItemVO>();
        if (rows == null) {
            return items;
        }
        for (AppMyPatientRow row : rows) {
            AppMyPatientItemVO item = new AppMyPatientItemVO();
            item.setId(row.getId());
            item.setNickname(resolveNickname(row.getNickname(), row.getPhone()));
            item.setAvatar(trimToEmpty(row.getAvatar()));
            item.setCreatedAt(row.getCreatedAt());
            items.add(item);
        }
        return items;
    }
 
    /**
     * 将直播积分记录转换为 APP 页面展示结构。
     */
    private List<AppMyPatientLiveItemVO> toLiveItems(List<AppMyPatientLiveRow> rows) {
        List<AppMyPatientLiveItemVO> items = new ArrayList<AppMyPatientLiveItemVO>();
        if (rows == null) {
            return items;
        }
        for (AppMyPatientLiveRow row : rows) {
            AppMyPatientLiveItemVO item = new AppMyPatientLiveItemVO();
            item.setId(row.getId());
            item.setPatientId(row.getPatientId());
            item.setNickname(resolveNickname(row.getPatientNickname(), row.getPatientPhone()));
            item.setAvatar(trimToEmpty(row.getPatientAvatar()));
            item.setCourseTitle(trimToEmpty(row.getCourseTitle()));
            item.setOperatedAt(row.getOperatedAt());
            item.setPoints(normalizeNumber(row.getPoints()));
            items.add(item);
        }
        return items;
    }
 
    /**
     * 将兑换数据查询结果转换为 APP 页面展示结构。
     */
    private List<AppMyPatientExchangeItemVO> toExchangeItems(List<AppMyPatientExchangeRow> rows) {
        List<AppMyPatientExchangeItemVO> items = new ArrayList<AppMyPatientExchangeItemVO>();
        if (rows == null) {
            return items;
        }
        for (AppMyPatientExchangeRow row : rows) {
            AppMyPatientExchangeItemVO item = new AppMyPatientExchangeItemVO();
            item.setId(row.getId());
            item.setOrderNo(trimToEmpty(row.getOrderNo()));
            item.setGiftName(trimToEmpty(row.getGiftName()));
            item.setGiftCover(trimToEmpty(row.getGiftCover()));
            item.setNickname(trimToEmpty(row.getNickname()));
            item.setActivityPeriodText(buildActivityPeriodText(row.getActivityStartAt(), row.getActivityEndAt()));
            item.setStatus(row.getStatus());
            item.setStatusText(resolveExchangeStatusText(row.getStatus()));
            item.setCount(normalizeNumber(row.getCount()));
            item.setCanConfirmReceived(Boolean.valueOf(Boolean.TRUE.equals(row.getCanConfirmReceived())));
            items.add(item);
        }
        return items;
    }
 
    /**
     * 将主订单下子订单列表转换为 APP 页面展示结构。
     */
    private List<AppMyPatientExchangeOrderItemVO> toExchangeOrderItemList(List<AppMyPatientExchangeOrderItemRow> rows) {
        List<AppMyPatientExchangeOrderItemVO> items = new ArrayList<AppMyPatientExchangeOrderItemVO>();
        if (rows == null) {
            return items;
        }
        for (AppMyPatientExchangeOrderItemRow row : rows) {
            AppMyPatientExchangeOrderItemVO item = new AppMyPatientExchangeOrderItemVO();
            item.setId(row.getId());
            item.setOrderId(row.getOrderId());
            item.setPatientId(row.getPatientId());
            item.setPatientName(trimToEmpty(row.getPatientName()));
            item.setPatientPhone(trimToEmpty(row.getPatientPhone()));
            item.setSettledPoints(normalizeNumber(row.getSettledPoints()));
            item.setStatus(row.getStatus());
            item.setStatusText(resolveExchangeItemStatusText(row.getStatus()));
            item.setCreatedAt(row.getCreatedAt());
            items.add(item);
        }
        return items;
    }
 
    /**
     * 将兑换页状态筛选值转换为数据库状态码。
     */
    private Integer normalizeExchangeStatus(String status) {
        String normalized = trimToNull(status);
        if (normalized == null || "all".equalsIgnoreCase(normalized)) {
            return null;
        }
        if ("pending".equalsIgnoreCase(normalized)) {
            return Integer.valueOf(EXCHANGE_STATUS_PENDING);
        }
        if ("received".equalsIgnoreCase(normalized)) {
            return Integer.valueOf(EXCHANGE_STATUS_SHIPPED);
        }
        throw new IllegalArgumentException("兑换状态不合法");
    }
 
    /**
     * 解析列表日期筛选的开始时间。
     */
    private LocalDateTime parseStartDate(String value) {
        String normalized = trimToNull(value);
        if (normalized == null) {
            return null;
        }
        try {
            return LocalDate.parse(normalized, DATE_FORMATTER).atStartOfDay();
        } catch (DateTimeParseException ex) {
            throw new IllegalArgumentException("开始日期格式不正确");
        }
    }
 
    /**
     * 解析列表日期筛选的结束时间,并自动补到当天结束。
     */
    private LocalDateTime parseEndDate(String value) {
        String normalized = trimToNull(value);
        if (normalized == null) {
            return null;
        }
        try {
            return LocalDate.parse(normalized, DATE_FORMATTER).plusDays(1).atStartOfDay().minusSeconds(1);
        } catch (DateTimeParseException ex) {
            throw new IllegalArgumentException("结束日期格式不正确");
        }
    }
 
    /**
     * 拼接兑换活动期数字段展示文案。
     */
    private String buildActivityPeriodText(LocalDateTime activityStartAt, LocalDateTime activityEndAt) {
        String startText = formatDate(activityStartAt);
        String endText = formatDate(activityEndAt);
        if (startText.isEmpty() && endText.isEmpty()) {
            return "";
        }
        return startText + "-" + endText;
    }
 
    /**
     * 将日期时间格式化为页面期数字段所需的 yyyy.MM.dd。
     */
    private String formatDate(LocalDateTime value) {
        if (value == null) {
            return "";
        }
        return value.toLocalDate().toString().replace('-', '.');
    }
 
    /**
     * 解析兑换记录状态文案。
     */
    private String resolveExchangeStatusText(Integer status) {
        if (status != null && status.intValue() == EXCHANGE_STATUS_PENDING) {
            return "待发货";
        }
        if (status != null && status.intValue() == EXCHANGE_STATUS_SHIPPED) {
            return "待收货";
        }
        return "已收货";
    }
 
    /**
     * 解析兑换子订单状态文案。
     */
    private String resolveExchangeItemStatusText(Integer status) {
        if (status != null && status.intValue() == EXCHANGE_ITEM_STATUS_PENDING) {
            return "待发货";
        }
        if (status != null && status.intValue() == EXCHANGE_ITEM_STATUS_PICKUP) {
            return "待取货";
        }
        return "兑换完成";
    }
 
    /**
     * 校验并返回当前医生可操作的兑换主订单。
     */
    private PointsExchangeOrder requireDoctorOrder(Long orderId, Long doctorUserId) {
        if (orderId == null) {
            throw new IllegalArgumentException("兑换订单不存在");
        }
        PointsExchangeOrder order = pointsExchangeOrderMapper.selectOne(new LambdaQueryWrapper<PointsExchangeOrder>()
                .eq(PointsExchangeOrder::getId, orderId)
                .eq(PointsExchangeOrder::getDeleted, 0)
                .last("limit 1"));
        if (order == null) {
            throw new IllegalArgumentException("兑换订单不存在");
        }
        Long count = pointsExchangeOrderItemMapper.selectCount(new LambdaQueryWrapper<PointsExchangeOrderItem>()
                .eq(PointsExchangeOrderItem::getDeleted, 0)
                .eq(PointsExchangeOrderItem::getOrderId, orderId)
                .isNull(PointsExchangeOrderItem::getCancelledAt)
                .inSql(PointsExchangeOrderItem::getAppUserId,
                        "select id from tcm_app_user where deleted = 0 and type = 0 and superior_clinic_user_id = " + doctorUserId));
        if (count == null || count.longValue() <= 0L) {
            throw new IllegalArgumentException("兑换订单不存在");
        }
        return order;
    }
 
    /**
     * 用昵称优先、手机号兜底的策略统一展示患者名称。
     */
    private String resolveNickname(String nickname, String phone) {
        String normalizedNickname = trimToEmpty(nickname);
        if (!normalizedNickname.isEmpty()) {
            return normalizedNickname;
        }
        return trimToEmpty(phone);
    }
 
    /**
     * 将空白文本统一转换为 null。
     */
    private String trimToNull(String value) {
        String text = trimToEmpty(value);
        return text.isEmpty() ? null : text;
    }
 
    /**
     * 将可空文本归一化为空字符串。
     */
    private String trimToEmpty(String value) {
        if (value == null) {
            return "";
        }
        return value.trim();
    }
 
    /**
     * 将分页页码归一化到合法范围。
     */
    private int normalizePage(int page) {
        return page <= 0 ? 1 : page;
    }
 
    /**
     * 将分页大小归一化到合法范围。
     */
    private int normalizePageSize(int pageSize) {
        return pageSize <= 0 ? 10 : pageSize;
    }
 
    /**
     * 将积分数值归一化,避免返回空值影响 APP 展示。
     */
    private Integer normalizeNumber(Integer value) {
        return value == null ? Integer.valueOf(0) : value;
    }
}