# -*- coding: utf-8 -*- ''' 内容订单数据(全量同步,订单量多了,再用增量),定时任务,每10分钟运行一次 ''' import logging import os import sys from common.mysql_uitl import fetch_all, save_result, 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_path = get_file_path() file_name = sys.argv[0] file_name = sys.argv[0] task_file = os.path.split(__file__)[-1].split(".")[0] def dwd_user_content_order(data_dt): content_order_dict = query_dwd_user_content_order() row = save_result('tamp_data_dwd', 'dwd_user_content_order', content_order_dict, file_name) now_time = now_str(YMDHMS_FORMAT) save_etl_log('tamp_data_dwd', 'dwd_user_content_order', data_dt, row, 'done', task_file, now_time) def query_dwd_user_content_order(): function_name = sys._getframe().f_code.co_name logging.info(f'{function_name} start') sql = ''' select date_format(o.createtime, '%Y-%m-%d') as data_dt -- 下单日期 ,o.createby as user_id ,u.real_name ,u.user_name ,u.nickname ,u.telephone ,u.team_id ,o.id ,o.ab_ordernum as order_id -- 订单id ,case when o.ab_pay_mode in ('1','99') then 0 when o.ab_pay_mode = '2' then coalesce(ab_score_deduct,ab_account_deduct,0) when o.ab_pay_mode = '3' then (ab_price / 100) when o.ab_pay_mode in ('4','5','6','7','8') then (ab_payment / 100) end as payment_amount ,o.ab_pay_mode ,case when o.ab_pay_mode = '1' then '免费' when o.ab_pay_mode = '2' then '积分支付' when o.ab_pay_mode = '3' then '现金支付' when o.ab_pay_mode = '4' then '探普贝支付' when o.ab_pay_mode = '5' then '微信支付' when o.ab_pay_mode = '6' then '支付宝支付' when o.ab_pay_mode = '7' then '苹果支付' when o.ab_pay_mode = '8' then '微信支付(公众号)' when o.ab_pay_mode = '99' and ab_order_type = 0 then '系统赠送' when o.ab_pay_mode = '99' and ab_order_type = 2 then '好友赠送' end as pay_mode ,o.ab_type as res_type ,case when o.ab_type = '1' then '购买栏目' when o.ab_type = '3' then '购买直播' when o.ab_type = '4' then '购买老版视频课程' when o.ab_type = '5' then '购买老版音频课程' when o.ab_type = '6' then '购买探普贝' when o.ab_type = '7' then '购买课件' when o.ab_type = '300' then '购买新版课程' when o.ab_type = '305' then '购买新版课程课件' when o.ab_type = '323' then '线下活动报名' end res_type_name ,case when o.ab_order_type = 0 then '普通订单' when o.ab_order_type = 1 then '赠送型订单' when o.ab_order_type = 2 then '领取型订单' when o.ab_order_type = 3 then '拼团型订单' end as order_type -- 订单类型 0正常订单 1赠送型订单 2领取型订单 3拼团型订单, ,coalesce(o.ab_proid, '') as res_id ,o.transaction_serial_no as transaction_no -- 交易记录 ,o.ab_status as order_status -- ,o.ab_order_type -- 订单类型 0正常订单 1赠送型订单 2领取型订单 3拼团型订单, ,o.present_status -- (ab_order_type为1时有值)赠送状态 0赠送中 1赠送完成 ,o.group_buy_status -- (ab_order_type为3时有值)赠送状态 0拼团失败 1拼团成功 2拼团进行中 ,o.order_number -- 订单份数 ,o.createtime as create_time -- 订单创建时间 ,o.pay_time -- 订单支付时间 ,o.complete_time -- 订单完成时间 from tamp_user.order_flow o left join tamp_analysis.user_info_view u on o.createby = u.user_id where o.deletetag = '0' # and o.ab_proid <> '' # and o.ab_proid is not null and o.is_sand_box <> 1 and o.group_buy_status in (1, 2) order by data_dt desc ''' content_order_dict = fetch_all(sql, None) logging.info(f'{function_name} success') return content_order_dict if __name__ == '__main__': dwd_user_content_order() # import datetime # begin = datetime.date(2021, 9, 14) # end = datetime.date(2021, 9, 14) # data_dt = begin # delta = datetime.timedelta(days=1) # while data_dt <= end: # print(data_dt.strftime("%Y-%m-%d")) # dwd_user_content_order(data_dt) # data_dt += delta