analysis_select.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from model.UserAuthUtils import super_auth,get_auth_channel
  2. from model.CommonUtils import getLimitSumData,get_dict_list
  3. from model.DataBaseUtils import CkUtils
  4. def channel_analysis_select(arg):
  5. """公众号订单跟踪查询"""
  6. ck = CkUtils()
  7. start = arg.get("start")
  8. end = arg.get("end")
  9. st = arg.get("st")
  10. et = arg.get("et")
  11. channel = arg.get("channel")
  12. user_id = arg.get('user_id')
  13. page = arg.get("page")
  14. page_size = arg.get("page_size")
  15. order_by = arg.get("order_by", 'cost')
  16. order = arg.get("order", 'desc')
  17. if not (start and end and st and et):
  18. return {}
  19. if user_id in super_auth():
  20. op = ''
  21. else:
  22. op = f' and channel in {tuple(get_auth_channel(user_id))}'
  23. op1 = f" and channel in {tuple(channel.split(','))}" if channel else ''
  24. op2 = f" order by {order_by} {order}" if order_by and order else ''
  25. op3 = f" limit {(page - 1) * page_size},{page_size} " if (page and page_size) else ''
  26. sql = f"""
  27. select a.channel,cost,amount from
  28. (select channel,round(sum(cost),2) cost from dw_daily_channel_cost
  29. where dt>='{start}' and dt<='{end}' {op} {op1} group by channel) a
  30. left join
  31. (select channel,round(sum(amount),2) amount from `order` where date>='{st}' and date<='{et}' and
  32. formatDateTime(reg_time,'%Y-%m-%d')>='{start}' and formatDateTime(reg_time,'%Y-%m-%d')<='{end}' {op} {op1}
  33. group by channel) b
  34. using (channel) {op2} {op3}"""
  35. print(sql)
  36. data = get_dict_list(['channel','cost','amount'],ck.getDataList(sql))
  37. sum_sql = f"""select round(sum(cost),2) cost,round(sum(amount),2) amount from ({sql}) a"""
  38. total_data = get_dict_list(['cost','amount'],ck.getDataList(sum_sql))[0]
  39. total_sql = f"""select count(1) from ({sql}) a"""
  40. total = ck.getDataOne(total_sql)
  41. # print(total_data,total)
  42. return {"data":data,"total_data":total_data,"total":total}
  43. def channel_analysis_select_daily(arg):
  44. ck = CkUtils()
  45. start = arg.get("start")
  46. end = arg.get("end")
  47. st = arg.get("st")
  48. et = arg.get("et")
  49. channel = arg.get("channel")
  50. if not (start and end and st and et and channel):
  51. return {}
  52. 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}'
  53. and date>='{st}' and date<='{et}' and channel = '{channel}'
  54. group by date order by date desc limit 10"""
  55. data = get_dict_list(['dt','amount'],ck.getDataList(sql))
  56. # print(data)
  57. return {"data": data}
  58. if __name__ == '__main__':
  59. arg={"channel":"白雁阅读",'start':'2021-03-01','end':'2021-03-10','st':'2021-04-01','et':'2021-04-10'}
  60. channel_analysis_select_daily(arg)