dwd_user_studio_add_content.py 10.9 KB
Newer Older
侯双强's avatar
侯双强 committed
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
# -*- coding: utf-8 -*-

'''
用户添加工作室记录,数据量不大,全量更新
'''
import logging
import os
import sys

from common.mysql_uitl import save_result, fetch_all, save_etl_log
from common.time_util import now_str, YMDHMS_FORMAT


logging.basicConfig(format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO)
file_name = sys.argv[0]
task_file = os.path.split(__file__)[-1].split(".")[0]


def dwd_user_studio_add_content(data_dt):
    # 公募基金
    public_fund_dict = query_studio_add_public_fund()
    row1 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', public_fund_dict, file_name)

    # 私募基金
    private_equity_dict = query_studio_add_private_equity()
    row2 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', private_equity_dict, file_name)

    # 白名单基金
    tamp_fund_dict = query_studio_add_tamp_fund()
    row3 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', tamp_fund_dict, file_name)

    # 私有基金
    private_fund_dict = query_studio_add_private_fund()
    row4 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', private_fund_dict, file_name)
    #
    # 无净值产品
    ifa_nonav_dcit = query_studio_add_ifa_nonav()
    row5 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', ifa_nonav_dcit, file_name)

    # 直播
    live_dict = query_studio_add_live()
    row6 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', live_dict, file_name)

    # 课程
    course_dict = query_studio_add_course()
    row7 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', course_dict, file_name)

    # 短视频
    short_video_dict = query_studio_add_short_video()
    row8 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', short_video_dict, file_name)

    # 探普文章
    tamp_article_dict = query_studio_add_tamp_article()
    row9 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', tamp_article_dict, file_name)

    # 私有文章
    self_article_dict = query_studio_add_self_article()
    row10 = save_result('tamp_data_dwd', 'dwd_user_studio_add_content', self_article_dict, file_name)

    row = row1 + row2 + row3 + row4 + row5 + row6 + row7 + row8 + row9 + row10
    now_time = now_str(YMDHMS_FORMAT)
    save_etl_log('tamp_data_dwd', 'dwd_user_studio_add_content', data_dt, row, 'done', task_file, now_time)


def query_studio_add_public_fund():
    '''添加公募基金'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'public_fund' as res_type
                ,p.target_id as res_id
                ,t.fund_name as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join  tamp_fund.tx_fund_info t
        on 		p.target_id = t.id
        where   p.`type` = 'product'
        and 	p.sub_type = '0'
    '''
    public_fund_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return public_fund_dict


def query_studio_add_private_equity():
    '''添加私募基金'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'private_equity' as res_type
                ,p.target_id as res_id
                ,t.fund_name as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join  tamp_fund.fund_info t
        on 		p.target_id = t.id
        where   p.`type` = 'product'
        and 	p.sub_type = '1'
    '''
    private_equity_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return private_equity_dict


def query_studio_add_tamp_fund():
    '''添加白名单基金'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'tamp_fund' res_type
                ,p.target_id as res_id
                ,t.fund_name as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join  tamp_product.fund_info t
        on 		p.target_id = t.id
        where   p.`type` = 'product'
        and 	p.sub_type = '2'
    '''
    tamp_fund_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return tamp_fund_dict


def query_studio_add_private_fund():
    '''添加私有基金'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'private_fund' as res_type
                ,p.target_id as res_id
                ,t.fund_name as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join  tamp_fund.ifa_imported_fund_info t
        on 		p.target_id = t.id
        where   p.`type` = 'product'
        and 	p.sub_type = '3'
    '''
    public_fund_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return public_fund_dict


def query_studio_add_ifa_nonav():
    '''添加无净值产品'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'ifa_nonav' as res_type
                ,p.target_id as res_id
                ,t.fund_name as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join tamp_fund.ifa_imported_nonav_fund t
        on 		p.target_id = t.id
        where   p.`type` = 'product'
        and 	p.sub_type = '4'
    '''
    ifa_nonav_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return ifa_nonav_dict


def query_studio_add_live():
    '''添加直播'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'live' as res_type
                ,p.target_id as res_id
                ,t.zt_name as res_name
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join tamp_zhibo.zhibo_theme t
        on 		p.target_id = t.id
        where   p.`type` = 'zhibo'
    '''
    live_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return live_dict


def query_studio_add_course():
    '''添加课程'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'course' as res_type
                ,p.target_id as res_id
                ,t.title as res_name 
                ,p.course_packageId as course_id
                ,q.main_title as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join tamp_course.course t
        on 		p.target_id = t.id
        inner   join  tamp_course.course_package q 
        on      p.course_packageId = q.id 
        where   p.`type` = 'course'
    '''
    course_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return course_dict


def query_studio_add_short_video():
    '''添加短视频'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'short_video' as res_type
                ,p.target_id as res_id
                ,t.res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join tamp_analysis.short_video_view t
        on 		p.target_id = t.res_id
        where   p.`type` = 'shortVideo'
    '''
    short_video_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return short_video_dict


def query_studio_add_tamp_article():
    '''添加探普文章'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,case when t.ar_title is not null then 'tamp_article'
                      when q.af_title is not null then 'self_article'
                      else ''
                end as res_type
                ,p.target_id as res_id
                ,coalesce(t.ar_title,q.af_title) as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        left    join tamp_user.article t
        on 		p.target_id = t.id
        left    join tamp_user.friend_article q
        on 		p.target_id = q.id 
        where   p.`type` = 'article' 
        and     p.sub_type = 'tamp'
    '''
    tamp_article_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return tamp_article_dict


def query_studio_add_self_article():
    '''添加私有文章'''
    function_name = sys._getframe().f_code.co_name
    logging.info(f'{function_name} start')
    sql = '''
         select p.id 
                ,p.user_id
                ,'self_article' as res_type
                ,p.target_id as res_id
                ,t.af_title as res_name 
                ,p.course_packageId as course_id
                ,'' as course_name
                ,p.create_time
                ,p.update_time
                ,p.delete_tag 
        from    tamp_user.user_poster_attachement p
        inner   join tamp_user.friend_article t
        on 		p.target_id = t.id
        where   p.`type` = 'article' 
        and     p.sub_type = 'self'
    '''
    self_article_dict = fetch_all(sql, None)
    logging.info(f'{function_name} success')
    return self_article_dict


if __name__ == '__main__':
    dwd_user_studio_add_content()