123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- from model.DataBaseUtils import MysqlUtils,CkUtils
- from model.DateUtils import DateUtils
- from model.log import logger
- log=logger()
- db = MysqlUtils()
- ck = CkUtils()
- dt = DateUtils()
- from datetime import datetime
- def dw_daily_channel(dt):
- sql = """select '{0}' as dt,c.channel as channel,pitcher,stage,order_count,order_user,order_amount,first_order_count,first_order_user,first_order_amount,
- view_count,click_count, cost
- from
- (select channel,order_count,order_user,order_amount,first_order_count,first_order_user,first_order_amount from
- (select channel,count(1) as order_count,count(distinct user_id) as order_user,sum(amount) as order_amount from order
- where date='{0}'
- group by channel) a
- left outer JOIN
- (select channel,count(1) as first_order_count,
- count(distinct user_id) as first_order_user,
- sum(amount) as first_order_amount from order
- where formatDateTime(reg_time,'%Y-%m-%d')='{0}' and date ='{0}'
- group by channel ) b
- on a.channel=b.channel) p
- right OUTER JOIN
- ( select channel,pitcher,stage,sum(cost) as cost, sum(view_count) as view_count,sum(valid_click_count) as click_count from
- (select account_id,cost,view_count,valid_click_count from daily_qq where date='{0}'
- union all
- select account_id,cost,view_count,valid_click_count from daily_vx where date='{0}') h
- right outer join
- (select account_id,name as channel,stage,pitcher from advertiser_vx_qq where name!='') j on h.account_id=j.account_id
- group by channel,stage,pitcher) c
- on p.channel=c.channel""".format(dt)
- data = ck.execute(sql)
- data1 = []
- for i in data:
- li = list(i)
- li[6] = int(li[6])
- li[9] = int(li[9])
- data1.append(tuple(li))
- insert_sql = "insert into dw_daily_channel values {}".format(str(data1)[1:-1])
- # print(insert_sql)
- db.quchen_text.execute(insert_sql)
- def channel_by_account_daily(ymd):
- """返回当天消耗账户对应的公众号表"""
- sql="""replace into channel_by_account_daily
- select '{0}' as dt,a.account_id as account_id, ifnull(ifnull(b.name,a.name),'') as channel from
- (select account_id,name from advertiser_qq) a
- left join
- (select b.account_id,b.name from
- (select min(end_time) as end_time,account_id from account_change where end_time>'{0}' GROUP BY account_id) a
- left join account_change b on a.end_time=b.end_time and a.account_id=b.account_id) b on a.account_id=b.account_id""".format(ymd)
- db.quchen_text.execute(sql)
- def channel_info_daily(ymd):
- """获取公众号某天的期数,投手,平台,书籍
- @ return [[]]
- """
- # 获取现在的全量公众号信息
- sql="""select '{}' as dt,a.name ,ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from (
- select name from advertiser_vx where name is not null group by name-- 公众号全量表
- union
- select name from advertiser_qq where name is not null group by name
- union
- select name from account_change group by name
- union
- select channel as name from pitcher_change group by channel
- union
- select name from platform_change group by name
- union
- select name from book_change group by name) a left join (
- select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_qq where name is not null group by name,stage,pitcher,platform,book
- union
- select name,ifnull(stage,'') stage,ifnull(pitcher,'') pitcher,ifnull(platform,'') platform,ifnull(book,'') book from advertiser_vx where name is not null
- ) b on a.name=b.name""".format(ymd)
- data=db.quchen_text.get_data_list(sql)
- pitcher_change=db.quchen_text.getData(
- "select b.channel as channel,pitcher from "
- "(select min(end_time) as end_time,channel from pitcher_change "
- " where end_time>'{}' GROUP BY channel) a"
- " left join pitcher_change b on a.end_time=b.end_time and a.channel=b.channel".format(ymd))
- platform_change=db.quchen_text.getData("select b.name as channel,primary_platform as platform from (select min(change_date) as change_date,name from platform_change "
- "where change_date>'{}' GROUP BY name) a "
- "left join platform_change b on a.change_date=b.change_date and a.name=b.name".format(ymd))
- book_change=db.quchen_text.getData("select b.name as channel,book from (select min(end_time) as end_time,name from book_change "
- "where end_time>'{}' GROUP BY name) a "
- "left join book_change b on a.end_time=b.end_time and a.name=b.name".format(ymd))
- for i in data:
- for j in pitcher_change:
- if i[1]==j[0]:
- i[3]=j[1]
- for k in platform_change:
- if i[1]==k[0]:
- i[4]=k[1]
- for h in book_change:
- if i[1]==h[0]:
- i[5]=h[1]
- print(data)
- insert_sql="replace into channel_info_daily values (%s,%s,%s,%s,%s,%s) "
- db.quchen_text.executeMany(insert_sql,data)
- def clean_order(dt):
- sql=""" replace into ods_order
- select
- case platform when '掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d')
- when '掌读' then from_unixtime(order_time, '%Y-%m-%d')
- ELSE left(order_time,10) end date,
- stage,platform,channel,channel_id,user_id,
- case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(order_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
- when platform='掌读' then from_unixtime(order_time, '%Y-%m-%d %H:%i:%s')
- ELSE order_time end order_time,
- case when platform='掌中云' then DATE_FORMAT(STR_TO_DATE(reg_time,'%Y-%m-%dT%H:%i:%s'),'%Y-%m-%d %H:%i:%s')
- when platform='掌读' then from_unixtime(reg_time, '%Y-%m-%d %H:%i:%s')
- ELSE reg_time end reg_time,
- amount,from_novel,order_id from `order` where date=UNIX_TIMESTAMP('{}')
- """.format(dt)
- db.quchen_text.execute(sql)
- def order_account_text():
- db.quchen_text.execute("truncate order_account_text")
- with open('./wending_account_config.csv',encoding='utf-8') as f:
- for i in f.readlines():
- db.quchen_text.execute("insert into order_account_text(platform,text) values ('文鼎','{}')".format(i))
- if __name__ == '__main__':
- # channel_info_daily('2020-12-14')
- for i in dt.getDateLists('2019-03-18','2020-12-16'):
- print(i)
- channel_info_daily(i)
|