admin
2019-01-29 033a7cd02b9799a0acf42290fdc84d74bdcee79c
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?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.order.CommonOrderMapper">
    <resultMap id="BaseResultMap" type="com.yeshi.fanli.entity.order.CommonOrder">
        <id column="co_id" property="id" jdbcType="BIGINT" />
        <result column="co_order_no" property="orderNo" jdbcType="VARCHAR" />
        <result column="co_source_type" property="sourceType" jdbcType="INTEGER" />
        <result column="co_source_position" property="sourcePosition"
            jdbcType="VARCHAR" />
        <result column="co_count" property="count" jdbcType="INTEGER" />
        <result column="co_state" property="state" jdbcType="INTEGER" />
        <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
        <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
        <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
        <result column="co_payment" property="payment" jdbcType="DECIMAL" />
        <result column="co_settlement" property="settlement" jdbcType="DECIMAL" />
        <result column="co_third_create_time" property="thirdCreateTime"
            jdbcType="TIMESTAMP" />
        <result column="co_settle_time" property="settleTime" jdbcType="TIMESTAMP" />
        <result column="co_order_by" property="orderBy" jdbcType="INTEGER" />
        <result column="co_create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="co_update_time" property="updateTime" jdbcType="TIMESTAMP" />
 
        <association property="userInfo" column="co_uid"
            javaType="com.yeshi.fanli.entity.bus.user.UserInfo">
            <id column="co_uid" property="id" jdbcType="BIGINT" />
        </association>
 
        <association property="commonOrderGoods" column="co_order_goods_id"
            javaType="com.yeshi.fanli.entity.order.CommonOrderGoods">
            <id column="co_order_goods_id" property="id" jdbcType="BIGINT" />
        </association>
    </resultMap>
 
 
    <resultMap id="BaseDetailResultMap" type="com.yeshi.fanli.entity.order.CommonOrder">
        <id column="co_id" property="id" jdbcType="BIGINT" />
        <result column="co_order_no" property="orderNo" jdbcType="VARCHAR" />
        <result column="co_source_type" property="sourceType" jdbcType="INTEGER" />
        <result column="co_source_position" property="sourcePosition"
            jdbcType="VARCHAR" />
        <result column="co_count" property="count" jdbcType="INTEGER" />
        <result column="co_state" property="state" jdbcType="INTEGER" />
        <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
        <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
        <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
        <result column="co_payment" property="payment" jdbcType="DECIMAL" />
        <result column="co_settlement" property="settlement" jdbcType="DECIMAL" />
        <result column="co_third_create_time" property="thirdCreateTime"
            jdbcType="TIMESTAMP" />
        <result column="co_settle_time" property="settleTime" jdbcType="TIMESTAMP" />
        <result column="co_order_by" property="orderBy" jdbcType="INTEGER" />
        <result column="co_create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="co_update_time" property="updateTime" jdbcType="TIMESTAMP" />
 
        <association property="userInfo" column="co_uid"
            javaType="com.yeshi.fanli.entity.bus.user.UserInfo">
            <id column="co_uid" property="id" jdbcType="BIGINT" />
        </association>
 
        <association property="commonOrderGoods"
            resultMap="com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper.BaseResultMap">
 
        </association>
    </resultMap>
 
    <resultMap id="ResultMap" type="com.yeshi.fanli.vo.order.CommonOrderVO">
        <id column="co_id" property="id" jdbcType="BIGINT" />
        <result column="co_order_no" property="orderNo" jdbcType="VARCHAR" />
        <result column="co_source_type" property="sourceType" jdbcType="INTEGER" />
        <result column="co_source_position" property="sourcePosition"
            jdbcType="VARCHAR" />
        <result column="co_count" property="count" jdbcType="INTEGER" />
        <result column="co_state" property="state" jdbcType="INTEGER" />
        <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
        <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
        <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
        <result column="co_payment" property="payment" jdbcType="DECIMAL" />
        <result column="co_settlement" property="settlement" jdbcType="DECIMAL" />
        <result column="co_third_create_time" property="thirdCreateTime"
            jdbcType="TIMESTAMP" />
        <result column="co_settle_time" property="settleTime" jdbcType="TIMESTAMP" />
        <result column="co_order_by" property="orderBy" jdbcType="INTEGER" />
        <result column="co_create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="co_update_time" property="updateTime" jdbcType="TIMESTAMP" />
 
        <result column="totalMoney" property="hongBao" jdbcType="DECIMAL" />
        <result column="hongBaoState" property="hongBaoState" jdbcType="INTEGER" />
        <result column="hongBaoType" property="hongBaoType" jdbcType="INTEGER" />
        <result column="accountTime" property="accountTime" jdbcType="TIMESTAMP" />
        <result column="preAccountTime" property="preAccountTime"
            jdbcType="TIMESTAMP" />
 
        <result column="totalCount" property="totalCount" jdbcType="INTEGER" />
        <result column="totalSettlement" property="totalSettlement" jdbcType="DECIMAL" />
        <result column="totalPayment" property="totalPayment" jdbcType="DECIMAL" />
        
        
        <result column="userId" property="userId" jdbcType="VARCHAR" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="userPortrait" property="userPortrait" jdbcType="VARCHAR" />
        
        <result column="levelOneId" property="levelOneId" jdbcType="VARCHAR" />
        <result column="levelOneMoney" property="levelOneMoney" jdbcType="VARCHAR" />
        <result column="levelTwoId" property="levelTwoId" jdbcType="VARCHAR" />
        <result column="levelTwoMoney" property="levelTwoMoney" jdbcType="VARCHAR" />
 
        <association property="userInfo" column="co_uid"
            resultMap="com.yeshi.fanli.dao.mybatis.UserInfoMapper.BaseResultMap">
        </association>
 
        <association property="commonOrderGoods" column="co_order_goods_id"
            resultMap="com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper.BaseResultMap">
        </association>
 
    </resultMap>
    
 
    <sql id="Base_Column_List">co_id,co_uid,co_order_no,co_source_type,co_source_position,co_order_goods_id,co_count,co_state,co_state_whole_order,co_estimate,co_eIncome,co_payment,co_settlement,co_third_create_time,co_settle_time,co_order_by,co_create_time,co_update_time
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap"
        parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List" />
        from yeshi_ec_common_order where co_id = #{id,jdbcType=BIGINT}
    </select>
    <select id="countByOrderNOAndOrderType" resultType="java.lang.Integer">
        select
        count(co_id) from yeshi_ec_common_order where co_order_no=#{orderNO}
        and co_source_type=#{orderType}
    </select>
 
    <select id="selectByOrderNoAndOrderTypeAndOrderBy" resultMap="BaseResultMap">
        select
        * from yeshi_ec_common_order where co_order_no=#{orderNo}
        and
        co_source_type=#{orderType} and co_order_by=#{orderBy}
    </select>
 
    <select id="countByUidAndOrderStateWithOrderBalanceTime"
        resultType="java.lang.Long">
        SELECT COUNT(*) FROM yeshi_ec_common_order co WHERE
        co.`co_state`=#{state} and co.`co_uid`=#{uid} and co.`co_settle_time`
        is not null and co.`co_settle_time`>=#{minDate} and
        #{maxDate}>co.`co_settle_time`
    </select>
    
    
    <select id="listBySourceTypeAndOrderNo"
        resultMap="BaseResultMap">
        SELECT * FROM yeshi_ec_common_order co WHERE
        co.co_source_type=#{sourceType} and co_order_no=#{orderNo}
    </select>
    
 
 
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">delete from
        yeshi_ec_common_order where co_id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" parameterType="com.yeshi.fanli.entity.order.CommonOrder"
        useGeneratedKeys="true" keyProperty="id">insert into yeshi_ec_common_order
        (co_id,co_uid,co_order_no,co_source_type,co_source_position,co_order_goods_id,co_count,co_state,co_estimate,co_eIncome,co_payment,co_settlement,co_third_create_time,co_settle_time,co_create_time,co_update_time)
        values
        (#{id,jdbcType=BIGINT},#{userInfo.id,jdbcType=BIGINT},#{orderNo,jdbcType=VARCHAR},#{sourceType,jdbcType=INTEGER},#{sourcePosition,jdbcType=VARCHAR},#{commonOrderGoods.id,jdbcType=BIGINT},#{count,jdbcType=INTEGER},#{state,jdbcType=INTEGER},#{estimate,jdbcType=DECIMAL},#{eIncome,jdbcType=DECIMAL},#{payment,jdbcType=DECIMAL},#{settlement,jdbcType=DECIMAL},#{thirdCreateTime,jdbcType=TIMESTAMP},#{settleTime,jdbcType=TIMESTAMP},#{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP})
    </insert>
    <insert id="insertSelective" parameterType="com.yeshi.fanli.entity.order.CommonOrder"
        useGeneratedKeys="true" keyProperty="id">
        insert into yeshi_ec_common_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">co_id,</if>
            <if test="userInfo != null">co_uid,</if>
            <if test="orderNo != null">co_order_no,</if>
            <if test="sourceType != null">co_source_type,</if>
            <if test="sourcePosition != null">co_source_position,</if>
            <if test="commonOrderGoods != null">co_order_goods_id,</if>
            <if test="count != null">co_count,</if>
            <if test="state != null">co_state,</if>
            <if test="stateWholeOrder != null">co_state_whole_order,</if>
            <if test="estimate != null">co_estimate,</if>
            <if test="eIncome != null">co_eIncome,</if>
            <if test="payment != null">co_payment,</if>
            <if test="settlement != null">co_settlement,</if>
            <if test="thirdCreateTime != null">co_third_create_time,</if>
            <if test="settleTime != null">co_settle_time,</if>
            <if test="orderBy != null">co_order_by,</if>
            <if test="createTime != null">co_create_time,</if>
            <if test="updateTime != null">co_update_time,</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=BIGINT},</if>
            <if test="userInfo != null">#{userInfo.id,jdbcType=BIGINT},</if>
            <if test="orderNo != null">#{orderNo,jdbcType=VARCHAR},</if>
            <if test="sourceType != null">#{sourceType,jdbcType=INTEGER},</if>
            <if test="sourcePosition != null">#{sourcePosition,jdbcType=VARCHAR},</if>
            <if test="commonOrderGoods != null">#{commonOrderGoods.id,jdbcType=BIGINT},</if>
            <if test="count != null">#{count,jdbcType=INTEGER},</if>
            <if test="state != null">#{state,jdbcType=INTEGER},</if>
            <if test="stateWholeOrder != null">#{stateWholeOrder,jdbcType=INTEGER},</if>
            <if test="estimate != null">#{estimate,jdbcType=DECIMAL},</if>
            <if test="eIncome != null">#{eIncome,jdbcType=DECIMAL},</if>
            <if test="payment != null">#{payment,jdbcType=DECIMAL},</if>
            <if test="settlement != null">#{settlement,jdbcType=DECIMAL},</if>
            <if test="thirdCreateTime != null">#{thirdCreateTime,jdbcType=TIMESTAMP},</if>
            <if test="settleTime != null">#{settleTime,jdbcType=TIMESTAMP},</if>
            <if test="orderBy != null">#{orderBy,jdbcType=INTEGER},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.yeshi.fanli.entity.order.CommonOrder">update
        yeshi_ec_common_order set co_uid =
        #{userInfo.id,jdbcType=BIGINT},co_order_no =
        #{orderNo,jdbcType=VARCHAR},co_source_type =
        #{sourceType,jdbcType=INTEGER},co_source_position =
        #{sourcePosition,jdbcType=VARCHAR},co_order_goods_id =
        #{commonOrderGoods.id,jdbcType=BIGINT},co_count =
        #{count,jdbcType=INTEGER},co_state =
        #{state,jdbcType=INTEGER},co_state_whole_order =
        #{stateWholeOrder,jdbcType=INTEGER},co_estimate =
        #{estimate,jdbcType=DECIMAL},co_eIncome =
        #{eIncome,jdbcType=DECIMAL},co_payment =
        #{payment,jdbcType=DECIMAL},co_settlement =
        #{settlement,jdbcType=DECIMAL},co_third_create_time =
        #{thirdCreateTime,jdbcType=TIMESTAMP},co_settle_time =
        #{settleTime,jdbcType=TIMESTAMP},co_order_by =
        #{orderBy,jdbcType=INTEGER},co_create_time =
        #{createTime,jdbcType=TIMESTAMP},co_update_time =
        #{updateTime,jdbcType=TIMESTAMP} where co_id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.yeshi.fanli.entity.order.CommonOrder">
        update yeshi_ec_common_order
        <set>
            <if test="userInfo != null">co_uid=#{userInfo.id,jdbcType=BIGINT},</if>
            <if test="orderNo != null">co_order_no=#{orderNo,jdbcType=VARCHAR},</if>
            <if test="sourceType != null">co_source_type=#{sourceType,jdbcType=INTEGER},</if>
            <if test="sourcePosition != null">co_source_position=#{sourcePosition,jdbcType=VARCHAR},
            </if>
            <if test="commonOrderGoods != null">co_order_goods_id=#{commonOrderGoods.id,jdbcType=BIGINT},
            </if>
            <if test="count != null">co_count=#{count,jdbcType=INTEGER},</if>
            <if test="state != null">co_state=#{state,jdbcType=INTEGER},</if>
            <if test="stateWholeOrder != null">co_state_whole_order=#{stateWholeOrder,jdbcType=INTEGER},</if>
            <if test="estimate != null">co_estimate=#{estimate,jdbcType=DECIMAL},</if>
            <if test="eIncome != null">co_eIncome=#{eIncome,jdbcType=DECIMAL},</if>
            <if test="payment != null">co_payment=#{payment,jdbcType=DECIMAL},</if>
            <if test="settlement != null">co_settlement=#{settlement,jdbcType=DECIMAL},</if>
            <if test="thirdCreateTime != null">co_third_create_time=#{thirdCreateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="settleTime != null">co_settle_time=#{settleTime,jdbcType=TIMESTAMP},</if>
            <if test="orderBy != null">co_order_by=#{orderBy,jdbcType=INTEGER},</if>
            <if test="createTime != null">co_create_time=#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime != null">co_update_time=#{updateTime,jdbcType=TIMESTAMP},</if>
        </set>
        where co_id = #{id,jdbcType=BIGINT}
    </update>
 
 
    
    <sql id="SELECT_PARAM_ORDER_STATE">
        <if test="orderState != null and 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 != null and orderState ==  2">
            AND co.co_state = 3 <!-- 维权订单 -->
        </if>
        <if test="orderState != null and orderState ==  3">
            <!-- 整个订单失效 -->
            AND co.co_state_whole_order = 3 
        </if>
    </sql>
 
    <sql id="SELECT_PARAM_ORDER_CREATE_TIME">
        <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="day != null and day == 1"> <!-- 今天 -->
            AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
        </if>
        <if test="day != null and day == 2">  <!-- 昨天 -->
            AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
        </if>
        <if test="day != null and day == 3">  <!-- 本月 -->
            AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
            DATE_FORMAT(CURDATE(),'%Y%m')
        </if>
        <if test="day != null and day == 4">  <!-- 上月 -->
            AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
            DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
        </if>
    </sql>
    
    <sql id="SELECT_PARAM_HONGBAO_TYPE">
        <if test="type != null and type == 1"> <!-- 自购订单 -->
            AND (v2.hb_type =1 or v2.hb_type =2)
        </if>
        <if test="type != null and type == 2"> <!-- 分享订单 -->
            AND v2.`hb_type` = 20
        </if>
        <if test="type != null and type == 3"> <!-- 邀请订单 -->
            AND (v2.`hb_type` = 5 OR v2.`hb_type` = 6 OR v2.`hb_type` = 7 OR v2.`hb_type` = 21 OR v2.`hb_type` = 22)
        </if>
    </sql>
    
    <sql id="SELECT_PARAM_HONGBAO_STATE">
        <if test="state != null and state == 1">  <!-- 未到账 -->
            AND (hb.hb_state = 1 or hb.hb_state = 2)
        </if>
        <if test="state != null and state == 2"> <!-- 已到账 -->
            AND hb.hb_state = 3
        </if>
        <if test="state != null and state == 3">  <!-- 红包已失效、整个订单失效 -->
            AND hb.hb_state =4 AND co.co_state_whole_order = 3 
        </if>
    </sql>
    
    <sql id="Hongbao_Column_List">
        hb_id,hb_uid,hb_urank,hb_pid,hb_type,hb_state,hb_pre_get_time,hb_get_time,hb_version,
        IF(v2.`hb_state` = 4, 0, v2.hb_money)AS hb_money
    </sql>
 
    <select id="listUserOrder" resultMap="ResultMap">   
        <!-- 查询用户订单 -->
        SELECT COALESCE(SUM(hb.`hb_money`),0)AS totalMoney,
                hb.hb_state AS hongBaoState,
                hb.`hb_type` AS hongBaoType,
                MAX(hb.`hb_get_time`) AS accountTime,
                MAX(hb.`hb_pre_get_time`)AS preAccountTime,
                co.*
        FROM yeshi_ec_hongbao_order ho 
        
        LEFT JOIN (SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= #{uid} 
                        <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                )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
              <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
              <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
              <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
              <if test="orderNo != null and orderNo !='' ">
                AND co.co_order_no = #{orderNo}
              </if>
        GROUP BY co.`co_order_no`, co.`co_source_type`
        ORDER BY co.co_third_create_time DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countUserOrder" resultType="java.lang.Long">   
        <!-- 统计用户订单数量 -->
        SELECT IFNULL(COUNT(ho_id),0) FROM 
            (SELECT ho.ho_id FROM yeshi_ec_hongbao_order ho 
             LEFT JOIN (SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= #{uid} 
                        <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                )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
                     <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
                   <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
                   <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
                   <if test="orderNo != null and orderNo !='' ">
                        AND co.co_order_no = #{orderNo}
                   </if>
             GROUP BY co.`co_order_no`, co.`co_source_type`
        )A
    </select>
 
    <select id="listOrderGoodsInfo" resultMap="ResultMap"    parameterType="java.util.List">
         <!-- 查询用户订单商品信息 -->
        SELECT COALESCE(SUM(tc.`co_payment`),0)AS totalPayment,COALESCE(SUM(tc.`co_settlement`),0)AS totalSettlement,
            COALESCE(SUM(tc.`co_count`),0)AS totalCount,tc.`co_source_type`,tc.`co_order_no`,tg.*,tc.co_id
        FROM
        yeshi_ec_common_order_goods tg
        LEFT JOIN yeshi_ec_common_order tc ON tc.`co_order_goods_id` = tg.`cog_id`
        WHERE
        <foreach collection="list" item="item" separator=" OR ">
            (tc.`co_source_type` = #{item.sourceType,jdbcType=INTEGER} AND
            tc.`co_order_no` = #{item.orderNo,jdbcType=VARCHAR})
        </foreach>
        GROUP BY tc.`co_order_no`,tc.`co_order_goods_id`
    </select>
 
 
    <select id="countHistoryOrder" resultType="java.util.HashMap">
        <!-- 统计历史订单数量 -->
        SELECT SUM(A.self)AS totalSelf,SUM(A.shared)AS totalShared,SUM(A.invite)AS totalInvite
        FROM ( 
            <!-- 返利订单 -->
            SELECT IFNULL(COUNT(ho_id),0) AS self,0 AS shared,0 AS invite
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho 
                LEFT JOIN ( 
                    SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= ${uid} AND (v2.`hb_type` = 1 OR v2.`hb_type` = 2)
                    ) hb  ON hb.hb_id=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
                <if test="day != null and day == 1"> <!-- 今天 -->
                    AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
                </if>
                <if test="day != null and day == 2">  <!-- 昨天 -->
                    AND TO_DAYS(NOW()) - TO_DAYS(co.co_third_create_time) = 1
                </if>
                <if test="day != null and day == 3">  <!-- 本月 -->
                    AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
                    DATE_FORMAT(CURDATE(),'%Y%m')
                </if>
                <if test="day != null and day == 4">  <!-- 上月 -->
                    AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
                    DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
                </if>
            GROUP BY co.`co_order_no`, co.`co_source_type`)YX
            
            UNION ALL
            <!-- 分享订单 -->
            SELECT 0 AS self,IFNULL(COUNT(ho_id),0) AS shared,0 AS invite
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho 
                    LEFT JOIN ( 
                        SELECT * FROM yeshi_ec_hongbao_v2 v2 
                        WHERE v2.`hb_uid`= ${uid} AND v2.`hb_type` = 20
                    ) hb  ON hb.hb_id=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
                    <if test="day != null and day == 1"> <!-- 今天 -->
                        AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
                    </if>
                    <if test="day != null and day == 2">  <!-- 昨天 -->
                        AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
                    </if>
                    <if test="day != null and day == 3">  <!-- 本月 -->
                        AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
                        DATE_FORMAT(CURDATE(),'%Y%m')
                    </if>
                    <if test="day != null and day == 4">  <!-- 上月 -->
                        AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
                        DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
                    </if>
                GROUP BY co.`co_order_no`, co.`co_source_type`)WQ
                
            UNION ALL
            <!-- 邀请订单 -->
            SELECT 0 AS self,0 AS shared,IFNULL(COUNT(ho_id),0) AS invite 
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho     
                LEFT JOIN ( 
                    SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= ${uid}
                         AND (v2.`hb_type` = 5 OR v2.`hb_type` = 6 OR v2.`hb_type` = 7
                               OR v2.`hb_type` = 21 OR v2.`hb_type` = 22 )
                    ) hbp  ON hbp.hb_pid=ho.`ho_hongbao_id`
                LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
                WHERE  hbp.hb_id IS NOT NULL 
                    <if test="day != null and day == 1"> <!-- 今天 -->
                        AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
                    </if>
                    <if test="day != null and day == 2">  <!-- 昨天 -->
                        AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
                    </if>
                    <if test="day != null and day == 3">  <!-- 本月 -->
                        AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
                        DATE_FORMAT(CURDATE(),'%Y%m')
                    </if>
                    <if test="day != null and day == 4">  <!-- 上月 -->
                        AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
                        DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
                    </if>
                GROUP BY co.`co_order_no`, co.`co_source_type`)SX
        )A
    </select>
    
    
    <select id="countByUidAndOrderState" resultType="java.util.HashMap">
        SELECT SUM(A.valid)AS totalValid, SUM(A.proces)AS totalProces,SUM(A.Invalid)AS totalInvite
        FROM ( 
            <!-- 有效订单 -->
            SELECT IFNULL(COUNT(ho_id),0) AS valid,0 AS proces,0 AS Invalid
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho 
            LEFT JOIN ( 
                SELECT * FROM yeshi_ec_hongbao_v2 v2 
                WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
            ) 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
                  <![CDATA[AND co.`co_state` <> 3]]>
                  AND (co.`co_state_whole_order` =1 OR co.`co_state_whole_order` = 2) 
                <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            GROUP BY co.`co_order_no`, co.`co_source_type`)YX
            
            UNION ALL
            <!-- 维权订单 -->
            SELECT 0 AS valid,IFNULL(COUNT(ho_id),0) AS proces,0 AS Invalid
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho 
                LEFT JOIN ( 
                    SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                ) 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` = 3 
                    <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
                GROUP BY co.`co_order_no`, co.`co_source_type`)WQ
                
            UNION ALL
            <!-- 失效订单 -->
            SELECT 0 AS valid,0 AS proces,IFNULL(COUNT(ho_id),0) AS Invalid 
            FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho     
                LEFT JOIN ( 
                    SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                ) 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_whole_order` = 3 
                    <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
                GROUP BY co.`co_order_no`, co.`co_source_type`)SX
        )A
        
    </select>
 
 
    <select id="countBonusOrderNumber" resultType="java.lang.Long">
        <!-- 奖金统计:订单数量 -->
        SELECT IFNULL(COUNT(ho_id),0)
        FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho     
                LEFT JOIN ( 
                    SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                ) 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 
                    <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
                GROUP BY co.`co_order_no`, co.`co_source_type`
        )A
    </select>
 
    <select id="countBonusOrderMoney" resultType="java.math.BigDecimal">
        <!-- 奖金统计:订单总返利金额 去掉已失效 -->
        SELECT COALESCE(SUM(hb.`hb_money`),0)AS totalmoney
        FROM yeshi_ec_hongbao_order ho     
        LEFT JOIN ( 
            SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2 
            WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                  AND (v2.hb_state = 1 or v2.hb_state = 2 or v2.hb_state = 3)
        ) 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
            <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            <if test="day != null and day == 4"> 
                AND co.`co_state` = 2  <!-- 上月 :统计已收货 -->
            </if>
    </select>
    
    <select id="countBonusOrderMoneyAndNumber" resultType="java.util.HashMap">
        SELECT COUNT(*) AS totalNum, CAST(SUM(moneys) AS DECIMAL(19,2)) AS totalmoney FROM 
            (SELECT CAST(SUM(hb.`hb_money`) AS DECIMAL(19,2))AS moneys    
            FROM yeshi_ec_hongbao_order ho     
            LEFT JOIN ( 
                SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2 
                WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                      AND (v2.hb_state = 1 or v2.hb_state = 2 or v2.hb_state = 3)
            ) 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 
                <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
                <if test="day != null and day == 4"> 
                    AND co.`co_state` = 2 <!-- 上月 :统计已收货 -->
                </if>
                GROUP BY co.`co_order_no`, co.`co_source_type`
            )A
    </select>
    
    <select id="listQueryByUid" resultMap="ResultMap">   
        <!-- 查询用户订单 -->
        SELECT  hb.`hb_money` AS totalMoney,
                hb.hb_state AS hongBaoState,
                hb.`hb_type` AS hongBaoType,
                hb.`hb_get_time`  AS accountTime,
                hb.`hb_pre_get_time` AS preAccountTime,
                co.*,cog.*
        FROM yeshi_ec_hongbao_order ho 
        
        LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= #{uid} 
                        <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                        <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
                )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`
        LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
        WHERE hb.hb_id IS NOT NULL
              <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
              <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
              <if test="orderNo != null and orderNo !='' ">
                AND co.co_order_no = #{orderNo}
              </if>
        ORDER BY co.co_third_create_time DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countQueryByUid" resultType="java.lang.Long">   
        <!-- 统计用户订单数量 -->
        SELECT IFNULL(COUNT(ho_id),0) FROM 
            (SELECT ho.ho_id FROM yeshi_ec_hongbao_order ho 
             LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2 
                    WHERE v2.`hb_uid`= #{uid} 
                        <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                        <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
                )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
                     <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
                   <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
                   <if test="orderNo != null and orderNo !='' ">
                        AND co.co_order_no = #{orderNo}
                   </if>
        )A
    </select>
 
    <select id="getBuFenOrderState" resultMap="ResultMap">
        SELECT co.co_state,v2.`hb_state` AS hongBaoState  FROM `yeshi_ec_common_order` co 
        LEFT JOIN  yeshi_ec_hongbao_order ho  ON co.`co_id`=ho.`ho_order_id`
        LEFT JOIN  yeshi_ec_hongbao_v2 v2 ON IF(v2.hb_pid IS NULL,v2.hb_id,v2.hb_pid)=ho.`ho_hongbao_id`
        WHERE co.`co_order_no` = #{orderNo} AND co.co_state<![CDATA[<>]]> 4 AND v2.hb_uid = #{uid}
        ORDER BY co.`co_state` DESC 
        LIMIT 1
    </select>
 
    <select id="listQuery" resultMap="ResultMap">   
       
        SELECT A.*,u.id AS userId,u.`nick_name` AS userName,u.`portrait` AS userPortrait,
           pv2.`hb_uid` AS levelOneId,pv2.`hb_money` AS levelOneMoney,
           ppv2.`hb_uid` AS levelTwoId,ppv2.`hb_money` AS levelTwoMoney
           
        FROM  (SELECT  hb.hb_id,hb.hb_uid,hb.`hb_money` AS totalMoney,
                hb.hb_state AS hongBaoState,
                hb.`hb_type` AS hongBaoType,
                hb.`hb_get_time`  AS accountTime,
                hb.`hb_pre_get_time` AS preAccountTime,
                co.*,cog.*
            FROM yeshi_ec_hongbao_order ho 
            
            LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2 
                       WHERE 1=1
                            <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                            <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
                    )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`
            LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
            WHERE hb.hb_id IS NOT NULL
                <include refid="SELECT_PARAM_ORDER_STATE"/>   <!-- 订单状态 -->
                <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="key != null and key != '' ">
                    <if test="keyType == 1 ">
                        AND co.co_order_no = ${key}
                    </if>
                    <if test="keyType == 2 ">
                        AND hb.hb_uid like = ${key}
                    </if>
                </if>
                
            ORDER BY co.co_third_create_time DESC
            LIMIT ${start},${count}
        ) A
        LEFT JOIN  yeshi_ec_user u ON  u.id = A.hb_uid
        LEFT JOIN  yeshi_ec_hongbao_v2 pv2 ON pv2.`hb_pid` = A.hb_id 
        LEFT JOIN  yeshi_ec_hongbao_v2 ppv2 ON ppv2.`hb_pid` = A.hb_id 
        
    </select>
 
    <select id="countQuery" resultType="java.lang.Long">   
        SELECT  count(ho.ho_id)
        FROM yeshi_ec_hongbao_order ho 
        
        LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2 
                   WHERE 1=1
                        <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                        <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
                )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`
        LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
        WHERE hb.hb_id IS NOT NULL
            <include refid="SELECT_PARAM_ORDER_STATE"/>   <!-- 订单状态 -->
            
            <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="key != null and key != '' ">
                <if test="keyType == 1 ">
                    AND co.co_order_no = ${key}
                </if>
                <if test="keyType == 2 ">
                    AND hb.hb_uid like = ${key}
                </if>
            </if>
    </select>
 
</mapper>