analysis_select.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if len(get_auth_channel(user_id)) == 1:
  23. op = f" and channel = '{get_auth_channel(user_id)[0]}'"
  24. else:
  25. op = f' and channel in {tuple(get_auth_channel(user_id))}'
  26. op1 = f" and channel in {tuple(channel.split(','))}" if channel else ''
  27. op2 = f" order by {order_by} {order}" if order_by and order else ''
  28. op3 = f" limit {(page - 1) * page_size},{page_size} " if (page and page_size) else ''
  29. sql = f"""
  30. select a.channel,cost,amount from
  31. (select channel,round(sum(cost),2) cost from dw_daily_channel_cost
  32. where dt>='{start}' and dt<='{end}' {op} {op1} group by channel) a
  33. left join
  34. (select channel,round(sum(amount),2) amount from `order` where date>='{st}' and date<='{et}' and status=2 and
  35. formatDateTime(reg_time,'%Y-%m-%d')>='{start}' and formatDateTime(reg_time,'%Y-%m-%d')<='{end}' {op} {op1}
  36. group by channel) b
  37. using (channel) {op2} {op3}"""
  38. print(sql)
  39. data = get_dict_list(['channel','cost','amount'],ck.getDataList(sql))
  40. sum_sql = f"""select round(sum(cost),2) cost,round(sum(amount),2) amount from ({sql}) a"""
  41. total_data = get_dict_list(['cost','amount'],ck.getDataList(sum_sql))[0]
  42. total_sql = f"""select count(1) from ({sql}) a"""
  43. total = ck.getDataOne(total_sql)
  44. # print(total_data,total)
  45. return {"data":data,"total_data":total_data,"total":total}
  46. def channel_analysis_select_daily(arg):
  47. ck = CkUtils()
  48. start = arg.get("start")
  49. end = arg.get("end")
  50. st = arg.get("st")
  51. et = arg.get("et")
  52. channel = arg.get("channel")
  53. if not (start and end and st and et and channel):
  54. return {}
  55. 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}'
  56. and date>='{st}' and date<='{et}' and channel = '{channel}' and status=2
  57. group by date order by date desc limit 30"""
  58. data = get_dict_list(['dt','amount'],ck.getDataList(sql))
  59. # print(data)
  60. return {"data": data}
  61. if __name__ == '__main__':
  62. arg={"channel":"白雁阅读",'start':'2021-03-01','end':'2021-03-10','st':'2021-04-01','et':'2021-04-10'}
  63. channel_analysis_select_daily(arg)