فهرست منبع

Merge branch 'dev'

ck 4 سال پیش
والد
کامیت
5ae808f0fb
5فایلهای تغییر یافته به همراه132 افزوده شده و 1 حذف شده
  1. 75 0
      data_manage/analysis_select.py
  2. 1 1
      data_manage/public_analysis.py
  3. 44 0
      handlers/AnalysisHandler.py
  4. 8 0
      model/DataBaseUtils.py
  5. 4 0
      urls.py

+ 75 - 0
data_manage/analysis_select.py

@@ -0,0 +1,75 @@
+from model.UserAuthUtils import super_auth,get_auth_channel
+from model.CommonUtils import getLimitSumData,get_dict_list
+from model.DataBaseUtils import CkUtils
+
+
+def channel_analysis_select(arg):
+    """公众号订单跟踪查询"""
+    ck = CkUtils()
+    start = arg.get("start")
+    end = arg.get("end")
+    st = arg.get("st")
+    et = arg.get("et")
+    channel = arg.get("channel")
+    user_id = arg.get('user_id')
+    page = arg.get("page")
+    page_size = arg.get("page_size")
+    order_by = arg.get("order_by", 'cost')
+    order = arg.get("order", 'desc')
+
+    if not (start and end and st and et):
+        return {}
+    if user_id in super_auth():
+        op = ''
+    else:
+        op = f' and channel in {tuple(get_auth_channel(user_id))}'
+
+    op1 = f" and channel in {tuple(channel.split(','))}" if channel else ''
+
+    op2 = f" order by {order_by} {order}" if order_by and order else ''
+
+    op3 = f" limit {(page - 1) * page_size},{page_size} " if (page and page_size) else ''
+
+
+
+    sql = f"""
+            select a.channel,cost,amount from
+            (select channel,round(sum(cost),2) cost  from dw_daily_channel_cost
+                where  dt>='{start}' and dt<='{end}' {op} {op1} group by channel) a
+                left join
+            (select channel,round(sum(amount),2) amount from `order` where date>='{st}' and date<='{et}'  and
+            formatDateTime(reg_time,'%Y-%m-%d')>='{start}' and formatDateTime(reg_time,'%Y-%m-%d')<='{end}' {op} {op1}
+            group by channel) b
+            using (channel) {op2} {op3}"""
+    print(sql)
+    data = get_dict_list(['channel','cost','amount'],ck.getDataList(sql))
+    sum_sql = f"""select round(sum(cost),2) cost,round(sum(amount),2) amount from ({sql}) a"""
+    total_data = get_dict_list(['cost','amount'],ck.getDataList(sum_sql))[0]
+    total_sql = f"""select count(1) from ({sql}) a"""
+    total = ck.getDataOne(total_sql)
+    # print(total_data,total)
+    return {"data":data,"total_data":total_data,"total":total}
+
+
+def channel_analysis_select_daily(arg):
+    ck = CkUtils()
+    start = arg.get("start")
+    end = arg.get("end")
+    st = arg.get("st")
+    et = arg.get("et")
+    channel = arg.get("channel")
+    if not (start and end and st and et and channel):
+        return {}
+
+
+    sql = f"""select formatDateTime(date,'%Y-%m-%d'),round(sum(amount),2) from `order` where formatDateTime(reg_time,'%Y-%m-%d')>='{start}' and formatDateTime(reg_time,'%Y-%m-%d')<='{end}'
+            and date>='{st}' and date<='{et}' and channel = '{channel}'
+            group by date order by date desc limit 10"""
+
+    data = get_dict_list(['dt','amount'],ck.getDataList(sql))
+    # print(data)
+    return {"data": data}
+
+if __name__ == '__main__':
+    arg={"channel":"白雁阅读",'start':'2021-03-01','end':'2021-03-10','st':'2021-04-01','et':'2021-04-10'}
+    channel_analysis_select_daily(arg)

+ 1 - 1
data_manage/public_analysis.py

@@ -401,7 +401,7 @@ def boss_panel_summary(user_id,start,end,channel,pitcher, type, stage, page, pag
     op3 = f" and dt<='{end}' " if end else ''
     op4 = f" and type='{type}' " if type else ''
     op5= f" and stage='{stage}' " if stage else ''
-    op6 = f" and channel='{channel}' " if stage else ''
+    op6 = f" and channel='{channel}' " if channel else ''
     op7 = f" order by {order_by} {order}" if order_by and order else 'order by dt desc'
 
     dm = MysqlUtils().dm

+ 44 - 0
handlers/AnalysisHandler.py

@@ -0,0 +1,44 @@
+from handlers.HandlerBase import BaseHandler
+from model.DataBaseUtils import MysqlUtils
+import json
+import requests
+from data_manage.analysis_select import *
+from model.DataBaseUtils import MysqlUtils
+db = MysqlUtils()
+
+
+class ChannelAnalysisHandler(BaseHandler):
+    APP_MAP = {
+        "channel_analysis_select": channel_analysis_select,
+        "channel_analysis_select_daily": channel_analysis_select_daily,
+
+    }
+
+
+    def post(self):
+
+        arg = self.get_args()
+        print(arg)
+        try:
+            data = self.run(arg)
+            print(data)
+
+            self.write_json(data=data.get("data"),total=data.get('total',1),total_data=data.get('total_data',{}))
+        except Exception as e:
+            print(e)
+            self.write_fail()
+
+    def run(self, arg):
+
+        app = self.request.full_url().split('/')[-1]
+        return self.run_function(self.APP_MAP[app], arg)
+
+    def run_function(self, func, arg):
+        return func(arg)
+
+
+
+
+
+
+

+ 8 - 0
model/DataBaseUtils.py

@@ -84,6 +84,14 @@ class CkUtils:
 
         return self.client.execute(sql)
 
+    def getDataList(self,sql):
+        data = self.client.execute(sql)
+        return [list(i) for i in data]
+
+    def getDataOne(self,sql):
+        data = self.client.execute(sql)
+        return data[0][0]
+
     def getData_pd(self, sql, col):
         """
 

+ 4 - 0
urls.py

@@ -4,6 +4,7 @@ from handlers.GitHookHandler import *
 from handlers.PublicAnalysisHandler import *
 from handlers.TaskHandler import *
 from handlers import OperateHandler
+from handlers import AnalysisHandler
 
 
 urls = [
@@ -51,6 +52,9 @@ urls = [
     # CRUD
     (r'/operate/channel_group.*', OperateHandler.ChannelGroupHandler),  # 公众号分组设置
 
+    # 自助分析
+    (r'/data/analysis.*', AnalysisHandler.ChannelAnalysisHandler),
+
 
 
 ]