recommend_strategy_fix.py 55.2 KB
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 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
# -*- coding:UTF-8 -*-
# @Time  : 2020/11/6 10:00
# @File  : recommend_strategy_fix.py
# @email : litao@igengmei.com
# @author : litao

import hashlib
import json

import pymysql
import xlwt, datetime
# import redis
# from pyhive import hive
# from maintenance.func_send_email_with_file import send_file_email
# from typing import Dict, List
# from elasticsearch_7 import Elasticsearch
# from elasticsearch_7.helpers import scan
# import sys
import time
from pyspark import SparkConf
from pyspark.sql import SparkSession, DataFrame
# from pyspark.sql.functions import lit
# import pytispark.pytispark as pti

db = pymysql.connect(host='172.16.50.175', port=3306, user='doris', passwd='o5gbA27hXHHm',
                     db='doris_olap')
cursor = db.cursor()


def con_sql(sql):
    # 从数据库的表里获取数据
    db = pymysql.connect(host='172.16.50.175', port=3306, user='doris', passwd='o5gbA27hXHHm',
                         db='doris_olap')
    cursor = db.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    db.close()
    return result


startTime = time.time()
sparkConf = SparkConf()
sparkConf.set("spark.sql.crossJoin.enabled", True)
sparkConf.set("spark.debug.maxToStringFields", "100")
# sparkConf.set("spark.tispark.plan.allow_index_double_read", False)
# sparkConf.set("spark.tispark.plan.allow_index_read", True)
sparkConf.set("spark.hive.mapred.supports.subdirectories", True)
sparkConf.set("spark.hadoop.mapreduce.input.fileinputformat.input.dir.recursive", True)
sparkConf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
# sparkConf.set("mapreduce.output.fileoutputformat.compress", False)
# sparkConf.set("mapreduce.map.output.compress", False)
sparkConf.set("prod.gold.jdbcuri",
              "jdbc:mysql://172.16.30.136/doris_prod?user=doris&password=o5gbA27hXHHm&rewriteBatchedStatements=true")
sparkConf.set("prod.mimas.jdbcuri",
              "jdbc:mysql://172.16.30.138/mimas_prod?user=mimas&password=GJL3UJe1Ck9ggL6aKnZCq4cRvM&rewriteBatchedStatements=true")
sparkConf.set("prod.gaia.jdbcuri",
              "jdbc:mysql://172.16.30.143/zhengxing?user=work&password=BJQaT9VzDcuPBqkd&rewriteBatchedStatements=true")
sparkConf.set("prod.tidb.jdbcuri",
              "jdbc:mysql://172.16.40.158:4000/eagle?user=st_user&password=aqpuBLYzEV7tML5RPsN1pntUzFy&rewriteBatchedStatements=true")
# sparkConf.set("prod.jerry.jdbcuri",
#               "jdbc:mysql://172.16.40.158:4000/jerry_prod?user=st_user&password=aqpuBLYzEV7tML5RPsN1pntUzFy&rewriteBatchedStatements=true")
# sparkConf.set("prod.tispark.pd.addresses", "172.16.40.158:2379")
# sparkConf.set("prod.tispark.pd.addresses", "172.16.40.170:4000")
# sparkConf.set("prod.tidb.database", "jerry_prod")

spark = (SparkSession.builder.config(conf=sparkConf).appName(
    "recommend_strategy_fix").enableHiveSupport().getOrCreate())

spark.sql("ADD JAR hdfs:///user/hive/share/lib/udf/brickhouse-0.7.1-SNAPSHOT.jar")
spark.sql("ADD JAR hdfs:///user/hive/share/lib/udf/hive-udf-1.0-SNAPSHOT.jar")
spark.sql("CREATE TEMPORARY FUNCTION json_map AS 'brickhouse.udf.json.JsonMapUDF'")
spark.sql("CREATE TEMPORARY FUNCTION is_json AS 'com.gmei.hive.common.udf.UDFJsonFormatCheck'")
spark.sql("CREATE TEMPORARY FUNCTION arrayMerge AS 'com.gmei.hive.common.udf.UDFArryMerge'")


