HomeDataController.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. import datetime
  10. import openpyxl
  11. import requests
  12. from django.db.models import Sum
  13. from django.http import HttpResponse
  14. from django.utils.encoding import escape_uri_path
  15. from django.views.generic.base import View
  16. from Model.models import DeviceUserSummary, OrdersSummary, DeviceInfoSummary
  17. from Service.CommonService import CommonService
  18. # 业务数据
  19. class HomeDataView(View):
  20. def get(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. operation = kwargs.get('operation')
  23. return self.validation(request.GET, request, operation)
  24. def post(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. operation = kwargs.get('operation')
  27. return self.validation(request.POST, request, operation)
  28. def validation(self, request_dict, request, operation):
  29. token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
  30. if token_code != 0:
  31. return response.json(token_code)
  32. if operation == 'allData': # 查询首页数据
  33. return self.query_all_data(response)
  34. elif operation == 'salesVolume': # 查询销售额数据
  35. return self.query_sales_volume_data(request_dict, response)
  36. elif operation == 'global/allData': # 查询全球首页数据
  37. return self.query_global_all_data(request, request_dict, response)
  38. elif operation == 'global/salesVolume': # 查询全球销售额数据
  39. return self.query_global_sales_volume_data(request, request_dict, response)
  40. elif operation == 'exportData': # 查询全球销售额数据
  41. return self.export_data(request_dict, response)
  42. else:
  43. return response.json(414)
  44. @classmethod
  45. def query_all_data(cls, response):
  46. """
  47. 查询首页数据
  48. @param response:响应对象
  49. @return:
  50. """
  51. end_time = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
  52. start_time = end_time + datetime.timedelta(days=-1)
  53. end_time_stamp = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  54. start_time_stamp = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  55. try:
  56. # 昨日用户增长数据
  57. user_increase_qs = DeviceUserSummary.objects.filter(time__gte=start_time_stamp, time__lt=end_time_stamp,
  58. query_type=0).values('count', 'country')
  59. user_increase_count = user_increase_qs.aggregate(count=Sum('count'))['count']
  60. user_increase_count = user_increase_count if user_increase_count else 0
  61. user_increase_region_dict = {}
  62. user_increase_region_list = []
  63. for item in user_increase_qs:
  64. user_increase_region_temp_dict = eval(item['country'])
  65. for k, v in user_increase_region_temp_dict.items():
  66. if k in user_increase_region_dict:
  67. user_increase_region_dict[k] += v
  68. else:
  69. user_increase_region_dict[k] = v
  70. for k, v in user_increase_region_dict.items():
  71. user_increase_region_list.append({
  72. 'countryName': k,
  73. 'count': v
  74. })
  75. # 所有用户数据
  76. user_all_qs = DeviceUserSummary.objects.filter(time__lte=start_time_stamp, query_type=0).values('count',
  77. 'country')
  78. user_all_region_dict = {}
  79. user_all_region_list = []
  80. for item in user_all_qs:
  81. user_all_region_temp_dict = eval(item['country'])
  82. for k, v in user_all_region_temp_dict.items():
  83. if k in user_all_region_dict:
  84. user_all_region_dict[k] += v
  85. else:
  86. user_all_region_dict[k] = v
  87. for k, v in user_all_region_dict.items():
  88. user_all_region_list.append({
  89. 'countryName': k,
  90. 'count': v
  91. })
  92. user_all_count = user_all_qs.aggregate(count=Sum('count'))['count']
  93. user_all_count = user_all_count if user_all_count else 0
  94. # 活跃用户数据
  95. user_active_count = DeviceUserSummary.objects.filter(time__gte=start_time_stamp, time__lt=end_time_stamp,
  96. query_type=1).aggregate(count=Sum('count'))['count']
  97. user_active_count = round(user_active_count, 2) if user_active_count else 0
  98. # 新增设备数据
  99. device_increase_count = DeviceInfoSummary.objects.filter(time__gte=start_time_stamp,
  100. time__lt=end_time_stamp,
  101. query_type=0).aggregate(count=Sum('count'))
  102. device_increase_count = device_increase_count['count'] if device_increase_count['count'] else 0
  103. # 活跃设备数据
  104. device_active_count = DeviceInfoSummary.objects.filter(time__gte=start_time_stamp,
  105. time__lt=end_time_stamp,
  106. query_type=1).aggregate(count=Sum('count'))['count']
  107. device_active_count = device_active_count if device_active_count else 0
  108. # 所有设备数据
  109. device_all_count = DeviceInfoSummary.objects.filter(time__lte=start_time_stamp,
  110. query_type=0).aggregate(count=Sum('count'))['count']
  111. device_all_count = device_all_count if device_all_count else 0
  112. # 昨日订单销售额
  113. order_qs = OrdersSummary.objects.filter(time__gte=start_time_stamp,
  114. time__lt=end_time_stamp,
  115. query_type=0).values('total')
  116. cny_total = 0
  117. usd_total = 0
  118. for item in order_qs:
  119. temp_total = eval(item['total'])
  120. cny_total = round(cny_total + temp_total.get('CNY', 0), 2)
  121. usd_total = round(usd_total + temp_total.get('USD', 0), 2)
  122. order_total = {'cnyTotal': cny_total, 'usdTotal': usd_total}
  123. # 昨日云存订单销售额
  124. vod_order_total = order_qs.filter(service_type=0)
  125. vod_cny_total = 0
  126. vod_usd_total = 0
  127. for item in vod_order_total:
  128. temp_total = eval(item['total'])
  129. vod_cny_total = round(vod_cny_total + temp_total.get('CNY', 0), 2)
  130. vod_usd_total = round(vod_usd_total + temp_total.get('USD', 0), 2)
  131. vod_order_total = {'cnyTotal': vod_cny_total, 'usdTotal': vod_usd_total}
  132. # 昨日AI订单销售额
  133. ai_order_total = order_qs.filter(service_type=1)
  134. ai_cny_total = 0
  135. ai_usd_total = 0
  136. for item in ai_order_total:
  137. temp_total = eval(item['total'])
  138. ai_cny_total = round(ai_cny_total + temp_total.get('CNY', 0), 2)
  139. ai_usd_total = round(ai_usd_total + temp_total.get('USD', 0), 2)
  140. ai_order_total = {'cnyTotal': ai_cny_total, 'usdTotal': ai_usd_total}
  141. # 昨日云盘订单销售额
  142. icloud_order_total = order_qs.filter(service_type=4)
  143. icloud_cny_total = 0
  144. icloud_usd_total = 0
  145. for item in icloud_order_total:
  146. temp_total = eval(item['total'])
  147. icloud_cny_total = round(icloud_cny_total + temp_total.get('CNY', 0), 2)
  148. icloud_usd_total = round(icloud_usd_total + temp_total.get('USD', 0), 2)
  149. icloud_order_total = {'cnyTotal': icloud_cny_total, 'usdTotal': icloud_usd_total}
  150. # 昨日联通订单销售额
  151. unicom_order_total = order_qs.filter(service_type__in=[2, 3])
  152. unicom_cny_total = 0
  153. unicom_usd_total = 0
  154. for item in unicom_order_total:
  155. temp_total = eval(item['total'])
  156. unicom_cny_total = round(unicom_cny_total + temp_total.get('CNY', 0), 2)
  157. unicom_usd_total = round(unicom_usd_total + temp_total.get('USD', 0), 2)
  158. unicom_order_total = {'cnyTotal': unicom_cny_total, 'usdTotal': unicom_usd_total}
  159. # 所有订单销售额
  160. order_all_qs = OrdersSummary.objects.filter(time__lte=start_time_stamp, query_type=0).values('total')
  161. cny_all_total = 0
  162. usd_all_total = 0
  163. for item in order_all_qs:
  164. temp_total = eval(item['total'])
  165. cny_all_total = round(cny_all_total + temp_total.get('CNY', 0), 2)
  166. usd_all_total = round(usd_all_total + temp_total.get('USD', 0), 2)
  167. order_all_total = {'cnyTotal': cny_all_total, 'usdTotal': usd_all_total}
  168. # 所有云存订单销售额
  169. vod_order_all_total = order_all_qs.filter(service_type=0)
  170. vod_cny_all_total = 0
  171. vod_usd_all_total = 0
  172. for item in vod_order_all_total:
  173. temp_total = eval(item['total'])
  174. vod_cny_all_total = round(vod_cny_all_total + temp_total.get('CNY', 0), 2)
  175. vod_usd_all_total = round(vod_usd_all_total + temp_total.get('USD', 0), 2)
  176. vod_order_all_total = {'cnyTotal': vod_cny_all_total, 'usdTotal': vod_usd_all_total}
  177. # 所有AI订单销售额
  178. ai_order_all_total = order_all_qs.filter(service_type=1)
  179. ai_cny_all_total = 0
  180. ai_usd_all_total = 0
  181. for item in ai_order_all_total:
  182. temp_total = eval(item['total'])
  183. ai_cny_all_total = round(ai_cny_all_total + temp_total.get('CNY', 0), 2)
  184. ai_usd_all_total = round(ai_usd_all_total + temp_total.get('USD', 0), 2)
  185. ai_order_all_total = {'cnyTotal': ai_cny_all_total, 'usdTotal': ai_usd_all_total}
  186. # 所有云盘订单销售额
  187. icloud_order_all_total = order_all_qs.filter(service_type=4)
  188. icloud_cny_all_total = 0
  189. icloud_usd_all_total = 0
  190. for item in icloud_order_all_total:
  191. temp_total = eval(item['total'])
  192. icloud_cny_all_total = round(icloud_cny_all_total + temp_total.get('CNY', 0), 2)
  193. icloud_usd_all_total = round(icloud_usd_all_total + temp_total.get('USD', 0), 2)
  194. icloud_order_all_total = {'cnyTotal': icloud_cny_all_total, 'usdTotal': icloud_usd_all_total}
  195. # 所有联通订单销售额
  196. unicom_order_all_total = order_all_qs.filter(service_type=2)
  197. unicom_cny_all_total = 0
  198. unicom_usd_all_total = 0
  199. for item in unicom_order_all_total:
  200. temp_total = eval(item['total'])
  201. unicom_cny_all_total = round(unicom_cny_all_total + temp_total.get('CNY', 0), 2)
  202. unicom_usd_all_total = round(unicom_usd_all_total + temp_total.get('USD', 0), 2)
  203. unicom_order_all_total = {'cnyTotal': unicom_cny_all_total, 'usdTotal': unicom_usd_all_total}
  204. res = {
  205. 'userIncreaseCount': user_increase_count,
  206. 'userActiveCount': user_active_count,
  207. 'userAllCount': user_all_count,
  208. 'deviceIncreaseCount': device_increase_count,
  209. 'deviceActiveCount': device_active_count,
  210. 'deviceAllCount': device_all_count,
  211. 'orderTotal': order_total,
  212. 'vodOrderTotal': vod_order_total,
  213. 'aiOrderTotal': ai_order_total,
  214. 'icloudOrderTotal': icloud_order_total,
  215. 'unicomOrderTotal': unicom_order_total,
  216. 'orderAllTotal': order_all_total,
  217. 'vodOrderAllTotal': vod_order_all_total,
  218. 'aiOrderAllTotal': ai_order_all_total,
  219. 'icloudOrderAllTotal': icloud_order_all_total,
  220. 'unicomOrderAllTotal': unicom_order_all_total,
  221. 'userIncreaseRegion': user_increase_region_list,
  222. 'userAllRegion': user_all_region_list
  223. }
  224. return response.json(0, res)
  225. except Exception as e:
  226. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  227. @classmethod
  228. def query_sales_volume_data(cls, request_dict, response):
  229. """
  230. 查询销售额数据
  231. @param request_dict:请求参数
  232. @request_dict startTime:开始时间
  233. @request_dict endTime:结束时间
  234. @request_dict timeUnit:时间单位
  235. @param response:响应对象
  236. @return:
  237. """
  238. start_time = request_dict.get('startTime', None)
  239. end_time = request_dict.get('endTime', None)
  240. time_unit = request_dict.get('timeUnit', None)
  241. if not all([start_time, end_time, time_unit]):
  242. return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
  243. try:
  244. order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=0).values(
  245. 'total')
  246. start_time = datetime.datetime.fromtimestamp(int(start_time))
  247. end_time = datetime.datetime.fromtimestamp(int(end_time))
  248. time_list = CommonService.cutting_time(start_time, end_time, time_unit)
  249. order_list = []
  250. for item in time_list:
  251. temp_order_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
  252. cny_total = 0
  253. usd_total = 0
  254. for each in temp_order_qs:
  255. temp_total = eval(each['total'])
  256. cny_total += temp_total.get('CNY', 0)
  257. usd_total += temp_total.get('USD', 0)
  258. res = {
  259. 'cnyTotal': cny_total,
  260. 'usdTotal': usd_total,
  261. 'startTime': item[0],
  262. 'endTime': item[1]
  263. }
  264. order_list.append(res)
  265. return response.json(0, order_list)
  266. except Exception as e:
  267. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  268. @classmethod
  269. def query_global_all_data(cls, request, request_dict, response):
  270. """
  271. 查询全球首页数据
  272. @param request:请求
  273. @param request_dict:请求参数
  274. @param response:响应对象
  275. @return:
  276. """
  277. url_list = CommonService.get_domain_name()
  278. try:
  279. headers = {
  280. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  281. }
  282. user_increase_count = 0
  283. user_active_count = 0
  284. user_all_count = 0
  285. device_increase_count = 0
  286. device_active_count = 0
  287. device_all_count = 0
  288. order_total = {'cnyTotal': 0, 'usdTotal': 0}
  289. vod_order_total = {'cnyTotal': 0, 'usdTotal': 0}
  290. ai_order_total = {'cnyTotal': 0, 'usdTotal': 0}
  291. icloud_order_total = {'cnyTotal': 0, 'usdTotal': 0}
  292. unicom_order_total = {'cnyTotal': 0, 'usdTotal': 0}
  293. order_all_total = {'cnyTotal': 0, 'usdTotal': 0}
  294. vod_order_all_total = {'cnyTotal': 0, 'usdTotal': 0}
  295. ai_order_all_total = {'cnyTotal': 0, 'usdTotal': 0}
  296. icloud_order_all_total = {'cnyTotal': 0, 'usdTotal': 0}
  297. unicom_order_all_total = {'cnyTotal': 0, 'usdTotal': 0}
  298. user_increase_temp_list = []
  299. user_increase_list = []
  300. user_increase_other_dict = {'count': 0, 'countryName': '其他', 'rate': 0}
  301. user_all_temp_list = []
  302. user_all_list = []
  303. user_all_other_dict = {'count': 0, 'countryName': '其他', 'rate': 0}
  304. for url in url_list:
  305. url = url + request.path.replace('global/', '')
  306. res = requests.get(url=url, params=request_dict, headers=headers)
  307. result = res.json()
  308. if result['result_code'] == 0:
  309. user_increase_count += result['result']['userIncreaseCount']
  310. user_active_count += result['result']['userActiveCount']
  311. user_all_count += result['result']['userAllCount']
  312. device_increase_count += result['result']['deviceIncreaseCount']
  313. device_active_count += result['result']['deviceActiveCount']
  314. device_all_count += result['result']['deviceAllCount']
  315. order_total['cnyTotal'] = round(
  316. order_total['cnyTotal'] + result['result']['orderTotal']['cnyTotal'], 2)
  317. order_total['usdTotal'] = round(
  318. order_total['usdTotal'] + result['result']['orderTotal']['usdTotal'], 2)
  319. vod_order_total['cnyTotal'] = round(
  320. vod_order_total['cnyTotal'] + result['result']['vodOrderTotal']['cnyTotal'], 2)
  321. vod_order_total['usdTotal'] = round(
  322. vod_order_total['usdTotal'] + result['result']['vodOrderTotal']['usdTotal'], 2)
  323. ai_order_total['cnyTotal'] = round(
  324. ai_order_total['cnyTotal'] + result['result']['aiOrderTotal']['cnyTotal'], 2)
  325. ai_order_total['usdTotal'] = round(
  326. ai_order_total['usdTotal'] + result['result']['aiOrderTotal']['usdTotal'], 2)
  327. icloud_order_total['cnyTotal'] = round(
  328. icloud_order_total['cnyTotal'] + result['result']['icloudOrderTotal']['cnyTotal'], 2)
  329. icloud_order_total['usdTotal'] = round(
  330. icloud_order_total['usdTotal'] + result['result']['icloudOrderTotal']['usdTotal'], 2)
  331. unicom_order_total['cnyTotal'] = round(
  332. unicom_order_total['cnyTotal'] + result['result']['unicomOrderTotal']['cnyTotal'], 2)
  333. unicom_order_total['usdTotal'] = round(
  334. unicom_order_total['usdTotal'] + result['result']['unicomOrderTotal']['usdTotal'], 2)
  335. order_all_total['cnyTotal'] = round(
  336. order_all_total['cnyTotal'] + result['result']['orderAllTotal']['cnyTotal'], 2)
  337. order_all_total['usdTotal'] = round(
  338. order_all_total['usdTotal'] + result['result']['orderAllTotal']['usdTotal'], 2)
  339. vod_order_all_total['cnyTotal'] = round(
  340. vod_order_all_total['cnyTotal'] + result['result']['vodOrderAllTotal']['cnyTotal'], 2)
  341. vod_order_all_total['usdTotal'] = round(
  342. vod_order_all_total['usdTotal'] + result['result']['vodOrderAllTotal']['usdTotal'], 2)
  343. ai_order_all_total['cnyTotal'] = round(
  344. ai_order_all_total['cnyTotal'] + result['result']['aiOrderAllTotal']['cnyTotal'], 2)
  345. ai_order_all_total['usdTotal'] = round(
  346. ai_order_all_total['usdTotal'] + result['result']['aiOrderAllTotal']['usdTotal'], 2)
  347. icloud_order_all_total['cnyTotal'] = round(
  348. icloud_order_all_total['cnyTotal'] + result['result']['icloudOrderAllTotal']['cnyTotal'], 2)
  349. icloud_order_all_total['usdTotal'] = round(
  350. icloud_order_all_total['usdTotal'] + result['result']['icloudOrderAllTotal']['usdTotal'], 2)
  351. unicom_order_all_total['cnyTotal'] = round(
  352. unicom_order_all_total['cnyTotal'] + result['result']['unicomOrderAllTotal']['cnyTotal'], 2)
  353. unicom_order_all_total['usdTotal'] = round(
  354. unicom_order_all_total['usdTotal'] + result['result']['unicomOrderAllTotal']['usdTotal'], 2)
  355. for item in result['result']['userIncreaseRegion']:
  356. flag = 0
  357. for each in user_increase_temp_list:
  358. if item['countryName'] == each['countryName']:
  359. each['count'] += item['count']
  360. flag = 1
  361. break
  362. if flag == 0:
  363. user_increase_temp_list.append(item)
  364. for item in result['result']['userAllRegion']:
  365. flag = 0
  366. for each in user_all_temp_list:
  367. if item['countryName'] == each['countryName']:
  368. each['count'] += item['count']
  369. flag = 1
  370. break
  371. if flag == 0:
  372. user_all_temp_list.append(item)
  373. else:
  374. return response.json(result['result_code'], result['result'])
  375. if user_increase_temp_list:
  376. for item in user_increase_temp_list:
  377. if user_increase_count:
  378. rate = round(item['count'] / user_increase_count * 100, 2)
  379. else:
  380. rate = 0
  381. if rate >= 10:
  382. item['rate'] = rate
  383. user_increase_list.append(item)
  384. else:
  385. user_increase_other_dict['count'] += item['count']
  386. if user_increase_count:
  387. user_increase_other_dict['rate'] = round(
  388. user_increase_other_dict['count'] / user_increase_count * 100, 2)
  389. if user_increase_other_dict['count']:
  390. user_increase_list.append(user_increase_other_dict)
  391. if user_all_temp_list:
  392. for item in user_all_temp_list:
  393. if user_all_count:
  394. rate = round(item['count'] / user_all_count * 100, 2)
  395. else:
  396. rate = 0
  397. if rate >= 10:
  398. item['rate'] = rate
  399. user_all_list.append(item)
  400. else:
  401. user_all_other_dict['count'] += item['count']
  402. if user_all_count:
  403. user_all_other_dict['rate'] = round(user_all_other_dict['count'] / user_all_count * 100, 2)
  404. if user_all_other_dict['count']:
  405. user_all_list.append(user_all_other_dict)
  406. res = {
  407. 'userIncreaseCount': user_increase_count,
  408. 'userActiveCount': user_active_count,
  409. 'userAllCount': user_all_count,
  410. 'deviceIncreaseCount': device_increase_count,
  411. 'deviceActiveCount': device_active_count,
  412. 'deviceAllCount': device_all_count,
  413. 'orderTotal': order_total,
  414. 'vodOrderTotal': vod_order_total,
  415. 'aiOrderTotal': ai_order_total,
  416. 'icloudOrderTotal': icloud_order_total,
  417. 'unicomOrderTotal': unicom_order_total,
  418. 'orderAllTotal': order_all_total,
  419. 'vodOrderAllTotal': vod_order_all_total,
  420. 'aiOrderAllTotal': ai_order_all_total,
  421. 'icloudOrderAllTotal': icloud_order_all_total,
  422. 'unicomOrderAllTotal': unicom_order_all_total,
  423. 'userIncreaseRegion': user_increase_list,
  424. 'userAllRegion': user_all_list
  425. }
  426. return response.json(0, res)
  427. except Exception as e:
  428. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  429. @classmethod
  430. def query_global_sales_volume_data(cls, request, request_dict, response):
  431. """
  432. 查询全球销售额数据
  433. @param request:请求
  434. @param request_dict:请求参数
  435. @param response:响应对象
  436. @return:
  437. """
  438. url_list = CommonService.get_domain_name()
  439. try:
  440. headers = {
  441. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  442. }
  443. order_list = []
  444. for url in url_list:
  445. url = url + request.path.replace('global/', '')
  446. res = requests.get(url=url, params=request_dict, headers=headers)
  447. result = res.json()
  448. if result['result_code'] == 0:
  449. for item in result['result']:
  450. flag = 0
  451. for each in order_list:
  452. if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
  453. each['cnyTotal'] = round(each['cnyTotal'] + item['cnyTotal'], 2)
  454. each['usdTotal'] = round(each['usdTotal'] + item['usdTotal'], 2)
  455. flag = 1
  456. break
  457. if flag == 0:
  458. order_list.append(item)
  459. else:
  460. return response.json(result['result_code'], result['result'])
  461. return response.json(0, order_list)
  462. except Exception as e:
  463. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  464. @classmethod
  465. def export_data(cls, request_dict, response):
  466. """
  467. 下载文件
  468. @param request_dict:请求参数
  469. @request_dict tableData:表格数据
  470. @request_dict fileName:文件名
  471. @param response:响应对象
  472. @return:
  473. """
  474. table_data = request_dict.get('tableData', None)
  475. sheet_name = request_dict.get('fileName', None)
  476. if not all([table_data, sheet_name]):
  477. return response.json(444, {'error param': 'tableData or fileName'})
  478. table_data = eval(table_data)
  479. file_name = sheet_name + '.xlsx'
  480. try:
  481. res = HttpResponse(content_type='application/octet-stream')
  482. res['Content-Disposition'] = 'attachment; filename={}'.format(escape_uri_path(file_name))
  483. wb = openpyxl.Workbook()
  484. sh = wb.create_sheet(sheet_name, 0)
  485. for row, data in enumerate(table_data):
  486. if row == 0:
  487. sh.append(list(data.keys()))
  488. sh.append(list(data.values()))
  489. wb.save(res)
  490. # with open(file_path, 'rb') as f:
  491. # res = HttpResponse(f)
  492. # res['Content-Type'] = 'application/octet-stream'
  493. # res['Content-Disposition'] = 'attachment;filename="{}"'.format(file_name)
  494. return res
  495. except Exception as e:
  496. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))