huliguo
2025-06-05 0a492b64ca1a4e40cc9ea56eddd1afe2c09a12b3
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
<?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.ruoyi.system.mapper.OrderMapper">
 
 
    <select id="getProcessingCommissionPriceByShareUserId" resultType="java.math.BigDecimal">
        select  COALESCE(SUM(commission_price), 0.00) AS total_commission from tb_order where status in (4,5) and share_user_id =#{id}
    </select>
    <select id="getOrderPage" resultType="com.ruoyi.system.pojo.vo.OrderPageVO">
        select
            c.id,
            o.order_no,
            publish.user_name as publishName,
            publish.phone as publishPhone,
            o.price,
            book.user_name as bookName,
            book.phone as bookPhone,
            c.create_time as createTime,
            o.create_time as bookTime,
            o.finish_time as finishTime,
            -- 状态映射逻辑
            CASE
                WHEN c.status = 1 THEN 1
                WHEN c.status = 2 THEN 6
                WHEN c.status = 3 THEN 5
                WHEN c.status = 4 THEN
                    CASE
                        WHEN o.status = 2 THEN 2
                        WHEN o.status = 3 THEN 3
                        WHEN o.status IN (4, 5) THEN 4
                        ELSE c.status  -- 默认保持原状态(可根据需求调整)
                        END
                ELSE c.status  -- 其他状态保持不变
                END AS status
        from
            tb_company c
        left join tb_order o on c.id = o.company_id and o.status != -1
        left join (select id,user_name,phone from tb_user group by id) publish on c.user_id = publish.id
        left join (select id,user_name,phone from tb_user group by id) book on o.user_id = book.id
        where
            c.is_delete=0
 
            <if test="null != dto.orderNo">
                and o.order_no like concat('%',#{dto.orderNo},'%')
            </if>
            <if test="null != dto.publishName and '' != dto.publishName">
                and publish.user_name like concat('%',#{dto.publishName},'%')
            </if>
            <if test="null != dto.publishPhone and '' != dto.publishPhone">
                and publish.phone like concat('%',#{dto.publishPhone},'%')
            </if>
 
            <if test="null != dto.bookName and '' != dto.bookName">
                and book.user_name like concat('%',#{dto.bookName},'%')
            </if>
            <if test="null != dto.bookPhone and '' != dto.bookPhone">
                and book.phone like concat('%',#{dto.bookPhone},'%')
            </if>
 
            <if test="null != dto.createTimeStart and null != dto.createTimeEnd">
                and c.create_time between #{dto.createTimeStart} and  #{dto.createTimeEnd}
            </if>
 
            <if test="null != dto.bookTimeStart and null != dto.bookTimeEnd">
                and o.create_time between #{dto.bookTimeStart} and  #{dto.bookTimeEnd}
            </if>
 
            <if test="null != dto.finishTimeStart and null != dto.finishTimeEnd">
                and o.finish_time between #{dto.finishTimeStart} and  #{dto.finishTimeEnd}
            </if>
 
            <if test="null != dto.status">
                AND (
                <!-- 直接匹配原始状态 -->
                (c.status IN (1,2,3) AND
                CASE
                WHEN c.status = 1 THEN 1
                WHEN c.status = 2 THEN 6
                WHEN c.status = 3 THEN 5
                ELSE c.status
                END = #{dto.status})
                <!-- 匹配映射后的状态4(c.status=4且o.status=4/5) -->
                OR (c.status = 4 AND o.status IN (4,5) AND 4 = #{dto.status})
                <!-- 匹配映射后的状态2(c.status=4且o.status=2) -->
                OR (c.status = 4 AND o.status = 2 AND 2 = #{dto.status})
                <!-- 匹配映射后的状态3(c.status=4且o.status=3) -->
                OR (c.status = 4 AND o.status = 3 AND 3 = #{dto.status})
                )
            </if>
    </select>
    <select id="getDetailById" resultType="com.ruoyi.system.pojo.vo.OrderDetailVO">
 
        select
            c.id,
            -- 状态映射逻辑
            CASE
                WHEN c.status = 1 THEN 1
                WHEN c.status = 2 THEN 6
                WHEN c.status = 3 THEN 5
                WHEN c.status = 4 THEN
                    CASE
                        WHEN o.status = 2 THEN 2
                        WHEN o.status = 3 THEN 3
                        WHEN o.status IN (4, 5) THEN 4
                        ELSE c.status  -- 默认保持原状态(可根据需求调整)
                        END
                ELSE c.status  -- 其他状态保持不变
                END AS status,
            o.id as orderId,
            c.company_name,
            c.establish_time,
            concat(c.city,c.province,c.area) as place,
            c.registered_capital,
            ct.name as companyCategoryName,
            industry.name as industryName,
            c.taxpayer_type,
            c.tax_credit,
            c.official_seal_num,
            c.paid_in_funds,
            c.oper_name,
            c.scope,
            c.invoice_limit,
            c.social_security,
            c.tendering,
            c.have_trademark,
            c.have_patent,
            c.have_soft_works,
            info.need_rename,
            info.new_district,
            publish.user_name as publishName,
            publish.phone as publishPhone,
            c.create_time as createTime,
            book.user_name as bookName,
            book.phone as bookPhone,
            o.create_time as bookTime,
            o.price as price,
            o.pay_time as payTime,
            o.confirm_time,
            o.seller_finish_time,
            o.finish_time,
            -- 计算预计完成时间
            CASE
                WHEN
                    (
                        CASE
                            WHEN c.status = 1 THEN 1
                            WHEN c.status = 2 THEN 6
                            WHEN c.status = 3 THEN 5
                            WHEN c.status = 4 THEN
                                CASE
                                    WHEN o.status = 2 THEN 2
                                    WHEN o.status = 3 THEN 3
                                    WHEN o.status IN (4, 5) THEN 4
                                    ELSE c.status
                                    END
                            ELSE c.status
                            END
                        ) IN (3, 4, 5)
                    THEN
                    DATE_ADD(o.pay_time, INTERVAL (c.estimated_days+info.add_day) DAY)
                ELSE NULL
                END AS estimateTime
        from tb_company c
                 left join tb_company_type ct on c.company_category = ct.id
                 left join tb_industry industry on c.company_industry_id = industry.id
                 left join tb_order o on c.id= o.company_id and o.status != -1
        left join tb_buyer_company_info info on o.id = info.order_id
            left join tb_user publish on c.user_id = publish.id
            left join tb_user book on o.user_id = book.id
        where
            c.is_delete=0
          and
            c.id=#{id}
 
 
    </select>
    <select id="today" resultType="com.ruoyi.system.pojo.vo.TodayStatisticsVO">
        select
            ifnull(sum(price),0.00)as total,
            ifnull(sum(commission_platform),0.00) as profit
        from tb_order
        where
            status in (4,5,6)  --  4办理中 5卖家已完成 6买家完成
           and  pay_time between #{startTime} and #{endTime}
 
    </select>
    <select id="getDailyStatistics" resultType="com.ruoyi.system.pojo.model.DailyStatistics">
        SELECT
            DATE_FORMAT(pay_time, '%Y-%m-%d') as `date`,
            ifnull(SUM(price),0.00) as totalPrice,
            ifnull(SUM(commission_platform),0.00) as platformCommission
        FROM tb_order
        WHERE pay_time BETWEEN #{startTime} AND #{endTime}
          AND status IN (4, 5, 6)
        GROUP BY DATE(pay_time)
        ORDER BY DATE(pay_time) ASC
    </select>
    <select id="financeTop" resultType="com.ruoyi.system.pojo.vo.FinanceFlowsTopVO">
        SELECT
            SUM(price) AS totalIncome,
            SUM(commission_platform) AS totalProfit,
            SUM(commission_price) AS totalCommission,
            SUM(price - commission_platform - commission_price) AS sellerIncome
        FROM tb_order
        WHERE status IN (4, 5, 6)
          AND is_refund = 0
    </select>
    <select id="flowsPage" resultType="com.ruoyi.system.pojo.vo.FinanceFlowsPageVO">
        SELECT
            order_no,
            price,
            commission_price,
            commission_platform,
            pay_time AS operateTime,
            (price - commission_platform - commission_price) as seller,
            1 AS type  -- 收入类型
        FROM tb_order
        WHERE
            ((status IN (4, 5, 6))  -- 只查询已支付订单
            or
            (status=-1 and is_refund =1))  -- 已取消且已退款的订单
            <if test="null != dto.orderNo and '' != dto.orderNo">
                and order_no LIKE CONCAT('%', #{dto.orderNo}, '%')
            </if>
            <if test="null != dto.operateTimeStart and null != dto.operateTimeEnd">
                and pay_time between #{dto.operateTimeStart} and #{dto.operateTimeEnd}
            </if>
        AND ( #{dto.type} IS NULL OR 1 = #{dto.type} )
        UNION ALL
        SELECT
            order_no,
            0 AS price,
            0 AS commission_price,
            0 AS commission_platform,
            refund_time AS operateTime,
        0 as seller,
            2 AS type  -- 退款类型
        FROM tb_order
        WHERE is_refund = 1  -- 只查询已退款订单
        <if test="null != dto.orderNo and '' != dto.orderNo">
            and order_no LIKE CONCAT('%', #{dto.orderNo}, '%')
        </if>
        <if test="null != dto.operateTimeStart and null != dto.operateTimeEnd">
            and refund_time between #{dto.operateTimeStart} and #{dto.operateTimeEnd}
        </if>
        AND ( #{dto.type} IS NULL OR 2 = #{dto.type} )
        ORDER BY operateTime DESC
        LIMIT #{dto.pageNum}, #{dto.pageSize};
    </select>
    <select id="countFlowsPage" resultType="java.lang.Long">
        SELECT COUNT(*) FROM (
        select 1
        from
        tb_order
        WHERE
        ((status IN (4, 5, 6)) -- 只查询已支付订单
        or
        (status=-1 and is_refund =1)) -- 已取消且已退款的订单
        <if test="null != dto.orderNo and '' != dto.orderNo">
            and order_no LIKE CONCAT('%', #{dto.orderNo}, '%')
        </if>
        <if test="null != dto.operateTimeStart and null != dto.operateTimeEnd">
            and pay_time between #{dto.operateTimeStart} and #{dto.operateTimeEnd}
        </if>
        AND ( #{dto.type} IS NULL OR 1 = #{dto.type} )
        UNION ALL
        select 1
        FROM tb_order
        WHERE is_refund = 1 -- 只查询已退款订单
        <if test="null != dto.orderNo and '' != dto.orderNo">
            and order_no LIKE CONCAT('%', #{dto.orderNo}, '%')
        </if>
        <if test="null != dto.operateTimeStart and null != dto.operateTimeEnd">
            and refund_time between #{dto.operateTimeStart} and #{dto.operateTimeEnd}
        </if>
        AND ( #{dto.type} IS NULL OR 2 = #{dto.type} )
        ) AS t
    </select>
</mapper>