2
sunshine
2024-11-05 d1f9fb55a136e589a5ad588ed9a93ce9272917da
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?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.nova.sankuai.infra.mapper.ChannelLogMapper">
 
    <select id="importTotal" resultType="com.nova.sankuai.domain.vo.channel.ChannelReportTotalVo">
        select channelName,channelCode,sum(successCountSum) as successCountSum,sum(failCountSum) as failCountSum,
        #{reportDto.startDate} as startDate, #{reportDto.endDate} as endDate from (
        select ci.name as channelName,ci.code as channelCode,IF((cu.id is not null),1,0) as successCountSum,
        0 as failCountSum
        from channel_info ci
        left join customer cu on cu.source_channel = ci.code
        <if test="reportDto.startDate != null">
            and cu.apply_date &gt;= #{reportDto.startDate}
        </if>
        <if test="reportDto.endDate != null">
            and cu.apply_date &lt;= #{reportDto.endDate}
        </if>
        <where>
            ci.type = 0
            <if test="reportDto.channelCode != null">
                and ci.code = #{reportDto.channelCode}
            </if>
        </where>
        group by cu.id
        ) t group by channelCode
    </select>
 
 
    <select id="importDetail" resultType="com.nova.sankuai.domain.vo.channel.ChannelImportReportVo">
        select cu.id as recordId,
        cu.phone as phone,
        cu.name as name,
        su.company_name as mechanismName,
        ci.name as channelName,
        ci.code as channelCode,
        cu.apply_date as applyDate,
        cu.sync_channel_name as syncChannelName
        from customer cu
        left join channel_info ci on cu.source_channel = ci.code
        left join sys_user su on cu.mechanism_id = su.id
        <where>
            cu.source_channel = #{reportDto.channelCode}
            <if test="reportDto.startDate != null">
                and cu.apply_date &gt;= #{reportDto.startDate}
            </if>
            <if test="reportDto.endDate != null">
                and cu.apply_date &lt;= #{reportDto.endDate}
            </if>
            <if test="reportDto.name != null and reportDto.name !=''">
                and cu.name like concat('%',#{reportDto.name},'%')
            </if>
            <if test="reportDto.phone != null and reportDto.phone !=''">
                and cu.phone like concat('%',#{reportDto.phone},'%')
            </if>
            <if test="reportDto.mechanismName != null and reportDto.mechanismName !=''">
                and su.company_name like concat('%',#{reportDto.mechanismName},'%')
            </if>
        </where>
    </select>
 
 
    <select id="exportTotal" resultType="com.nova.sankuai.domain.vo.channel.ChannelReportTotalVo">
        select ci.name as channelName,ci.code as channelCode,
        sum(IF((cl.status='1'),1,0)) as successCountSum,
        #{reportDto.startDate} as startDate,
        #{reportDto.endDate} as endDate
        from (select * from channel_log group by customer_record_id,source,export_source,record_id,status,type) as cl
        inner join channel_info ci on cl.export_source = ci.code
        <if test="reportDto.startDate != null">
            and cl.creation_date &gt;= #{reportDto.startDate}
        </if>
        <if test="reportDto.endDate != null">
            and cl.creation_date &lt;= #{reportDto.endDate}
        </if>
        <where>
            ci.type = 1 and ci.status = 1 and ci.code!=12
            <if test="reportDto.channelCode != null">
                and ci.code = #{reportDto.channelCode}
            </if>
        </where>
        group by channelCode
    </select>
 
    <select id="exportSupermarketTotal" resultType="com.nova.sankuai.domain.vo.channel.ChannelReportTotalVo">
        select concat(ls.`name`,"(",ci.`name`,")") as channelName,
        cl.source as importChannel,
        cl.export_source as channelCode,
        count(cl.id) as successCountSum,
        #{reportDto.startDate} as startDate,
        #{reportDto.endDate} as endDate,
        ls.id as supermarketId
        from (
        select cl.id,cl.source,cl.export_source,cl.record_id, cl.creation_date,
        date_format(cl.creation_date,'%Y/%m/%d') as date
        from channel_log cl
        left join customer cu on cl.customer_record_id = cu.id
        where cl.export_source = 12 and cl.status = 1 and cl.type = 1
        group by cl.source,cl.record_id,cu.phone,date
        ) as cl
        left join loan_supermarket ls on record_id = ls.id
        left join channel_info ci on cl.source = ci.`code`
        <where>
            ls.delete_flag = 0
            <if test="reportDto.startDate != null">
                and cl.creation_date &gt;= #{reportDto.startDate}
            </if>
            <if test="reportDto.endDate != null">
                and cl.creation_date &lt;= #{reportDto.endDate}
            </if>
        </where>
        group by cl.record_id,cl.source
    </select>
 
 
    <select id="exportDetail" resultType="com.nova.sankuai.domain.vo.channel.ChannelExportReportVo">
        select cl.customer_record_id as recordId,
        cu.phone as phone,
        cu.name as name,
        su.company_name as mechanismName,
        ci.name as exportChannel,
        ici.name as importChannel,
        cl.export_source as channelCode,
        cu.apply_date as applyDate
        from (select * from channel_log group by customer_record_id,source,export_source,record_id,status,type) as cl
        left join channel_info ci on cl.export_source = ci.code
        left join channel_info ici on cl.source = ici.code
        left join customer cu on cl.customer_record_id = cu.id and cu.delete_flag = 0
        left join sys_user su on cu.mechanism_id = su.id
        <where>
            cl.export_source = #{reportDto.channelCode} and cl.status = 1 and cl.type = 1
            <if test="reportDto.startDate != null">
                and cl.creation_date &gt;= #{reportDto.startDate}
            </if>
            <if test="reportDto.endDate != null">
                and cl.creation_date &lt;= #{reportDto.endDate}
            </if>
            <if test="reportDto.importChannel != null">
                and cl.source = #{reportDto.importChannel}
            </if>
            <if test="reportDto.name != null and reportDto.name !=''">
                and cu.name like concat('%',#{reportDto.name},'%')
            </if>
            <if test="reportDto.phone != null and reportDto.phone !=''">
                and cu.phone like concat('%',#{reportDto.phone},'%')
            </if>
            <if test="reportDto.mechanismName != null and reportDto.mechanismName !=''">
                and su.company_name like concat('%',#{reportDto.mechanismName},'%')
            </if>
        </where>
    </select>
 
 
    <select id="exportLoanMarketDetail" resultType="com.nova.sankuai.domain.vo.channel.ChannelExportReportVo">
        select cl.customer_record_id as recordId,
        cl.phone as phone,
        cl.name as name,
        su.company_name as mechanismName,
        ci.name as exportChannel,
        ici.name as importChannel,
        cl.export_source as channelCode,
        cl.apply_date as applyDate,
        ls.name as supermarketName
        from (
        select cl.id,cl.source,cl.export_source,cl.record_id,cl.customer_record_id,
        cu.phone,cu.name,cu.apply_date,cu.mechanism_id, cl.creation_date,
        date_format(cl.creation_date,'%Y/%m/%d') as date
        from channel_log cl
        left join customer cu on cl.customer_record_id = cu.id and cu.delete_flag = 0
        where cl.export_source = 12 and cl.status = 1 and cl.type = 1
        group by cl.source,cl.record_id,cu.phone,date
        ) as cl
        left join channel_info ci on cl.export_source = ci.code
        left join channel_info ici on cl.source = ici.code
        left join sys_user su on cl.mechanism_id = su.id
        left join loan_supermarket ls on cl.record_id = ls.id
        <where>
            cl.export_source = #{reportDto.channelCode}
            <if test="reportDto.startDate != null">
                and cl.creation_date &gt;= #{reportDto.startDate}
            </if>
            <if test="reportDto.endDate != null">
                and cl.creation_date &lt;= #{reportDto.endDate}
            </if>
            <if test="reportDto.supermarketId != null">
                and cl.record_id = #{reportDto.supermarketId}
            </if>
            <if test="reportDto.importChannel != null">
                and cl.source = #{reportDto.importChannel}
            </if>
            <if test="reportDto.name != null and reportDto.name !=''">
                and cl.name like concat('%',#{reportDto.name},'%')
            </if>
            <if test="reportDto.phone != null and reportDto.phone !=''">
                and cl.phone like concat('%',#{reportDto.phone},'%')
            </if>
            <if test="reportDto.mechanismName != null and reportDto.mechanismName !=''">
                and su.company_name like concat('%',#{reportDto.mechanismName},'%')
            </if>
        </where>
    </select>
 
    <select id="selectDuplicateLogs" resultType="com.nova.sankuai.domain.entity.ChannelLog">
        select * from channel_log where source = #{channelLog.source}
        and export_source = #{channelLog.exportSource}
        and type = #{channelLog.type}
        and status = #{channelLog.status}
        and record_id = #{channelLog.recordId}
        and customer_record_id = #{channelLog.customerRecordId}
        and creation_date &lt;= #{endOfDay}
        and creation_date &gt;= #{beginOfDay}
    </select>
 
    <select id="getChannelProportion" resultType="com.nova.sankuai.domain.vo.ChannelProportionVo">
        SELECT
        D.tHour AS tHour,
        D.hHour AS hHour,
        D.hDay AS hDay,
        A.chName AS chName,
        A.uvSum AS uvSum,
        B.pkSum AS pkSum,
        concat(ROUND(B.pkSum / A.uvSum*100, 2),"%") AS pkPropertion,
        C.vipSum AS vipSum,
        concat(ROUND(C.vipSum / A.uvSum*100, 2),"%") AS vipPropertion
        FROM(
        select
        DATE_FORMAT(tHour,'%m-%d') AS hDay,
        DATE_FORMAT(tHour,'%H') AS hHour,
        tHour AS tHour
        from(
        <foreach collection="hourStr" item="item" index="index">
            <if test="item=='00'">
                select concat(#{startDayStr},' ',#{item}) as tHour
            </if>
            <if test="item!='00'">
                union select concat(#{startDayStr},' ',#{item}) as tHour
            </if>
        </foreach>
        <foreach collection="hourStr" item="item" index="index">
            union select concat(#{endDayStr},' ',#{item}) as tHour
        </foreach>
        ) as tmp
        ) as D
        left join
        (SELECT
        IF ((cv.visit_type = '0'),'UV','PV') AS vType,
        ci.name AS chName,
        count(cv.id) AS uvSum,
        DATE_FORMAT(cv.creation_date,'%m-%d') AS hDay,
        DATE_FORMAT(cv.creation_date,'%H') AS hHour,
        DATE_FORMAT(cv.creation_date,'%Y-%m-%d %H') AS tHour
        FROM channel_view_log cv
        LEFT JOIN channel_info ci ON cv.channel_id = ci.id
        WHERE ci.code =#{chCode}
        AND cv.visit_type = '0'
        and cv.creation_date &gt;= #{startDate} and cv.creation_date &lt;= #{endDate}
        GROUP BY tHour ORDER BY tHour
        ) AS A on D.tHour = A.tHour
        LEFT JOIN (SELECT
        count(od.id) AS pkSum,
        date_format(od.creation_date,'%Y-%m-%d %H') AS tHour
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE ci.code =#{chCode}
        AND od.status = 'END' AND od.vip_type = 1
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY tHour ORDER BY tHour DESC
        ) AS B ON D.tHour = B.tHour
        LEFT JOIN (
        SELECT count(od.id) AS vipSum,
        date_format(od.creation_date,'%Y-%m-%d %H') AS tHour
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE ci.code =#{chCode}
        AND od.status = 'END' AND od.vip_type = 0
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY tHour ORDER BY tHour DESC
        ) AS C ON D.tHour = C.tHour;
    </select>
 
    <select id="getLoanSupermarketStatistics" resultType="com.nova.sankuai.domain.vo.LoanSupermarketStatisticsVo">
        select
        ci.name as chName,
        ls.`name` as suName,
        count(cl.id) as uvSum,
        cl.tDay as tDay,
        cl.tHour as hHour
        from (
        select cl.id,cl.source,cl.export_source,cl.record_id, cl.creation_date,
        date_format(cl.creation_date,'%m/%d') as tDay,
        date_format(cl.creation_date,'%H') as tHour
        from channel_log cl
        left join customer cu on cl.customer_record_id = cu.id
        where cl.export_source = 12 and cl.status = 1 and cl.type = 1
        and cl.creation_date &gt;= #{startDate} and cl.creation_date &lt;=#{endDate}
        group by cl.source,cl.record_id,cu.phone,tHour
        ) as cl
        left join loan_supermarket ls on record_id = ls.id
        left join channel_info ci on cl.source = ci.`code`
        <where>
            ls.delete_flag = 0
            <if test="suId != null">
                and ls.id = #{suId}
            </if>
            <if test="chCode != null">
                AND cl.`source` = #{chCode}
            </if>
        </where>
        group by cl.tHour,cl.record_id,cl.source
    </select>
 
    <select id="getAutoStatistics" resultType="com.nova.sankuai.domain.vo.AutoStatisticsVo">
        select
        A.hDay,
        A.suName,
        A.chName,
        A.suCode,
        A.id,
        B.uv as uv,
        C.inletFlowSum as inletFlowSum,
        D.pkAmount as pkAmount,
        E.vipAmount as vipAmount,
        concat(ROUND(E.vipSum / C.inletFlowSum*100, 2),"%") AS vipPropertion
        from(
        SELECT IF ((has_third_channel = 1),name,'') AS suName,
        IF ((has_third_channel = 0),name, '') AS chName,
        IF ((has_third_channel = 1),code,auto_parent_code) AS suCode,
        `id` as id,
        `code` as chCode,DATE_FORMAT(#{startDate},'%m-%d') AS hDay from channel_info WHERE has_third_channel =1 or third_channel_code is not null
        ) as A
        LEFT JOIN
        (SELECT
        ci.code AS chCode,
        sum(IF ((cv.visit_type = '0' and ci.has_third_channel = 1),1,0)) AS uv,
        DATE_FORMAT(cv.creation_date,'%m-%d') AS hDay
        FROM channel_view_log cv
        LEFT JOIN channel_info ci ON cv.channel_id = ci.id
        WHERE
        cv.creation_date &gt;= #{startDate} and cv.creation_date &lt;= #{endDate}
        GROUP BY ci.id
        ) as B ON A.hDay = B.hDay and A.chCode = B.chCode
        left join
        (SELECT
        ci.`code` AS chCode,
        count(cu.id) AS inletFlowSum,
        DATE_FORMAT(cu.apply_date,'%m-%d') AS hDay
        FROM customer cu
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        where 1 = 1
        and cu.apply_date &gt;= #{startDate} and cu.apply_date &lt;= #{endDate}
        GROUP BY ci.code
        ) AS C ON A.hDay = C.hDay and A.chCode = C.chCode
        LEFT JOIN (SELECT
        sum(od.price) AS pkAmount,
        date_format(od.creation_date,'%m-%d') AS hDay,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE
        od.status = 'END' AND od.vip_type = 1
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY ci.code
        ) AS D ON A.hDay = D.hDay and A.chCode = D.chCode
        LEFT JOIN (
        SELECT
        sum(od.price) AS vipAmount,
        count(od.id) AS vipSum,
        date_format(od.creation_date,'%m-%d') AS hDay,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE
        od.status = 'END' AND od.vip_type = 0
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY ci.code
        ) AS E ON A.hDay = E.hDay and A.chCode = E.chCode
    </select>
 
    <select id="getWholeProcessStatistics" resultType="com.nova.sankuai.domain.vo.WholeProcessStatisticsVo">
        select
        E.date as date,
        IFNull(A.downloadCount,0) as downloadCount,
        IFNull(B.appLoginCount,0) as appLoginCount,
        IFNull(G.h5LoginCount,0) as h5LoginCount,
        IFNull(sum(C.aCount+C.bCount+C.cCount+C.C.dCount),0) as baseInfoCount,
        IFNull(sum(C.bCount+C.cCount+C.C.dCount),0) as ocrCount,
        IFNull(sum(C.cCount+C.C.dCount),0) as bankInfoCount,
        IFNull(sum(C.cCount),0) as capitalInfoCount,
        IFNull(D.nCount,0) as queueNormalCount,
        IFNull(D.vCount,0) as queueVipCount,
        IFNull(D.sCount,0) as payQueueCount,
        IFNull(cnt,0) as payVipCount
        from (select DATE_FORMAT(#{startDate},'%m-%d') AS date) as E
        left join(
        select
        DATE_FORMAT(cd.creation_date,'%m-%d') AS date,
        count(cd.id) as downloadCount
        from channel_download_log cd
        <if test="chCode != null">
            LEFT JOIN channel_info ci ON cd.channel_id = ci.id
        </if>
        where cd.creation_date &gt;= #{startDate} and cd.creation_date &lt;=#{endDate}
        <if test="chCode != null">
            and ci.`code` = #{chCode}
        </if>
        ) as A on E.date = A.date
        left join (
        select
        DATE_FORMAT(cu.app_last_updated_date,'%m-%d') AS date,
        count(cu.id) as appLoginCount
        from customer_user cu
        where cu.app_last_updated_date &gt;= #{startDate} and cu.app_last_updated_date &lt;=#{endDate}
        <if test="chCode != null">
            and cu.source_channel = #{chCode}
        </if>
        ) as B on E.date = B.date
        left join (
        select
        DATE_FORMAT(cu.h5_last_updated_date,'%m-%d') AS date,
        count(cu.id) as h5LoginCount
        from customer_user cu
        where cu.h5_last_updated_date &gt;= #{startDate} and cu.h5_last_updated_date &lt;=#{endDate}
        <if test="chCode != null">
            and cu.source_channel = #{chCode}
        </if>
        ) as G on G.date = E.date
        left join (
        select
        DATE_FORMAT(cu.apply_date,'%m-%d') AS date,
        sum(IF(cu.status = 0,1,0)) as aCount,
        sum(IF(cu.status = 1,1,0)) as bCount,
        sum(IF(cu.status = 2,1,0)) as cCount,
        sum(IF(cu.status = 3,1,0)) as dCount
        from customer cu
        where cu.apply_date &gt;= #{startDate} and cu.apply_date &lt;=#{endDate}
        <if test="chCode != null">
            and cu.source_channel = #{chCode}
        </if>
        ) as C on E.date = C.date
        left join(
        select
        DATE_FORMAT(od.creation_date,'%m-%d') AS date,
        sum(IF(vc.queue_vip_code = 'NORMAL',1,0)) as nCount,
        sum(IF(vc.queue_vip_code = 'NORMAL',0,1)) as vCount,
        sum(IF(od.status = 'END',1,0)) as sCount
        from vip_card_order od
        left join vip_card vc on od.card_id = vc.card_id
        <if test="chCode != null">
            LEFT JOIN customer_user cu ON od.user_id = cu.id
            LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        </if>
        where od.creation_date &gt;= #{startDate} and od.creation_date &lt;=#{endDate}
        and od.vip_type = 1
        <if test="chCode != null">
            and ci.code = #{chCode}
        </if>
        ) as D on E.date = D.date
        left join(
        select
        DATE_FORMAT(od.creation_date,'%m-%d') AS date,
        count(vc.id) as cnt
        from vip_card_order od
        left join vip_card vc on od.card_id = vc.card_id
        <if test="chCode != null">
            LEFT JOIN customer_user cu ON od.user_id = cu.id
            LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        </if>
        where od.creation_date &gt;= #{startDate} and od.creation_date &lt;=#{endDate}
        and od.vip_type = 0 and od.status = 'END'
        <if test="chCode != null">
            and ci.code = #{chCode}
        </if>
        ) as F on E.date = F.date
 
    </select>
 
    <select id="dataRepair">
        UPDATE `channel_info` ci1,`channel_info` ci2 SET ci1.auto_parent_code = ci2.`code`
        WHERE ci1.third_channel_code LIKE CONCAT(ci2.app_id,'%')
        AND ci1.id = #{id}
    </select>
 
    <select id="channelStatistics" resultType="com.nova.sankuai.domain.vo.ChannelStatisticsListVo">
        SELECT
        M.hDay AS hDay,
        M.chName as chName,
        A.uvSum AS uvSum,
        B.pkSum AS pkSum,
        ROUND(B.pkSum / A.uvSum, 4) AS pkPropertion,
        C.vipSum AS vipSum,
        ROUND(C.vipSum / A.uvSum, 4) AS vipPropertion
        FROM
        (
        select
        DATE_FORMAT(str.tDay,'%m-%d') AS hDay,
        ci.code as chCode,
        ci.name as chName
        from (
        select #{startDate} as tDay
        union select #{endDate} as tDay) as str
        ,channel_info ci
        <if test="chCodes != null">
            where ci.code in
            <foreach collection="chCodes" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ) as M
        left join (SELECT
        ci.name AS chName,
        count(cu.id) AS uvSum,
        DATE_FORMAT(cu.apply_date,'%m-%d') AS hDay,
        ci.code as chCode
        FROM customer cu
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE 1 = 1
        <if test="chCodes != null">
            and ci.code in
            <foreach collection="chCodes" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and cu.apply_date &gt;= #{startDate} and cu.apply_date &lt;= #{endDate}
        GROUP BY hDay ,chCode
        ) AS A on A.hDay = M.hDay and A.chCode = M.chCode
        LEFT JOIN (SELECT
        count(od.id) AS pkSum,
        date_format(od.creation_date,'%m-%d') AS hDay,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE od.status = 'END' AND od.vip_type = 1
        <if test="chCodes != null">
            and ci.code in
            <foreach collection="chCodes" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY hDay ,chCode
        ) AS B ON B.hDay = M.hDay and B.chCode = M.chCode
        LEFT JOIN (
        SELECT count(od.id) AS vipSum,
        date_format(od.creation_date,'%m-%d') AS hDay,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE od.status = 'END' AND od.vip_type = 0
        <if test="chCodes != null">
            and ci.code in
            <foreach collection="chCodes" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate}
        GROUP BY hDay ,chCode
        ) AS C ON C.hDay = M.hDay and C.chCode = M.chCode
    </select>
 
    <select id="getBaseProfitAnalysisReportVo" resultType="com.nova.sankuai.domain.vo.BaseProfitAnalysisReportVo">
        SELECT
        M.chName as chName,
        M.chCode as chCode,
        IFNull(N.uv,0) AS uvSum,
        IFNull(N.uvCost,0) AS uvCost,
        IFNull(B.pkIncome,0) AS pkIncome,
        IFNull(C.vipIncome,0) AS vipIncome,
        IFNull(D.loanIncome,0) AS loanIncome
        FROM
        (
        SELECT name as chName,
        code AS chCode
        FROM channel_info
        WHERE status = 1 and code = #{chCode}
        ) as M
        --         计算UV总数及UV成本
        LEFT JOIN (
        SELECT
        ci.code AS chCode,
        ci.name as chName,
        sum(IF ((cv.visit_type = '0'),1,0)) AS uv,
        sum(IF ((cv.visit_type = '0'),1,0))*cc.uv_price as uvCost
        FROM channel_view_log cv
        LEFT JOIN channel_info ci ON cv.channel_id = ci.id
        left JOIN cost_config cc
        ON ci.code = cc.channel_code and cc.is_delete = 0 and cc.default_flag = 1
        WHERE
        cv.creation_date &gt;= #{startDate} and cv.creation_date &lt;= #{endDate}
        and ci.type = 0 and ci.code = #{chCode}
        ) N ON N.chCode = M.chCode
        --         计算审批加速包收入
        LEFT JOIN (SELECT
        ci.name AS chName,
        sum(od.price) AS pkIncome,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE od.status = 'END' AND od.vip_type = 1
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate} and ci.code = #{chCode}
        ) AS B ON B.chCode = M.chCode
        --         计算会员购买收入
        LEFT JOIN (
        SELECT count(od.id) AS vipSum,
        sum(od.price) AS vipIncome,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE od.status = 'END' AND od.vip_type = 0
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate} and ci.code = #{chCode}
        ) AS C ON C.chCode = M.chCode
        --         计算贷超会员购买收入
        LEFT JOIN (
        SELECT count(od.id) AS loanSum,
        sum(od.price) AS loanIncome,
        ci.code as chCode
        FROM vip_card_order od
        LEFT JOIN customer_user cu ON od.user_id = cu.id
        LEFT JOIN channel_info ci ON cu.source_channel = ci.code
        WHERE od.status = 'END' AND od.vip_type = 2
        and od.creation_date &gt;= #{startDate} and od.creation_date &lt;= #{endDate} and ci.code = #{chCode}
        ) AS D ON D.chCode = M.chCode
    </select>
 
    <select id="getCityProfitAnalysisReportVo" resultType="com.nova.sankuai.domain.vo.CityProfitAnalysisReportVo">
        SELECT
        M.chName as chName,
        M.chCode as chCode,
        ifnull(A.registerSum,0) as registerSum,
        ifnull(A.registerPrice,0) as registerCost,
        ifnull(B.collisionSum,0) as collisionSum,
        ifnull(B.collisionPrice,0) as collisionIncome,
        ifnull(C.capitalSum,0) as capitalSum,
        ifnull(C.capitalPrice,0) as capitalIncome,
        ifnull(D.auditSum,0) as auditSum,
        ifnull(D.auditPrice,0) as auditIncome
        FROM
        --         以渠道为主表进行查询
        (SELECT name as chName,
        code AS chCode
        FROM channel_info
        WHERE status = 1 and code = #{chCode}
        ) as M
        --         计算注册总数及注册成本
        left join (
        select
        cu.chCode,
        sum(cu.registerSum) as registerSum,
        sum(cu.registerSum * ifnull(cc.register_price,de.register_price)) as registerPrice
        from (
        select cus.source_channel as chCode,
        count(cus.id) as registerSum,
        ifNull(cu.city,'默认') as cityId
        from customer_user cus
        left join (select id,phone,city from customer
        where source_channel = #{chCode}
        and apply_date &gt;= #{startDate} and apply_date &lt;=#{endDate} group by phone
        ) as cu on cus.phone = cu.phone
        where cus.source_channel = #{chCode}
        and cus.creation_date &gt;= #{startDate} and cus.creation_date &lt;=#{endDate}
        group by cu.city) as cu
        left join cost_config cc
        on cu.chCode = cc.channel_code and cu.cityId = cc.relation_city_id and cc.is_delete = 0 and cc.default_flag = 0
        left join cost_config de
        on cu.chCode = de.channel_code and de.default_flag = 1 and de.is_delete = 0
        ) as A on M.chCode = A.chCode
        --         计算联登人数及联登收入
        left join (
        select ccr.chCode,
        sum(ccr.collisionSum) as collisionSum,
        sum(ccr.collisionSum * ifnull(ic.collision_price,de.collision_price)) as collisionPrice
        from (select
        cu.source_channel as chCode,
        ccr.capital_info_code as capitalCode,
        count(ccr.id) as collisionSum,
        ifNull(cu.city,'默认') as cityId
        from customer_capital_result ccr
        inner join capital_info ci on ci.id = ccr.capital_info_id
        left join customer cu
        on ccr.customer_record_id = cu.id
        where ci.fee_mode = 1
        and ccr.approval_result = 1 and ccr.last_updated_date &gt;= #{startDate} and ccr.last_updated_date &lt;=#{endDate}
        and cu.source_channel = #{chCode}
        group by capitalCode,cityId) as ccr
        left join income_config ic on ccr.capitalCode = ic.config_record_code and ic.type = 0 and ic.default_flag = 0
        and ccr.cityId = ic.relation_city_id and ic.is_delete = 0
        left join income_config de on ccr.capitalCode = de.config_record_code and de.type = 0
        and de.default_flag = 1 and de.is_delete = 0
        ) as B on M.chCode = B.chCode
        --         计算资方列表人数及资方列表收入
        left join (
        select ccr.chCode,
        sum(ccr.capitalSum) as capitalSum,
        sum(ccr.capitalSum * ifnull(ic.capital_price,de.capital_price)) as capitalPrice
        from (select
        cu.source_channel as chCode,
        ccr.capital_info_code as capitalCode,
        count(ccr.id) as capitalSum,
        ifNull(cu.city,'默认') as cityId
        from customer_capital_result ccr
        inner join capital_info ci on ci.id = ccr.capital_info_id
        left join customer cu
        on ccr.customer_record_id = cu.id
        where ci.fee_mode = 0
        and ccr.approval_result = 1 and ccr.last_updated_date &gt;= #{startDate} and ccr.last_updated_date &lt;=#{endDate}
        and cu.source_channel = #{chCode}
        group by capitalCode,cityId) as ccr
        left join income_config ic on ccr.capitalCode = ic.config_record_code and ic.type = 0 and ic.default_flag = 0
        and ccr.cityId = ic.relation_city_id and ic.is_delete = 0
        left join income_config de on ccr.capitalCode = de.config_record_code and de.type = 0
        and de.default_flag = 1 and de.is_delete = 0
        ) as C on M.chCode = C.chCode
        --         计算审批分流人数及审批分流收入
        left join (
        select acr.chCode,
        sum(acr.auditSum) as auditSum,
        sum(acr.auditSum * ifnull(ic.audit_price,de.audit_price)) as auditPrice
        from (select
        cu.source_channel as chCode,
        ac.company_code as auditCode,
        count(acr.id) as auditSum,
        ifNull(cu.city,'默认') as cityId
        from api_company_record acr
        inner join api_company ac on ac.id = acr.company_id
        left join customer cu
        on acr.customer_record_id = cu.id
        where acr.status = 1
        and acr.creation_date &gt;= #{startDate} and acr.creation_date &lt;=#{endDate}
        and cu.source_channel = #{chCode}
        group by auditCode,cityId) as acr
        left join income_config ic on acr.auditCode = ic.config_record_code and ic.type =1 and ic.default_flag = 0
        and acr.cityId = ic.relation_city_id and ic.is_delete = 0
        left join income_config de on acr.auditCode = de.config_record_code and de.type = 1
        and de.default_flag = 1 and de.is_delete = 0
        ) as D on M.chCode = D.chCode;
    </select>
</mapper>