ServiceDataController.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : UserDataController.py
  4. @Time : 2022/8/16 10:44
  5. @Author : peng
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. from django.db.models import Q, Sum
  10. from django.views.generic.base import View
  11. import datetime
  12. import requests
  13. from Model.models import OrdersSummary, DeviceInfoSummary
  14. from Service.CommonService import CommonService
  15. # 服务数据
  16. class ServiceDataView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.GET, request, operation)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.POST, request, operation)
  25. def validation(self, request_dict, request, operation):
  26. token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
  27. if token_code != 0:
  28. return response.json(token_code)
  29. if operation == 'payOrder': # 查询付费订单数据
  30. return self.query_pay_order(request_dict, response)
  31. elif operation == 'freeOrder': # 查询免费订单数据
  32. return self.query_free_order(request_dict, response)
  33. elif operation == 'firstPayOrder': # 查询首次付费订单数据
  34. return self.query_first_pay_order(request_dict, response)
  35. elif operation == 'repeatPayOrder': # 查询复购订单数据
  36. return self.query_repeat_pay_order(request_dict, response)
  37. elif operation == 'global/payOrder': # 查询全球付费订单数据
  38. return self.query_global_pay_order(request, request_dict, response)
  39. elif operation == 'global/freeOrder': # 查询全球免费订单数据
  40. return self.query_global_free_order(request, request_dict, response)
  41. elif operation == 'global/firstPayOrder': # 查询全球首次付费订单数据
  42. return self.query_global_first_pay_order(request, request_dict, response)
  43. elif operation == 'global/repeatPayOrder': # 查询全球复购订单数据
  44. return self.query_global_repeat_pay_order(request, request_dict, response)
  45. else:
  46. return response.json(414)
  47. @classmethod
  48. def query_pay_order(cls, request_dict, response):
  49. """
  50. 查询付费订单数据
  51. @param request_dict:请求参数
  52. @request_dict startTime:开始时间
  53. @request_dict endTime:结束时间
  54. @request_dict timeUnit:时间单位
  55. @request_dict storeMealType:套餐类型
  56. @param response:响应对象
  57. @return:
  58. """
  59. start_time = request_dict.get('startTime', None)
  60. end_time = request_dict.get('endTime', None)
  61. time_unit = request_dict.get('timeUnit', None)
  62. store_meal_type = request_dict.get('storeMealType', None)
  63. if not all([start_time, end_time, time_unit, store_meal_type]):
  64. return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
  65. try:
  66. store_meal_type = int(store_meal_type)
  67. order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=0,
  68. service_type=store_meal_type).values('count', 'country', 'total',
  69. 'device_type', 'store_meal')
  70. all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
  71. Q(query_type=0) | Q(query_type=1)).values('total', 'count')
  72. all_order_count = 0
  73. all_order_cny_total = 0
  74. all_order_usd_total = 0
  75. for item in all_order_qs:
  76. all_order_count += item['count']
  77. temp_total = eval(item['total'])
  78. all_order_cny_total += temp_total.get('CNY', 0)
  79. all_order_usd_total += temp_total.get('USD', 0)
  80. start_time = datetime.datetime.fromtimestamp(int(start_time))
  81. end_time = datetime.datetime.fromtimestamp(int(end_time))
  82. time_list = CommonService.cutting_time(start_time, end_time, time_unit)
  83. # 订单数量统计
  84. order_list = []
  85. for item in time_list:
  86. order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
  87. temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
  88. temp_count = temp_count if temp_count else 0
  89. temp_cny_total = 0
  90. temp_usd_total = 0
  91. for each in order_temp_qs:
  92. temp_total = eval(each['total'])
  93. temp_cny_total += temp_total.get('CNY', 0)
  94. temp_usd_total += temp_total.get('USD', 0)
  95. order_dict = {
  96. 'count': temp_count,
  97. 'cnyTotal': temp_cny_total,
  98. 'usdTotal': temp_usd_total,
  99. 'startTime': item[0],
  100. 'endTime': item[1]
  101. }
  102. order_list.append(order_dict)
  103. device_type_dict = {}
  104. country_dict = {}
  105. store_meal_dict = {}
  106. for each in order_qs:
  107. device_type_temp_dict = eval(each['device_type'])
  108. country_temp_dict = eval(each['country'])
  109. store_meal_temp_dict = eval(each['store_meal'])
  110. for k, v in device_type_temp_dict.items():
  111. if k in device_type_dict:
  112. for a, b in v.items():
  113. if a not in device_type_dict[k]:
  114. device_type_dict[k][a] = b
  115. else:
  116. device_type_dict[k][a] += b
  117. else:
  118. device_type_dict[k] = v
  119. for k, v in country_temp_dict.items():
  120. if k in country_dict:
  121. for a, b in v.items():
  122. if a not in country_dict[k]:
  123. country_dict[k][a] = b
  124. else:
  125. country_dict[k][a] += b
  126. else:
  127. country_dict[k] = v
  128. for k, v in store_meal_temp_dict.items():
  129. if k in store_meal_dict:
  130. for a, b in v.items():
  131. if a not in store_meal_dict[k]:
  132. store_meal_dict[k][a] = b
  133. else:
  134. store_meal_dict[k][a] += b
  135. else:
  136. store_meal_dict[k] = v
  137. # 设备类型订单统计
  138. device_type_list = []
  139. for k, v in device_type_dict.items():
  140. type_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  141. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  142. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  143. device_temp_dict = {
  144. 'typeName': k,
  145. 'count': v['数量'],
  146. 'typeRate': type_rate,
  147. 'cnyTotalMoney': v.get('CNY', 0),
  148. 'usdTotalMoney': v.get('USD', 0),
  149. 'cnyTotalRate': cny_total_rate,
  150. 'usdTotalRate': usd_total_rate,
  151. }
  152. device_type_list.append(device_temp_dict)
  153. # 区域订单统计
  154. region_list = []
  155. for k, v in country_dict.items():
  156. rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  157. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  158. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  159. region_temp_dict = {
  160. 'countryName': k,
  161. 'count': v['数量'],
  162. 'rate': rate,
  163. 'cnyTotalMoney': v.get('CNY', 0),
  164. 'usdTotalMoney': v.get('USD', 0),
  165. 'cnyTotalRate': cny_total_rate,
  166. 'usdTotalRate': usd_total_rate,
  167. }
  168. region_list.append(region_temp_dict)
  169. # 套餐订单统计
  170. store_meal_list = []
  171. for k, v in store_meal_dict.items():
  172. count_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  173. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  174. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  175. store_temp_dict = {
  176. 'count': v['数量'],
  177. 'storeMealName': k,
  178. 'cnyTotalMoney': v.get('CNY', 0),
  179. 'usdTotalMoney': v.get('USD', 0),
  180. 'cnyTotalRate': cny_total_rate,
  181. 'usdTotalRate': usd_total_rate,
  182. 'rate': count_rate
  183. }
  184. store_meal_list.append(store_temp_dict)
  185. res = {
  186. 'orders': order_list,
  187. 'regions': region_list,
  188. 'deviceType': device_type_list,
  189. 'storeMeal': store_meal_list,
  190. 'allOrderCount': all_order_count,
  191. 'allCnyOrderTotal': all_order_cny_total,
  192. 'allUsdOrderTotal': all_order_usd_total,
  193. }
  194. return response.json(0, res)
  195. except Exception as e:
  196. return response.json(500, repr(e))
  197. @classmethod
  198. def query_free_order(cls, request_dict, response):
  199. """
  200. 查询免费订单数据
  201. @param request_dict:请求参数
  202. @request_dict startTime:开始时间
  203. @request_dict endTime:结束时间
  204. @request_dict timeUnit:时间单位
  205. @request_dict storeMealType:套餐类型
  206. @param response:响应对象
  207. @return:
  208. """
  209. start_time = request_dict.get('startTime', None)
  210. end_time = request_dict.get('endTime', None)
  211. time_unit = request_dict.get('timeUnit', None)
  212. store_meal_type = request_dict.get('storeMealType', None)
  213. if not all([start_time, end_time, time_unit, store_meal_type]):
  214. return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
  215. try:
  216. store_meal_type = int(store_meal_type)
  217. order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=1,
  218. service_type=store_meal_type).values('count', 'country',
  219. 'device_type')
  220. free_order_count = order_qs.aggregate(count=Sum('count'))['count']
  221. free_order_count = free_order_count if free_order_count else 0 # 免费订单数量
  222. all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
  223. Q(query_type=0) | Q(query_type=1)).aggregate(count=Sum('count'))
  224. all_order_count = all_order_qs['count'] if all_order_qs['count'] else 0 # 所有订单数量
  225. device_qs = DeviceInfoSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=0).values(
  226. 'vod_service', 'ai_service', 'unicom_service')
  227. new_device_count = 0 # 销售设备数量
  228. start_time = datetime.datetime.fromtimestamp(int(start_time))
  229. end_time = datetime.datetime.fromtimestamp(int(end_time))
  230. time_list = CommonService.cutting_time(start_time, end_time, time_unit)
  231. # 转化率
  232. for item in device_qs:
  233. if store_meal_type == 0:
  234. service = eval(item['vod_service'])
  235. elif store_meal_type == 1:
  236. service = eval(item['ai_service'])
  237. else:
  238. service = eval(item['unicom_service'])
  239. for each in service.values():
  240. new_device_count += each
  241. inversion_rate = round(free_order_count / new_device_count * 100, 2) if new_device_count else 0
  242. # 订单数量统计
  243. order_list = []
  244. for item in time_list:
  245. order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
  246. temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
  247. temp_count = temp_count if temp_count else 0
  248. order_dict = {
  249. 'count': temp_count,
  250. 'startTime': item[0],
  251. 'endTime': item[1]
  252. }
  253. order_list.append(order_dict)
  254. device_type_dict = {}
  255. country_dict = {}
  256. for each in order_qs:
  257. device_type_temp_dict = eval(each['device_type'])
  258. for k, v in device_type_temp_dict.items():
  259. if k in device_type_dict:
  260. device_type_dict[k] += v
  261. else:
  262. device_type_dict[k] = v
  263. country_temp_dict = eval(each['country'])
  264. for k, v in country_temp_dict.items():
  265. if k in country_dict:
  266. country_dict[k] += v
  267. else:
  268. country_dict[k] = v
  269. # 设备类型订单统计
  270. device_type_list = []
  271. for k, v in device_type_dict.items():
  272. type_rate = round(v / all_order_count * 100, 2) if all_order_count else 0
  273. device_temp_qs = {
  274. 'typeName': k,
  275. 'count': v,
  276. 'typeRate': type_rate,
  277. }
  278. device_type_list.append(device_temp_qs)
  279. # 区域订单统计
  280. region_list = []
  281. for k, v in country_dict.items():
  282. rate = round(v / all_order_count * 100, 2) if all_order_count else 0
  283. region_temp_dict = {
  284. 'countryName': k,
  285. 'count': v,
  286. 'rate': rate
  287. }
  288. region_list.append(region_temp_dict)
  289. res = {
  290. 'orders': order_list,
  291. 'regions': region_list,
  292. 'deviceType': device_type_list,
  293. 'inversionRate': inversion_rate,
  294. 'newDeviceCount': new_device_count,
  295. 'allOrderCount': all_order_count
  296. }
  297. return response.json(0, res)
  298. except Exception as e:
  299. return response.json(500, repr(e))
  300. @classmethod
  301. def query_first_pay_order(cls, request_dict, response):
  302. """
  303. 查询首次付费订单数据
  304. @param request_dict:请求参数
  305. @request_dict startTime:开始时间
  306. @request_dict endTime:结束时间
  307. @request_dict timeUnit:时间单位
  308. @request_dict storeMealType:套餐类型
  309. @param response:响应对象
  310. @return:
  311. """
  312. start_time = request_dict.get('startTime', None)
  313. end_time = request_dict.get('endTime', None)
  314. time_unit = request_dict.get('timeUnit', None)
  315. store_meal_type = request_dict.get('storeMealType', None)
  316. if not all([start_time, end_time, time_unit, store_meal_type]):
  317. return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
  318. try:
  319. order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=2,
  320. service_type=store_meal_type).values('count', 'country', 'total',
  321. 'device_type')
  322. all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
  323. Q(query_type=0) | Q(query_type=1)).values('total', 'count')
  324. all_order_count = 0
  325. all_order_cny_total = 0
  326. all_order_usd_total = 0
  327. for item in all_order_qs:
  328. all_order_count += item['count']
  329. temp_total = eval(item['total'])
  330. all_order_cny_total += temp_total.get('CNY', 0)
  331. all_order_usd_total += temp_total.get('USD', 0)
  332. start_time = datetime.datetime.fromtimestamp(int(start_time))
  333. end_time = datetime.datetime.fromtimestamp(int(end_time))
  334. time_list = CommonService.cutting_time(start_time, end_time, time_unit)
  335. # 订单数量统计
  336. order_list = []
  337. for item in time_list:
  338. order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
  339. temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
  340. temp_count = temp_count if temp_count else 0
  341. temp_cny_total = 0
  342. temp_usd_total = 0
  343. for each in order_temp_qs:
  344. temp_total = eval(each['total'])
  345. temp_cny_total += temp_total.get('CNY', 0)
  346. temp_usd_total += temp_total.get('USD', 0)
  347. order_dict = {
  348. 'count': temp_count,
  349. 'cnyTotal': temp_cny_total,
  350. 'usdTotal': temp_usd_total,
  351. 'startTime': item[0],
  352. 'endTime': item[1]
  353. }
  354. order_list.append(order_dict)
  355. country_dict = {}
  356. device_type_dict = {}
  357. for each in order_qs:
  358. country_temp_dict = eval(each['country'])
  359. device_type_temp_dict = eval(each['device_type'])
  360. for k, v in country_temp_dict.items():
  361. if k in country_dict:
  362. for a, b in v.items():
  363. if a not in country_dict[k]:
  364. country_dict[k][a] = b
  365. else:
  366. country_dict[k][a] += b
  367. else:
  368. country_dict[k] = v
  369. for k, v in device_type_temp_dict.items():
  370. if k in device_type_dict:
  371. for a, b in v.items():
  372. if a not in device_type_dict[k]:
  373. device_type_dict[k][a] = b
  374. else:
  375. device_type_dict[k][a] += b
  376. else:
  377. device_type_dict[k] = v
  378. # 设备类型订单统计
  379. device_type_list = []
  380. for k, v in device_type_dict.items():
  381. type_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  382. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  383. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  384. device_temp_qs = {
  385. 'typeName': k,
  386. 'count': v['数量'],
  387. 'cnyTotalRate': cny_total_rate,
  388. 'usdTotalRate': usd_total_rate,
  389. 'typeRate': type_rate,
  390. 'cnyTotalMoney': v.get('CNY', 0),
  391. 'usdTotalMoney': v.get('USD', 0),
  392. }
  393. device_type_list.append(device_temp_qs)
  394. # 区域订单统计
  395. region_list = []
  396. for k, v in country_dict.items():
  397. rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  398. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  399. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  400. region_temp_dict = {
  401. 'countryName': k,
  402. 'count': v['数量'],
  403. 'rate': rate,
  404. 'cnyTotalMoney': v.get('CNY', 0),
  405. 'usdTotalMoney': v.get('USD', 0),
  406. 'cnyTotalRate': cny_total_rate,
  407. 'usdTotalRate': usd_total_rate,
  408. }
  409. region_list.append(region_temp_dict)
  410. res = {
  411. 'orders': order_list,
  412. 'regions': region_list,
  413. 'deviceType': device_type_list,
  414. 'allOrderCount': all_order_count,
  415. 'allOrderUsdTotal': all_order_usd_total,
  416. 'allOrderCnyTotal': all_order_cny_total,
  417. }
  418. return response.json(0, res)
  419. except Exception as e:
  420. return response.json(500, repr(e))
  421. @classmethod
  422. def query_repeat_pay_order(cls, request_dict, response):
  423. """
  424. 查询复购订单数据
  425. @param request_dict:请求参数
  426. @request_dict startTime:开始时间
  427. @request_dict endTime:结束时间
  428. @request_dict timeUnit:时间单位
  429. @request_dict storeMealType:套餐类型
  430. @param response:响应对象
  431. @return:
  432. """
  433. start_time = request_dict.get('startTime', None)
  434. end_time = request_dict.get('endTime', None)
  435. time_unit = request_dict.get('timeUnit', None)
  436. store_meal_type = request_dict.get('storeMealType', None)
  437. if not all([start_time, end_time, time_unit, store_meal_type]):
  438. return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
  439. try:
  440. order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=3,
  441. service_type=store_meal_type).values('count', 'country', 'total',
  442. 'device_type')
  443. repeat_pay_order_count = order_qs.aggregate(count=Sum('count'))['count']
  444. repeat_pay_order_count = repeat_pay_order_count if repeat_pay_order_count else 0
  445. all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
  446. Q(query_type=0) | Q(query_type=1)).values('total', 'count')
  447. all_order_count = 0 # 所有订单数量
  448. all_order_cny_total = 0
  449. all_order_usd_total = 0
  450. for item in all_order_qs:
  451. all_order_count += item['count']
  452. temp_total = eval(item['total'])
  453. all_order_cny_total += temp_total.get('CNY', 0)
  454. all_order_usd_total += temp_total.get('USD', 0)
  455. # 订单复购率
  456. repeat_rate = round(repeat_pay_order_count / all_order_count * 100, 2) if all_order_count else 0
  457. start_time = datetime.datetime.fromtimestamp(int(start_time))
  458. end_time = datetime.datetime.fromtimestamp(int(end_time))
  459. time_list = CommonService.cutting_time(start_time, end_time, time_unit)
  460. # 订单数量统计
  461. order_list = []
  462. for item in time_list:
  463. order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
  464. temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
  465. temp_count = temp_count if temp_count else 0
  466. temp_cny_total = 0
  467. temp_usd_total = 0
  468. for each in order_temp_qs:
  469. temp_total = eval(each['total'])
  470. temp_cny_total += temp_total.get('CNY', 0)
  471. temp_usd_total += temp_total.get('USD', 0)
  472. order_dict = {
  473. 'count': temp_count,
  474. 'cnyTotal': temp_cny_total,
  475. 'usdTotal': temp_usd_total,
  476. 'startTime': item[0],
  477. 'endTime': item[1]
  478. }
  479. order_list.append(order_dict)
  480. device_type_dict = {}
  481. country_dict = {}
  482. for each in order_qs:
  483. country_temp_dict = eval(each['country'])
  484. device_type_temp_dict = eval(each['device_type'])
  485. for k, v in country_temp_dict.items():
  486. if k in country_dict:
  487. for a, b in v.items():
  488. if a not in country_dict[k]:
  489. country_dict[k][a] = b
  490. else:
  491. country_dict[k][a] += b
  492. else:
  493. country_dict[k] = v
  494. for k, v in device_type_temp_dict.items():
  495. if k in device_type_dict:
  496. for a, b in v.items():
  497. if a not in device_type_dict[k]:
  498. device_type_dict[k][a] = b
  499. else:
  500. device_type_dict[k][a] += b
  501. else:
  502. device_type_dict[k] = v
  503. # 设备类型订单统计
  504. device_type_list = []
  505. for k, v in device_type_dict.items():
  506. type_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  507. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  508. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  509. device_temp_qs = {
  510. 'typeName': k,
  511. 'count': v['数量'],
  512. 'typeRate': type_rate,
  513. 'cnyTotalMoney': v.get('CNY', 0),
  514. 'usdTotalMoney': v.get('USD', 0),
  515. 'cnyTotalRate': cny_total_rate,
  516. 'usdTotalRate': usd_total_rate,
  517. }
  518. device_type_list.append(device_temp_qs)
  519. # 区域订单统计
  520. region_list = []
  521. for k, v in country_dict.items():
  522. rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
  523. cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
  524. usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
  525. region_temp_dict = {
  526. 'countryName': k,
  527. 'count': v['数量'],
  528. 'rate': rate,
  529. 'cnyTotalMoney': v.get('CNY', 0),
  530. 'usdTotalMoney': v.get('USD', 0),
  531. 'cnyTotalRate': cny_total_rate,
  532. 'usdTotalRate': usd_total_rate,
  533. }
  534. region_list.append(region_temp_dict)
  535. res = {
  536. 'orders': order_list,
  537. 'regions': region_list,
  538. 'deviceType': device_type_list,
  539. 'repeatRate': repeat_rate,
  540. 'repeatOrderCount': repeat_pay_order_count,
  541. 'allOrderCount': all_order_count,
  542. 'allOrderCnyTotal': all_order_cny_total,
  543. 'allOrderUsdTotal': all_order_usd_total,
  544. }
  545. return response.json(0, res)
  546. except Exception as e:
  547. return response.json(500, repr(e))
  548. @classmethod
  549. def query_global_pay_order(cls, request, request_dict, response):
  550. """
  551. 查询全球付费订单数据
  552. @param request:请求
  553. @param request_dict:请求参数
  554. @param response:响应对象
  555. @return:
  556. """
  557. url_list = CommonService.get_domain_name()
  558. try:
  559. headers = {
  560. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  561. }
  562. order_list = []
  563. region_list = []
  564. device_type_list = []
  565. all_order_count = 0
  566. all_cny_order_total = 0
  567. all_usd_order_total = 0
  568. store_meal_list = []
  569. for url in url_list:
  570. url = url + request.path.replace('global/', '')
  571. res = requests.get(url=url, params=request_dict, headers=headers)
  572. result = res.json()
  573. if result['result_code'] == 0:
  574. # 处理订单
  575. for item in result['result']['orders']:
  576. flag = 0
  577. for each in order_list:
  578. if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
  579. each['count'] += int(item['count'])
  580. each['cnyTotal'] += item['cnyTotal']
  581. each['usdTotal'] += item['usdTotal']
  582. flag = 1
  583. break
  584. if flag == 0:
  585. order_list.append(item)
  586. # 处理地区
  587. for item in result['result']['regions']:
  588. flag = 0
  589. for each in region_list:
  590. if each['countryName'] == item['countryName']:
  591. each['count'] += int(item['count'])
  592. each['cnyTotalMoney'] += item['cnyTotalMoney']
  593. each['usdTotalMoney'] += item['usdTotalMoney']
  594. flag = 1
  595. break
  596. if flag == 0:
  597. region_list.append(item)
  598. # 处理设备类型
  599. for item in result['result']['deviceType']:
  600. flag = 0
  601. for each in device_type_list:
  602. if each['typeName'] == item['typeName']:
  603. each['count'] += int(item['count'])
  604. each['cnyTotalMoney'] += item['cnyTotalMoney']
  605. each['usdTotalMoney'] += item['usdTotalMoney']
  606. flag = 1
  607. break
  608. if flag == 0:
  609. device_type_list.append(item)
  610. # 处理套餐
  611. for item in result['result']['storeMeal']:
  612. flag = 0
  613. for each in store_meal_list:
  614. if each['storeMealName'] == item['storeMealName']:
  615. each['count'] += int(item['count'])
  616. each['cnyTotalMoney'] += item['cnyTotalMoney']
  617. each['usdTotalMoney'] += item['usdTotalMoney']
  618. flag = 1
  619. break
  620. if flag == 0:
  621. store_meal_list.append(item)
  622. all_cny_order_total += result['result']['allCnyOrderTotal']
  623. all_usd_order_total += result['result']['allUsdOrderTotal']
  624. all_order_count += result['result']['allOrderCount']
  625. else:
  626. return response.json(result['result_code'], result['result'])
  627. for item in order_list:
  628. item['cnyTotal'] = round(item['cnyTotal'], 2)
  629. item['usdTotal'] = round(item['usdTotal'], 2)
  630. for item in region_list:
  631. item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  632. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  633. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  634. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  635. 2) if all_cny_order_total else 0
  636. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  637. 2) if all_usd_order_total else 0
  638. for item in device_type_list:
  639. item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  640. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  641. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  642. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  643. 2) if all_cny_order_total else 0
  644. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  645. 2) if all_usd_order_total else 0
  646. for item in store_meal_list:
  647. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  648. 2) if all_cny_order_total else 0
  649. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  650. 2) if all_usd_order_total else 0
  651. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  652. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  653. item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  654. res = {
  655. 'orders': order_list,
  656. 'regions': CommonService.list_sort(region_list),
  657. 'deviceType': CommonService.list_sort(device_type_list),
  658. 'storeMeal': CommonService.list_sort(store_meal_list)
  659. }
  660. return response.json(0, res)
  661. except Exception as e:
  662. return response.json(500, repr(e))
  663. @classmethod
  664. def query_global_free_order(cls, request, request_dict, response):
  665. """
  666. 查询全球免费订单数据
  667. @param request:请求
  668. @param request_dict:请求参数
  669. @param response:响应对象
  670. @return:
  671. """
  672. url_list = CommonService.get_domain_name()
  673. try:
  674. headers = {
  675. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  676. }
  677. order_list = []
  678. region_list = []
  679. free_order_count = 0
  680. device_type_list = []
  681. new_device_count = 0
  682. all_order_count = 0
  683. for url in url_list:
  684. url = url + request.path.replace('global/', '')
  685. res = requests.get(url=url, params=request_dict, headers=headers)
  686. result = res.json()
  687. if result['result_code'] == 0:
  688. # 处理订单
  689. for item in result['result']['orders']:
  690. flag = 0
  691. for each in order_list:
  692. if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
  693. each['count'] += int(item['count'])
  694. flag = 1
  695. break
  696. if flag == 0:
  697. order_list.append(item)
  698. # 处理地区
  699. for item in result['result']['regions']:
  700. flag = 0
  701. for each in region_list:
  702. if each['countryName'] == item['countryName']:
  703. each['count'] += int(item['count'])
  704. free_order_count += int(item['count'])
  705. flag = 1
  706. break
  707. if flag == 0:
  708. region_list.append(item)
  709. free_order_count += int(item['count'])
  710. # 处理设备类型
  711. for item in result['result']['deviceType']:
  712. flag = 0
  713. for each in device_type_list:
  714. if each['typeName'] == item['typeName']:
  715. each['count'] += int(item['count'])
  716. flag = 1
  717. break
  718. if flag == 0:
  719. device_type_list.append(item)
  720. # 处理转化率
  721. new_device_count += int(result['result']['newDeviceCount'])
  722. all_order_count += int(result['result']['allOrderCount'])
  723. else:
  724. return response.json(result['result_code'], result['result'])
  725. inversion_rate = round(free_order_count / new_device_count * 100, 2) if new_device_count else 0
  726. for item in region_list:
  727. item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  728. for item in device_type_list:
  729. item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  730. res = {
  731. 'orders': order_list,
  732. 'regions': CommonService.list_sort(region_list),
  733. 'deviceType': CommonService.list_sort(device_type_list),
  734. 'inversionRate': inversion_rate
  735. }
  736. return response.json(0, res)
  737. except Exception as e:
  738. return response.json(500, repr(e))
  739. @classmethod
  740. def query_global_first_pay_order(cls, request, request_dict, response):
  741. """
  742. 查询全球首次付费订单数据
  743. @param request:请求
  744. @param request_dict:请求参数
  745. @param response:响应对象
  746. @return:
  747. """
  748. url_list = CommonService.get_domain_name()
  749. try:
  750. headers = {
  751. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  752. }
  753. order_list = []
  754. region_list = []
  755. device_type_list = []
  756. all_order_count = 0
  757. all_usd_order_total = 0
  758. all_cny_order_total = 0
  759. for url in url_list:
  760. url = url + request.path.replace('global/', '')
  761. res = requests.get(url=url, params=request_dict, headers=headers)
  762. result = res.json()
  763. if result['result_code'] == 0:
  764. # 处理订单
  765. for item in result['result']['orders']:
  766. flag = 0
  767. for each in order_list:
  768. if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
  769. each['count'] += int(item['count'])
  770. each['cnyTotal'] += item['cnyTotal']
  771. each['usdTotal'] += item['usdTotal']
  772. flag = 1
  773. break
  774. if flag == 0:
  775. order_list.append(item)
  776. # 处理地区
  777. for item in result['result']['regions']:
  778. flag = 0
  779. for each in region_list:
  780. if each['countryName'] == item['countryName']:
  781. each['count'] += int(item['count'])
  782. each['cnyTotalMoney'] += item['cnyTotalMoney']
  783. each['usdTotalMoney'] += item['usdTotalMoney']
  784. flag = 1
  785. break
  786. if flag == 0:
  787. region_list.append(item)
  788. # 处理设备类型
  789. for item in result['result']['deviceType']:
  790. flag = 0
  791. for each in device_type_list:
  792. if each['typeName'] == item['typeName']:
  793. each['count'] += int(item['count'])
  794. each['cnyTotalMoney'] += item['cnyTotalMoney']
  795. each['usdTotalMoney'] += item['usdTotalMoney']
  796. flag = 1
  797. break
  798. if flag == 0:
  799. device_type_list.append(item)
  800. all_order_count += result['result']['allOrderCount']
  801. all_cny_order_total += result['result']['allOrderCnyTotal']
  802. all_usd_order_total += result['result']['allOrderUsdTotal']
  803. else:
  804. return response.json(result['result_code'], result['result'])
  805. for item in order_list:
  806. item['cnyTotal'] = round(item['cnyTotal'], 2)
  807. item['usdTotal'] = round(item['usdTotal'], 2)
  808. for item in region_list:
  809. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  810. 2) if all_cny_order_total else 0
  811. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  812. 2) if all_usd_order_total else 0
  813. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  814. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  815. item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  816. for item in device_type_list:
  817. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  818. 2) if all_cny_order_total else 0
  819. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  820. 2) if all_usd_order_total else 0
  821. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  822. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  823. item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  824. res = {
  825. 'orders': order_list,
  826. 'regions': CommonService.list_sort(region_list),
  827. 'deviceType': CommonService.list_sort(device_type_list),
  828. }
  829. return response.json(0, res)
  830. except Exception as e:
  831. return response.json(500, repr(e))
  832. @classmethod
  833. def query_global_repeat_pay_order(cls, request, request_dict, response):
  834. """
  835. 查询全球复购订单数据
  836. @param request:请求
  837. @param request_dict:请求参数
  838. @param response:响应对象
  839. @return:
  840. """
  841. url_list = CommonService.get_domain_name()
  842. try:
  843. headers = {
  844. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  845. }
  846. order_list = []
  847. region_list = []
  848. repeat_order_count = 0
  849. device_type_list = []
  850. all_order_count = 0
  851. all_usd_order_total = 0
  852. all_cny_order_total = 0
  853. for url in url_list:
  854. url = url + request.path.replace('global/', '')
  855. res = requests.get(url=url, params=request_dict, headers=headers)
  856. result = res.json()
  857. if result['result_code'] == 0:
  858. # 处理订单
  859. for item in result['result']['orders']:
  860. flag = 0
  861. for each in order_list:
  862. if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
  863. each['count'] += int(item['count'])
  864. each['cnyTotal'] += item['cnyTotal']
  865. each['usdTotal'] += item['usdTotal']
  866. flag = 1
  867. break
  868. if flag == 0:
  869. order_list.append(item)
  870. # 处理地区
  871. for item in result['result']['regions']:
  872. flag = 0
  873. for each in region_list:
  874. if each['countryName'] == item['countryName']:
  875. each['count'] += int(item['count'])
  876. each['cnyTotalMoney'] += item['cnyTotalMoney']
  877. each['usdTotalMoney'] += item['usdTotalMoney']
  878. flag = 1
  879. break
  880. if flag == 0:
  881. region_list.append(item)
  882. # 处理设备类型
  883. for item in result['result']['deviceType']:
  884. flag = 0
  885. for each in device_type_list:
  886. if each['typeName'] == item['typeName']:
  887. each['count'] += int(item['count'])
  888. each['cnyTotalMoney'] += item['cnyTotalMoney']
  889. each['usdTotalMoney'] += item['usdTotalMoney']
  890. flag = 1
  891. break
  892. if flag == 0:
  893. device_type_list.append(item)
  894. # 处理订单复购率
  895. repeat_order_count += result['result']['repeatOrderCount']
  896. all_order_count += result['result']['allOrderCount']
  897. all_usd_order_total += result['result']['allOrderUsdTotal']
  898. all_cny_order_total += result['result']['allOrderCnyTotal']
  899. else:
  900. return response.json(result['result_code'], result['result'])
  901. repeat_rate = round(repeat_order_count / all_order_count * 100, 2) if all_order_count else 0
  902. for item in order_list:
  903. item['cnyTotal'] = round(item['cnyTotal'], 2)
  904. item['usdTotal'] = round(item['usdTotal'], 2)
  905. for item in device_type_list:
  906. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  907. 2) if all_cny_order_total else 0
  908. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  909. 2) if all_usd_order_total else 0
  910. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  911. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  912. item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  913. for item in region_list:
  914. item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
  915. item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
  916. item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
  917. 2) if all_cny_order_total else 0
  918. item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
  919. 2) if all_usd_order_total else 0
  920. item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
  921. res = {
  922. 'orders': order_list,
  923. 'regions': CommonService.list_sort(region_list),
  924. 'deviceType': CommonService.list_sort(device_type_list),
  925. 'repeatRate': repeat_rate,
  926. }
  927. return response.json(0, res)
  928. except Exception as e:
  929. return response.json(500, repr(e))