ck 4 years ago
parent
commit
16cf7db2f0
4 changed files with 87 additions and 73 deletions
  1. 48 67
      data_manage/pitcher_panel.py
  2. 6 6
      handlers/PitcherPanelHandler.py
  3. 14 0
      model/CommonUtils.py
  4. 19 0
      model/DataBaseOperation.py

+ 48 - 67
data_manage/pitcher_panel.py

@@ -7,14 +7,14 @@ du = DateUtils()
 log = logger()
 
 """根据公众号获取投手"""
-def get_channel_belong_pitcher(channel):
-
-    sql = f"select pitcher,channel from dw_daily_channel_cost where dt='{du.get_n_days(-1)}' and pitcher!='' and channel!=''"
-    data=ck.execute(sql)
-    di={}
-    for i in data:
-        di[i[1]]=i[0]
-    return di.get(channel,'')
+# def get_channel_belong_pitcher(channel):
+#
+#     sql = f"select pitcher,channel from dw_daily_channel_cost where dt='{du.get_n_days(-1)}' and pitcher!='' and channel!=''"
+#     data=ck.execute(sql)
+#     di={}
+#     for i in data:
+#         di[i[1]]=i[0]
+#     return di.get(channel,'')
 
 """根据名字获取其归属的公众号"""
 def get_channels_from_user(user):
@@ -25,82 +25,67 @@ def get_channels_from_user(user):
 
 
 def get_pitcher_panel_channel(pitcher,channel,start,end,page,page_size,order_by,order):
-    ck=CkUtils()
+    db=MysqlUtils()
     sql=f"""select channel,stage,platform,book,
-            formatDateTime(dt,'%Y-%m-%d') as date,
-            toDecimal32(cost,2),
-            toDecimal32(first_order_amount,2),
-           toDecimal32(if(cost=0,0,first_order_amount/cost),4) first_roi,
+           dt as date,cost,first_order_amount,
+           if(cost=0,0,first_order_amount/cost) first_roi,
            first_order_user,first_order_count,
-           toDecimal32(if(first_order_user=0,0,cost/first_order_user),2) first_per_cost,
+           if(first_order_user=0,0,cost/first_order_user) first_per_cost,
            view_count,click_count,follow_user,
-           toDecimal32(if(click_count=0,0,follow_user/click_count),4) follow_rate,
-           toDecimal32(if(follow_user=0,0,cost/follow_user),2) follow_per_cost,
-            toDecimal32(total_cost,2),
-           toDecimal32(if(total_cost=0,0,total_amount/total_cost),2) back_rate
-           from dw_daily_channel where dt>='{start}' and dt<='{end}'  """
+           if(click_count=0,0,follow_user/click_count) follow_rate,
+           if(follow_user=0,0,cost/follow_user) follow_per_cost,
+           total_cost,
+           if(total_cost=0,0,total_amount/total_cost) back_rate
+           from dw_channel_daily where dt>='{start}' and dt<='{end}'  """
     if pitcher!='all':
         sql += f" and pitcher='{pitcher}' "
 
     if channel!='':
         sql += f" and channel='{channel}' "
 
-    total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
-    sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
-
-    print(sql)
-    data=ck.execute(sql)
-
-    key=['channel','stage','platform','book','date','cost','first_order_amount','first_roi','first_order_user',
-         'first_order_count','first_per_cost','view_count','click_count','follow_user','follow_rate','follow_per_cost',
-         'total_cost','back_rate']
+    sql += f" order by {order_by} {order}"
 
-
-    return get_dict_list(key,get_round(data,4)),total
+    return getLimitData(db.quchen_text,sql,page,page_size)
 
 
 def get_pitcher_panel_daily(pitcher, start, end, page, page_size, order_by, order):
-    ck = CkUtils()
+    db=MysqlUtils()
+    op1= f" and pitcher='{pitcher}'" if pitcher else ''
+    op2=f" and dt>='{start}' " if start else ''
+    op3=f" and dt<='{end}' " if end else ''
+    op4=f" order by {order_by} {order}" if order_by and order else ''
+
     sql=f"""
