ServeManagementController.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import hashlib
  5. import json
  6. import time
  7. import urllib
  8. import uuid
  9. import boto3
  10. import threading
  11. import logging
  12. import xlwt
  13. from boto3.session import Session
  14. from django.http import JsonResponse, HttpResponseRedirect, HttpResponse, StreamingHttpResponse
  15. from django.views.generic.base import View
  16. from Model.models import Device_Info, Role, MenuModel, VodBucketModel, CDKcontextModel, Store_Meal, Order_Model, \
  17. UID_Bucket, ExperienceContextModel, Lang, Device_User, CloudLogModel, UidSetModel, Unused_Uid_Meal, VodHlsModel
  18. from Object.ResponseObject import ResponseObject
  19. from Object.TokenObject import TokenObject
  20. from Object.UidTokenObject import UidTokenObject
  21. from Service.CommonService import CommonService
  22. from django.db.models import F, Sum, Count
  23. class serveManagement(View):
  24. def get(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. operation = kwargs.get('operation')
  27. return self.validation(request.GET, request, operation)
  28. def post(self, request, *args, **kwargs):
  29. request.encoding = 'utf-8'
  30. operation = kwargs.get('operation')
  31. return self.validation(request.POST, request, operation)
  32. def validation(self, request_dict, request, operation):
  33. language = request_dict.get('language', 'en')
  34. response = ResponseObject(language, 'pc')
  35. if operation == 'exportCloudUserList': # 导出云存用户信息
  36. return self.exportCloudUserList(request_dict, response)
  37. elif operation == 'getCloudDataList':
  38. return self.getCloudDataList(request_dict, response)
  39. else:
  40. tko = TokenObject(
  41. request.META.get('HTTP_AUTHORIZATION'),
  42. returntpye='pc')
  43. if tko.code != 0:
  44. return response.json(tko.code)
  45. response.lang = tko.lang
  46. userID = tko.userID
  47. if operation == 'getVodBucketList':
  48. return self.getVodBucketList(userID, request_dict, response)
  49. elif operation == 'addOrEditVodBucket':
  50. return self.addOrEditVodBucket(userID, request_dict, response)
  51. elif operation == 'deleteVodBucket':
  52. return self.deleteVodBucket(userID, request_dict, response)
  53. elif operation == 'getStoreMealList':
  54. return self.getStoreMealList(userID, request_dict, response)
  55. elif operation == 'addOrEditStoreMeal':
  56. return self.addOrEditStoreMeal(userID, request_dict, response)
  57. elif operation == 'deleteStoreMeal':
  58. return self.deleteStoreMeal(userID, request_dict, response)
  59. elif operation == 'getStoreMealLanguage':
  60. return self.getStoreMealLanguage(
  61. userID, request_dict, response)
  62. elif operation == 'addOrEditStoreMealLanguage':
  63. return self.addOrEditStoreMealLanguage(
  64. userID, request_dict, response)
  65. elif operation == 'deleteStoreMealLanguage':
  66. return self.deleteStoreMealLanguage(
  67. userID, request_dict, response)
  68. elif operation == 'getCdkList':
  69. return self.getCdkList(userID, request_dict, response)
  70. elif operation == 'createCdk':
  71. return self.createCdk(request_dict, response)
  72. elif operation == 'deleteCdk':
  73. return self.deleteCdk(request_dict, response)
  74. elif operation == 'downloadCDK':
  75. return self.downloadCDK(request_dict, response)
  76. elif operation == 'getDeviceOrderList':
  77. return self.getDeviceOrderList(request_dict, response)
  78. elif operation == 'deleteDeviceOrder':
  79. return self.deleteDeviceOrder(userID, request_dict, response)
  80. elif operation == 'getDevicePackageList': # 云存设备套餐
  81. return self.getDevicePackageList(request_dict, response)
  82. elif operation == 'deleteDevicePackage':
  83. return self.deleteDevicePackage(userID, request_dict, response)
  84. elif operation == 'experiencereset': # 重置设备云存体验
  85. return self.do_experience_reset(request_dict, userID, response)
  86. elif operation == 'getCloudUserList': # 获取云存用户信息
  87. return self.getCloudUserList(request_dict, response)
  88. elif operation == 'deviceAttritionAlert': # 流失预警
  89. return self.deviceAttritionAlert(request_dict, response)
  90. else:
  91. return response.json(404)
  92. def getVodBucketList(self, userID, request_dict, response):
  93. # 查询存储桶数据
  94. print('request_dict: ', request_dict)
  95. isSelect = request_dict.get('isSelect', None)
  96. if isSelect:
  97. # 获取全部数据作为存储桶选项
  98. vod_bucket_qs = VodBucketModel.objects.all().values('id', 'bucket')
  99. return response.json(
  100. 0, {'list': CommonService.qs_to_list(vod_bucket_qs)})
  101. bucket = request_dict.get('bucket', None)
  102. mold = request_dict.get('mold', None)
  103. is_free = request_dict.get('is_free', None)
  104. pageNo = request_dict.get('pageNo', None)
  105. pageSize = request_dict.get('pageSize', None)
  106. if not all([pageNo, pageSize]):
  107. return response.json(444)
  108. page = int(pageNo)
  109. line = int(pageSize)
  110. try:
  111. if bucket or mold or is_free: # 条件查询
  112. if bucket:
  113. vod_bucket_qs = VodBucketModel.objects.filter(
  114. bucket=bucket)
  115. elif mold:
  116. vod_bucket_qs = VodBucketModel.objects.filter(
  117. mold=int(mold))
  118. elif is_free:
  119. vod_bucket_qs = VodBucketModel.objects.filter(
  120. is_free=int(is_free))
  121. else: # 查询全部
  122. vod_bucket_qs = VodBucketModel.objects.filter().all()
  123. total = len(vod_bucket_qs)
  124. vod_buckets = vod_bucket_qs[(page - 1) * line:page * line]
  125. vod_bucket_list = []
  126. for vod_bucket in vod_buckets:
  127. vod_bucket_list.append({
  128. 'bucketID': vod_bucket.id,
  129. 'bucket': vod_bucket.bucket,
  130. 'content': vod_bucket.content,
  131. 'mold': vod_bucket.mold,
  132. 'area': vod_bucket.area,
  133. 'region': vod_bucket.region,
  134. 'endpoint': vod_bucket.endpoint,
  135. 'is_free': vod_bucket.is_free,
  136. 'storeDay': vod_bucket.storeDay,
  137. 'region_id': vod_bucket.region_id,
  138. 'addTime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(vod_bucket.addTime)),
  139. 'updTime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(vod_bucket.updTime)),
  140. })
  141. print('vod_bucket_list: ', vod_bucket_list)
  142. return response.json(
  143. 0, {'list': vod_bucket_list, 'total': total})
  144. except Exception as e:
  145. print(e)
  146. return response.json(500, repr(e))
  147. def addOrEditVodBucket(self, userID, request_dict, response):
  148. # 添加/编辑存储桶
  149. print('request_dict: ', request_dict)
  150. bucketID = request_dict.get('bucketID', None)
  151. bucket = request_dict.get('bucket', '').strip() # 移除字符串头尾的空格
  152. content = request_dict.get('content', '').strip()
  153. mold = int(request_dict.get('mold', 1))
  154. area = request_dict.get('area', '').strip()
  155. region = request_dict.get('region', '').strip()
  156. endpoint = request_dict.get('endpoint', '').strip()
  157. is_free = int(request_dict.get('is_free', 0))
  158. storeDay = int(request_dict.get('storeDay', 0))
  159. region_id = int(request_dict.get('region_id', 1))
  160. isEdit = request_dict.get('isEdit', None)
  161. if not all([bucket, content, area, region, endpoint]):
  162. return response.json(444)
  163. try:
  164. now_time = int(time.time())
  165. vod_bucket_data = {
  166. 'bucket': bucket,
  167. 'content': content,
  168. 'mold': mold,
  169. 'area': area,
  170. 'region': region,
  171. 'endpoint': endpoint,
  172. 'is_free': is_free,
  173. 'storeDay': storeDay,
  174. 'region_id': region_id,
  175. }
  176. if isEdit:
  177. if not bucketID:
  178. return response.json(444)
  179. vod_bucket_data['updTime'] = now_time
  180. VodBucketModel.objects.filter(
  181. id=bucketID).update(
  182. **vod_bucket_data)
  183. else:
  184. vod_bucket_data['addTime'] = now_time
  185. VodBucketModel.objects.create(**vod_bucket_data)
  186. return response.json(0)
  187. except Exception as e:
  188. print(e)
  189. return response.json(500, repr(e))
  190. def deleteVodBucket(self, userID, request_dict, response):
  191. # 删除存储桶
  192. print('request_dict: ', request_dict)
  193. bucketID = request_dict.get('bucketID', None)
  194. if not bucketID:
  195. return response.json(444)
  196. try:
  197. VodBucketModel.objects.filter(id=bucketID).delete()
  198. return response.json(0)
  199. except Exception as e:
  200. print(e)
  201. return response.json(500, repr(e))
  202. def getStoreMealList(self, userID, request_dict, response):
  203. # 获取云存套餐信息数据
  204. print('request_dict: ', request_dict)
  205. isSelect = request_dict.get('isSelect', None)
  206. if isSelect:
  207. # 获取套餐ID作为选项
  208. store_meal_qs = Store_Meal.objects.all().values('id', 'bucket__bucket')
  209. return response.json(
  210. 0, {'list': CommonService.qs_to_list(store_meal_qs)})
  211. bucket = request_dict.get('bucket', None)
  212. pageNo = request_dict.get('pageNo', None)
  213. pageSize = request_dict.get('pageSize', None)
  214. if not all([pageNo, pageSize]):
  215. return response.json(444)
  216. page = int(pageNo)
  217. line = int(pageSize)
  218. try:
  219. if bucket: # 条件查询
  220. bucket_id = VodBucketModel.objects.filter(
  221. bucket=bucket).values('id')[0]['id']
  222. store_meal_qs = Store_Meal.objects.filter(
  223. bucket_id=bucket_id)
  224. else: # 查询全部
  225. store_meal_qs = Store_Meal.objects.filter()
  226. store_meal_val = store_meal_qs.values(
  227. 'id',
  228. 'bucket__bucket',
  229. 'day',
  230. 'expire',
  231. 'commodity_type',
  232. 'commodity_code',
  233. 'is_discounts',
  234. 'discount_price',
  235. 'virtual_price',
  236. 'price',
  237. 'currency',
  238. 'symbol',
  239. 'is_show',
  240. 'add_time',
  241. 'update_time')
  242. total = len(store_meal_val)
  243. store_meals = store_meal_val[(page - 1) * line:page * line]
  244. store_meal_list = []
  245. for store_meal in store_meals:
  246. # 获取支付方式列表
  247. pay_type_list = [
  248. pay_type['id'] for pay_type in Store_Meal.objects.get(
  249. id=store_meal['id']).pay_type.values('id')]
  250. # 组织响应数据
  251. store_meal_list.append({
  252. 'storeMealID': store_meal['id'],
  253. 'bucket': store_meal['bucket__bucket'],
  254. 'day': store_meal['day'],
  255. 'expire': store_meal['expire'],
  256. 'commodity_type': store_meal['commodity_type'],
  257. 'pay_type': pay_type_list,
  258. 'commodity_code': store_meal['commodity_code'],
  259. 'is_discounts': store_meal['is_discounts'],
  260. 'discount_price': store_meal['discount_price'],
  261. 'virtual_price': store_meal['virtual_price'],
  262. 'price': store_meal['price'],
  263. 'currency': store_meal['currency'],
  264. 'symbol': store_meal['symbol'],
  265. 'is_show': store_meal['is_show'],
  266. 'addTime': store_meal['add_time'].strftime("%Y-%m-%d %H:%M:%S"),
  267. 'updTime': store_meal['update_time'].strftime("%Y-%m-%d %H:%M:%S"),
  268. })
  269. print('store_meal_list: ', store_meal_list)
  270. return response.json(
  271. 0, {'list': store_meal_list, 'total': total})
  272. except Exception as e:
  273. print(e)
  274. return response.json(500, repr(e))
  275. def addOrEditStoreMeal(self, userID, request_dict, response):
  276. # 添加/编辑套餐
  277. print('request_dict: ', request_dict)
  278. storeMealID = request_dict.get('storeMealID', None)
  279. bucket = request_dict.get('bucket', '')
  280. day = int(request_dict.get('day', 0))
  281. expire = int(request_dict.get('expire', 0))
  282. commodity_type = int(request_dict.get('commodity_type', 0))
  283. pay_type = request_dict.get(
  284. 'pay_type', '')[
  285. 1:-1].split(',') # '[1,2]' -> ['1','2']
  286. commodity_code = request_dict.get('commodity_code', '')
  287. is_discounts = int(request_dict.get('is_discounts', 0))
  288. discount_price = request_dict.get('discount_price', '')
  289. virtual_price = request_dict.get('virtual_price', '')
  290. price = request_dict.get('price', '')
  291. currency = request_dict.get('currency', '')
  292. symbol = request_dict.get('symbol', '')
  293. is_show = int(request_dict.get('is_show', 0))
  294. isEdit = request_dict.get('isEdit', None)
  295. if not all([bucket, pay_type, price, currency, symbol]):
  296. return response.json(444)
  297. try:
  298. bucket_id = VodBucketModel.objects.filter(
  299. bucket=bucket).values('id')[0]['id']
  300. store_meal_data = {
  301. 'bucket_id': bucket_id,
  302. 'day': day,
  303. 'expire': expire,
  304. 'commodity_type': commodity_type,
  305. 'commodity_code': commodity_code,
  306. 'is_discounts': is_discounts,
  307. 'discount_price': discount_price,
  308. 'virtual_price': virtual_price,
  309. 'price': price,
  310. 'currency': currency,
  311. 'symbol': symbol,
  312. 'is_show': is_show,
  313. }
  314. if isEdit:
  315. if not storeMealID:
  316. return response.json(444)
  317. Store_Meal.objects.filter(
  318. id=storeMealID).update(
  319. **store_meal_data)
  320. Store_Meal.objects.get(id=storeMealID).pay_type.set(pay_type)
  321. else:
  322. Store_Meal.objects.create(
  323. **store_meal_data).pay_type.set(pay_type)
  324. return response.json(0)
  325. except Exception as e:
  326. print(e)
  327. return response.json(500, repr(e))
  328. def deleteStoreMeal(self, userID, request_dict, response):
  329. # 删除套餐信息
  330. print('request_dict: ', request_dict)
  331. storeMealID = request_dict.get('storeMealID', None)
  332. if not storeMealID:
  333. return response.json(444)
  334. try:
  335. Store_Meal.objects.filter(id=storeMealID).delete()
  336. return response.json(0)
  337. except Exception as e:
  338. print(e)
  339. return response.json(500, repr(e))
  340. def getStoreMealLanguage(self, userID, request_dict, response):
  341. # 获取套餐语言
  342. print('request_dict: ', request_dict)
  343. storeMealID = request_dict.get('storeMealID', None)
  344. pageNo = request_dict.get('pageNo', None)
  345. pageSize = request_dict.get('pageSize', None)
  346. if not all([pageNo, pageSize]):
  347. return response.json(444)
  348. page = int(pageNo)
  349. line = int(pageSize)
  350. try:
  351. if storeMealID: # 条件查询
  352. store_meal_lang_qs = Store_Meal.objects.filter(id=storeMealID)
  353. else: # 查询全部
  354. store_meal_lang_qs = Store_Meal.objects.filter(
  355. lang__isnull=False)
  356. store_meal_lang_val = store_meal_lang_qs.values(
  357. 'id',
  358. 'lang__id',
  359. 'lang__lang',
  360. 'lang__title',
  361. 'lang__content',
  362. 'lang__discount_content',
  363. )
  364. total = len(store_meal_lang_val)
  365. store_meal_langs = store_meal_lang_val[(
  366. page - 1) * line:page * line]
  367. store_meal_lang_list = []
  368. for store_meal_lang in store_meal_langs:
  369. store_meal_lang_list.append({
  370. 'storeMealID': store_meal_lang['id'],
  371. 'langID': store_meal_lang['lang__id'],
  372. 'lang': store_meal_lang['lang__lang'],
  373. 'title': store_meal_lang['lang__title'],
  374. 'content': store_meal_lang['lang__content'],
  375. 'discountContent': store_meal_lang['lang__discount_content'],
  376. })
  377. print('store_meal_lang_list: ', store_meal_lang_list)
  378. return response.json(
  379. 0, {'list': store_meal_lang_list, 'total': total})
  380. except Exception as e:
  381. print(e)
  382. return response.json(500, repr(e))
  383. def addOrEditStoreMealLanguage(self, userID, request_dict, response):
  384. # 添加/编辑套餐语言
  385. print('request_dict: ', request_dict)
  386. storeMealID = request_dict.get('storeMealID', None)
  387. lang = request_dict.get('lang', None)
  388. title = request_dict.get('title', None)
  389. content = request_dict.get('content', None)
  390. discount_content = request_dict.get('discount_content', '')
  391. isEdit = request_dict.get('isEdit', None)
  392. if not all([storeMealID, lang, title, content]):
  393. return response.json(444)
  394. try:
  395. # 查询套餐是否存在
  396. store_meal_qs = Store_Meal.objects.get(id=storeMealID)
  397. if not store_meal_qs:
  398. return response.json(173)
  399. if isEdit: # 编辑
  400. langID = request_dict.get('langID', None)
  401. if not langID:
  402. return response.json(444)
  403. Lang.objects.filter(
  404. id=langID).update(
  405. lang=lang,
  406. title=title,
  407. content=content,
  408. discount_content=discount_content)
  409. else: # 添加
  410. lang_obj = Lang.objects.filter(
  411. lang=lang,
  412. title=title,
  413. content=content,
  414. discount_content=discount_content)
  415. if not lang_obj.exists():
  416. # 数据不存在,lang表创建数据
  417. Lang.objects.create(
  418. lang=lang,
  419. title=title,
  420. content=content,
  421. discount_content=discount_content)
  422. lang_obj = Lang.objects.filter(
  423. lang=lang,
  424. title=title,
  425. content=content,
  426. discount_content=discount_content)
  427. store_meal_qs.lang.add(*lang_obj) # store_meal表添加语言数据
  428. return response.json(0)
  429. except Exception as e:
  430. print(e)
  431. return response.json(500, repr(e))
  432. def deleteStoreMealLanguage(self, userID, request_dict, response):
  433. # 删除套餐语言
  434. storeMealID = request_dict.get('storeMealID', None)
  435. langID = request_dict.get('langID', None)
  436. if not all([storeMealID, langID]):
  437. return response.json(444)
  438. try:
  439. storeMeal_qs = Store_Meal.objects.get(id=storeMealID)
  440. if not storeMeal_qs:
  441. return response.json(173)
  442. lang_qs = Lang.objects.filter(id=langID)
  443. storeMeal_qs.lang.remove(*lang_qs)
  444. return response.json(0)
  445. except Exception as e:
  446. print(e)
  447. return response.json(500, repr(e))
  448. def getCdkList(self, userID, request_dict, response):
  449. # 获取激活码列表
  450. pageNo = request_dict.get('pageNo', None)
  451. pageSize = request_dict.get('pageSize', None)
  452. cdk = request_dict.get('cdk', None)
  453. order = request_dict.get('order', None)
  454. is_activate = request_dict.get('is_activate', None)
  455. mold = request_dict.get('mold', None)
  456. lang = request_dict.get('lang', 'cn')
  457. if not all([pageNo, pageSize]):
  458. return response.json(444)
  459. page = int(pageNo)
  460. line = int(pageSize)
  461. try:
  462. if cdk:
  463. searchVal = cdk.strip()
  464. if order:
  465. searchVal = order.strip()
  466. if is_activate:
  467. searchVal = is_activate.strip()
  468. cdk_qs = CDKcontextModel.objects.filter().all()
  469. if cdk:
  470. cdk_qs = cdk_qs.filter(cdk__contains=searchVal)
  471. if order:
  472. cdk_qs = cdk_qs.filter(order__contains=searchVal)
  473. if is_activate:
  474. cdk_qs = cdk_qs.filter(is_activate=searchVal)
  475. if mold:
  476. cdk_qs = cdk_qs.filter(rank__bucket__mold=mold)
  477. cdk_qs = cdk_qs.filter(rank__lang__lang=lang)
  478. cdk_qs = cdk_qs.annotate(rank__title=F('rank__lang__title'))
  479. cdk_qs = cdk_qs.values(
  480. 'id',
  481. 'cdk',
  482. 'create_time',
  483. 'valid_time',
  484. 'is_activate',
  485. 'is_down',
  486. 'rank__id',
  487. 'rank__title',
  488. 'order',
  489. 'create_time',
  490. 'rank__bucket__mold')
  491. cdk_qs = cdk_qs.order_by('-create_time') # 根据CDK创建时间降序排序
  492. count = cdk_qs.count()
  493. cdk_qs = cdk_qs[(page - 1) * line:page * line]
  494. return response.json(
  495. 0, {'list': list(cdk_qs), 'total': count})
  496. except Exception as e:
  497. print(e)
  498. return response.json(500, repr(e))
  499. def createCdk(self, request_dict, response):
  500. cdk_num = request_dict.get("cdknum", None)
  501. mold = request_dict.get('mold', None)
  502. order = request_dict.get('order', None)
  503. cdk_list = []
  504. sm_qs = Store_Meal.objects.filter(
  505. pay_type__payment='cdk_pay', bucket__mold=mold, is_show=0)
  506. if not sm_qs.exists():
  507. return response.json(173)
  508. rank = sm_qs[0].id
  509. for i in range(int(cdk_num)):
  510. nowTime = int(time.time())
  511. cdk = hashlib.md5((str(uuid.uuid1()) +
  512. str(nowTime)).encode('utf-8')).hexdigest()
  513. cdk_model = CDKcontextModel(
  514. cdk=cdk,
  515. create_time=nowTime,
  516. valid_time=0,
  517. is_activate=0,
  518. is_down=0,
  519. rank_id=rank,
  520. order=order,
  521. )
  522. cdk_list.append(cdk_model)
  523. try:
  524. CDKcontextModel.objects.bulk_create(cdk_list)
  525. except Exception as e:
  526. return response.json(404, repr(e))
  527. else:
  528. return response.json(0)
  529. def deleteCdk(self, request_dict, response):
  530. cdk_id = request_dict.get("id", None)
  531. try:
  532. CDKcontextModel.objects.get(id=cdk_id).delete()
  533. return response.json(0)
  534. except Exception as e:
  535. return response.json(500, repr(e))
  536. def downloadCDK(self, request_dict, response):
  537. region = request_dict.get('region', None)
  538. content = ''
  539. if region == 'cn':
  540. # 下载国内未使用激活码
  541. content += '激活码(国内)\n'
  542. cdk_inactivate_qs = CDKcontextModel.objects.filter(is_down=0, is_activate=0, rank__bucket__mold=0, rank__is_show=0).values('cdk')
  543. else:
  544. # 下载国外未使用激活码
  545. content += '激活码(国外)\n'
  546. cdk_inactivate_qs = CDKcontextModel.objects.filter(is_down=0, is_activate=0, rank__bucket__mold=1, rank__is_show=0).values('cdk')
  547. for cdk_inactivate in cdk_inactivate_qs:
  548. content += cdk_inactivate['cdk'] + '\n'
  549. # print(content)
  550. cdk_inactivate_qs.update(is_down=1)
  551. response = StreamingHttpResponse(content)
  552. response['Content-Type'] = 'application/octet-stream'
  553. response['Content-Disposition'] = 'attachment;filename="CDK.txt"'
  554. return response
  555. def getDeviceOrderList(self, request_dict, response):
  556. print('request_dict: ', request_dict)
  557. pageNo = request_dict.get('pageNo', None)
  558. pageSize = request_dict.get('pageSize', None)
  559. uid = request_dict.get('uid', None)
  560. channel = request_dict.get('channel', None)
  561. orderID = request_dict.get('orderID', None)
  562. userID__username = request_dict.get('userID__username', None)
  563. currency = request_dict.get('currency', None)
  564. payType = request_dict.get('payType', None)
  565. status = request_dict.get('status', None)
  566. timeRange = request_dict.getlist('timeRange[]', None)
  567. orderType = request_dict.get('orderType', None)
  568. if not all([pageNo, pageSize]):
  569. return response.json(444)
  570. page = int(pageNo)
  571. line = int(pageSize)
  572. try:
  573. omqs = Order_Model.objects.all()
  574. # 筛选指定设备id的订单
  575. if uid:
  576. omqs = omqs.filter(UID=uid)
  577. if channel:
  578. omqs = omqs.filter(channel=channel)
  579. if orderID:
  580. omqs = omqs.filter(orderID=orderID)
  581. if userID__username:
  582. omqs = omqs.filter(userID__username=userID__username)
  583. if currency:
  584. omqs = omqs.filter(currency=currency)
  585. if payType:
  586. omqs = omqs.filter(payType=payType)
  587. if status:
  588. omqs = omqs.filter(status=status)
  589. if orderType:
  590. omqs = omqs.filter(order_type=int(orderType))
  591. if timeRange:
  592. startTime, endTime = int(
  593. timeRange[0][:-3]), int(timeRange[1][:-3])
  594. omqs = omqs.filter(
  595. addTime__gte=startTime,
  596. addTime__lte=endTime)
  597. if not omqs.exists():
  598. return response.json(0, [])
  599. count = omqs.count()
  600. order_ql = omqs.values(
  601. "orderID",
  602. "UID",
  603. "userID__username",
  604. "userID__NickName",
  605. "channel",
  606. "desc",
  607. "price",
  608. "refunded_amount",
  609. "currency",
  610. "addTime",
  611. "updTime",
  612. "paypal",
  613. "payType",
  614. "rank__day",
  615. "rank__price",
  616. "status",
  617. "order_type")
  618. order_ql = order_ql.order_by('-addTime') # 根据CDK创建时间降序排序
  619. order_ql = order_ql[(page - 1) * line:page * line]
  620. return response.json(
  621. 0, {'list': list(order_ql), 'total': count})
  622. except Exception as e:
  623. print(e)
  624. return response.json(500, repr(e))
  625. def deleteDeviceOrder(self, userID, request_dict, response):
  626. orderID = request_dict.get('orderID', None)
  627. if orderID:
  628. Order_Model.objects.filter(orderID=orderID).delete()
  629. return response.json(0)
  630. else:
  631. return response.json(444)
  632. def getDevicePackageList(self, request_dict, response):
  633. pageNo = request_dict.get('pageNo', None)
  634. pageSize = request_dict.get('pageSize', None)
  635. uid = request_dict.get('uid', None)
  636. if not all([pageNo, pageSize]):
  637. return response.json(444)
  638. page = int(pageNo)
  639. line = int(pageSize)
  640. try:
  641. ubqs = UID_Bucket.objects.all()
  642. if uid:
  643. ubqs = ubqs.filter(uid__contains=uid)
  644. if not ubqs.exists():
  645. return response.json(0, [])
  646. count = ubqs.count()
  647. ubqs = ubqs.values(
  648. 'id',
  649. 'uid',
  650. 'channel',
  651. 'status',
  652. 'endTime',
  653. 'bucket__bucket',
  654. 'bucket__storeDay',
  655. 'bucket__area')
  656. ubqs = ubqs.order_by('-addTime') # 根据CDK创建时间降序排序
  657. ubqs = ubqs[(page - 1) * line:page * line]
  658. return response.json(
  659. 0, {'list': list(ubqs), 'total': count})
  660. except Exception as e:
  661. print(e)
  662. return response.json(500, repr(e))
  663. def deleteDevicePackage(self, userID, request_dict, response):
  664. orderID = request_dict.get('orderID', None)
  665. if orderID:
  666. Order_Model.objects.filter(orderID=orderID).delete()
  667. return response.json(0)
  668. else:
  669. return response.json(444)
  670. # 重置设备云存体验
  671. def do_experience_reset(self, request_dict, userID, response):
  672. bid = request_dict.get("id", None)
  673. ubq = UID_Bucket.objects.filter(id=bid)
  674. if ubq:
  675. eq = ExperienceContextModel.objects.filter(uid=ubq[0].uid)
  676. if eq:
  677. eq.delete()
  678. Order_Model.objects.filter(uid_bucket_id=bid).delete()
  679. ubq.delete()
  680. return response.json(0)
  681. else:
  682. return response.json(10007)
  683. else:
  684. return response.json(0, '重置云存体验失败')
  685. @classmethod
  686. def getCloudUserList(cls, request_dict, response):
  687. print('request_dict: ', request_dict)
  688. # UID_Bucket表查询数据
  689. uid = request_dict.get('uid', None)
  690. status = request_dict.get('status', None)
  691. use_status = request_dict.get('use_status', None)
  692. has_unused = request_dict.get('has_unused', None)
  693. addTimeRange = request_dict.getlist('addTimeRange[]', None)
  694. endTimeRange = request_dict.getlist('endTimeRange[]', None)
  695. # Order_Model表查询数据
  696. username = request_dict.get('username', None)
  697. phone = request_dict.get('phone', None)
  698. userEmail = request_dict.get('userEmail', None)
  699. payType = request_dict.get('payType', None)
  700. # uid_set 表查询
  701. ucode = request_dict.getlist('ucode', None)
  702. version = request_dict.getlist('version', None)
  703. # 日志表查询
  704. logTimeRange = request_dict.getlist('logTimeRange[]', None)
  705. pageNo = request_dict.get('pageNo', None)
  706. pageSize = request_dict.get('pageSize', None)
  707. if not all([pageNo, pageSize]):
  708. return response.json(444)
  709. page = int(pageNo)
  710. line = int(pageSize)
  711. try:
  712. uid_bucket_qs = UID_Bucket.objects.all()
  713. if uid:
  714. uid_bucket_qs = uid_bucket_qs.filter(uid__icontains=uid)
  715. if status:
  716. uid_bucket_qs = uid_bucket_qs.filter(status=status)
  717. if use_status:
  718. uid_bucket_qs = uid_bucket_qs.filter(use_status=use_status)
  719. if has_unused:
  720. uid_bucket_qs = uid_bucket_qs.filter(has_unused=has_unused)
  721. if addTimeRange:
  722. addStartTime, addEndTime = int(
  723. addTimeRange[0][:-3]), int(addTimeRange[1][:-3])
  724. uid_bucket_qs = uid_bucket_qs.filter(
  725. addTime__gte=addStartTime,
  726. addTime__lte=addEndTime)
  727. if endTimeRange:
  728. endStartTime, endEndTime = int(
  729. endTimeRange[0][:-3]), int(endTimeRange[1][:-3])
  730. uid_bucket_qs = uid_bucket_qs.filter(
  731. addTime__gte=endStartTime,
  732. addTime__lte=endEndTime)
  733. uid_list = []
  734. uid_set_dict = {}
  735. if ucode and ucode != ['']:
  736. uid_set_qs = UidSetModel.objects.filter(ucode__in=ucode).values('uid', 'ucode', 'version').distinct()
  737. for uid_set in uid_set_qs:
  738. uid_list.append(uid_set['uid'])
  739. uid_set_dict[uid_set['uid']] = {
  740. 'ucode': uid_set['ucode'],
  741. 'version': uid_set['version']
  742. }
  743. uid_bucket_qs = uid_bucket_qs.filter(uid__in=uid_list)
  744. else:
  745. uid_set_qs = UidSetModel.objects.filter().values('uid', 'ucode', 'version').distinct()
  746. for uid_set in uid_set_qs:
  747. uid_list.append(uid_set['uid'])
  748. uid_set_dict[uid_set['uid']] = {
  749. 'ucode': uid_set['ucode'],
  750. 'version': uid_set['version']
  751. }
  752. if not uid_bucket_qs.exists():
  753. return response.json(0, [])
  754. order_qs = Order_Model.objects.filter(uid_bucket_id__in=uid_bucket_qs.values('id'))
  755. if username or phone or userEmail or payType:
  756. if username:
  757. order_qs = order_qs.filter(userID__username=username)
  758. if phone:
  759. order_qs = order_qs.filter(userID__phone__contains=phone)
  760. if userEmail:
  761. order_qs = order_qs.filter(
  762. userID__userEmail__contains=userEmail)
  763. if payType:
  764. order_qs = order_qs.filter(payType=int(payType))
  765. # 过滤套餐关联的UID_Bucket数据
  766. uid_bucket_qs = uid_bucket_qs.filter(
  767. id__in=order_qs.values_list(
  768. 'uid_bucket_id', flat=True))
  769. cg_qs = CloudLogModel.objects.filter(
  770. operation='cloudstorage/queryvodlist')
  771. if logTimeRange:
  772. logStartTime, logEndTime = int(
  773. logTimeRange[0][:-3]), int(logTimeRange[1][:-3])
  774. cg_qs = cg_qs.filter(
  775. time__gte=logStartTime,
  776. time__lte=logEndTime)
  777. # 过滤套餐关联的UID_Bucket数据
  778. uid_bucket_qs = uid_bucket_qs.filter(
  779. uid__in=cg_qs.values('uid'))
  780. list_data = []
  781. count = uid_bucket_qs.count()
  782. uid_bucket_qs = uid_bucket_qs.order_by('-addTime')[(page - 1) * line:page * line]
  783. for uid_bucket in uid_bucket_qs:
  784. for order in order_qs.filter(
  785. uid_bucket_id=uid_bucket.id).values(
  786. 'uid_bucket_id',
  787. 'desc',
  788. 'userID__userID',
  789. 'UID',
  790. 'price',
  791. 'payType',
  792. 'userID__username',
  793. 'userID__phone',
  794. 'userID__userEmail',
  795. 'userID__data_joined'):
  796. # 套餐到期时间累加未使用套餐
  797. unused_qs = Unused_Uid_Meal.objects.filter(uid=uid_bucket.uid).values('num', 'expire')
  798. if unused_qs.exists():
  799. addMonth = 0
  800. for unused in unused_qs:
  801. addMonth += unused['num'] * unused['expire']
  802. endTime = CommonService.calcMonthLater(addMonth, uid_bucket.endTime)
  803. endTime = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(endTime))
  804. else:
  805. endTime = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(uid_bucket.endTime))
  806. nowTime = int(time.time())
  807. Time = time.strptime(endTime, "%Y--%m--%d %H:%M:%S")
  808. Time = time.mktime(Time)
  809. nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nowTime))
  810. if nowTime < endTime:
  811. Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(Time))
  812. nowTime = datetime.datetime.strptime(nowTime, '%Y-%m-%d %H:%M:%S')
  813. Time = datetime.datetime.strptime(Time, '%Y-%m-%d %H:%M:%S')
  814. expirationDate = (Time - nowTime).days
  815. else:
  816. expirationDate = 0
  817. uid = uid_bucket.uid.upper()
  818. data = {
  819. 'id': uid_bucket.id,
  820. 'uid': uid,
  821. 'channel': uid_bucket.channel,
  822. 'status': uid_bucket.status,
  823. 'endTime': endTime,
  824. 'ExpirationDate': expirationDate,
  825. 'addTime': time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(uid_bucket.addTime)),
  826. 'use_status': uid_bucket.use_status,
  827. 'has_unused': uid_bucket.has_unused,
  828. 'desc': order['desc'],
  829. 'payType': order['payType'],
  830. 'price': order['price'],
  831. 'username': order['userID__username'],
  832. 'phone': order['userID__phone'],
  833. 'userEmail': order['userID__userEmail'],
  834. 'data_joined': order['userID__data_joined'].strftime("%Y-%m-%d %H:%M:%S"),
  835. 'playcount': cg_qs.filter(operation='cloudstorage/queryvodlist', uid=order['UID']).count()
  836. }
  837. if uid in uid_set_dict:
  838. data['ucode'] = uid_set_dict[uid]['ucode']
  839. data['version'] = uid_set_dict[uid]['version']
  840. list_data.append(data)
  841. return response.json(
  842. 0, {'list': list_data, 'total': count})
  843. except Exception as e:
  844. print(e)
  845. return response.json(500, repr(e))
  846. def exportCloudUserList(self, request_dict, response):
  847. # UID_Bucket表查询数据
  848. uid = request_dict.get('uid', None)
  849. status = request_dict.get('status', None)
  850. use_status = request_dict.get('use_status', None)
  851. has_unused = request_dict.get('has_unused', None)
  852. addTimeRange = request_dict.getlist('addTimeRange[]', None)
  853. endTimeRange = request_dict.getlist('endTimeRange[]', None)
  854. # Order_Model表查询数据
  855. username = request_dict.get('username', None)
  856. phone = request_dict.get('phone', None)
  857. userEmail = request_dict.get('userEmail', None)
  858. payType = request_dict.get('payType', None)
  859. #uid_set 表查询
  860. ucode = request_dict.getlist('ucode', None)
  861. version = request_dict.getlist('version', None)
  862. # 日志表查询
  863. logTimeRange = request_dict.getlist('logTimeRange[]', None)
  864. pageNo = request_dict.get('pageNo', None)
  865. pageSize = request_dict.get('pageSize', None)
  866. if not all([pageNo, pageSize]):
  867. return response.json(444)
  868. page = int(pageNo)
  869. line = int(pageSize)
  870. try:
  871. uid_bucket_qs = UID_Bucket.objects.all()
  872. if uid:
  873. uid_bucket_qs = uid_bucket_qs.filter(uid__contains=uid)
  874. if status:
  875. uid_bucket_qs = uid_bucket_qs.filter(status=status)
  876. if use_status:
  877. uid_bucket_qs = uid_bucket_qs.filter(use_status=use_status)
  878. if has_unused:
  879. uid_bucket_qs = uid_bucket_qs.filter(has_unused=has_unused)
  880. if addTimeRange:
  881. addStartTime, addEndTime = int(
  882. addTimeRange[0][:-3]), int(addTimeRange[1][:-3])
  883. uid_bucket_qs = uid_bucket_qs.filter(
  884. addTime__gte=addStartTime,
  885. addTime__lte=addEndTime)
  886. if endTimeRange:
  887. endStartTime, endEndTime = int(
  888. endTimeRange[0][:-3]), int(endTimeRange[1][:-3])
  889. uid_bucket_qs = uid_bucket_qs.filter(
  890. addTime__gte=endStartTime,
  891. addTime__lte=endEndTime)
  892. if not uid_bucket_qs.exists():
  893. return response.json(0, [])
  894. order_qs = Order_Model.objects.filter(
  895. uid_bucket_id__in=uid_bucket_qs.values('id'))
  896. if username or phone or userEmail or payType:
  897. if username:
  898. order_qs = order_qs.filter(userID__username=username)
  899. if phone:
  900. order_qs = order_qs.filter(userID__phone__contains=phone)
  901. if userEmail:
  902. order_qs = order_qs.filter(
  903. userID__userEmail__contains=userEmail)
  904. if payType:
  905. order_qs = order_qs.filter(payType=int(payType))
  906. # 过滤套餐关联的UID_Bucket数据
  907. uid_bucket_qs = uid_bucket_qs.filter(
  908. id__in=order_qs.values_list(
  909. 'uid_bucket_id', flat=True))
  910. uidset_qs = UidSetModel.objects.filter(
  911. uid__in=uid_bucket_qs.values('uid'))
  912. if ucode or version:
  913. if ucode:
  914. uidset_qs = uidset_qs.filter(ucode=ucode)
  915. if version:
  916. uidset_qs = uidset_qs.filter(version=version)
  917. cg_qs = CloudLogModel.objects.filter(
  918. operation='cloudstorage/queryvodlist')
  919. if logTimeRange:
  920. logStartTime, logEndTime = int(
  921. logTimeRange[0][:-3]), int(logTimeRange[1][:-3])
  922. cg_qs = cg_qs.filter(
  923. time__gte=logStartTime,
  924. time__lte=logEndTime)
  925. list_data = []
  926. count = uid_bucket_qs.count()
  927. uid_bucket_qs = uid_bucket_qs.order_by('-addTime')[(page - 1) * line:page * line]
  928. for uid_bucket in uid_bucket_qs:
  929. data = {
  930. 'id': uid_bucket.id,
  931. 'uid': uid_bucket.uid,
  932. 'channel': uid_bucket.channel,
  933. 'status': uid_bucket.status,
  934. 'endTime': time.strftime(
  935. "%Y--%m--%d %H:%M:%S",
  936. time.localtime(
  937. uid_bucket.endTime)),
  938. 'addTime': time.strftime(
  939. "%Y--%m--%d %H:%M:%S",
  940. time.localtime(
  941. uid_bucket.addTime)),
  942. 'use_status': uid_bucket.use_status,
  943. 'has_unused': uid_bucket.has_unused}
  944. for order in order_qs.filter(
  945. uid_bucket_id=uid_bucket.id).values(
  946. 'uid_bucket_id',
  947. 'desc',
  948. 'userID__userID',
  949. 'UID',
  950. 'price',
  951. 'payType',
  952. 'userID__username',
  953. 'userID__phone',
  954. 'userID__userEmail',
  955. 'userID__data_joined'):
  956. data['desc'] = order['desc']
  957. data['payType'] = order['payType']
  958. data['price'] = order['price']
  959. data['username'] = order['userID__username']
  960. data['phone'] = order['userID__phone']
  961. data['userEmail'] = order['userID__userEmail']
  962. data['data_joined'] = order['userID__data_joined'].strftime(
  963. "%Y-%m-%d %H:%M:%S")
  964. data['playcount'] = cg_qs.filter(
  965. operation='cloudstorage/queryvodlist', uid=order['UID']).count()
  966. for uidset in uidset_qs.filter(
  967. uid=uid_bucket.uid).values(
  968. 'ucode',
  969. 'version'):
  970. data['ucode'] = uidset['ucode']
  971. data['version'] = uidset['version']
  972. list_data.append(data)
  973. response = HttpResponse(content_type='application/vnd.ms-excel')
  974. response['Content-Disposition'] = 'attachment; filename=userinfo.xls'
  975. workbook = xlwt.Workbook(encoding='utf-8')
  976. sheet1 = workbook.add_sheet('UID')
  977. headtitle = [
  978. 'id',
  979. '用户账号',
  980. '用户手机号',
  981. '用户邮箱',
  982. '注册时间',
  983. '设备UID',
  984. '设备通道',
  985. '云存状态',
  986. '添加时间',
  987. '到期时间',
  988. '使用状态',
  989. '是否有未使用套餐',
  990. '套餐描述',
  991. '支付方式',
  992. '价格',
  993. '播放次数',
  994. '产品编码',
  995. '版本'
  996. ]
  997. headnum = 0
  998. for title in headtitle:
  999. sheet1.write(0, headnum, title)
  1000. headnum= headnum+1
  1001. fields = [
  1002. 'id',
  1003. 'username',
  1004. 'phone',
  1005. 'userEmail',
  1006. 'data_joined',
  1007. 'uid',
  1008. 'channel',
  1009. 'status',
  1010. 'addTime',
  1011. 'endTime',
  1012. 'use_status',
  1013. 'has_unused',
  1014. 'desc',
  1015. 'payType',
  1016. 'price',
  1017. 'playcount',
  1018. 'ucode',
  1019. 'version'
  1020. ]
  1021. num = 1
  1022. for item in list_data:
  1023. fieldnum = 0
  1024. for key in fields:
  1025. val = item[key]
  1026. if key == 'payType':
  1027. if val == 1:
  1028. val = 'PayPal'
  1029. if val == 2:
  1030. val = '支付宝'
  1031. if val == 3:
  1032. val = '微信支付'
  1033. if val == 10:
  1034. val = '免费体验'
  1035. if val == 11:
  1036. val = '激活码'
  1037. sheet1.write(num, fieldnum, val)
  1038. fieldnum = fieldnum+1
  1039. num =num+1
  1040. workbook.save(response)
  1041. return response
  1042. except Exception as e:
  1043. print(e)
  1044. return response.json(500, repr(e))
  1045. def getCloudDataList(self, request_dict, response):
  1046. year = request_dict.get('year', None)
  1047. Jan = int(time.mktime(time.strptime(year + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1048. Feb = int(time.mktime(time.strptime(year + '-2-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1049. Mar = int(time.mktime(time.strptime(year + '-3-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1050. Apr = int(time.mktime(time.strptime(year + '-4-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1051. May = int(time.mktime(time.strptime(year + '-5-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1052. Jun = int(time.mktime(time.strptime(year + '-6-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1053. Jul = int(time.mktime(time.strptime(year + '-7-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1054. Aug = int(time.mktime(time.strptime(year + '-8-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1055. Sep = int(time.mktime(time.strptime(year + '-9-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1056. Oct = int(time.mktime(time.strptime(year + '-10-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1057. Nov = int(time.mktime(time.strptime(year + '-11-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1058. Dec = int(time.mktime(time.strptime(year + '-12-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1059. Jan_next = int(time.mktime(time.strptime(str(int(year)+1) + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  1060. list_data = []
  1061. vod_bucket_qs = VodBucketModel.objects.filter()
  1062. if not vod_bucket_qs.exists():
  1063. return response.json(173)
  1064. try:
  1065. for vod_bucket in vod_bucket_qs:
  1066. vod_bucket_id = vod_bucket.id
  1067. store_meal = Store_Meal.objects.filter(bucket_id=vod_bucket_id, lang__lang='cn').values('lang__title', 'lang__content')
  1068. if not store_meal.exists():
  1069. continue
  1070. name = store_meal[0]['lang__title']+'-'+store_meal[0]['lang__content']
  1071. order = Order_Model.objects.filter(rank__bucket_id=vod_bucket_id)
  1072. Jan_count = order.filter(status=1, addTime__range=[Jan, Feb]).count()
  1073. Feb_count = order.filter(status=1, addTime__range=[Feb, Mar]).count()
  1074. Mar_count = order.filter(status=1, addTime__range=[Mar, Apr]).count()
  1075. Apr_count = order.filter(status=1, addTime__range=[Apr, May]).count()
  1076. May_count = order.filter(status=1, addTime__range=[May, Jun]).count()
  1077. Jun_count = order.filter(status=1, addTime__range=[Jun, Jul]).count()
  1078. Jul_count = order.filter(status=1, addTime__range=[Jul, Aug]).count()
  1079. Aug_count = order.filter(status=1, addTime__range=[Aug, Sep]).count()
  1080. Sep_count = order.filter(status=1, addTime__range=[Sep, Oct]).count()
  1081. Oct_count = order.filter(status=1, addTime__range=[Oct, Nov]).count()
  1082. Nov_count = order.filter(status=1, addTime__range=[Nov, Dec]).count()
  1083. Dec_count = order.filter(status=1, addTime__range=[Dec, Jan_next]).count()
  1084. data = [Jan_count, Feb_count, Mar_count, Apr_count, May_count, Jun_count, Jul_count, Aug_count, Sep_count,
  1085. Oct_count, Nov_count, Dec_count]
  1086. cloud_data = {
  1087. 'name': name,
  1088. 'type': 'line',
  1089. 'data': data,
  1090. }
  1091. list_data.append(cloud_data)
  1092. return response.json(0, {'list': list_data})
  1093. except Exception as e:
  1094. print(e)
  1095. return response.json(500, repr(e))
  1096. @classmethod
  1097. def deviceAttritionAlert(cls, request_dict, response):
  1098. """
  1099. 流失预警界面
  1100. @param request_dict:
  1101. @param response:
  1102. """
  1103. pageNo = request_dict.get('pageNo', None)
  1104. pageSize = request_dict.get('pageSize', None)
  1105. use_status = request_dict.get('use_status', None)
  1106. if not all([pageNo, pageSize]):
  1107. return response.json(444)
  1108. page = int(pageNo)
  1109. line = int(pageSize)
  1110. nowTime = int(time.time())
  1111. attrition_list = []
  1112. r = 0
  1113. uid_buncket_qs = UID_Bucket.objects.filter(status=0).values('use_status', 'uid', 'endTime', 'addTime',
  1114. 'use_status').annotate(count=Count('uid')).order_by(
  1115. '-addTime')
  1116. if use_status:
  1117. uid_buncket_qs = uid_buncket_qs.filter(use_status=use_status)
  1118. count = uid_buncket_qs.count()
  1119. uid_buncket_qs = uid_buncket_qs[(page - 1) * line:page * line]
  1120. try:
  1121. for uid_buncket in uid_buncket_qs:
  1122. day = 0
  1123. endTime = uid_buncket['endTime']
  1124. addTime = uid_buncket['addTime']
  1125. start_time = addTime
  1126. start_time = datetime.datetime.fromtimestamp(int(start_time))
  1127. r += 1
  1128. if r > 1:
  1129. nowTime = str(nowTime)
  1130. time_tuple = time.strptime(nowTime, ('%Y-%m-%d %H:%M:%S'))
  1131. nowTime = time.mktime(time_tuple) # 把格式化好的时间转换成时间戳
  1132. nowTime = datetime.datetime.fromtimestamp(int(nowTime))
  1133. time_list = CommonService.cutting_time(start_time, nowTime, 'day')
  1134. vod_hls_qs = VodHlsModel.objects.filter(uid=uid_buncket['uid']).values('uid')
  1135. # 获取自云存开通起没有上传数据天数
  1136. for date in time_list:
  1137. vod_hls_qs = vod_hls_qs.filter(time__gte=date[0], time__lt=date[1])
  1138. if not vod_hls_qs.exists():
  1139. day += 1
  1140. if day > 29:
  1141. break
  1142. day = day
  1143. level = ''
  1144. use_status = uid_buncket['use_status']
  1145. if not use_status == 1:
  1146. level = '八号预警'
  1147. else:
  1148. # 统计设备目前未上传天数
  1149. vod_hls_qs = VodHlsModel.objects.filter(uid=uid_buncket['uid']).values('uid')
  1150. if 15 <= day < 25:
  1151. if vod_hls_qs.exists():
  1152. level = '取消预警'
  1153. else:
  1154. level = '一号预警'
  1155. if day >= 25:
  1156. if vod_hls_qs.exists():
  1157. level = '取消预警'
  1158. else:
  1159. startTime = uid_buncket['addTime'] # 开始时间
  1160. nowTime = str(nowTime)
  1161. time_tuple = time.strptime(nowTime, ('%Y-%m-%d %H:%M:%S'))
  1162. nowTime = time.mktime(time_tuple) # 把格式化好的时间转换成时间戳
  1163. startTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(startTime)) # 开始时间
  1164. nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nowTime)) # 结束时间
  1165. startTime = datetime.datetime.strptime(startTime, '%Y-%m-%d %H:%M:%S') # 开始时间
  1166. nowTime = datetime.datetime.strptime(nowTime, '%Y-%m-%d %H:%M:%S') # 结束时间
  1167. day = (nowTime - startTime).days
  1168. level = '二号预警'
  1169. device_user = Device_User.objects.filter(device_info__UID=uid_buncket['uid']).values('username',
  1170. 'userID')
  1171. username = device_user[0]['username'] if device_user.exists() else ''
  1172. userID = device_user[0]['userID'] if device_user.exists() else ''
  1173. order_qs = Order_Model.objects.filter(order_type=0, userID=userID).values('UID').annotate(
  1174. count=Count('UID'))
  1175. # 用户设备购买云存数量
  1176. if not order_qs.exists():
  1177. device_count = 0
  1178. else:
  1179. uid_count = order_qs.count()
  1180. if uid_count == 1:
  1181. device_count = 'N/A'
  1182. else:
  1183. device_count = uid_count
  1184. order_uid_list = [order[uid] for order in order_qs for uid in order]
  1185. vod_hls_qs = VodHlsModel.objects.filter(uid__in=order_uid_list).values('uid')
  1186. # 套餐使用期间是否上传过数据
  1187. if vod_hls_qs.exists():
  1188. other = '有'
  1189. else:
  1190. other = '无'
  1191. data = {
  1192. 'userID': userID,
  1193. 'uid': uid_buncket['uid'],
  1194. 'endTime': CommonService.timestamp_to_str(endTime),
  1195. 'addTime': CommonService.timestamp_to_str(addTime),
  1196. 'status': uid_buncket['use_status'],
  1197. 'level': level,
  1198. 'day': day,
  1199. 'other': other,
  1200. 'username': username,
  1201. 'count': device_count,
  1202. }
  1203. attrition_list.append(data)
  1204. test_list = [list for list in attrition_list if list['day'] > 14] # 输出15天及以上的数据
  1205. return response.json(0, {'test_list': test_list, 'total': count})
  1206. except Exception as e:
  1207. return response.json(500, repr(e))