for t in range(0, 3):
    day_num = 0 - t
    now = (datetime.datetime.now() + datetime.timedelta(days=day_num))
    last_30_day_str = (now + datetime.timedelta(days=-30)).strftime("%Y%m%d")
    today_str = now.strftime("%Y%m%d")
    yesterday_str = (now + datetime.timedelta(days=-1)).strftime("%Y%m%d")
    one_week_age_str = (now + datetime.timedelta(days=-7)).strftime("%Y%m%d")


    ctr_sql = """
    SELECT
        t1.partition_date as day_id,
        t1.device_os_type as device_os_type,
        t1.active_type as active_type,
        t2.card_content_type as card_content_type,
        t2.recommend_type as recommend_type,
        NVL(sum(t3.session_pv),0) as card_click,
        NVL(sum(t2.session_pv),0) as card_exposure,
        NVL(round(sum(page_stay)/count(distinct t4.cl_id)/60,2),0) as avg_page_stay,
        NVL(sum(t4_pv.pv),0) as page_pv,
        NVL(sum(t4_pv.pv_20),0) as page_pv_20,
        NVL(sum(navbar_pv),0) as navbar_search,
        NVL(sum(highlight_pv),0) as highlight_word,
        NVL(sum(self_wel_pv),0) as self_welfare_card,
        NVL(sum(recom_wel_pv),0)-NVL(sum(self_wel_pv),0) as recommend_welfare_card,--需要排除关联的商品卡片点击
        NVL(sum(recom_content_pv),0) as recommend_content_card,
        NULL as recommend_special_card,
        NVL(sum(referral_pv),0) as transfer_card,
        NVL(sum(video_pv),0) as video_consultation,
        NVL(sum(post_pv),0) as total_post_pv,
        NVL(sum(post_click_pv),0) as post_click_pv
    
    FROM

    (
        SELECT partition_date
             ,device_os_type
             ,CASE WHEN active_type = '4'  THEN '老活'
                   WHEN active_type  IN ('1','2')  THEN '新增' END AS active_type
             ,device_id
        FROM online.ml_device_day_active_status
        WHERE partition_date={partition_day}
          AND active_type IN ('1','2','4')
          AND first_channel_source_type not IN ('yqxiu1','yqxiu2','yqxiu3','yqxiu4','yqxiu5','mxyc1','mxyc2','mxyc3'
            ,'wanpu','jinshan','jx','maimai','zhuoyi','huatian','suopingjingling','mocha','mizhe','meika','lamabang'
            ,'js-az1','js-az2','js-az3','js-az4','js-az5','jfq-az1','jfq-az2','jfq-az3','jfq-az4','jfq-az5','toufang1'
            ,'toufang2','toufang3','toufang4','toufang5','toufang6','TF-toufang1','TF-toufang2','TF-toufang3','TF-toufang4'
            ,'TF-toufang5','tf-toufang1','tf-toufang2','tf-toufang3','tf-toufang4','tf-toufang5','benzhan','promotion_aso100'
            ,'promotion_qianka','promotion_xiaoyu','promotion_dianru','promotion_malioaso','promotion_malioaso-shequ'
            ,'promotion_shike','promotion_julang_jl03','promotion_zuimei')
          AND first_channel_source_type not LIKE 'promotion\_jf\_%'
    )t1 JOIN
    
     (--卡片,卡片id和session_id去重
     
     SELECT  partition_date,
                    cl_id
                from 
           ( SELECT  partition_date,
                    cl_id,
                    count(distinct app_session_id) as session_pv0
            FROM
            (
               SELECT partition_date,
                      cl_id,
                      case when params['card_content_type'] in ('qa','answer') then 'qa'
                           when params['card_content_type'] in ('special_pool') then 'special' else params['card_content_type'] end as card_content_type,
                      CASE when params['transaction_type'] in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when params['transaction_type'] in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (params['transaction_type'] like '%ctr' and params['transaction_type'] not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when params['transaction_type'] in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN params['transaction_type'] like '%cvr' THEN array('cvr预估','合计')
                           WHEN params['transaction_type'] in ('-1','smr') THEN array('smr','合计')
                           when params['transaction_type'] in ('pgc','hotspot') then array('热点卡片')
                           when params['transaction_type'] in ('newdata') then array('保量卡片')
                           when params['transaction_type'] in ('hotspot_feed') then array('hotspot_feed','合计')
                           when params['transaction_type'] in ('aistragegy') then array('新用户AI帖优先','合计')
                           when params['transaction_type'] in ('excestragegy') then array('新用户精华帖优先','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when params['transaction_type'] like 'deeplink%' then array('deeplink策略','合计')
                           end AS recommend_type,
                      params['card_id'] as card_id,
                      app_session_id
               from online.bl_hdfs_maidian_updates
               WHERE partition_date={partition_day}
               AND action='on_click_card'
               AND params['page_name'] ='home'
               AND params['tab_name'] = '精选'
               AND (params['transaction_type'] in ('-1','smr','hotspot','pgc','newdata','hotspot_feed','aistragegy','excestragegy','FIXEDSTRATEGY','FIXEDSTRATEGY_VIDEO')
                    or params['transaction_type'] like '%ctr' or params['transaction_type'] like '%cvr' or params['transaction_type'] like 'deeplink%')
               AND params['card_content_type'] in ('qa','diary','user_post','answer','special_pool')
               GROUP BY partition_date,
                      cl_id,
                      case when params['card_content_type'] in ('qa','answer') then 'qa'
                           when params['card_content_type'] in ('special_pool') then 'special' else params['card_content_type'] end,
                      CASE when params['transaction_type'] in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when params['transaction_type'] in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (params['transaction_type'] like '%ctr' and params['transaction_type'] not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when params['transaction_type'] in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN params['transaction_type'] like '%cvr' THEN array('cvr预估','合计')
                           WHEN params['transaction_type'] in ('-1','smr') THEN array('smr','合计')
                           when params['transaction_type'] in ('pgc','hotspot') then array('热点卡片')
                           when params['transaction_type'] in ('newdata') then array('保量卡片')
                           when params['transaction_type'] in ('hotspot_feed') then array('hotspot_feed','合计')
                           when params['transaction_type'] in ('aistragegy') then array('新用户AI帖优先','合计')
                           when params['transaction_type'] in ('excestragegy') then array('新用户精华帖优先','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when params['transaction_type'] like 'deeplink%' then array('deeplink策略','合计') end,
                      params['card_id'],
                      app_session_id
            )a
            LATERAL VIEW explode (a.recommend_type) v as recommend_type
            group by partition_date,card_content_type,cl_id,v.recommend_type,card_id having session_pv0 >0
        
          UNION 
     SELECT  partition_date,            
                    cl_id,
                    count(distinct card_id) as session_pv0
            FROM
                     (SELECT partition_date,
                      cl_id,
                      case when card_content_type in ('qa','answer') then 'qa'
                           when card_content_type in ('special_pool') then 'special' else card_content_type end as card_content_type,
                      CASE when transaction_type in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when transaction_type in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (transaction_type like '%ctr' and transaction_type not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr') ) THEN array('ctr预估','合计')
                           when transaction_type in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN transaction_type like '%cvr' THEN array('cvr预估','合计')
                           WHEN transaction_type in ('-1','smr') THEN array('smr','合计')
                           when transaction_type in ('pgc','hotspot') then array('热点卡片')
                           when transaction_type in ('newdata') then array('保量卡片')
                           when transaction_type in ('hotspot_feed') then array('hotspot_feed','合计')
                           when transaction_type in ('aistragegy') then array('新用户AI帖优先','合计')
                           when transaction_type in ('excestragegy') then array('新用户精华帖优先','合计')
                           when transaction_type in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when transaction_type in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when transaction_type like 'deeplink%' then array('deeplink策略','合计')
                           end AS recommend_type,
                      card_id,
                      app_session_id
               from online.ml_community_precise_exposure_detail
               WHERE partition_date={partition_day}
               AND action in ('page_precise_exposure','home_choiceness_card_exposure') --7745版本action改为page_precise_exposure
               AND is_exposure = '1'  ----精准曝光
               AND page_name ='home'
               AND tab_name = '精选'
               AND (transaction_type in ('-1','smr','hotspot','pgc','newdata','hotspot_feed','aistragegy','excestragegy','FIXEDSTRATEGY','FIXEDSTRATEGY_VIDEO')
                    or transaction_type like '%ctr' or transaction_type like '%cvr' or transaction_type like 'deeplink%')
               AND card_content_type in ('qa','diary','user_post','answer','special_pool')
               group by partition_date,
                      case when card_content_type in ('qa','answer') then 'qa'
                           when card_content_type in ('special_pool') then 'special' else card_content_type end,
                      cl_id,
                      CASE when transaction_type in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when transaction_type in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (transaction_type like '%ctr' and transaction_type not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when transaction_type in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN transaction_type like '%cvr' THEN array('cvr预估','合计')
                           WHEN transaction_type in ('-1','smr') THEN array('smr','合计')
                           when transaction_type in ('pgc','hotspot') then array('热点卡片')
                           when transaction_type in ('newdata') then array('保量卡片')
                           when transaction_type in ('hotspot_feed') then array('hotspot_feed','合计')
                           when transaction_type in ('aistragegy') then array('新用户AI帖优先','合计')
                           when transaction_type in ('excestragegy') then array('新用户精华帖优先','合计')
                           when transaction_type in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when transaction_type in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when transaction_type like 'deeplink%' then array('deeplink策略','合计') end,
                      card_id,
                      app_session_id
            )a
            LATERAL VIEW explode (a.recommend_type) v as recommend_type
            group by partition_date,cl_id having  session_pv0 >= 4) group by partition_date,cl_id 
    ) t0  on t1.device_id = t0.cl_id 
 LEFT JOIN
        (--精准曝光,卡片id和session_id去重
            SELECT  partition_date,
                    card_content_type,
                    cl_id,
                    v.recommend_type,
                    card_id,
                    count(distinct app_session_id) as session_pv
            FROM
            (
               SELECT partition_date,
                      cl_id,
                      case when card_content_type in ('qa','answer') then 'qa'
                           when card_content_type in ('special_pool') then 'special' else card_content_type end as card_content_type,
                      CASE when transaction_type in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when transaction_type in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (transaction_type like '%ctr' and transaction_type not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr') ) THEN array('ctr预估','合计')
                           when transaction_type in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN transaction_type like '%cvr' THEN array('cvr预估','合计')
                           WHEN transaction_type in ('-1','smr') THEN array('smr','合计')
                           when transaction_type in ('pgc','hotspot') then array('热点卡片')
                           when transaction_type in ('newdata') then array('保量卡片')
                           when transaction_type in ('hotspot_feed') then array('hotspot_feed','合计')
                           when transaction_type in ('aistragegy') then array('新用户AI帖优先','合计')
                           when transaction_type in ('excestragegy') then array('新用户精华帖优先','合计')
                           when transaction_type in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when transaction_type in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when transaction_type like 'deeplink%' then array('deeplink策略','合计')
                           end AS recommend_type,
                      card_id,
                      app_session_id
               from online.ml_community_precise_exposure_detail
               WHERE partition_date={partition_day}
               AND action in ('page_precise_exposure','home_choiceness_card_exposure') --7745版本action改为page_precise_exposure
               AND is_exposure = '1'  ----精准曝光
               AND page_name ='home'
               AND tab_name = '精选'
               AND (transaction_type in ('-1','smr','hotspot','pgc','newdata','hotspot_feed','aistragegy','excestragegy','FIXEDSTRATEGY','FIXEDSTRATEGY_VIDEO')
                    or transaction_type like '%ctr' or transaction_type like '%cvr' or transaction_type like 'deeplink%')
               AND card_content_type in ('qa','diary','user_post','answer','special_pool')
               group by partition_date,
                      case when card_content_type in ('qa','answer') then 'qa'
                           when card_content_type in ('special_pool') then 'special' else card_content_type end,
                      cl_id,
                      CASE when transaction_type in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when transaction_type in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (transaction_type like '%ctr' and transaction_type not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when transaction_type in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN transaction_type like '%cvr' THEN array('cvr预估','合计')
                           WHEN transaction_type in ('-1','smr') THEN array('smr','合计')
                           when transaction_type in ('pgc','hotspot') then array('热点卡片')
                           when transaction_type in ('newdata') then array('保量卡片')
                           when transaction_type in ('hotspot_feed') then array('hotspot_feed','合计')
                           when transaction_type in ('aistragegy') then array('新用户AI帖优先','合计')
                           when transaction_type in ('excestragegy') then array('新用户精华帖优先','合计')
                           when transaction_type in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when transaction_type in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when transaction_type like 'deeplink%' then array('deeplink策略','合计') end,
                      card_id,
                      app_session_id
            )a
            LATERAL VIEW explode (a.recommend_type) v as recommend_type
            group by partition_date,card_content_type,cl_id,v.recommend_type,card_id
          )t2  
        on t0.cl_id=t2.cl_id and t0.partition_date=t2.partition_date 
        LEFT JOIN
        (--卡片,卡片id和session_id去重
            SELECT  partition_date,
                    card_content_type,
                    cl_id,
                    v.recommend_type,
                    card_id,
                    count(distinct app_session_id) as session_pv
            FROM
            (
               SELECT partition_date,
                      cl_id,
                      case when params['card_content_type'] in ('qa','answer') then 'qa'
                           when params['card_content_type'] in ('special_pool') then 'special' else params['card_content_type'] end as card_content_type,
                      CASE when params['transaction_type'] in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when params['transaction_type'] in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (params['transaction_type'] like '%ctr' and params['transaction_type'] not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when params['transaction_type'] in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN params['transaction_type'] like '%cvr' THEN array('cvr预估','合计')
                           WHEN params['transaction_type'] in ('-1','smr') THEN array('smr','合计')
                           when params['transaction_type'] in ('pgc','hotspot') then array('热点卡片')
                           when params['transaction_type'] in ('newdata') then array('保量卡片')
                           when params['transaction_type'] in ('hotspot_feed') then array('hotspot_feed','合计')
                           when params['transaction_type'] in ('aistragegy') then array('新用户AI帖优先','合计')
                           when params['transaction_type'] in ('excestragegy') then array('新用户精华帖优先','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when params['transaction_type'] like 'deeplink%' then array('deeplink策略','合计')
                           end AS recommend_type,
                      params['card_id'] as card_id,
                      app_session_id
               from online.bl_hdfs_maidian_updates
               WHERE partition_date={partition_day}
               AND action='on_click_card'
               AND params['page_name'] ='home'
               AND params['tab_name'] = '精选'
               AND (params['transaction_type'] in ('-1','smr','hotspot','pgc','newdata','hotspot_feed','aistragegy','excestragegy','FIXEDSTRATEGY','FIXEDSTRATEGY_VIDEO')
                    or params['transaction_type'] like '%ctr' or params['transaction_type'] like '%cvr' or params['transaction_type'] like 'deeplink%')
               AND params['card_content_type'] in ('qa','diary','user_post','answer','special_pool')
               GROUP BY partition_date,
                      cl_id,
                      case when params['card_content_type'] in ('qa','answer') then 'qa'
                           when params['card_content_type'] in ('special_pool') then 'special' else params['card_content_type'] end,
                      CASE when params['transaction_type'] in ('fmctr','samecity_fmctr') then array('fmctr','合计')
                           when params['transaction_type'] in ('high_quality_fmctr') then array('high_quality_fmctr','合计')
                           WHEN (params['transaction_type'] like '%ctr' and params['transaction_type'] not in ('high_quality_ctr','high_quality_fmctr','fmctr','samecity_fmctr')) THEN array('ctr预估','合计')
                           when params['transaction_type'] in ('high_quality_ctr') then array('high_quality_ctr','合计')
                           WHEN params['transaction_type'] like '%cvr' THEN array('cvr预估','合计')
                           WHEN params['transaction_type'] in ('-1','smr') THEN array('smr','合计')
                           when params['transaction_type'] in ('pgc','hotspot') then array('热点卡片')
                           when params['transaction_type'] in ('newdata') then array('保量卡片')
                           when params['transaction_type'] in ('hotspot_feed') then array('hotspot_feed','合计')
                           when params['transaction_type'] in ('aistragegy') then array('新用户AI帖优先','合计')
                           when params['transaction_type'] in ('excestragegy') then array('新用户精华帖优先','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY') then array('新氧新用户策略一','合计')
                           when params['transaction_type'] in ('FIXEDSTRATEGY_VIDEO') then array('新氧新用户策略二','合计')
                           when params['transaction_type'] like 'deeplink%' then array('deeplink策略','合计') end,
                      params['card_id'],
                      app_session_id
            )a
            LATERAL VIEW explode (a.recommend_type) v as recommend_type
            group by partition_date,card_content_type,cl_id,v.recommend_type,card_id
          )t3
        on t2.partition_date=t3.partition_date
            and t2.cl_id=t3.cl_id
            and t2.card_id=t3.card_id
            and t2.card_content_type=t3.card_content_type
            and t2.recommend_type=t3.recommend_type
        LEFT JOIN
        (--页面浏览时长
            select partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name
                    ,sum(page_stay) as page_stay
            from
            (
              SELECT  partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                      page_stay,time_str
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action='page_view'
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND referrer='home'
              AND page_stay>=0 AND page_stay<1000
              GROUP BY partition_date,cl_id,business_id,
                        case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end,page_stay,time_str
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
            group by partition_date,cl_id,coalesce(b.id,a.business_id),a.page_name
        )t4
        on t4.partition_date=t3.partition_date
            and t4.cl_id=t3.cl_id
            and t4.business_id=t3.card_id
            and t4.page_name=t3.card_content_type
        LEFT JOIN
        (--页面浏览时长
            select partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name
                   ,count(1) as pv
                   ,count(case when page_stay>=20 then 1 end) as pv_20
            from
            (
              SELECT  partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,time_str,page_stay
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action='page_view'
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND referrer='home'
              GROUP BY partition_date,cl_id,business_id,
                        case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end,time_str,page_stay
            )a
            left join
            (--在8月份新增了内容专题卡片,需要转换下id
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
            group by partition_date,cl_id,coalesce(b.id,a.business_id),a.page_name
        )t4_pv
        on t4_pv.partition_date=t3.partition_date
            and t4_pv.cl_id=t3.cl_id
            and t4_pv.business_id=t3.card_id
            and t4_pv.page_name=t3.card_content_type
        LEFT JOIN
        (--搜索框和点击行为
            select partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,navbar_pv
            from
            (
              SELECT partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                     count(1) as navbar_pv
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action in ('on_click_navbar_search','do_search')
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
              group by partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
          )t5
        on t5.partition_date=t3.partition_date
            and t5.cl_id=t3.cl_id
            and t5.business_id=t3.card_id
            and t5.page_name=t3.card_content_type
        LEFT JOIN
        (--点击高亮词
            select partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,highlight_pv
            from
            (
              SELECT partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                     count(1) as highlight_pv
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action='on_click_card'
              and params['card_type']='highlight_word'
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
              group by partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
        )t6
        on t6.partition_date=t3.partition_date
            and t6.cl_id=t3.cl_id
            and t6.business_id=t3.card_id
            and t6.page_name=t3.card_content_type
        LEFT JOIN
        (--关联的美购卡片
            SELECT partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,count(1) as self_wel_pv
            FROM
            (
                SELECT  partition_date,cl_id,business_id,app_session_id,params['card_id'] as card_id,
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                        count(1) as pv
                FROM online.bl_hdfs_maidian_updates
                WHERE partition_date={partition_day}
                AND (get_json_object(params['extra_param'], '$.type')='交互栏'
               or get_json_object(params['extra_param'], '$.jump_from')='msg_link'
               or params['in_page_pos']='top'
               or (params['in_page_pos']='bottom' and params['position'] is null and cl_type='android')
            or (params['in_page_pos']='bottom' and params['card_but_pos'] is not null and cl_type='ios'))
                AND action='on_click_card'
                and params['card_content_type']='service'
                AND page_name IN ('diary_detail','topic_detail')
                AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
                group by partition_date,cl_id,business_id,app_session_id,params['card_id'],
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
            group by partition_date,cl_id,coalesce(b.id,a.business_id),a.page_name
          )t7
        on t7.partition_date=t3.partition_date
            and t7.cl_id=t3.cl_id
            and t7.business_id=t3.card_id
            and t7.page_name=t3.card_content_type
        LEFT JOIN
        (--推荐的美购卡片(需要排除作者消费的美购)
            SELECT partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,count(1) as recom_wel_pv
            FROM
            (
                SELECT  partition_date,cl_id,business_id,app_session_id,params['card_id'] as card_id,
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                        count(1) as service_pv
                FROM online.bl_hdfs_maidian_updates
                WHERE partition_date={partition_day}
                AND (action='on_click_card'and  params['card_content_type']='service'
                       or action='on_click_button' and params['button_name']='unfold' and page_name in ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail')
                       or action='on_click_button' and params['button_name'] = 'more_recommendations')
                AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
                AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
                group by partition_date,cl_id,business_id,app_session_id,params['card_id'],
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
            group by partition_date,cl_id,coalesce(b.id,a.business_id),a.page_name
        )t8
        on t8.partition_date=t3.partition_date
            and t8.cl_id=t3.cl_id
            and t8.business_id=t3.card_id
            and t8.page_name=t3.card_content_type
        LEFT JOIN
        (--推荐的内容卡片
            SELECT partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,count(1) as recom_content_pv
            FROM
            (
                SELECT  partition_date,cl_id,business_id,app_session_id,params['card_id'] as card_id,
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                        count(1) as service_pv
                FROM online.bl_hdfs_maidian_updates
                WHERE partition_date={partition_day}
                AND action='on_click_card'
                and params['card_content_type'] in ('qa','diary','user_post','answer')
                AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
                AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
                group by partition_date,cl_id,business_id,app_session_id,params['card_id'],
                         case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
            )a
            left join
            (
              select id,visual_page_id,'special' as page_name
              from tl.tl_zx_api_special_pool
              where partition_day ={partition_day}
              group by id,visual_page_id
            )b
            on a.business_id=b.visual_page_id and a.page_name=b.page_name
            group by partition_date,cl_id,coalesce(b.id,a.business_id),a.page_name
          )t9
        on t9.partition_date=t3.partition_date
            and t9.cl_id=t3.cl_id
            and t9.business_id=t3.card_id
            and t9.page_name=t3.card_content_type
        LEFT JOIN
        (--视频面诊点击
          select  partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,video_pv
          from
          (
            SELECT partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                     count(1) as video_pv
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action='on_click_button'
              and params['button_name']='video_interview'
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
              group by partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
          )a
          left join
          (
            select id,visual_page_id,'special' as page_name
            from tl.tl_zx_api_special_pool
            where partition_day ={partition_day}
            group by id,visual_page_id
          )b
          on a.business_id=b.visual_page_id and a.page_name=b.page_name
        )t10
        on t10.partition_date=t3.partition_date
            and t10.cl_id=t3.cl_id
            and t10.business_id=t3.card_id
            and t10.page_name=t3.card_content_type
        LEFT JOIN
        (--转诊按钮点击
          select partition_date,cl_id,coalesce(b.id,a.business_id) as business_id,a.page_name,referral_pv
          from
          (
            SELECT partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end as page_name,
                     count(1) as referral_pv
              FROM online.bl_hdfs_maidian_updates
              WHERE partition_date={partition_day}
              AND action='on_click_button'
              and params['button_name']='referral'
              AND page_name IN ('diary_detail','topic_detail','post_detail','user_post_detail','doctor_post_detail','question_detail','answer_detail','question_answer_detail','custom_special')
              AND (referrer='home' or
                      (params['referrer_link'] like '%[%' and
                       json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
              group by partition_date,cl_id,business_id,
                      case when page_name in ('diary_detail','topic_detail') then 'diary'
                           when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                           when page_name in ('question_detail','answer_detail','question_answer_detail') then 'qa'
                           when page_name in ('custom_special') then 'special' else null end
          )a
          left join
          (
            select id,visual_page_id,'special' as page_name
            from tl.tl_zx_api_special_pool
            where partition_day ={partition_day}
            group by id,visual_page_id
          )b
          on a.business_id=b.visual_page_id and a.page_name=b.page_name
        )t11
        on t11.partition_date=t3.partition_date
            and t11.cl_id=t3.cl_id
            and t11.business_id=t3.card_id
            and t11.page_name=t3.card_content_type
        LEFT JOIN
        (--从帖子页到帖子页
            SELECT partition_date,cl_id,params['referrer_id'] as business_id,
                    case when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                         else null end as page_name,
                   count(distinct params['business_id'],app_session_id) as post_pv
            FROM online.bl_hdfs_maidian_updates
            WHERE partition_date={partition_day}
            AND action='page_view'
            AND page_name IN ('post_detail','user_post_detail','doctor_post_detail','custom_special')
            AND (json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]IN ('post_detail','user_post_detail','doctor_post_detail')
                  and json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-2]='home')
            group by partition_date,cl_id,params['referrer_id'],
                    case when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                         else null end
        )t12
        on t12.partition_date=t3.partition_date
            and t12.cl_id=t3.cl_id
            and t12.business_id=t3.card_id
            and t12.page_name=t3.card_content_type
        left join
        (--在帖子页点击帖子
            SELECT partition_date,cl_id,params['business_id'] as business_id,
                    case when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                         else null end as page_name,
                   count(distinct params['card_id'],app_session_id) as post_click_pv
            FROM online.bl_hdfs_maidian_updates
            WHERE partition_date={partition_day}
            AND action='on_click_card'
            and params['card_content_type'] in ('user_post')
            AND page_name IN ('post_detail','user_post_detail','doctor_post_detail')
            AND (referrer='home' or
                  (params['referrer_link'] like '%[%' and
                   json_split(params['referrer_link'])[size(json_split(params['referrer_link']))-1]='home'))
            group by partition_date,cl_id,params['business_id'],
                    case when page_name in ('post_detail','user_post_detail','doctor_post_detail') then 'user_post'
                         else null end
        )t13
        on t13.partition_date=t3.partition_date
            and t13.cl_id=t3.cl_id
            and t13.business_id=t3.card_id
            and t13.page_name=t3.card_content_type
        LEFT JOIN
        (
    select distinct device_id
    from ML.ML_D_CT_DV_DEVICECLEAN_DIMEN_D
    where PARTITION_DAY = '{partition_day}'
    AND is_abnormal_device = 'true'
)dev
        on  t2.cl_id=dev.device_id
    WHERE dev.device_id is null
    GROUP BY t1.partition_date,t1.device_os_type,t1.active_type,t2.card_content_type,t2.recommend_type
    order by day_id,device_os_type,active_type,card_content_type,recommend_type
    """.format(partition_day=today_str)
    ctr_df = spark.sql(ctr_sql)
    ctr_df.createOrReplaceTempView("temp_ctr")

    grey_select_sql = """SELECT *,
        NVL(ROUND((navbar_search+highlight_word+self_welfare_card+recommend_welfare_card+recommend_content_card*0.2+transfer_card+video_consultation)/card_exposure,5),0) as recommend_ctr,
        NVL(ROUND(card_click/card_exposure,5),0) as click_ctr,
        NVL(ROUND((navbar_search+highlight_word+self_welfare_card+recommend_welfare_card+recommend_content_card*0.2+transfer_card+video_consultation)/card_click,5),0) as second_jump_ctr ,
        NVL(ROUND(page_pv_20/page_pv,5),0) as page_pv_20_percent 
        FROM temp_ctr"""
    device_df = spark.sql(grey_select_sql)

    device_df.show(1, False)
    sql_res = device_df.collect()

    print("-----------------------------------------------------------------------------")
    for res in sql_res:
        print(res)
        day_id = res.day_id
        device_os_type = res.device_os_type
        active_type = res.active_type
        card_content_type = res.card_content_type
        recommend_type = res.recommend_type
        card_click = res.card_click
        card_exposure = res.card_exposure
        avg_page_stay = res.avg_page_stay
        navbar_search = res.navbar_search
        highlight_word = res.highlight_word
        self_welfare_card = res.self_welfare_card
        page_pv_20 = res.page_pv_20
        page_pv_20_percent = res.page_pv_20_percent
        if not card_content_type or not recommend_type:
            continue

        recommend_welfare_card = res.recommend_welfare_card
        recommend_content_card = res.recommend_content_card
        if not recommend_content_card:
            recommend_content_card = 0
        recommend_special_card = res.recommend_special_card
        if not recommend_special_card:
            recommend_special_card = 0
        transfer_card = res.transfer_card
        video_consultation = res.video_consultation
        partition_day = today_str
        recommend_ctr = res.recommend_ctr
        second_jump_ctr = res.second_jump_ctr
        click_ctr = res.click_ctr
        pid = hashlib.md5((day_id + device_os_type + active_type + card_content_type + recommend_type).encode("utf8")).hexdigest()

        instert_sql = """replace into recommend_strategy_d_fix(
        day_id,device_os_type,active_type,card_content_type,recommend_type,card_click,card_exposure,avg_page_stay,navbar_search,
        highlight_word,self_welfare_card,recommend_welfare_card,recommend_content_card,recommend_special_card,transfer_card,video_consultation,
        partition_day,pid,recommend_ctr,second_jump_ctr,click_ctr,page_pv_20_percent
        ) VALUES('{day_id}','{device_os_type}','{active_type}','{card_content_type}','{recommend_type}',{card_click},{card_exposure},
        {avg_page_stay},{navbar_search},{highlight_word},{self_welfare_card},{recommend_welfare_card},{recommend_content_card},{recommend_special_card},
        {transfer_card},{video_consultation},'{partition_day}','{pid}',{recommend_ctr},{second_jump_ctr},{click_ctr},{page_pv_20_percent});""".format(
            day_id=day_id, device_os_type=device_os_type, active_type=active_type, card_content_type=card_content_type,
            card_click=card_click, recommend_type=recommend_type, card_exposure=card_exposure, avg_page_stay=avg_page_stay,
            navbar_search=navbar_search, self_welfare_card=self_welfare_card, recommend_welfare_card=recommend_welfare_card,
            recommend_content_card=recommend_content_card, recommend_special_card=recommend_special_card,page_pv_20_percent=page_pv_20_percent,
            transfer_card=transfer_card,
            video_consultation=video_consultation, partition_day=partition_day, pid=pid, recommend_ctr=recommend_ctr,
            second_jump_ctr=second_jump_ctr, click_ctr=click_ctr,highlight_word=highlight_word
        )
        print(instert_sql)
        # cursor.execute("set names 'UTF8'")
        res = cursor.execute(instert_sql)
        db.commit()
        print(res)
# cursor.executemany()
db.close()