-        select formatDateTime(dt,'%Y-%m-%d') date,'{pitcher}' pitcher,cost,first_order_amount,first_roi,
-order_amount,today_roi,total_amount,total_cost,total_amount-total_cost total_profit,
-if(total_cost=0,0,total_amount/total_cost) total_roi from 
-        (select dt,
-               round(sum(cost),2) cost,
-               round(sum(first_order_amount),2) first_order_amount,
-               round(if(cost=0,0,first_order_amount/cost),4) first_roi ,
-               round(sum(order_amount),2) order_amount,
-               round(if(cost=0,0,sum(reg_order_amount)/cost),4)  today_roi
-               from dw_daily_channel 
-                where dt>='{start}' and dt<='{end}' and pitcher='{pitcher}' 
-                group by dt) a
-        left outer join (
-        select dt,total_amount,total_cost  from dw_daily_pitcher where pitcher='{pitcher}'
-) b on a.dt=b.dt  
+            select dt date,
+            pitcher,cost,
+            first_reg_amount first_order_amount,
+            round(if(cost=0,0,first_reg_amount/cost),4) first_roi,
+            amount order_amount,
+            round(if(cost=0,0,reg_amount/cost),4)  today_roi,
+            total_amount,
+            total_cost,
+            total_amount-total_cost total_profit,
+            if(total_cost=0,0,total_amount/total_cost) total_roi
+            from dw_pitcher_daily where 1=1 {op1} {op2} {op3} {op4}
 """
     print(sql)
-    total = ck.execute(f"select count(1) from ({sql}) a")[0][0]
-    sql += f" order by {order_by} {order} limit {(page-1)*page_size},{page_size} "
-    print(sql)
-    data = ck.execute(sql)
-
-
-    key=['date','pitcher','cost','first_order_amount','first_roi','order_amount','today_roi','total_amount','total_cost','total_profit','total_roi']
-    print(get_dict_list(key,get_round(data)))
-    return get_dict_list(key,get_round(data)),total
+    return getLimitData(db.quchen_text,sql,page,page_size)
 
 
 def get_pitcher_panel_overview(pitcher):
     db=MysqlUtils()
+    op1= f" and pitcher='{pitcher}'" if pitcher else ''
+
+
     sql=f"""select  pitcher,
         total_cost cost,
         total_amount amount,
-        total_roi roi, 
+        if(total_cost=0,0,total_amount/total_cost) roi, 
         channel_count,
         on_channel_count,
        off_channel_count,
        this_month_cost this_month_cost,
-      this_month_amount this_month_amount,
+    this_month_amount this_month_amount,
     this_month_roi this_month_roi,
     last_month_cost last_month_cost,
     last_month_amount last_month_amount,
@@ -108,17 +93,13 @@ def get_pitcher_panel_overview(pitcher):
     last_month_far_amount last_month_far_amount,
        follow_user,
     last_month_far_roi last_month_far_roi
-       from  dm_pitcher_daily_overview where  dt='{du.get_n_days(0)}'"""
-
-    if pitcher != 'all':
-        sql += f" and pitcher='{pitcher}' "
-
+       from  dm_pitcher_daily_overview a 
+left join (
+select total_cost,total_amount,pitcher pitcher2 from dw_pitcher_daily where dt='{du.get_n_days(0)}') b on pitcher=pitcher2
+where 1=1 {op1}
+    """
     print(sql)
-    data=db.quchen_text.getData(sql)
-    print(data)
-    key=['pitcher','cost','amount','roi','channel_count','on_channel_count','off_channel_count','this_month_cost','this_month_amount','this_month_roi',
-         'last_month_cost','last_month_amount','last_month_roi','last_month_far_amount','follow_user','last_month_far_roi']
-    return get_dict_list(key,get_round(data))
+    return db.quchen_text.getData_json(sql)
 
 
 def get_channel_overview(channel,pitcher,start,end,page,page_size,order_by,order):

+ 6 - 6
handlers/PitcherPanelHandler.py

@@ -27,11 +27,11 @@ class PitcherPanelDaily(BaseHandler):
     def post(self):
         args=self.get_args()
         print(args)
-        pitcher = args.get("pitcher", '')
-        start = args.get("start", self.get_n_days(-1))
-        end = args.get("end", self.get_n_days(0))
-        page = args.get("page", '1')
-        page_size = args.get("page_size", 10)
+        pitcher = args.get("pitcher")
+        start = args.get("start")
+        end = args.get("end")
+        page = args.get("page")
+        page_size = args.get("page_size")
         order_by = args.get("order_by", 'date')
         order = args.get("order", 'desc')
         if 'date_range' in list(args.keys()):
@@ -45,7 +45,7 @@ class PitcherPanelDaily(BaseHandler):
 class PitcherPanelOverview(BaseHandler):
     def post(self):
         args=self.get_args()
-        pitcher = args.get("pitcher", '')
+        pitcher = args.get("pitcher")
         data = get_pitcher_panel_overview(pitcher)
         self.write_json(data)
 

+ 14 - 0
model/CommonUtils.py

@@ -30,3 +30,17 @@ def get_round(li,p=4):
     return y
 
 
+def getLimitData(DataBase,sql,page,page_size):
+    total = DataBase.getData(f"select count(1) from ({sql}) a")[0][0]
+    if page and page_size:
+        sql += f" limit {(page - 1) * page_size},{page_size} "
+    print(sql)
+    data = DataBase.getData_json(sql)
+    return data,total
+
+
+
+
+
+
+

+ 19 - 0
model/DataBaseOperation.py

@@ -106,6 +106,25 @@ class MysqlOperation:
             log.info('sql cost: %s' % (time.time() - start))
         return df
 
+    def getData_json(self,sql):
+        """
+        :param sql:
+        :return: [{},{}]
+        """
+        self.cursor.execute(sql)
+        num_fields = len(self.cursor.description)
+        field_names = [i[0] for i in self.cursor.description]
+        df = self.cursor.fetchall()
+        li=[]
+        for i in list(df):
+            li.append(dict(zip(field_names,i)))
+        return li
+
+
+
+
+
+
     def insertData(self, sql, args=None):
         # if args:
         #     log.debug(sql % tuple(args))