admin
2020-04-13 dd5b15229cb15459fa7c31ccea77dac28cbfafbd
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?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.yeshi.fanli.dao.mybatis.HongBaoV2CountMapper">
 
    <resultMap id="BaseResultMap" type="com.yeshi.fanli.vo.order.HongBaoV2VO">
        <id column="hb_id" property="id" jdbcType="BIGINT" />
        <result column="hb_urank" property="urank" jdbcType="INTEGER" />
        <result column="hb_money" property="money" jdbcType="DECIMAL" />
        <result column="hb_type" property="type" jdbcType="INTEGER" />
        <result column="hb_state" property="state" jdbcType="INTEGER" />
        <result column="hb_version" property="version" jdbcType="INTEGER" />
        <result column="hb_beizhu" property="beizhu" jdbcType="VARCHAR" />
        <result column="hb_pre_get_time" property="preGetTime"
            jdbcType="TIMESTAMP" />
        <result column="hb_get_time" property="getTime" jdbcType="TIMESTAMP" />
        <result column="hb_create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="hb_update_time" property="updateTime" jdbcType="TIMESTAMP" />
 
        <result column="totalMoney" property="totalMoney" jdbcType="DECIMAL" />
 
        <association property="userInfo" column="hb_uid"
            javaType="com.yeshi.fanli.entity.bus.user.UserInfo">
            <id column="hb_uid" property="id" jdbcType="BIGINT" />
        </association>
 
        <association property="parent" column="hb_pid"
            javaType="com.yeshi.fanli.entity.bus.user.HongBaoV2">
            <id column="hb_pid" property="id" jdbcType="BIGINT" />
        </association>
    </resultMap>
    
    
    <resultMap id="ChartMap" type="com.yeshi.fanli.dto.ChartTDO">
        <result column="showDate" property="showDate" jdbcType="VARCHAR" />
        <result column="showValue" property="showValue" jdbcType="VARCHAR" />
    </resultMap>
 
 
    <sql id="Column_DateType">
        <if test="dateType == 1">
            DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d') AS 'showDate'
        </if>
        <if test="dateType == 2">
            DATE_FORMAT(t.`hb_create_time`,'%m') AS 'showDate'
        </if>
        <if test="dateType == 3">
            DATE_FORMAT(t.`hb_create_time`,'%Y') AS 'showDate'
        </if>
    </sql>
 
    <sql id="Count_Select_DateType">
        <if test="startTime != null and startTime != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d')<![CDATA[ >= ]]>'${startTime}'
        </if>
        <if test="endTime != null and endTime != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d') <![CDATA[ <= ]]>'${endTime}'
        </if>
        <if test="year != null and year != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y') = '${year}'
        </if>
    </sql>
 
    <sql id="Count_Group_DateType">
        <if test="dateType == 1">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d')
        </if>
        <if test="dateType == 2">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y-%m')
        </if>
        <if test="dateType == 3">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y')
        </if>
    </sql>
    <sql id="Count_Select_State">
        <if test="state != null and state == 1">  <!-- 未到账 -->
            AND (t.hb_state = 1 or t.hb_state = 2)
        </if>
        <if test="state != null and state == 2"> <!-- 已到账 -->
            AND t.hb_state = 3
        </if>
        <if test="state != null and state == 3">  <!-- 红包已失效 -->
            AND t.hb_state =4
        </if>
    </sql>
 
    <sql id="Count_Select_Type">
        <if test="type != null and type == 1"> <!-- 自购订单 -->
            AND (t.hb_type =1 or t.hb_type =2)
        </if>
        <if test="type != null and type == 2"> <!-- 分享订单 -->
            AND t.`hb_type` = 20
        </if>
        <if test="type != null and type == 3"> <!-- 邀请订单 -->
            AND (t.`hb_type` = 5 OR t.`hb_type` = 6 OR t.`hb_type` = 7 OR
            t.`hb_type` = 21 OR t.`hb_type` = 22)
        </if>
    </sql>
 
    <select id="countHongBaoMoney" resultMap="ChartMap">
        SELECT CAST(SUM(t.`hb_money`)AS DECIMAL(19,2)) AS showValue,
        <include refid="Column_DateType" />
        FROM yeshi_ec_hongbao_v2 t
        <if test="channel != null and  channel != 'all' ">
            LEFT JOIN (SELECT a.`lua_uid` AS uid FROM
            `yeshi_ec_log_user_active` a WHERE a.`lua_channel`='${channel}' GROUP
            BY a.`lua_uid`) a ON t.`hb_uid`=a.uid
        </if>
        WHERE t.`hb_uid` IS NOT NULL AND (t.`hb_type`=1 OR t.`hb_type`=20)
        <include refid="Count_Select_DateType" />
        <include refid="Count_Select_State" />
        <if test="channel != null and  channel != 'all' ">
            AND a.uid is not null
        </if>
 
        <include refid="Count_Group_DateType" />
        ORDER BY t.`hb_create_time`
    </select>
 
    <select id="countHongBaoNum" resultMap="ChartMap">
        SELECT COUNT(t.`hb_id`)AS showValue,
        <include refid="Column_DateType" />
        FROM yeshi_ec_hongbao_v2 t
        <if test="channel != null and  channel != 'all' ">
            LEFT JOIN (SELECT a.`lua_uid` AS uid FROM
            `yeshi_ec_log_user_active` a WHERE a.`lua_channel`='${channel}' GROUP
            BY a.`lua_uid`) a ON t.`hb_uid`=a.uid
        </if>
        WHERE t.`hb_uid` IS NOT NULL AND (t.`hb_type`=1 OR t.`hb_type`=20)
 
        <include refid="Count_Select_DateType" />
        <include refid="Count_Select_State" />
        <if test="channel != null and  channel != 'all' ">
            AND a.uid is not null
        </if>
 
        <include refid="Count_Group_DateType" />
        ORDER BY t.`hb_create_time`
    </select>
 
    <select id="countNumberByUid" resultType="java.lang.Long">
        select count(hb_id)
        from yeshi_ec_hongbao_v2 where hb_uid=#{0}
    </select>
 
 
    <select id="countHongBaoType" resultMap="ChartMap">
        SELECT COUNT(t.`hb_id`)AS showValue,
        <include refid="Column_DateType" />
        FROM yeshi_ec_hongbao_v2 t
        WHERE t.`hb_uid` IS NOT NULL
        <include refid="Count_Select_DateType" />
        <include refid="Count_Select_Type" />
        <include refid="Count_Group_DateType" />
        ORDER BY t.`hb_create_time`
    </select>
 
    <select id="countHongBaoTotalNum" resultMap="ChartMap">
        SELECT COUNT(t.`hb_id`)AS showValue,
        <include refid="Column_DateType" />
        FROM yeshi_ec_hongbao_v2 t
        WHERE t.`hb_uid` IS NOT NULL AND t.`hb_type`
        not in(1,3,4)
        <include refid="Count_Select_DateType" />
        <include refid="Count_Group_DateType" />
        ORDER BY t.`hb_create_time`
    </select>
 
 
    <select id="countMoneyByUidAndState" resultType="java.math.BigDecimal">
        SELECT CAST(SUM(hb.`hb_money`)AS DECIMAL(19,2))
        FROM
        `yeshi_ec_hongbao_v2` hb
        WHERE hb.`hb_uid` = #{uid}
        <if test="state != null">
            AND hb.`hb_state` = #{state}
        </if>
    </select>
 
    <select id="countNumberByUidAndState" resultType="java.lang.Integer">
        SELECT COALESCE(count(hb_id),0) FROM yeshi_ec_hongbao_v2
        WHERE hb_uid =
        #{uid}
        <if test="state != null">
            AND hb_state = #{state}
        </if>
    </select>
 
 
    <select id="getTotalTiChengCount" resultType="java.lang.Integer"
        parameterType="java.lang.Long">
        SELECT count(*) FROM `yeshi_ec_hongbao_v2` h
        WHERE
        h.`hb_uid`=#{0}
        AND (h.`hb_type`=20 OR h.`hb_type`=21 OR h.`hb_type`=22
        OR h.`hb_type`=6
        OR h.`hb_type`=7)
    </select>
    
    <select id="getHongBaoCount" resultType="Long">
        SELECT IFNULL(count(v.`hb_id`),0) FROM `yeshi_ec_hongbao_v2` v
        WHERE v.`hb_uid`=#{uid}
        <if test="type == 1"> <!-- 自购订单 -->
            AND (v.hb_type =1 or v.hb_type =2)
        </if>
        <if test="type == 2"> <!-- 分享订单 -->
            AND v.`hb_type` = 20
        </if>
        <if test="type == 3"> <!-- 邀请订单 -->
            AND (v.`hb_type` = 5 OR v.`hb_type` = 6 OR v.`hb_type` = 7 OR v.`hb_type` = 21 OR v.`hb_type` = 22)
        </if>
    </select>
    
 
    <select id="countValidNumberByUid" resultType="java.lang.Long">
        <!--统计有效订单数量 -->
        SELECT COALESCE(count(hb.hb_id),0) FROM `yeshi_ec_hongbao_v2` hb
        WHERE
        hb.hb_uid =#{uid}
        AND hb.hb_type not in (3,4)
        AND <![CDATA[hb.hb_state <> 4]]>
 
        <if test="dateType != null and dateType == 1">
            AND TO_DAYS(FROM_UNIXTIME(hb.`hb_create_time`/1000)) =
            TO_DAYS(NOW())
        </if>
        <if test="dateType != null and dateType == 2">
            AND DATE_FORMAT(
            FROM_UNIXTIME(hb.`hb_create_time`/1000),'%Y%m' ) =
            DATE_FORMAT(
            CURDATE( ) , '%Y%m' )
        </if>
    </select>
 
    <select id="countWillGetMoneyByUid" resultType="java.math.BigDecimal">
        SELECT
        CAST(SUM(hb.`hb_money`)AS DECIMAL(19,2))
        FROM `yeshi_ec_hongbao_v2` hb
        WHERE hb.`hb_uid` = #{uid} AND hb.`hb_state`
        in (1,2)
    </select>
 
    <select id="getLastHongBaoTime" resultType="java.util.Date">
        <!-- 最近一次下单时间 -->
        SELECT h.`hb_create_time` FROM yeshi_ec_hongbao_v2 h WHERE h.`hb_type`
        IN(1,2) <![CDATA[AND h.hb_state <>4]]>
        AND h.`hb_uid` = #{uid}
        ORDER BY h.`hb_create_time` DESC
        LIMIT 1;
    </select>
 
 
    <select id="count24HOderByChannel" resultMap="ChartMap">
        SELECT <!-- COALESCE(SUM(b.money*2),0) AS 'commision', -->
        COALESCE(COUNT(b.hb_id),0) AS 'showValue',
        <!-- COALESCE(SUM(b.payMoney),0) AS 'payMoney', -->
        <if test="type == 1">
            FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d') AS
            'showDate'
        </if>
        <if test="type == 2">
            FROM_UNIXTIME(u.`createtime`/1000,'%m') AS 'showDate'
        </if>
        <if test="type == 3">
            FROM_UNIXTIME(u.`createtime`/1000,'%Y') AS 'showDate'
        </if>
        FROM
        (
        SELECT h.* FROM yeshi_ec_hongbao_v2 h
        LEFT JOIN (SELECT
        a.`lua_uid` AS uid FROM `yeshi_ec_log_user_active` a
        WHERE
        a.`lua_channel`='${channel}' GROUP BY a.`lua_uid`) a
        ON
        a.uid=h.`hb_uid` WHERE a.uid IS NOT NULL AND (h.`hb_type`=1 OR
        h.`hb_type`=20)
        ) b
        LEFT JOIN yeshi_ec_user u ON u.`id`=b.hb_uid
        WHERE <![CDATA[ b.hb_create_time<FROM_UNIXTIME((u.`createtime`+1000*60*60*24)/1000,'%Y-%m-%d %h:%m:%s')]]>
        <if test="startTime != null and startTime != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d')<![CDATA[ >= ]]>'${startTime}'
        </if>
        <if test="endTime != null and endTime != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d') <![CDATA[ <= ]]>
            '${endTime}'
        </if>
        <if test="years != null and years != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y') = '${years}'
        </if>
        <if test="type == 1">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d')
        </if>
        <if test="type == 2">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m')
        </if>
        <if test="type == 3">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y')
        </if>
        ORDER BY u.`createtime`
 
    </select>
 
    <select id="listShareAndInviteMoney" resultMap="BaseResultMap">
        SELECT IFNULL(SUM(h.`hb_money`),0) as totalMoney,h.*
        FROM
        `yeshi_ec_hongbao_v2` h
        WHERE h.hb_state <![CDATA[<>]]>4
        AND DATE_FORMAT(h.hb_create_time, '%Y-%m-%d' )= #{date}
        AND (
        h.`hb_type` =5 OR h.`hb_type` =6 OR h.`hb_type` =7 OR h.`hb_type` =20
        OR h.`hb_type` =21 OR h.`hb_type` =22 )
        GROUP BY h.`hb_uid`
        ORDER BY
        totalMoney DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countRebateCouponMoney" resultType="java.math.BigDecimal">
        SELECT
        IFNULL(SUM(h.`hb_money`),0) FROM `yeshi_ec_hongbao_v2` h
        where h.`hb_type` = 10
    </select>
 
 
    <select id="sumAlreadyGetMoneyByUid" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(h.`hb_money`),0) FROM `yeshi_ec_hongbao_v2` h
        where h.`hb_uid` = #{uid}
        <if test="typeList!=null">
            <foreach collection="typeList" item="type" open=" and ("
                separator=" or " close=")">
                h.hb_type=#{type}
            </foreach>
        </if>
 
        <if test="minGetTime!=null">
            and h.hb_get_time>=#{minGetTime}
        </if>
 
        <if test="maxGetTime!=null">
            and #{maxGetTime}>h.hb_get_time
        </if>
 
    </select>
 
 
    <select id="countAlreadyGetMoneyByUid" resultType="java.lang.Long">
        SELECT count(h.hb_id) FROM `yeshi_ec_hongbao_v2` h
        where h.`hb_uid` = #{uid}
        <if test="typeList!=null">
            <foreach collection="typeList" item="type" open=" and ("
                separator=" or " close=")">
                h.hb_type=#{type}
            </foreach>
        </if>
 
        <if test="minGetTime!=null">
            and h.hb_get_time>=#{minGetTime}
        </if>
 
        <if test="maxGetTime!=null">
            and #{maxGetTime}>h.hb_get_time
        </if>
    </select>
    
    
    <select id="countRebateOrder" resultType="Long">
        SELECT IFNULL(COUNT(hb_id),0) FROM `yeshi_ec_hongbao_v2`
        WHERE hb_uid = #{uid} AND (hb_type =1 OR hb_type =2)
    </select>
 
    <select id="countShareOrInviteOrder" resultType="Long">
        SELECT IFNULL(COUNT(hb_id),0) FROM `yeshi_ec_hongbao_v2` 
        WHERE hb_uid = #{uid}
                AND (`hb_type` =5 OR `hb_type` =6 OR `hb_type` =7 OR `hb_type` =20 OR `hb_type`=21 OR `hb_type` =22 )
    </select>
 
 
     <select id="getOrderRewardByTeamUid" resultType="BigDecimal">
        SELECT SUM(v2.`hb_money`) FROM yeshi_ec_hongbao_v2 v2 
        LEFT JOIN yeshi_ec_hongbao_v2 p ON p.`hb_id` = v2.`hb_pid`
        WHERE v2.`hb_state` = 3 AND (v2.`hb_type` = 6 OR v2.`hb_type` = 21) 
            AND v2.`hb_uid` = #{uid} AND p.`hb_uid` = #{teamUid}
            AND v2.`hb_create_time`<![CDATA[<=]]> #{endTime}
    </select>
    
    <select id="getOrderRewardByTeamUidTheMonth" resultType="BigDecimal">
        SELECT SUM(v2.`hb_money`) FROM yeshi_ec_hongbao_v2 v2 
        LEFT JOIN yeshi_ec_hongbao_v2 p ON p.`hb_id` = v2.`hb_pid`
        WHERE  v2.`hb_state` = 3 AND (v2.`hb_type` = 6 OR v2.`hb_type` = 21) 
            AND v2.`hb_uid` = #{uid} AND p.`hb_uid` = #{teamUid}
            AND DATE_FORMAT(CURDATE(), '%Y%m') = DATE_FORMAT(v2.`hb_get_time`, '%Y%m')
    </select>
 
    <select id="getRewardMoneyByUid" resultType="BigDecimal">
        SELECT IFNULL(SUM(v2.`hb_money`),0) FROM yeshi_ec_hongbao_v2 v2
        WHERE v2.`hb_uid` = #{uid}  AND v2.`hb_state` = 3 
        <if test="list != null and list.size() > 0">
            AND
            <foreach collection="list" item="item" open="(" separator="OR" close=")">
                v2.`hb_type` = #{item}
            </foreach>
        </if>
    </select>
        
    <select id="countValidOrderByUidAndTime" resultType="Long">
    SELECT COUNT(co.`co_id`) FROM yeshi_ec_hongbao_v2 v2
    LEFT JOIN yeshi_ec_hongbao_order h ON v2.`hb_id` = h.`ho_hongbao_id`
    LEFT JOIN yeshi_ec_common_order co ON h.`ho_order_id` = co.`co_id`
    WHERE v2.`hb_uid` = #{uid} AND (v2.`hb_type` = 1 OR v2.`hb_type` = 20)  AND v2.`hb_state` = 3 
            AND co.`co_payment` >= #{payment} AND co.`co_third_create_time` >= #{limitTime}
    </select>
    
    
    <select id="counOrderByUidAndOrderType" resultType="Long">
        SELECT COUNT(0)  FROM (SELECT co.`co_order_no` FROM yeshi_ec_hongbao_v2 v2
        LEFT JOIN yeshi_ec_hongbao_order h ON v2.`hb_id` = h.`ho_hongbao_id`
        LEFT JOIN yeshi_ec_common_order co ON h.`ho_order_id` = co.`co_id`
        WHERE v2.`hb_uid` = #{uid} AND v2.`hb_state` = 3 AND co.`co_payment` >=  #{payment} AND v2.`hb_type` = #{type}
        GROUP BY co.`co_order_no`,co.`co_source_type`)A
    </select>
    
    <select id="countOrderByUidAndSettled" resultType="Long">
        SELECT COUNT(0)  FROM (SELECT co.`co_order_no` FROM yeshi_ec_hongbao_v2 v2
        LEFT JOIN yeshi_ec_hongbao_order h ON v2.`hb_id` = h.`ho_hongbao_id`
        LEFT JOIN yeshi_ec_common_order co ON h.`ho_order_id` = co.`co_id`
        WHERE v2.`hb_uid` = #{uid} AND co.`co_settle_time` IS NOT NULL AND co.`co_payment` >=  #{payment} 
        AND (v2.`hb_type` = 1 OR v2.`hb_type` = 20)
        GROUP BY co.`co_order_no`,co.`co_source_type`)A
    </select>
    
    <select id="getRewardMoneyByDate" resultType="BigDecimal">
        SELECT (COALESCE(SUM(v2.`hb_money`),0)- COALESCE(SUM(d.`wr_money`),0))AS totalmoney FROM (
            SELECT * FROM yeshi_ec_hongbao_v2 v 
            WHERE v.hb_state <![CDATA[<>]]> 4 AND v.`hb_uid`= #{uid}
            <if test="type == 1"> <!-- 自购订单 -->
                AND (v.hb_type =1 or v.hb_type =2)
            </if>
            <if test="type == 2"> <!-- 分享订单 -->
                AND v.`hb_type` = 20
            </if>
            <if test="type == 3"> <!-- 邀请订单 -->
                AND (v.`hb_type` = 5 OR v.`hb_type` = 6 OR v.`hb_type` = 7 OR v.`hb_type` = 21 OR v.`hb_type` = 22)
            </if>
            <if test="type == 2 and day == 4"> <!-- 本月月将要到账 -->
                AND DATE_FORMAT(v.`hb_pre_get_time`,'%Y%m') =    DATE_FORMAT(CURDATE(),'%Y%m')
            </if>
            <if test="type == 3 and day == 4"> <!-- 本月将要到账 -->
                AND DATE_FORMAT(v.`hb_pre_get_time`,'%Y%m') =    DATE_FORMAT(CURDATE(),'%Y%m')
            </if>
            
            <if test="moneyState != null and moneyState == 1"> <!-- 未到账 -->
                AND (v.hb_state = 1 or v.hb_state = 2)
            </if>
            <if test="moneyState != null and moneyState == 2"> <!-- 已到账 -->
                AND v.hb_state = 3
            </if>
            <if test="moneyState != null and moneyState == 3"> <!-- 红包已失效 -->
                AND v.hb_state =4
            </if>
            <if test="moneyState != null and moneyState == 4"> <!-- 红包未失效 -->
                AND v.hb_state <![CDATA[<>]]> 4
            </if>
        )v2
        LEFT JOIN yeshi_ec_hongbao_order ho ON ho.`ho_hongbao_id` = IF(v2.hb_pid IS NULL,v2.hb_id,v2.hb_pid)
        LEFT JOIN yeshi_ec_common_order co ON co.`co_id` = ho.`ho_order_id`
        LEFT JOIN yeshi_ec_user_order_weiquan_record d ON d.wr_trade_id = co.`co_trade_id` AND d.wr_source_type = co.`co_source_type` AND d.wr_uid = #{uid}
        WHERE co.`co_state`<![CDATA[<>]]> 4
        <if test="day == 1"> <!-- 今天产生-->
            AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
        </if>
        <if test="day == 2"> <!-- 昨天产生-->
            AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
        </if>
        <if test="day == 3 or day == 10"> <!-- 本月产生 -->
            AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =    DATE_FORMAT(CURDATE(),'%Y%m')
        </if>
        <if test="day == 11"> <!-- 近3个月产生 -->
            AND co.`co_third_create_time` BETWEEN DATE_SUB(NOW(),INTERVAL 3 MONTH) AND NOW()
        </if>
        <if test="day == 12"> <!-- 近半年产生  -->
            AND co.`co_third_create_time` BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW()
        </if>
        <if test="day == 4 and type == 1"> <!-- 上月产生自购 -->
            AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'), DATE_FORMAT(co.`co_third_create_time`, '%Y%m')) = 1
        </if>
        
        <if test="moneyState != null and moneyState == 3"> <!-- 整个订单失效 -->
            AND co.co_state_whole_order = 3
        </if>
        
        <if test="orderState ==  1"> <!-- 有效订单: 整个订单有效 -->
            AND <![CDATA[co.`co_state` <> 3]]>
            AND (co.co_state_whole_order = 1 or co.co_state_whole_order = 2)
        </if>
        <if test="orderState ==  2"> <!-- 维权订单 -->
            AND co.co_state = 3
        </if>
        
        <!-- 订单号查询 -->
        <if test="orderNo != null and orderNo !='' ">AND co.co_order_no = #{orderNo}</if>
        
        <!-- 时间段筛选 -->
        <if test="startTime != null and startTime != '' ">
            AND <![CDATA[co.co_third_create_time >= #{startTime}]]>
        </if>
        <if test="endTime != null and endTime != '' ">
            AND <![CDATA[co.co_third_create_time <= #{endTime}]]>
        </if>
        
        <if test="listSource != null and listSource.size() > 0">
            <foreach collection="listSource" item="item" open="AND (" close=")" separator="or">
                co.co_source_type = #{item}
            </foreach>
        </if>
    </select>
    
    
    <select id="countOrderByTypeAndDate" resultType="Long">
        SELECT COUNT(1) FROM (SELECT ho.* FROM yeshi_ec_hongbao_order ho 
            LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2 
            WHERE 1=1
            <if test="orderType == 1"> <!-- 自购订单 -->
                AND (v2.hb_type =1 or v2.hb_type =2)
            </if>
            <if test="orderType == 2"> <!-- 分享订单 -->
                AND v2.`hb_type` = 20
            </if>
            <if test="orderType == 3"> <!-- 邀请订单 -->
                AND  v2.`hb_type` = 6
            </if>
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
        LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
        WHERE hb.hb_id IS NOT NULL AND (co.`co_state`=1 OR co.`co_state`=2) 
            AND DATE_FORMAT(co.`co_third_create_time`,'%Y-%m-%d') = #{preDay}
        GROUP BY co.`co_order_no`, co.`co_source_type`)A
    </select>
    
</mapper>