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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tcm.training.admin.app.patient.mapper.AppMyPatientQueryMapper">
 
    <select id="selectPatientPage" resultType="com.tcm.training.admin.app.patient.row.AppMyPatientRow">
        SELECT
            u.id,
            u.nickname,
            u.phone,
            u.avatar,
            u.created_at AS createdAt
        FROM tcm_app_user u
        WHERE u.deleted = 0
          AND u.type = 0
          AND u.superior_clinic_user_id = #{doctorUserId}
        <if test="keyword != null and keyword.trim() != ''">
            AND (
                u.nickname LIKE CONCAT('%', #{keyword}, '%')
                OR u.phone LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        ORDER BY u.created_at DESC, u.id DESC
    </select>
 
    <select id="selectLiveRecordPage" resultType="com.tcm.training.admin.app.patient.row.AppMyPatientLiveRow">
        SELECT
            l.id,
            u.id AS patientId,
            u.nickname AS patientNickname,
            u.phone AS patientPhone,
            u.avatar AS patientAvatar,
            l.content_name AS courseTitle,
            l.operated_at AS operatedAt,
            l.points_delta AS points
        FROM tcm_points_ledger l
        INNER JOIN tcm_app_user u ON u.id = l.app_user_id AND u.deleted = 0
        WHERE l.deleted = 0
          AND l.business_type = #{businessType}
          AND l.income_expense_type = #{incomeExpenseType}
          AND u.type = 0
          AND u.superior_clinic_user_id = #{doctorUserId}
        <if test="keyword != null and keyword.trim() != ''">
            AND (
                u.nickname LIKE CONCAT('%', #{keyword}, '%')
                OR u.phone LIKE CONCAT('%', #{keyword}, '%')
                OR l.content_name LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        <if test="startDate != null">
            AND l.operated_at <![CDATA[>=]]> #{startDate}
        </if>
        <if test="endDate != null">
            AND l.operated_at <![CDATA[<=]]> #{endDate}
        </if>
        ORDER BY l.operated_at DESC, l.id DESC
    </select>
 
    <select id="selectExchangeRecordPage" resultType="com.tcm.training.admin.app.patient.row.AppMyPatientExchangeRow">
        SELECT
            o.id,
            o.order_no AS orderNo,
            o.gift_name AS giftName,
            o.gift_cover AS giftCover,
            MAX(COALESCE(NULLIF(TRIM(u.phone), ''), NULLIF(TRIM(i.user_phone), ''))) AS nickname,
            o.status,
            COUNT(i.id) AS count,
            CASE WHEN o.status = 1 THEN TRUE ELSE FALSE END AS canConfirmReceived,
            o.activity_start_at AS activityStartAt,
            o.activity_end_at AS activityEndAt
        FROM tcm_points_exchange_order o
        INNER JOIN tcm_points_exchange_order_item i ON i.order_id = o.id AND i.deleted = 0
        INNER JOIN tcm_app_user u ON u.id = i.app_user_id AND u.deleted = 0
        WHERE o.deleted = 0
          AND u.type = 0
          AND u.superior_clinic_user_id = #{doctorUserId}
          AND (i.cancelled_at IS NULL)
        <if test="keyword != null and keyword.trim() != ''">
            AND (
                u.nickname LIKE CONCAT('%', #{keyword}, '%')
                OR u.phone LIKE CONCAT('%', #{keyword}, '%')
                OR o.gift_name LIKE CONCAT('%', #{keyword}, '%')
                OR o.order_no LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        <if test="status != null">
            AND o.status = #{status}
        </if>
        GROUP BY o.id, o.order_no, o.gift_name, o.gift_cover, o.status, o.activity_start_at, o.activity_end_at
        ORDER BY o.created_at DESC, o.id DESC
    </select>
 
    <select id="selectExchangeOrderItemPage" resultType="com.tcm.training.admin.app.patient.row.AppMyPatientExchangeOrderItemRow">
        SELECT
            i.id,
            i.order_id AS orderId,
            u.id AS patientId,
            COALESCE(NULLIF(TRIM(u.phone), ''), NULLIF(TRIM(i.user_phone), ''), '用户') AS patientName,
            COALESCE(NULLIF(TRIM(u.phone), ''), NULLIF(TRIM(i.user_phone), '')) AS patientPhone,
            i.settled_points AS settledPoints,
            i.status,
            i.created_at AS createdAt
        FROM tcm_points_exchange_order_item i
        INNER JOIN tcm_app_user u ON u.id = i.app_user_id AND u.deleted = 0
        WHERE i.deleted = 0
          AND i.order_id = #{orderId}
          AND u.type = 0
          AND u.superior_clinic_user_id = #{doctorUserId}
          AND (i.cancelled_at IS NULL)
        ORDER BY i.created_at DESC, i.id DESC
    </select>
 
</mapper>