CronTaskController.py 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. #!/usr/bin/python3.6
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2022 #
  5. # @Time : 2022/4/1 11:27
  6. # @Author : ming
  7. # @Email : zhangdongming@asj6.wecom.work
  8. # @File : CronTaskController.py
  9. # @Software: PyCharm
  10. import datetime
  11. import io
  12. import json
  13. import threading
  14. import time
  15. import zipfile
  16. import paypalrestsdk
  17. import requests
  18. import csv
  19. import math
  20. from django.db import connection, connections, transaction
  21. from django.db.models import Q, Sum, Count
  22. from django.views import View
  23. from Ansjer.config import USED_SERIAL_REDIS_LIST, UNUSED_SERIAL_REDIS_LIST, CONFIG_INFO, CONFIG_US, \
  24. RESET_REGION_ID_SERIAL_REDIS_LIST, LOGGER, PAYPAL_CRD, CONFIG_EUR, DETECT_PUSH_DOMAINS, ACCESS_KEY_ID, \
  25. SECRET_ACCESS_KEY, REGION_NAME, CONFIG_CN
  26. from Model.models import Device_User, Device_Info, UidSetModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
  27. VodHlsModel, ExperienceContextModel, AiService, VodHlsSummary, VideoPlaybackTimeModel, DeviceUserSummary, \
  28. CountryModel, DeviceTypeModel, OrdersSummary, DeviceInfoSummary, CompanySerialModel, \
  29. CloudLogModel, UidCloudStorageCount, UserExModel, DeviceDomainRegionModel, VodHlsTag, VodHlsTagType, \
  30. Store_Meal, Lang, VodBucketModel, UnicomComboOrderInfo, UnicomDeviceInfo, AbnormalOrder, DailyReconciliation, \
  31. CustomizedPush, UIDCompanySerialModel, UIDModel, LogModel, OperatingCosts, UidBucketStatistics, AppScannedSerial, \
  32. SerialUnbindUID, UidUserModel, UidPushModel, iotdeviceInfoModel, ExperienceAiModel
  33. from Object.AWS.AmazonS3Util import AmazonS3Util
  34. from Object.AWS.S3Email import S3Email
  35. from Object.RedisObject import RedisObject
  36. from Object.ResponseObject import ResponseObject
  37. from Object.utils import LocalDateTimeUtil
  38. from Service.CommonService import CommonService
  39. from Service.EquipmentInfoService import EQUIPMENT_INFO_MODEL_LIST, EquipmentInfoService
  40. from Service.VodHlsService import SplitVodHlsObject
  41. from Object.UnicomObject import UnicomObjeect
  42. from Object.WechatPayObject import WechatPayObject
  43. from Object.AliPayObject import AliPayObject
  44. from dateutil.relativedelta import relativedelta
  45. class CronDelDataView(View):
  46. def get(self, request, *args, **kwargs):
  47. request.encoding = 'utf-8'
  48. operation = kwargs.get('operation')
  49. return self.validation(request.GET, request, operation)
  50. def post(self, request, *args, **kwargs):
  51. request.encoding = 'utf-8'
  52. operation = kwargs.get('operation')
  53. return self.validation(request.POST, request, operation)
  54. def validation(self, request_dict, request, operation):
  55. response = ResponseObject()
  56. if operation == 'delAccessLog': # 定时删除访问接口数据
  57. return self.delAccessLog(response)
  58. elif operation == 'delPushInfo': # 定时删除推送数据
  59. return self.delPushInfo(response)
  60. elif operation == 'delPushInfoV2': # 定时删除推送数据V2
  61. return self.delPushInfoV2(response)
  62. elif operation == 'delSysMsg': # 定时删除系统消息数据
  63. return self.delSysMsg(response)
  64. elif operation == 'delVodHls': # 定时删除云存播放列表
  65. return self.delVodHls(response)
  66. elif operation == 'delCloudLog': # 定时删除云存接口数据
  67. return self.delCloudLog(response)
  68. elif operation == 'delTesterDevice': # 定时删除测试账号下的设备数据
  69. return self.delTesterDevice(response)
  70. elif operation == 'delAppLog': # 定时删除app日志
  71. return self.delAppLog(response)
  72. elif operation == 'delDeviceLog': # 定时删除设备日志
  73. return self.delDeviceLog(response)
  74. elif operation == 'UpdateConfiguration': # 定时更新配置
  75. return self.UpdateConfiguration(response)
  76. elif operation == 'cloud-log':
  77. return self.uid_cloud_storage_upload_count(response)
  78. elif operation == 'delDeviceLog': # 定时删除设备日志
  79. return self.del_device_log(response)
  80. else:
  81. return response.json(404)
  82. @staticmethod
  83. def UpdateConfiguration(response):
  84. """
  85. 定时更新配置
  86. @param response: 响应对象
  87. @return:
  88. """
  89. try:
  90. ucode_list = ['823C01552AA',
  91. '823C01550AA',
  92. '823C01550XA',
  93. '823C01850XA',
  94. '730201350AA',
  95. '730201350AA',
  96. '730201450AA',
  97. '730201450MA',
  98. '72V201252AA',
  99. '72V201253AA',
  100. '72V201353AA',
  101. '72V201354AA',
  102. '72V201355AA',
  103. '72V201254AA',
  104. 'V82301850AA',
  105. 'V82301850XA',
  106. '72V201257AA', '72V201256AA']
  107. UidSetModel.objects.filter(ucode__in=ucode_list, is_human=0).update(is_human=1)
  108. ucode_list = ['72V201257AA', '72V201254AA'] # 4G规格码
  109. UidSetModel.objects.filter(ucode__in=ucode_list, mobile_4g=0).update(mobile_4g=1)
  110. # 根据设备规格码定时更新默认算法类型类型
  111. ucode_list = ['823C01552AA', '823C01550AA', '823C01550XA', 'C18201550KA',
  112. '823C01550TA', '823C01550VA', '823C01850XA', 'C18201850KA',
  113. '823C01850TA', '823C01850VA', 'C22501850VA']
  114. UidSetModel.objects.filter(ucode__in=ucode_list, ai_type=0).update(ai_type=47)
  115. ucode_list = ['730201350AA', '730201450AA', '730201450MA', '730201450NA']
  116. UidSetModel.objects.filter(ucode__in=ucode_list, ai_type=0).update(ai_type=7)
  117. ucode_list = ['V82301850AA', 'V82301850XA']
  118. UidSetModel.objects.filter(ucode__in=ucode_list, ai_type=0).update(ai_type=2031)
  119. # 根据设备规格码更新默认个性化语音值
  120. ucode_list = ['823C01552AA', '823C01550XA', 'C18201550KA', '823C01550TA',
  121. '823C01550VA', '823C01850XA', 'C18201850KA', '823C01850TA', '823C01850VA',
  122. '730201450AA', '730201450MA', '730201450NA', '72V201252AA', '72V201253AA',
  123. '72V201353AA', '72V201354AA', '72V201355AA', '72V201254AA', 'C22501850VA',
  124. 'V82301850AA', 'V82301850XA', '72V201257AA', '72V201256AA']
  125. UidSetModel.objects.filter(ucode__in=ucode_list, is_custom_voice=0).update(is_custom_voice=1)
  126. # 根据设备规格码更新is_ai
  127. ucode_list = ['823C01552AA', '823C01550XA', 'C18201550KA', '823C01550TA',
  128. '823C01550VA', '823C01850XA', 'C18201850KA', '823C01850TA', '823C01850VA',
  129. '730201450AA', '730201450MA', '730201450NA', '72V201252AA', '72V201253AA',
  130. '72V201353AA', '72V201354AA', '72V201355AA', '72V201254AA', 'C22501850VA',
  131. 'V82301850AA', 'V82301850XA', '72V201257AA', '72V201256AA', '730201350AA']
  132. UidSetModel.objects.filter(ucode__in=ucode_list, is_ai=2).update(is_ai=1)
  133. # 根据设备规格码更新alexa
  134. ucode_list = ['823C01552AA', '823C01550AA', '823C01550XA', '522001352AA',
  135. '823C01550TA', '823C01550VA', '823C01850XA', 'C18201850KA', '823C01850TA',
  136. 'C22501850VA', 'V82301850AA', 'V82301850XA', '730201350AA', '72V201252AA',
  137. '72V201253AA', '72V201353AA', '72V201354AA', '72V201355AA', '72V201256AA']
  138. UidSetModel.objects.filter(ucode__in=ucode_list, is_alexa=0).update(is_alexa=1)
  139. # 根据ai类型和设备类型修改516 type
  140. uid_list = UidSetModel.objects.filter(ai_type=18563).values_list('uid')
  141. Device_Info.objects.filter(Type=10, UID__in=uid_list).update(Type=24)
  142. return response.json(0)
  143. except Exception as e:
  144. LOGGER.info('UpdateConfiguration异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  145. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  146. @staticmethod
  147. def delAppLog(response):
  148. """
  149. 定时删除app日志
  150. @param response: 响应对象
  151. @return:
  152. """
  153. nowTime = int(time.time())
  154. try:
  155. cursor = connection.cursor()
  156. month_ago_time = nowTime - 30 * 24 * 60 * 60 # 保留近30天的数据
  157. sql = 'DELETE FROM `app_log` WHERE add_time<{}'.format(month_ago_time)
  158. cursor.execute(sql)
  159. cursor.close()
  160. return response.json(0)
  161. except Exception as e:
  162. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  163. @staticmethod
  164. def delDeviceLog(response):
  165. """
  166. 定时删除设备日志
  167. @param response: 响应对象
  168. @return:
  169. """
  170. nowTime = int(time.time())
  171. size = 5000
  172. try:
  173. cursor = connection.cursor()
  174. month_ago_time = nowTime - 30 * 24 * 60 * 60 # 保留近30天的数据
  175. month_ago_time_str = CommonService.timestamp_to_str(month_ago_time)
  176. sql = "DELETE FROM `device_log` WHERE add_time<'{}' LIMIT {}".format(month_ago_time_str, size)
  177. cursor.execute(sql)
  178. cursor.close()
  179. return response.json(0)
  180. except Exception as e:
  181. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  182. @staticmethod
  183. def uid_cloud_storage_upload_count(response):
  184. try:
  185. now_time = int(time.time())
  186. local_time = LocalDateTimeUtil.get_before_days_timestamp(now_time)
  187. format_str = '%Y-%m-%d'
  188. date_str = LocalDateTimeUtil.time_stamp_to_time(local_time, format_str)
  189. start_time, end_time = LocalDateTimeUtil.get_start_and_end_time(date_str, format_str)
  190. cs_uid_qs = UID_Bucket.objects.filter(addTime__gte=int(1669824000)).values('uid')
  191. if not cs_uid_qs.exists():
  192. return response.json(0)
  193. for item in cs_uid_qs:
  194. uid = item['uid']
  195. cloud_log_qs = CloudLogModel.objects.filter(uid=uid, operation=r'cloudstorage/storeplaylist',
  196. time__gte=start_time, time__lte=end_time)
  197. cloud_log_qs = cloud_log_qs.values('uid')[0:1]
  198. if not cloud_log_qs.exists():
  199. continue
  200. count_data = {'uid': uid, 'count': cloud_log_qs.count(), 'created_time': end_time,
  201. 'updated_time': end_time}
  202. UidCloudStorageCount.objects.create(**count_data)
  203. return response.json(0)
  204. except Exception as e:
  205. LOGGER.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  206. return response.json(500)
  207. @staticmethod
  208. def delAccessLog(response):
  209. try:
  210. cursor = connection.cursor()
  211. # 删除7天前的数据
  212. last_week = LocalDateTimeUtil.get_last_week()
  213. sql = 'DELETE FROM access_log WHERE time < %s limit %s'
  214. cursor.execute(sql, [last_week, 10000])
  215. # 关闭游标
  216. cursor.close()
  217. connection.close()
  218. return response.json(0)
  219. except Exception as e:
  220. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  221. @classmethod
  222. def delPushInfo(cls, response):
  223. now_time = int(time.time())
  224. try:
  225. # 当前时间转日期
  226. local_date_now = str(datetime.datetime.fromtimestamp(int(now_time)).date())
  227. # 根据日期获取周几
  228. week_val = LocalDateTimeUtil.date_to_week(local_date_now)
  229. # 根据当前时间获取7天前时间戳
  230. expiration_time = LocalDateTimeUtil.get_before_days_timestamp(now_time, 7)
  231. # 异步删除推送消息
  232. kwargs = {
  233. 'week_val': week_val,
  234. 'expiration_time': expiration_time
  235. }
  236. del_push_info_thread = threading.Thread(
  237. target=cls.del_push_info_data,
  238. kwargs=kwargs)
  239. del_push_info_thread.start()
  240. return response.json(0)
  241. except Exception as e:
  242. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  243. @staticmethod
  244. def del_push_info_data(**kwargs):
  245. cursor = connections['mysql02'].cursor()
  246. # 获取删除星期列表
  247. week_val = kwargs['week_val']
  248. del_week_val_list = [i for i in range(1, 8)]
  249. # 移除当天和前后两天
  250. del_week_val_list.remove(week_val)
  251. if week_val == 1:
  252. pre_week_val = 7
  253. else:
  254. pre_week_val = week_val - 1
  255. del_week_val_list.remove(pre_week_val)
  256. if week_val == 7:
  257. nex_week_val = 1
  258. else:
  259. nex_week_val = week_val + 1
  260. del_week_val_list.remove(nex_week_val)
  261. expiration_time = kwargs['expiration_time']
  262. # 每次删除条数
  263. size = 5000
  264. # 删除7天前的数据
  265. sql = "DELETE FROM equipment_info WHERE addTime<= %s LIMIT %s "
  266. cursor.execute(sql, [expiration_time, size])
  267. for week_val in del_week_val_list:
  268. if week_val == 1:
  269. sql = "DELETE FROM equipment_info_monday WHERE add_time<= %s LIMIT %s "
  270. if week_val == 2:
  271. sql = "DELETE FROM equipment_info_tuesday WHERE add_time<= %s LIMIT %s "
  272. if week_val == 3:
  273. sql = "DELETE FROM equipment_info_wednesday WHERE add_time<= %s LIMIT %s "
  274. if week_val == 4:
  275. sql = "DELETE FROM equipment_info_thursday WHERE add_time<= %s LIMIT %s "
  276. if week_val == 5:
  277. sql = "DELETE FROM equipment_info_friday WHERE add_time<= %s LIMIT %s "
  278. if week_val == 6:
  279. sql = "DELETE FROM equipment_info_saturday WHERE add_time<= %s LIMIT %s "
  280. if week_val == 7:
  281. sql = "DELETE FROM equipment_info_sunday WHERE add_time<= %s LIMIT %s "
  282. cursor.execute(sql, [expiration_time, size])
  283. # 关闭游标
  284. cursor.close()
  285. @classmethod
  286. def delPushInfoV2(cls, response):
  287. try:
  288. del_push_info_thread = threading.Thread(
  289. target=cls.del_push_info_data_v2)
  290. del_push_info_thread.start()
  291. return response.json(0)
  292. except Exception as e:
  293. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  294. @staticmethod
  295. def delSysMsg(response):
  296. try:
  297. cursor = connections['mysql02'].cursor()
  298. # 获取90天前时间戳
  299. now_time = int(time.time())
  300. expiration_time = LocalDateTimeUtil.get_before_days_timestamp(now_time, 90)
  301. # 每次删除条数
  302. size = 5000
  303. sql = "DELETE FROM sys_msg WHERE addTime< %s LIMIT %s "
  304. cursor.execute(sql, [expiration_time, size])
  305. # 关闭游标
  306. cursor.close()
  307. return response.json(0)
  308. except Exception as e:
  309. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  310. @staticmethod
  311. def del_push_info_data_v2():
  312. cursor = connections['mysql02'].cursor()
  313. # 获取7天前时间戳
  314. now_time = int(time.time())
  315. expiration_time = LocalDateTimeUtil.get_before_days_timestamp(now_time, 7)
  316. # 每次删除条数
  317. size = 5000
  318. for i in range(1, len(EQUIPMENT_INFO_MODEL_LIST) + 1):
  319. sql = "DELETE FROM equipment_info_{} WHERE add_time< %s LIMIT %s ".format(i)
  320. cursor.execute(sql, [expiration_time, size])
  321. # 关闭游标
  322. cursor.close()
  323. @staticmethod
  324. def delVodHls(response):
  325. nowTime = int(time.time())
  326. try:
  327. CronDelDataView.del_vod_hls_tag()
  328. cursor = connection.cursor()
  329. month_ago_time = nowTime - 3 * 30 * 24 * 60 * 60 # 删除3个月前的数据
  330. sql = 'DELETE FROM `vod_hls` WHERE endTime<{} LIMIT 50000'.format(month_ago_time)
  331. cursor.execute(sql)
  332. cursor.close()
  333. # 删除vod_hls分表数据
  334. split_vod_hls_obj = SplitVodHlsObject()
  335. split_vod_hls_obj.del_vod_hls_data(end_time__lt=month_ago_time)
  336. return response.json(0)
  337. except Exception as e:
  338. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  339. @staticmethod
  340. def del_vod_hls_tag():
  341. """
  342. 删除AI标签记录
  343. """
  344. e_time = LocalDateTimeUtil.get_before_days_timestamp(int(time.time()), 30)
  345. VodHlsTagType.objects.filter(created_time__lt=e_time).delete()
  346. VodHlsTag.objects.filter(created_time__lt=e_time).delete()
  347. @staticmethod
  348. def delCloudLog(response):
  349. nowTime = int(time.time())
  350. cursor = connection.cursor()
  351. try:
  352. # 删除3个月前的数据
  353. sql = "DELETE FROM `cloud_log` WHERE time<={} LIMIT 50000".format(
  354. nowTime - 3 * 30 * 24 * 60 * 60)
  355. cursor.execute(sql)
  356. # 关闭游标
  357. cursor.close()
  358. return response.json(0)
  359. except Exception as e:
  360. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  361. @staticmethod
  362. def delTesterDevice(response):
  363. try:
  364. userID_list = [
  365. 'tech01@ansjer.com',
  366. 'tech02@ansjer.com',
  367. 'tech03@ansjer.com',
  368. 'tech04@ansjer.com',
  369. 'tech05@ansjer.com',
  370. 'tech06@ansjer.com',
  371. 'tech07@ansjer.com',
  372. 'tech08@ansjer.com',
  373. 'tech09@ansjer.com',
  374. 'tech10@ansjer.com',
  375. 'fix01@ansjer.com',
  376. 'fix02@ansjer.com',
  377. 'fix03@ansjer.com',
  378. 'fix04@ansjer.com',
  379. 'fix05@ansjer.com']
  380. device_user = Device_User.objects.filter(username__in=userID_list)
  381. device_info_qs = Device_Info.objects.filter(
  382. userID__in=device_user).values('UID')
  383. uid_list = []
  384. for device_info in device_info_qs:
  385. uid_list.append(device_info['UID'])
  386. with transaction.atomic():
  387. # 删除设备云存相关数据
  388. UidSetModel.objects.filter(uid__in=uid_list).delete()
  389. UID_Bucket.objects.filter(uid__in=uid_list).delete()
  390. Unused_Uid_Meal.objects.filter(uid__in=uid_list).delete()
  391. # Order_Model.objects.filter(UID__in=uid_list).delete()
  392. StsCrdModel.objects.filter(uid__in=uid_list).delete()
  393. VodHlsModel.objects.filter(uid__in=uid_list).delete()
  394. # 删除vod_hls分表数据
  395. split_vod_hls_obj = SplitVodHlsObject()
  396. split_vod_hls_obj.del_vod_hls_data(uid__in=uid_list)
  397. ExperienceContextModel.objects.filter(uid__in=uid_list).delete()
  398. Device_Info.objects.filter(userID__in=device_user).delete()
  399. return response.json(0)
  400. except Exception as e:
  401. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  402. @staticmethod
  403. def del_device_log(response):
  404. """
  405. 定时删除设备日志
  406. @param response: 响应对象
  407. @return:
  408. """
  409. nowTime = int(time.time())
  410. try:
  411. cursor = connection.cursor()
  412. month_ago_time = nowTime - 30 * 24 * 60 * 60 # 保留近30天的数据
  413. sql = 'DELETE FROM `device_log` WHERE unix_timestamp(add_time)<{}'.format(month_ago_time)
  414. cursor.execute(sql)
  415. cursor.close()
  416. return response.json(0)
  417. except Exception as e:
  418. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  419. class CronUpdateDataView(View):
  420. def get(self, request, *args, **kwargs):
  421. request.encoding = 'utf-8'
  422. operation = kwargs.get('operation')
  423. return self.validation(request.GET, request, operation)
  424. def post(self, request, *args, **kwargs):
  425. request.encoding = 'utf-8'
  426. operation = kwargs.get('operation')
  427. return self.validation(request.POST, request, operation)
  428. def validation(self, request_dict, request, operation):
  429. response = ResponseObject()
  430. if operation == 'updateUnusedUidBucket': # 定时更新过期云存关联的未使用套餐状态
  431. return self.updateUnusedUidBucket(response)
  432. elif operation == 'updateUnusedAiService': # 定时更新过期ai关联的未使用套餐状态
  433. return self.updateUnusedAiService(response)
  434. elif operation == 'reqUpdateSerialStatus': # 定时请求更新序列号状态
  435. return self.reqUpdateSerialStatus(response)
  436. elif operation == 'updateSerialStatus': # 更新序列号状态
  437. return self.updateSerialStatus(request_dict, response)
  438. elif operation == 'deleteUidData': # 清除uid数据
  439. return self.deleteUidData(request_dict, response)
  440. elif operation == 'reset-region-id': # 重置地区id
  441. return self.reset_region_id(request_dict, response)
  442. elif operation == 'updateVodMeal': # 定时修改体验套餐有效期为1个月
  443. return self.update_vod_meal(request_dict, response)
  444. elif operation == 'checkCustomizedPush': # 定时检查定制化推送,重新执行没有推送成功的请求
  445. return self.check_customized_push(response)
  446. else:
  447. return response.json(404)
  448. @staticmethod
  449. def updateUnusedUidBucket(response):
  450. """
  451. 监控云存套餐过期修改状态
  452. @param response:
  453. @return:
  454. """
  455. # 定时更新已过期套餐修改状态为2
  456. now_time = int(time.time())
  457. expired_uid_bucket = UID_Bucket.objects.filter(endTime__lte=now_time)
  458. expired_uid_bucket = expired_uid_bucket.filter(~Q(use_status=2)).values('id')
  459. if expired_uid_bucket.exists():
  460. # 如果没有未使用套餐,或下个未使用套餐不是AI云存套餐,mqtt下发停用AI指令
  461. # 下个未使用套餐是AI云存套餐,mqtt下发启用AI指令
  462. for bucket in expired_uid_bucket:
  463. uid_bucket = UID_Bucket.objects.get(id=bucket['id'])
  464. uid = uid_bucket.uid
  465. # 存在序列号则为使用序列号作为物品名
  466. thing_name = CommonService.query_serial_with_uid(uid)
  467. topic_name = 'ansjer/generic/{}'.format(thing_name)
  468. msg = {'commandType': 'AIDisable'}
  469. if not uid_bucket.has_unused:
  470. CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
  471. else:
  472. next_unused = Unused_Uid_Meal.objects.filter(uid=uid).order_by('addTime').first()
  473. if next_unused is None or not getattr(next_unused, 'is_ai', 0):
  474. CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
  475. else:
  476. msg = {'commandType': 'AIEnable'}
  477. CommonService.req_publish_mqtt_msg(thing_name, topic_name, msg)
  478. expired_uid_bucket.update(use_status=2)
  479. # 监控有未使用套餐则自动生效
  480. expired_uid_buckets = UID_Bucket.objects.filter(endTime__lte=now_time, has_unused=1).values("id", "uid")[0:1000]
  481. for expired_uid_bucket in expired_uid_buckets:
  482. unuseds = Unused_Uid_Meal.objects.filter(
  483. uid=expired_uid_bucket['uid']).values(
  484. "id",
  485. "uid",
  486. "channel",
  487. "addTime",
  488. "expire",
  489. "is_ai",
  490. "bucket_id",
  491. "order_id").order_by('addTime')
  492. if not unuseds.exists():
  493. continue
  494. unused = unuseds[0]
  495. try:
  496. with transaction.atomic():
  497. count_unused = Unused_Uid_Meal.objects.filter(uid=expired_uid_bucket['uid']).count()
  498. has_unused = 1 if count_unused > 1 else 0
  499. end_time = CommonService.calcMonthLater(unused['expire'])
  500. UID_Bucket.objects.filter(
  501. uid=expired_uid_bucket['uid']).update(
  502. channel=unused['channel'],
  503. endTime=end_time,
  504. bucket_id=unused['bucket_id'],
  505. updateTime=now_time,
  506. use_status=1,
  507. has_unused=has_unused,
  508. orderId=unused['order_id'])
  509. if unused['is_ai']:
  510. ai_service = AiService.objects.filter(uid=expired_uid_bucket['uid'], channel=unused['channel'])
  511. if ai_service.exists():
  512. ai_service.update(updTime=now_time, use_status=1, orders_id=unused['order_id'],
  513. endTime=end_time)
  514. else:
  515. AiService.objects.create(uid=expired_uid_bucket['uid'], channel=unused['channel'],
  516. detect_status=1, addTime=now_time, orders_id=unused['order_id'],
  517. updTime=now_time, endTime=end_time, use_status=1)
  518. Unused_Uid_Meal.objects.filter(id=unused['id']).delete()
  519. StsCrdModel.objects.filter(uid=expired_uid_bucket['uid']).delete() # 删除sts记录
  520. except Exception as e:
  521. print(repr(e))
  522. continue
  523. return response.json(0)
  524. @staticmethod
  525. def updateUnusedAiService(response):
  526. now_time = int(time.time())
  527. ai_service_qs = AiService.objects.filter(
  528. endTime__lte=now_time,
  529. use_status=1).values(
  530. 'id',
  531. 'uid')[
  532. 0:200]
  533. for ai_service in ai_service_qs:
  534. try:
  535. with transaction.atomic():
  536. AiService.objects.filter(
  537. id=ai_service['id']).update(
  538. use_status=2) # 更新过期ai订单状态
  539. # 如果存在未使用套餐,更新为使用
  540. unused_ai_service = AiService.objects.filter(
  541. uid=ai_service['uid'],
  542. use_status=0).order_by('addTime')[
  543. :1].values(
  544. 'id',
  545. 'endTime')
  546. if unused_ai_service.exists():
  547. # 未使用套餐的endTime在购买的时候保存为有效时间
  548. effective_day = unused_ai_service[0]['endTime']
  549. endTime = now_time + effective_day
  550. AiService.objects.filter(
  551. id=unused_ai_service[0]['id']).update(
  552. use_status=1, endTime=endTime, updTime=now_time)
  553. except Exception:
  554. continue
  555. return response.json(0)
  556. @classmethod
  557. def reqUpdateSerialStatus(cls, response):
  558. redis_obj = RedisObject()
  559. # 更新已使用序列号其他服务器的状态
  560. used_serial_redis_list = redis_obj.lrange(USED_SERIAL_REDIS_LIST, 0, -1) # 读取redis已使用序列号
  561. if used_serial_redis_list:
  562. LOGGER.info('---请求更新已使用序列号列表---used_serial_redis_list:{}'.format(used_serial_redis_list))
  563. used_serial_redis_list = [str(i, 'utf-8') for i in used_serial_redis_list]
  564. cls.do_request_function(used_serial_redis_list, 3)
  565. # 更新未使用序列号其他服务器的状态
  566. unused_serial_redis_list = redis_obj.lrange(UNUSED_SERIAL_REDIS_LIST, 0, -1) # 读取redis未使用序列号
  567. if unused_serial_redis_list:
  568. LOGGER.info('---请求更新未使用序列号列表---unused_serial_redis_list:{}'.format(unused_serial_redis_list))
  569. unused_serial_redis_list = [str(i, 'utf-8') for i in unused_serial_redis_list]
  570. cls.do_request_function(unused_serial_redis_list, 1)
  571. # 重置地区id
  572. reset_region_id_serial_redis_list = redis_obj.lrange(RESET_REGION_ID_SERIAL_REDIS_LIST, 0, -1) # 读取redis未使用序列号
  573. if reset_region_id_serial_redis_list:
  574. LOGGER.info('---请求重置地区id的序列号列表---:{}'.format(reset_region_id_serial_redis_list))
  575. reset_region_id_serial_redis_list = [str(i, 'utf-8') for i in reset_region_id_serial_redis_list]
  576. cls.do_request_reset_region_id(reset_region_id_serial_redis_list)
  577. return response.json(0)
  578. @staticmethod
  579. def do_request_function(serial_redis_list, status):
  580. """
  581. 请求更新序列号状态
  582. @param serial_redis_list: 序列号redis列表
  583. @param status: 状态, 1: 未使用, 3: 已占用
  584. """
  585. data = {
  586. 'serial_redis_list': str(serial_redis_list),
  587. 'status': status
  588. }
  589. # 确认域名列表
  590. orders_domain_name_list = CommonService.get_orders_domain_name_list()
  591. redis_obj = RedisObject()
  592. LOGGER.info('---请求更新序列号线程---data:{},orders_domain_name_list:{}'.format(data, orders_domain_name_list))
  593. try:
  594. requests_failed_flag = False # 请求失败标志位
  595. for domain_name in orders_domain_name_list:
  596. url = '{}cron/update/updateSerialStatus'.format(domain_name)
  597. response = requests.post(url=url, data=data, timeout=5)
  598. LOGGER.info('---请求更新序列号响应时间---:{}'.format(response.elapsed.total_seconds()))
  599. result = response.json()
  600. if result['result_code'] != 0: # 请求失败标志位置位
  601. requests_failed_flag = True
  602. break
  603. # 状态为未使用,重置扫码记录和美洲服的地区id
  604. if status == 1:
  605. # 扫码记录
  606. AppScannedSerial.objects.filter(serial__in=serial_redis_list).delete()
  607. # 地区id
  608. # 美洲服直接更新
  609. if CONFIG_INFO == CONFIG_US:
  610. DeviceDomainRegionModel.objects.filter(~Q(region_id=0), serial_number__in=serial_redis_list). \
  611. update(region_id=0)
  612. # 其他服请求到美洲服更新
  613. else:
  614. req_url = 'https://www.dvema.com/cron/update/reset-region-id'
  615. req_data = {
  616. 'serial_redis_list': str(serial_redis_list)
  617. }
  618. response = requests.post(url=req_url, data=req_data, timeout=5)
  619. LOGGER.info('---请求重置地区id响应时间---:{}'.format(response.elapsed.total_seconds()))
  620. result = response.json()
  621. if result['result_code'] != 0: # 请求失败标志位置位
  622. requests_failed_flag = True
  623. break
  624. if not requests_failed_flag: # 请求成功删除redis序列号
  625. if status == 1:
  626. for i in serial_redis_list:
  627. redis_obj.lrem(UNUSED_SERIAL_REDIS_LIST, 0, i)
  628. elif status == 3:
  629. for i in serial_redis_list:
  630. redis_obj.lrem(USED_SERIAL_REDIS_LIST, 0, i)
  631. except Exception as e:
  632. LOGGER.info('---更新序列号状态异常---:{}'.format(repr(e)))
  633. @staticmethod
  634. def do_request_reset_region_id(reset_region_id_serial_redis_list):
  635. """
  636. 请求重置地区id
  637. @param reset_region_id_serial_redis_list: 序列号redis列表
  638. """
  639. redis_obj = RedisObject()
  640. requests_failed_flag = False # 请求失败标志位
  641. data = {
  642. 'serial_redis_list': str(reset_region_id_serial_redis_list),
  643. }
  644. url = 'https://www.dvema.com/cron/update/reset-region-id'
  645. try:
  646. response = requests.post(url=url, data=data, timeout=5)
  647. result = response.json()
  648. if result['result_code'] != 0: # 请求失败标志位置位
  649. requests_failed_flag = True
  650. if not requests_failed_flag: # 请求成功删除redis序列号
  651. for serial in reset_region_id_serial_redis_list:
  652. redis_obj.lrem(RESET_REGION_ID_SERIAL_REDIS_LIST, 0, serial)
  653. except Exception as e:
  654. LOGGER.info('---请求重置地区id异常---:{}'.format(repr(e)))
  655. @staticmethod
  656. def updateSerialStatus(request_dict, response):
  657. """
  658. 更新序列号状态
  659. @param request_dict: 请求参数
  660. @request_dict serial_redis_list: 序列号redis列表
  661. @request_dict status: 状态, 1: 未使用, 3: 已占用
  662. @param response: 响应对象
  663. """
  664. serial_redis_list = request_dict.get('serial_redis_list', None)
  665. status = request_dict.get('status', None)
  666. LOGGER.info('---更新序列号状态参数---serial_redis_list:{},status:{}'.format(serial_redis_list, status))
  667. if not all([serial_redis_list, status]):
  668. return response.json(444)
  669. now_time = int(time.time())
  670. try:
  671. serial_redis_list = eval(serial_redis_list)
  672. CompanySerialModel.objects.filter(serial_number__in=serial_redis_list).update(status=int(status),
  673. update_time=now_time)
  674. uid_serial_qs = UIDCompanySerialModel.objects.filter(company_serial__serial_number__in=serial_redis_list)
  675. if uid_serial_qs:
  676. uid_list = list(uid_serial_qs.values_list('uid__uid', flat=True))
  677. serial_list = list(uid_serial_qs.values_list('company_serial__serial_number', flat=True))
  678. UIDModel.objects.filter(uid__in=uid_list).update(status=3, mac='', update_time=now_time)
  679. uid_serial_qs.delete()
  680. # 记录操作日志
  681. content = json.loads(json.dumps(request_dict))
  682. log = {
  683. 'ip': '127.0.0.1',
  684. 'user_id': 1,
  685. 'status': 200,
  686. 'time': now_time,
  687. 'content': json.dumps(content),
  688. 'url': 'cron/update/updateSerialStatus',
  689. 'operation': '序列号{}解绑uid: {}'.format(serial_list, uid_list),
  690. }
  691. LogModel.objects.create(**log)
  692. return response.json(0)
  693. except Exception as e:
  694. LOGGER.info('---更新序列号状态异常---:{}'.format(repr(e)))
  695. return response.json(500)
  696. @staticmethod
  697. def deleteUidData(request_dict, response):
  698. """
  699. 清除uid数据
  700. @param request_dict: 请求参数
  701. @request_dict serial_redis_list: 序列号redis列表
  702. @param response: 响应对象
  703. """
  704. serial_unbind_uid_qs = SerialUnbindUID.objects.filter(status=0).values('serial', 'uid')
  705. # 没有需要清除的数据直接返回
  706. if not serial_unbind_uid_qs.exists():
  707. return response.json(0)
  708. # 获取序列号,uid列表
  709. serial_list, uid_list = [], []
  710. for serial_unbind_uid in serial_unbind_uid_qs:
  711. serial_list.append(serial_unbind_uid['serial'])
  712. uid_list.append(serial_unbind_uid['uid'])
  713. now_time = int(time.time())
  714. redis_obj = RedisObject()
  715. try:
  716. with transaction.atomic():
  717. # 更新序列号解绑uid表状态
  718. serial_unbind_uid_qs.update(status=1, updated_time=now_time)
  719. # 更新序列号状态
  720. CompanySerialModel.objects.filter(serial_number__in=serial_list).update(status=1, update_time=now_time)
  721. UIDCompanySerialModel.objects.filter(company_serial__serial_number__in=serial_list).delete()
  722. # 删除设备相关数据,参考后台的设备重置删除的数据
  723. Device_Info.objects.filter(UID__in=uid_list).delete()
  724. UidSetModel.objects.filter(uid__in=uid_list).delete()
  725. UidUserModel.objects.filter(UID__in=uid_list).delete()
  726. UidPushModel.objects.filter(uid_set__uid__in=uid_list).delete()
  727. iotdeviceInfoModel.objects.filter(serial_number__in=serial_list).delete()
  728. # 删除推送消息
  729. EquipmentInfoService.delete_all_equipment_info(device_uid__in=uid_list)
  730. # 重置设备云存
  731. UID_Bucket.objects.filter(uid__in=uid_list).delete()
  732. Unused_Uid_Meal.objects.filter(uid__in=uid_list).delete()
  733. # Order_Model.objects.filter(UID__in=uid_list).delete()
  734. StsCrdModel.objects.filter(uid__in=uid_list).delete()
  735. VodHlsModel.objects.filter(uid__in=uid_list).delete()
  736. # 删除vod_hls分表数据
  737. split_vod_hls_obj = SplitVodHlsObject()
  738. split_vod_hls_obj.del_vod_hls_data(uid__in=uid_list)
  739. ExperienceContextModel.objects.filter(uid__in=uid_list).delete()
  740. # 重置AI
  741. ExperienceAiModel.objects.filter(uid__in=uid_list).delete()
  742. AiService.objects.filter(uid__in=uid_list).delete()
  743. # 写入未使用序列号redis列表
  744. redis_obj.rpush_list(UNUSED_SERIAL_REDIS_LIST, serial_list)
  745. # 重置region_id,不为美洲服,则写入redis列表
  746. if CONFIG_INFO == CONFIG_US:
  747. DeviceDomainRegionModel.objects.filter(serial_number__in=serial_list).update(region_id=0)
  748. else:
  749. redis_obj.rpush_list(RESET_REGION_ID_SERIAL_REDIS_LIST, serial_list)
  750. # 重置已使用的uid的使用状态为未使用,更新时间
  751. UIDModel.objects.filter(uid__in=uid_list, status=2).update(status=0, mac='', update_time=now_time)
  752. # 重置扫码记录
  753. AppScannedSerial.objects.filter(serial__in=serial_list).delete()
  754. # 记录操作日志
  755. end_time = int(time.time())
  756. log = {
  757. 'user_id': 1,
  758. 'status': 200,
  759. 'time': now_time,
  760. 'url': 'cron/update/deleteUidData',
  761. 'operation': '已解绑序列号{}清除uid{}成功,执行时间{}秒'.format(serial_list, uid_list, end_time-now_time)
  762. }
  763. LogModel.objects.create(**log)
  764. return response.json(0)
  765. except Exception as e:
  766. # 记录操作日志
  767. log = {
  768. 'user_id': 1,
  769. 'status': 200,
  770. 'time': now_time,
  771. 'url': 'cron/update/deleteUidData',
  772. 'operation': '已解绑序列号{}清除uid{}异常:{}'.format(serial_list, uid_list, repr(e))
  773. }
  774. LogModel.objects.create(**log)
  775. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  776. @staticmethod
  777. def reset_region_id(request_dict, response):
  778. """
  779. 重置地区id
  780. @param request_dict: 请求参数
  781. @request_dict serial_redis_list: 序列号redis列表
  782. @param response: 响应对象
  783. """
  784. serial_redis_list = request_dict.get('serial_redis_list', None)
  785. LOGGER.info('---重置地区id参数---serial_redis_list:{}'.format(serial_redis_list))
  786. if not serial_redis_list:
  787. return response.json(444)
  788. try:
  789. serial_redis_list = eval(serial_redis_list)
  790. DeviceDomainRegionModel.objects.filter(serial_number__in=serial_redis_list).update(region_id=0)
  791. return response.json(0)
  792. except Exception as e:
  793. LOGGER.info('---重置地区id异常---:{}'.format(repr(e)))
  794. return response.json(500)
  795. @staticmethod
  796. def update_vod_meal(request_dict, response):
  797. """
  798. 定时修改体验套餐有效期为1个月
  799. @param request_dict: 请求参数
  800. @param response: 响应对象
  801. """
  802. try:
  803. Store_Meal.objects.filter(is_show=0, expire=12, pixel_level=0).update(price='39.99',
  804. virtual_price='56.6',
  805. sort=1)
  806. Store_Meal.objects.filter(is_show=0, cycle_config_id=1, pixel_level=0).update(price='3.65',
  807. virtual_price='5.66',
  808. sort=2)
  809. Store_Meal.objects.filter(id=12).update(price='3.99', virtual_price='5.66', sort=3)
  810. Store_Meal.objects.filter(id__in=(16, 17, 18)).update(is_show=0)
  811. return response.json(0)
  812. except Exception as e:
  813. LOGGER.info('---修改云存套餐内容异常---:{}'.format(repr(e)))
  814. return response.json(500)
  815. @staticmethod
  816. def check_customized_push(response):
  817. try:
  818. now_time = int(time.time())
  819. # 查询推送时间小于当前时间且推送状态为待推送的数据
  820. customized_push_qs = CustomizedPush.objects.filter(push_timestamp__lt=now_time, push_satus=0).values('id')
  821. if customized_push_qs.exists():
  822. for customized_push in customized_push_qs:
  823. customized_push_id = customized_push['id']
  824. data = {'customized_push_id': customized_push_id}
  825. url = DETECT_PUSH_DOMAINS + 'customized_push/start'
  826. req = requests.post(url=url, data=data, timeout=8)
  827. return response.json(0)
  828. except Exception as e:
  829. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  830. class CronCollectDataView(View):
  831. def get(self, request, *args, **kwargs):
  832. request.encoding = 'utf-8'
  833. operation = kwargs.get('operation')
  834. return self.validation(request.GET, request, operation)
  835. def post(self, request, *args, **kwargs):
  836. request.encoding = 'utf-8'
  837. operation = kwargs.get('operation')
  838. return self.validation(request.POST, request, operation)
  839. def validation(self, request_dict, request, operation):
  840. response = ResponseObject()
  841. if operation == 'collectPlayBack': # 定时保存云存视频回放
  842. return self.collect_play_back(response)
  843. elif operation == 'collectDeviceUser': # 定时保存用户数据
  844. return self.collect_device_user(response)
  845. elif operation == 'collectActivityUser': # 定时保存用户数据
  846. return self.collect_activity_user(response)
  847. elif operation == 'collectOrder': # 定时保存订单数据
  848. return self.collect_order(response)
  849. elif operation == 'collectDeviceInfo': # 定时保存设备数据
  850. return self.collect_device_info(response)
  851. elif operation == 'collectFlowInfo': # 定时保存设备数据
  852. return self.collect_flow_info(response)
  853. elif operation == 'collectOperatingCosts': # 定时运营成本
  854. return self.collect_operating_costs(response)
  855. elif operation == 'collectObjSize': # 定时设备s3存储量
  856. return self.collect_obj_size(response)
  857. elif operation == 'checkRequest': # 定时检查各个服的https请求
  858. return self.check_request(response)
  859. else:
  860. return response.json(404)
  861. @staticmethod
  862. def collect_play_back(response):
  863. try:
  864. now_time = int(time.time())
  865. today = datetime.datetime.today()
  866. start_time = datetime.datetime(today.year, today.month, today.day)
  867. end_time = start_time + datetime.timedelta(days=1)
  868. start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  869. end_time = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  870. this_month_str = datetime.datetime(today.year, today.month, 1)
  871. this_month_stamp = CommonService.str_to_timestamp(this_month_str.strftime('%Y-%m-%d %H:%M:%S'))
  872. video_play_back_time_qs = VideoPlaybackTimeModel.objects.filter(startTime__gte=start_time,
  873. startTime__lt=end_time,
  874. playMode='cloud').values('uid').annotate(
  875. play_duration=Sum('duration'), play_frequency=Count('uid'))
  876. with transaction.atomic():
  877. for item in video_play_back_time_qs:
  878. vod_hls_summary_qs = VodHlsSummary.objects.filter(uid=item['uid'], time=this_month_stamp)
  879. if vod_hls_summary_qs.exists():
  880. vod_hls_summary = vod_hls_summary_qs.first()
  881. vod_hls_summary.play_duration += item['play_duration']
  882. vod_hls_summary.play_frequency += 1
  883. vod_hls_summary.updated_time = now_time
  884. vod_hls_summary.save()
  885. else:
  886. VodHlsSummary.objects.create(uid=item['uid'], time=this_month_stamp, created_time=now_time,
  887. play_duration=item['play_duration'], play_frequency=1,
  888. updated_time=now_time)
  889. return response.json(0)
  890. except Exception as e:
  891. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  892. @staticmethod
  893. def collect_device_user(response):
  894. try:
  895. created_time = int(time.time())
  896. today = datetime.datetime.today()
  897. start_time = datetime.datetime(today.year, today.month, today.day)
  898. end_time = start_time + datetime.timedelta(days=1)
  899. increase_user_qs = Device_User.objects.filter(data_joined__year=today.year, data_joined__month=today.month,
  900. data_joined__day=today.day).values('region_country')
  901. start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  902. end_time = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  903. active_user_qs = UserExModel.objects.filter(updTime__gte=start_time, updTime__lt=end_time).values(
  904. 'userID__region_country')
  905. country_qs = CountryModel.objects.all().values('id', 'region__name', 'country_name')
  906. country_dict = {}
  907. continent_dict = {}
  908. for item in country_qs:
  909. country_dict[item['id']] = item['country_name']
  910. continent_dict[item['country_name']] = item['region__name']
  911. with transaction.atomic():
  912. if increase_user_qs.exists():
  913. increase_user_count = increase_user_qs.count()
  914. increase_user_country_list = increase_user_qs.values('region_country').annotate(
  915. count=Count('region_country')).order_by('count')
  916. increase_user_country_dict = {}
  917. increase_user_continent_dict = {}
  918. for item in increase_user_country_list:
  919. country_name = country_dict.get(item['region_country'], '未知国家')
  920. continent_name = continent_dict.get(country_name, '未知大洲')
  921. increase_user_country_dict[country_name] = item['count']
  922. if continent_name not in increase_user_continent_dict:
  923. increase_user_continent_dict[continent_name] = 0
  924. increase_user_continent_dict[continent_name] += item['count']
  925. DeviceUserSummary.objects.create(time=start_time, count=increase_user_count,
  926. country=increase_user_country_dict, created_time=created_time,
  927. continent=increase_user_continent_dict)
  928. if active_user_qs.exists():
  929. active_user_count = active_user_qs.count()
  930. active_user_country_list = active_user_qs.values('userID__region_country').annotate(
  931. count=Count('userID__region_country')).order_by('count')
  932. active_user_country_dict = {}
  933. active_user_continent_dict = {}
  934. for item in active_user_country_list:
  935. country_name = country_dict.get(item['userID__region_country'], '未知国家')
  936. continent_name = continent_dict.get(country_name, '未知大洲')
  937. active_user_country_dict[country_name] = item['count']
  938. if continent_name not in active_user_continent_dict:
  939. active_user_continent_dict[continent_name] = 0
  940. active_user_continent_dict[continent_name] += item['count']
  941. user_qs = DeviceUserSummary.objects.filter(time=start_time, query_type=1)
  942. if user_qs.exists():
  943. user_qs.update(count=active_user_count, country=active_user_country_dict,
  944. continent=active_user_continent_dict)
  945. else:
  946. DeviceUserSummary.objects.create(time=start_time, query_type=1, count=active_user_count,
  947. country=active_user_country_dict, created_time=created_time,
  948. continent=active_user_continent_dict)
  949. return response.json(0)
  950. except Exception as e:
  951. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  952. @staticmethod
  953. def collect_order(response):
  954. try:
  955. created_time = int(time.time())
  956. today = datetime.datetime.today()
  957. start_time = datetime.datetime(today.year, today.month, today.day)
  958. end_time = start_time + datetime.timedelta(days=1)
  959. start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  960. end_time = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  961. order_qs = Order_Model.objects.filter(addTime__gte=start_time, addTime__lt=end_time,
  962. status=1).values('UID', 'order_type',
  963. 'store_meal_name', 'price',
  964. 'addTime', 'currency').order_by(
  965. 'addTime')
  966. uid_list = []
  967. all_order_qs = Order_Model.objects.filter(addTime__lt=start_time, status=1).values('UID')
  968. for item in all_order_qs:
  969. if item['UID'] not in uid_list:
  970. uid_list.append(item['UID'])
  971. # 国家表数据
  972. country_qs = CountryModel.objects.values('id', 'country_name')
  973. country_dict = {}
  974. for item in country_qs:
  975. country_dict[item['id']] = item['country_name']
  976. # 设备类型数据
  977. device_type_qs = DeviceTypeModel.objects.values('name', 'type')
  978. device_type_dict = {}
  979. for item in device_type_qs:
  980. device_type_dict[item['type']] = item['name']
  981. with transaction.atomic():
  982. for item in order_qs:
  983. is_pay = 0
  984. price = float(item['price'])
  985. currency = item['currency']
  986. uid_set_qs = UidSetModel.objects.filter(uid=item['UID']).values('tb_country')
  987. country_id = uid_set_qs[0]['tb_country'] if uid_set_qs.exists() else 0
  988. country_name = country_dict.get(country_id, '未知国家')
  989. order_type = item['order_type']
  990. device_info_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
  991. device_type_id = device_info_qs[0]['Type'] if device_info_qs.exists() else 0
  992. device_type_name = device_type_dict.get(device_type_id, '未知设备')
  993. store_meal_name = item['store_meal_name']
  994. add_time_stamp = item['addTime']
  995. add_time_str = datetime.datetime.fromtimestamp(int(add_time_stamp))
  996. add_time_str = datetime.datetime(add_time_str.year, add_time_str.month, add_time_str.day)
  997. add_time_stamp = CommonService.str_to_timestamp(add_time_str.strftime('%Y-%m-%d %H:%M:%S'))
  998. if price == 0:
  999. is_pay = 1
  1000. order_summary_qs = OrdersSummary.objects.filter(time=add_time_stamp, query_type=1,
  1001. service_type=order_type)
  1002. else:
  1003. order_summary_qs = OrdersSummary.objects.filter(time=add_time_stamp, query_type=0,
  1004. service_type=order_type)
  1005. if item['UID'] not in uid_list:
  1006. pay_order_summary_qs = OrdersSummary.objects.filter(time=add_time_stamp, query_type=2,
  1007. service_type=order_type)
  1008. query_type = 2
  1009. else:
  1010. pay_order_summary_qs = OrdersSummary.objects.filter(time=add_time_stamp, query_type=3,
  1011. service_type=order_type)
  1012. query_type = 3
  1013. if pay_order_summary_qs.exists():
  1014. pay_order_summary = pay_order_summary_qs.first()
  1015. pay_order_summary.count += 1
  1016. temp_total = eval(pay_order_summary.total)
  1017. if currency not in temp_total:
  1018. temp_total[currency] = price
  1019. else:
  1020. temp_total[currency] = round(temp_total[currency] + price, 2)
  1021. pay_order_summary.total = temp_total
  1022. country_temp_dict = eval(pay_order_summary.country)
  1023. if country_name in country_temp_dict:
  1024. country_temp_dict[country_name]['数量'] += 1
  1025. if currency not in country_temp_dict[country_name]:
  1026. country_temp_dict[country_name][currency] = price
  1027. else:
  1028. country_temp_dict[country_name][currency] = round(
  1029. country_temp_dict[country_name][currency] + price, 2)
  1030. else:
  1031. country_temp_dict[country_name] = {'数量': 1, currency: price}
  1032. pay_order_summary.country = country_temp_dict
  1033. device_type_temp_dict = eval(pay_order_summary.device_type)
  1034. if device_type_name in device_type_temp_dict:
  1035. device_type_temp_dict[device_type_name]['数量'] += 1
  1036. if currency not in device_type_temp_dict[device_type_name]:
  1037. device_type_temp_dict[device_type_name][currency] = price
  1038. else:
  1039. device_type_temp_dict[device_type_name][currency] = round(
  1040. device_type_temp_dict[device_type_name][currency] + price, 2)
  1041. else:
  1042. device_type_temp_dict[device_type_name] = {'数量': 1, currency: price}
  1043. pay_order_summary.device_type = device_type_temp_dict
  1044. store_meal_temp_dict = eval(pay_order_summary.store_meal)
  1045. if store_meal_name in store_meal_temp_dict:
  1046. store_meal_temp_dict[store_meal_name]['数量'] += 1
  1047. if currency not in store_meal_temp_dict[store_meal_name]:
  1048. store_meal_temp_dict[store_meal_name][currency] = price
  1049. else:
  1050. store_meal_temp_dict[store_meal_name][currency] = round(
  1051. store_meal_temp_dict[store_meal_name][currency] + price, 2)
  1052. else:
  1053. store_meal_temp_dict[store_meal_name] = {'数量': 1, currency: price}
  1054. pay_order_summary.store_meal = store_meal_temp_dict
  1055. pay_order_summary.save()
  1056. else:
  1057. final_total = {currency: price}
  1058. country_temp_dict = {
  1059. country_name: {
  1060. '数量': 1,
  1061. currency: price
  1062. }
  1063. }
  1064. device_type_temp_dict = {
  1065. device_type_name: {
  1066. '数量': 1,
  1067. currency: price
  1068. }
  1069. }
  1070. store_meal_temp_dict = {
  1071. store_meal_name: {
  1072. '数量': 1,
  1073. currency: price
  1074. }
  1075. }
  1076. OrdersSummary.objects.create(time=add_time_stamp, count=1, query_type=query_type,
  1077. service_type=order_type, total=final_total,
  1078. country=country_temp_dict, created_time=created_time,
  1079. device_type=device_type_temp_dict,
  1080. store_meal=store_meal_temp_dict)
  1081. if order_summary_qs.exists():
  1082. order_summary = order_summary_qs.first()
  1083. order_summary.count += 1
  1084. temp_total = eval(order_summary.total)
  1085. if currency not in temp_total:
  1086. temp_total[currency] = price
  1087. else:
  1088. temp_total[currency] = round(temp_total[currency] + price, 2)
  1089. order_summary.total = temp_total
  1090. country_temp_dict = eval(order_summary.country)
  1091. if country_name in country_temp_dict:
  1092. if is_pay == 0:
  1093. country_temp_dict[country_name]['数量'] += 1
  1094. if currency not in country_temp_dict[country_name]:
  1095. country_temp_dict[country_name][currency] = price
  1096. else:
  1097. country_temp_dict[country_name][currency] = round(
  1098. country_temp_dict[country_name][currency] + price, 2)
  1099. else:
  1100. country_temp_dict[country_name] += 1
  1101. else:
  1102. if is_pay == 0:
  1103. country_temp_dict[country_name] = {'数量': 1, currency: price}
  1104. else:
  1105. country_temp_dict[country_name] = 1
  1106. order_summary.country = country_temp_dict
  1107. device_type_temp_dict = eval(order_summary.device_type)
  1108. if device_type_name in device_type_temp_dict:
  1109. if is_pay == 0:
  1110. device_type_temp_dict[device_type_name]['数量'] += 1
  1111. if currency not in device_type_temp_dict[device_type_name]:
  1112. device_type_temp_dict[device_type_name][currency] = price
  1113. else:
  1114. device_type_temp_dict[device_type_name][currency] = round(
  1115. device_type_temp_dict[device_type_name][currency] + price, 2)
  1116. else:
  1117. device_type_temp_dict[device_type_name] += 1
  1118. else:
  1119. if is_pay == 0:
  1120. device_type_temp_dict[device_type_name] = {'数量': 1, currency: price}
  1121. else:
  1122. device_type_temp_dict[device_type_name] = 1
  1123. order_summary.device_type = device_type_temp_dict
  1124. store_meal_temp_dict = eval(order_summary.store_meal)
  1125. if store_meal_name in store_meal_temp_dict:
  1126. if is_pay == 0:
  1127. store_meal_temp_dict[store_meal_name]['数量'] += 1
  1128. if currency not in store_meal_temp_dict[store_meal_name]:
  1129. store_meal_temp_dict[store_meal_name][currency] = price
  1130. else:
  1131. store_meal_temp_dict[store_meal_name][currency] = round(
  1132. store_meal_temp_dict[store_meal_name][currency] + price, 2)
  1133. else:
  1134. store_meal_temp_dict[store_meal_name] += 1
  1135. else:
  1136. if is_pay == 0:
  1137. store_meal_temp_dict[store_meal_name] = {'数量': 1, currency: price}
  1138. else:
  1139. store_meal_temp_dict[store_meal_name] = 1
  1140. order_summary.store_meal = store_meal_temp_dict
  1141. order_summary.save()
  1142. else:
  1143. final_total = {currency: price}
  1144. if is_pay == 0:
  1145. country_temp_dict = {
  1146. country_name: {
  1147. '数量': 1,
  1148. currency: price
  1149. }
  1150. }
  1151. device_type_temp_dict = {
  1152. device_type_name: {
  1153. '数量': 1,
  1154. currency: price
  1155. }
  1156. }
  1157. store_meal_temp_dict = {
  1158. store_meal_name: {
  1159. '数量': 1,
  1160. currency: price
  1161. }
  1162. }
  1163. else:
  1164. device_type_temp_dict = {
  1165. device_type_name: 1
  1166. }
  1167. store_meal_temp_dict = {
  1168. store_meal_name: 1
  1169. }
  1170. country_temp_dict = {
  1171. country_name: 1
  1172. }
  1173. OrdersSummary.objects.create(time=add_time_stamp, count=1, query_type=is_pay,
  1174. service_type=order_type, total=final_total,
  1175. country=country_temp_dict, created_time=created_time,
  1176. device_type=device_type_temp_dict, store_meal=store_meal_temp_dict)
  1177. return response.json(0)
  1178. except Exception as e:
  1179. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1180. @staticmethod
  1181. def collect_operating_costs(response):
  1182. try:
  1183. today = datetime.datetime.today()
  1184. end_time = datetime.datetime(today.year, today.month, today.day)
  1185. yesterday = end_time - datetime.timedelta(days=1)
  1186. start_time = datetime.datetime(yesterday.year, yesterday.month, 1)
  1187. start_time_stamp = int(start_time.timestamp())
  1188. end_time_stamp = int(end_time.timestamp())
  1189. thread = threading.Thread(target=CronCollectDataView.thread_collect_operating_costs,
  1190. args=(start_time_stamp, end_time_stamp, start_time, end_time))
  1191. thread.start() # 启动线程
  1192. return response.json(0)
  1193. except Exception as e:
  1194. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1195. @staticmethod
  1196. def thread_collect_operating_costs(start_time_stamp, end_time_stamp, start_time, end_time):
  1197. try:
  1198. create_time = int(time.time())
  1199. today_end_time = end_time_stamp + 86400
  1200. operating_costs_qs_1 = OperatingCosts.objects.filter(time=start_time_stamp).exclude(
  1201. created_time__gte=end_time_stamp, created_time__lt=today_end_time).values('order_id', 'end_time', 'uid')
  1202. operating_costs_qs_2 = OperatingCosts.objects.filter(time=start_time_stamp,
  1203. created_time__gte=end_time_stamp,
  1204. created_time__lt=today_end_time, start_time=0).values(
  1205. 'order_id', 'end_time', 'uid')
  1206. operating_costs_qs = operating_costs_qs_1.union(operating_costs_qs_2)
  1207. storage_univalence = 0.023 / 30
  1208. api_univalence = 0.005 / 1000
  1209. region = '国内' if CONFIG_INFO == CONFIG_CN else '国外'
  1210. country_qs = CountryModel.objects.values('id', 'country_name')
  1211. country_dict = {}
  1212. for item in country_qs:
  1213. country_dict[item['id']] = item['country_name']
  1214. for item in operating_costs_qs:
  1215. order_qs = Order_Model.objects.filter(orderID=item['order_id'], order_type__in=[0, 1]).values('price',
  1216. 'payTime',
  1217. 'rank__expire',
  1218. 'fee',
  1219. 'payType',
  1220. 'userID__region_country')
  1221. if order_qs.exists():
  1222. order = order_qs[0]
  1223. country_name = country_dict.get(order['userID__region_country'], '未知国家')
  1224. order_type = '云存'
  1225. expire = str(order_qs[0]['rank__expire']) + '个月'
  1226. price = float(order['price'])
  1227. if order['payType'] in [2, 3]:
  1228. fee = round(0.0054 * price, 2)
  1229. else:
  1230. fee = float(order['fee']) if order['fee'] else 0
  1231. order_start_time = int((datetime.datetime.fromtimestamp(item['end_time']) - relativedelta(
  1232. months=order['rank__expire'])).timestamp())
  1233. order_days = math.ceil((item['end_time'] - order_start_time) / 86400)
  1234. if item['end_time'] > end_time_stamp: # 订单结束时间大于统计时间
  1235. if order_start_time <= start_time_stamp: # 订单月初之前开始
  1236. settlement_days = (end_time - start_time).days # 当月结算天数=月初-月底
  1237. uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=start_time_stamp,
  1238. time__lte=end_time_stamp,
  1239. uid=item['uid'])
  1240. elif order_start_time >= end_time_stamp: # 订单在统计时间之后开始
  1241. settlement_days = 1
  1242. uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=end_time_stamp,
  1243. time__lt=order_start_time,
  1244. uid=item['uid'])
  1245. else: # 订单月初和统计时间之间开始
  1246. settlement_days = math.ceil((end_time_stamp - order_start_time) / 86400)
  1247. uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=order_start_time,
  1248. time__lte=end_time_stamp,
  1249. uid=item['uid'])
  1250. remaining_usage_time = math.ceil((item['end_time'] - end_time_stamp) / 86400) # 剩余使用时间
  1251. else: # 订单结束时间小于统计时间
  1252. if order_start_time <= start_time_stamp:
  1253. settlement_days = math.ceil((item['end_time'] - start_time_stamp) / 86400)
  1254. uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=start_time_stamp,
  1255. time__lt=item['end_time'],
  1256. uid=item['uid'])
  1257. else:
  1258. settlement_days = math.ceil((item['end_time'] - order_start_time) / 86400)
  1259. uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=order_start_time,
  1260. time__lt=item['end_time'],
  1261. uid=item['uid'])
  1262. remaining_usage_time = 0
  1263. day_average_price = round(price / order_days, 2) # 收入分摊/天
  1264. month_average_price = round(day_average_price * settlement_days, 2) # 收入分摊/月
  1265. monthly_income = round((price - fee) / order_days * settlement_days, 2) # 当月结算收入
  1266. real_income = round(price - fee, 2)
  1267. result = uid_bucket_statistics.aggregate(size=Sum('storage_size'), api_count=Sum('api_count'))
  1268. actual_storage = round(result['size'], 2) if result['size'] else 0
  1269. actual_api = result['api_count'] if result['api_count'] else 0
  1270. storage_cost = actual_storage / 1024 * storage_univalence * settlement_days
  1271. api_cost = actual_api * api_univalence
  1272. if CONFIG_INFO == CONFIG_CN: # 国内要换算汇率
  1273. storage_cost = storage_cost * 7
  1274. api_cost = api_cost * 7
  1275. profit = round(monthly_income - storage_cost - api_cost, 2) # 利润=月结算金额-月成本
  1276. storage_cost = round(storage_cost, 2)
  1277. api_cost = round(api_cost, 2)
  1278. if monthly_income == 0.0:
  1279. profit_margin = 0
  1280. else:
  1281. profit_margin = round(profit / month_average_price, 2) # 利润率=利润/每月收入分摊
  1282. OperatingCosts.objects.filter(time=start_time_stamp, order_id=item['order_id'],
  1283. uid=item['uid']).update(day_average_price=day_average_price,
  1284. month_average_price=month_average_price,
  1285. monthly_income=monthly_income,
  1286. actual_storage=actual_storage,
  1287. settlement_days=settlement_days,
  1288. actual_api=actual_api, fee=fee,
  1289. created_time=create_time, region=region,
  1290. start_time=order_start_time,
  1291. remaining_usage_time=remaining_usage_time,
  1292. storage_cost=storage_cost, api_cost=api_cost,
  1293. profit=profit, profit_margin=profit_margin,
  1294. real_income=real_income, price=price,
  1295. country_name=country_name,
  1296. order_type=order_type, expire=expire)
  1297. print('结束')
  1298. except Exception as e:
  1299. LOGGER.info(
  1300. 'thread_collect_operating_costs接口异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1301. @staticmethod
  1302. def collect_obj_size(response):
  1303. try:
  1304. today = datetime.datetime.today()
  1305. end_time = datetime.datetime(today.year, today.month, today.day)
  1306. start_time = end_time - datetime.timedelta(days=1)
  1307. first_date = datetime.datetime(start_time.year, start_time.month, 1)
  1308. start_time_stamp = int(start_time.timestamp())
  1309. end_time_stamp = int(end_time.timestamp())
  1310. first_date_stamp = int(first_date.timestamp())
  1311. thread = threading.Thread(target=CronCollectDataView.thread_collect_obj_size,
  1312. args=(start_time_stamp, end_time_stamp, first_date_stamp))
  1313. thread.start() # 启动线程
  1314. return response.json(0)
  1315. except Exception as e:
  1316. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1317. @staticmethod
  1318. def thread_collect_obj_size(start_time, end_time, first_date):
  1319. try:
  1320. creat_time = int(time.time())
  1321. uid_list = UidBucketStatistics.objects.filter(time=start_time).values_list('uid', flat=True)
  1322. uid_vod = UID_Bucket.objects.filter(Q(endTime__gte=start_time), ~Q(uid__in=uid_list)).values('uid',
  1323. 'bucket__bucket',
  1324. 'orderId',
  1325. 'channel',
  1326. 'endTime')
  1327. s3_obj = AmazonS3Util(ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME)
  1328. for item in uid_vod:
  1329. path = '{uid}/vod{channel}'.format(uid=item['uid'], channel=item['channel'])
  1330. s3_result = s3_obj.get_object_list(item['bucket__bucket'], path,
  1331. path + '/{}'.format(start_time), end_time)
  1332. actual_storage = 0
  1333. actual_api = 0
  1334. for obj in s3_result:
  1335. temp_time = int(obj['Key'].split('/')[2])
  1336. if temp_time < end_time:
  1337. actual_storage += obj['Size']
  1338. actual_api += 1
  1339. actual_storage = round(actual_storage / 1024 / 1024, 2)
  1340. with transaction.atomic():
  1341. if actual_api:
  1342. UidBucketStatistics.objects.create(uid=item['uid'], storage_size=actual_storage,
  1343. api_count=actual_api,
  1344. created_time=creat_time,
  1345. time=start_time)
  1346. operating_costs_qs = OperatingCosts.objects.filter(order_id=item['orderId'], uid=item['uid'],
  1347. time=first_date)
  1348. if not operating_costs_qs.exists():
  1349. OperatingCosts.objects.create(order_id=item['orderId'], uid=item['uid'],
  1350. created_time=creat_time, time=first_date,
  1351. end_time=item['endTime'])
  1352. print(actual_storage, actual_api)
  1353. print('结束')
  1354. except Exception as e:
  1355. LOGGER.info('统计s3信息异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1356. @classmethod
  1357. def check_request(cls, response):
  1358. domain_name_list = [
  1359. 'test.zositechc.cn', 'test.push.zositechc.cn',
  1360. 'www.zositechc.cn', 'api.loocam2.cn', 'common.neutral2.cn', 'api.aiotserver.cn', 'push.zositechc.cn',
  1361. 'www.dvema.com', 'api.zositech2.com', 'api.loocam2.com', 'common.neutral2.com', '149.neutral2.com',
  1362. '365.neutral2.com', 'push.dvema.com',
  1363. 'www.zositeche.com', 'api.zositeche.com', 'api.loocam3.com', 'common.neutral3.com',
  1364. 'push.zositeche.com',
  1365. 'www.zositech.xyz', 'smart.loocam2.com'
  1366. ]
  1367. for domain_name in domain_name_list:
  1368. thread = threading.Thread(target=cls.initial_request, args=(domain_name,))
  1369. thread.start()
  1370. return response.json(0)
  1371. @staticmethod
  1372. def initial_request(domain_name):
  1373. url = 'https://{}/init/health-check'.format(domain_name)
  1374. try:
  1375. requests.post(url=url, timeout=30)
  1376. except Exception as e:
  1377. email_content = 'https请求域名{}出现异常!error_msg:{}'.format(domain_name, repr(e))
  1378. S3Email().faEmail(email_content, 'servers@ansjer.com')
  1379. @staticmethod
  1380. def collect_device_info(response):
  1381. try:
  1382. created_time = int(time.time())
  1383. today = datetime.datetime.today()
  1384. start_time = datetime.datetime(today.year, today.month, today.day)
  1385. end_time = start_time + datetime.timedelta(days=1)
  1386. start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  1387. end_time = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  1388. increase_device_qs = UidSetModel.objects.filter(addTime__gte=start_time, addTime__lt=end_time).values(
  1389. 'tb_country',
  1390. 'uid',
  1391. 'device_type',
  1392. 'cloud_vod',
  1393. 'is_ai',
  1394. 'mobile_4g',
  1395. 'addTime')
  1396. video_play_back_time_qs = VideoPlaybackTimeModel.objects.filter(startTime__gte=start_time,
  1397. startTime__lt=end_time).values('uid')
  1398. active_device_qs = UidSetModel.objects.filter(uid__in=video_play_back_time_qs).values('tb_country',
  1399. 'addTime',
  1400. 'device_type',
  1401. 'cloud_vod',
  1402. 'is_ai',
  1403. 'mobile_4g',
  1404. 'uid')
  1405. increase_device_count = increase_device_qs.count()
  1406. active_device_count = active_device_qs.count()
  1407. # 国家表数据
  1408. country_qs = CountryModel.objects.values('id', 'country_name', 'region__name')
  1409. country_dict = {}
  1410. continent_dict = {}
  1411. for item in country_qs:
  1412. country_dict[item['id']] = item['country_name']
  1413. continent_dict[item['country_name']] = item['region__name']
  1414. # 设备类型数据
  1415. device_type_qs = DeviceTypeModel.objects.values('name', 'type')
  1416. device_type_dict = {}
  1417. for item in device_type_qs:
  1418. device_type_dict[item['type']] = item['name']
  1419. with transaction.atomic():
  1420. if increase_device_qs.exists():
  1421. # 国家大洲设备数据
  1422. increase_device_country_list = increase_device_qs.values('tb_country').annotate(
  1423. count=Count('tb_country')).order_by('count')
  1424. increase_device_country_dict = {}
  1425. increase_device_continent_dict = {}
  1426. for item in increase_device_country_list:
  1427. country_name = country_dict.get(item['tb_country'], '未知国家')
  1428. continent_name = continent_dict.get(country_name, '未知大洲')
  1429. increase_device_country_dict[country_name] = item['count']
  1430. if continent_name not in increase_device_continent_dict:
  1431. increase_device_continent_dict[continent_name] = 0
  1432. increase_device_continent_dict[continent_name] += item['count']
  1433. # 设备类型数据
  1434. increase_device_type_list = increase_device_qs.values('device_type').annotate(
  1435. count=Count('device_type')).order_by('count')
  1436. increase_device_type_dict = {}
  1437. for item in increase_device_type_list:
  1438. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1439. increase_device_type_dict[type_name] = item['count']
  1440. # 云存设备类型数据
  1441. increase_device_vod_list = increase_device_qs.filter(~Q(cloud_vod=2)).values(
  1442. 'device_type').annotate(
  1443. count=Count('device_type')).order_by('count')
  1444. increase_device_vod_dict = {}
  1445. for item in increase_device_vod_list:
  1446. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1447. increase_device_vod_dict[type_name] = item['count']
  1448. # AI设备类型数据
  1449. increase_device_ai_list = increase_device_qs.filter(~Q(is_ai=2)).values('device_type').annotate(
  1450. count=Count('device_type')).order_by('count')
  1451. increase_device_ai_dict = {}
  1452. for item in increase_device_ai_list:
  1453. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1454. increase_device_ai_dict[type_name] = item['count']
  1455. # 联通设备类型数据
  1456. increase_device_unicom_list = increase_device_qs.filter(~Q(mobile_4g=2)).values(
  1457. 'device_type').annotate(
  1458. count=Count('device_type')).order_by('count')
  1459. increase_device_unicom_dict = {}
  1460. for item in increase_device_unicom_list:
  1461. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1462. increase_device_unicom_dict[type_name] = item['count']
  1463. DeviceInfoSummary.objects.create(time=start_time, count=increase_device_count,
  1464. query_type=0, created_time=created_time,
  1465. country=increase_device_country_dict,
  1466. continent=increase_device_continent_dict,
  1467. vod_service=increase_device_vod_dict,
  1468. ai_service=increase_device_ai_dict,
  1469. unicom_service=increase_device_unicom_dict,
  1470. device_type=increase_device_type_dict)
  1471. if active_device_qs.exists():
  1472. # 国家大洲设备数据
  1473. active_device_country_list = active_device_qs.values('tb_country').annotate(
  1474. count=Count('tb_country')).order_by('count')
  1475. active_device_country_dict = {}
  1476. active_device_continent_dict = {}
  1477. for item in active_device_country_list:
  1478. country_name = country_dict.get(item['tb_country'], '未知国家')
  1479. continent_name = continent_dict.get(country_name, '未知大洲')
  1480. active_device_country_dict[country_name] = item['count']
  1481. if continent_name not in active_device_continent_dict:
  1482. active_device_continent_dict[continent_name] = 0
  1483. active_device_continent_dict[continent_name] += item['count']
  1484. # 设备类型数据
  1485. active_device_type_list = active_device_qs.values('device_type').annotate(
  1486. count=Count('device_type')).order_by('count')
  1487. active_device_type_dict = {}
  1488. for item in active_device_type_list:
  1489. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1490. active_device_type_dict[type_name] = item['count']
  1491. # 云存设备类型数据
  1492. active_device_vod_list = active_device_qs.filter(~Q(cloud_vod=2)).values('device_type').annotate(
  1493. count=Count('device_type')).order_by('count')
  1494. active_device_vod_dict = {}
  1495. for item in active_device_vod_list:
  1496. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1497. active_device_vod_dict[type_name] = item['count']
  1498. # AI设备类型数据
  1499. active_device_ai_list = active_device_qs.filter(~Q(is_ai=2)).values('device_type').annotate(
  1500. count=Count('device_type')).order_by('count')
  1501. active_device_ai_dict = {}
  1502. for item in active_device_ai_list:
  1503. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1504. active_device_ai_dict[type_name] = item['count']
  1505. # 联通设备类型数据
  1506. active_device_unicom_list = active_device_qs.filter(~Q(mobile_4g=2)).values('device_type').annotate(
  1507. count=Count('device_type')).order_by('count')
  1508. active_device_unicom_dict = {}
  1509. for item in active_device_unicom_list:
  1510. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1511. active_device_unicom_dict[type_name] = item['count']
  1512. DeviceInfoSummary.objects.create(time=start_time, count=active_device_count,
  1513. query_type=1, created_time=created_time,
  1514. country=active_device_country_dict,
  1515. continent=active_device_continent_dict,
  1516. vod_service=active_device_vod_dict,
  1517. ai_service=active_device_ai_dict,
  1518. unicom_service=active_device_unicom_dict,
  1519. device_type=active_device_type_dict)
  1520. return response.json(0)
  1521. except Exception as e:
  1522. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1523. @staticmethod
  1524. def collect_activity_user(response):
  1525. try:
  1526. created_time = int(time.time())
  1527. today = datetime.datetime.today()
  1528. start_time = datetime.datetime(today.year, today.month, today.day)
  1529. end_time = start_time + datetime.timedelta(days=1)
  1530. start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
  1531. end_time = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
  1532. thread = threading.Thread(target=CronCollectDataView.thread_collect_activity_user,
  1533. args=(start_time, end_time, created_time))
  1534. thread.start() # 启动线程
  1535. return response.json(0)
  1536. except Exception as e:
  1537. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1538. @staticmethod
  1539. def thread_collect_activity_user(start_time, end_time, created_time):
  1540. try:
  1541. active_uid = UserExModel.objects.filter(updTime__gte=start_time, updTime__lt=end_time).values_list(
  1542. 'userID__device_info__UID', flat=True).distinct()
  1543. active_device_qs = UidSetModel.objects.filter(uid__in=active_uid).values('addTime', 'device_type', 'ucode')
  1544. # 设备类型数据
  1545. device_type_qs = DeviceTypeModel.objects.values('name', 'type')
  1546. device_type_dict = {}
  1547. for item in device_type_qs:
  1548. device_type_dict[item['type']] = item['name']
  1549. with transaction.atomic():
  1550. if active_device_qs.exists():
  1551. # 按设备设备类型
  1552. active_device_type_list = active_device_qs.values('device_type').annotate(
  1553. count=Count('device_type')).order_by('count')
  1554. active_device_type_dict = {}
  1555. for item in active_device_type_list:
  1556. type_name = device_type_dict.get(item['device_type'], '未知设备类型')
  1557. active_device_type_dict[type_name] = item['count']
  1558. # 按ucode分类
  1559. active_ucode_list = active_device_qs.values('ucode').annotate(count=Count('ucode')).order_by('count')
  1560. active_ucode_dict = {}
  1561. for item in active_ucode_list:
  1562. ucode = item['ucode']
  1563. if item['ucode'] == '':
  1564. ucode = '未知ucode'
  1565. active_ucode_dict[ucode] = item['count']
  1566. user_qs = DeviceUserSummary.objects.filter(time=start_time, query_type=1)
  1567. if user_qs.exists():
  1568. user_qs.update(device_type=active_device_type_dict, ucode=active_ucode_dict)
  1569. else:
  1570. DeviceUserSummary.objects.create(time=start_time, query_type=1, created_time=created_time,
  1571. device_type=active_device_type_dict, ucode=active_ucode_dict)
  1572. except Exception as e:
  1573. LOGGER.info(
  1574. 'thread_collect_activity_user接口异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno,
  1575. repr(e)))
  1576. @staticmethod
  1577. def collect_flow_info(response):
  1578. try:
  1579. unicom_qs = UnicomDeviceInfo.objects.filter(card_type=0).values('iccid').distinct().order_by('iccid')
  1580. asy = threading.Thread(target=CronCollectDataView.thread_collect_flow, args=(unicom_qs,))
  1581. asy.start()
  1582. return response.json(0)
  1583. except Exception as e:
  1584. return response.json(500, repr(e))
  1585. @staticmethod
  1586. def thread_collect_flow(qs):
  1587. try:
  1588. unicom_api = UnicomObjeect()
  1589. redis_obj = RedisObject()
  1590. for item in qs:
  1591. res = unicom_api.query_device_usage_history(**item)
  1592. if res.status_code == 200:
  1593. res_json = res.json()
  1594. if res_json['code'] == 0:
  1595. redis_dict = {}
  1596. for data in res_json['data']['deviceUsageHistory']:
  1597. year = data.get('year', None)
  1598. month = data.get('month', None)
  1599. flow = data.get('flowTotalUsage', None)
  1600. if not all([year, month, flow]):
  1601. continue
  1602. file = str(year) + '-' + str(month)
  1603. redis_dict[file] = flow
  1604. key = 'monthly_flow_' + item['iccid']
  1605. if redis_dict:
  1606. redis_obj.set_hash_data(key, redis_dict)
  1607. except Exception as e:
  1608. LOGGER.info('统计联通流量失败,时间为:{}'.format(int(time.time())))
  1609. class CronComparedDataView(View):
  1610. def get(self, request, *args, **kwargs):
  1611. request.encoding = 'utf-8'
  1612. operation = kwargs.get('operation')
  1613. return self.validation(request.GET, request, operation)
  1614. def post(self, request, *args, **kwargs):
  1615. request.encoding = 'utf-8'
  1616. operation = kwargs.get('operation')
  1617. return self.validation(request.POST, request, operation)
  1618. def validation(self, request_dict, request, operation):
  1619. response = ResponseObject()
  1620. if operation == 'PaypalOrder': # 定时对比paypal订单
  1621. return self.compared_paypal_order(request_dict, response)
  1622. elif operation == 'WechatOrder': # 定时对比微信订单
  1623. return self.compared_wechat_order(response)
  1624. elif operation == 'AlipayOrder': # 定时对比阿里订单
  1625. return self.compared_alipay_order(response)
  1626. elif operation == 'AnsjerOrder': # 定时对比后台订单
  1627. return self.compared_ansjer_order(request_dict, response)
  1628. else:
  1629. return response.json(404)
  1630. @staticmethod
  1631. def compared_paypal_order(request_dict, response):
  1632. time_stamp = request_dict.get('time', None)
  1633. if time_stamp:
  1634. end_date = datetime.datetime.fromtimestamp(int(time_stamp))
  1635. start_date = end_date - datetime.timedelta(days=1)
  1636. else:
  1637. today = datetime.datetime.today()
  1638. start_date = today - datetime.timedelta(days=2)
  1639. start_date = datetime.datetime(start_date.year, start_date.month, start_date.day)
  1640. end_date = start_date + datetime.timedelta(days=1)
  1641. try:
  1642. zosi_paypal_api = paypalrestsdk.Api(PAYPAL_CRD['Zosi'])
  1643. vsees_paypal_api = paypalrestsdk.Api(PAYPAL_CRD['Vsees'])
  1644. paypal_url = 'v1/reporting/transactions?start_date={}-{}-{}T08:00:00-0800&end_date={}-{}-{}T08:00:00-0800&fields=all&page_size=500&page=1&transaction_status=S'.format(
  1645. start_date.year, start_date.month, start_date.day, end_date.year, end_date.month, end_date.day)
  1646. zosi_order_list = zosi_paypal_api.get(paypal_url)
  1647. vsees_order_list = vsees_paypal_api.get(paypal_url)
  1648. order_list = zosi_order_list['transaction_details'] + vsees_order_list['transaction_details']
  1649. LOGGER.info('每日对账PayPal订单列表:{}'.format(order_list))
  1650. # data = (
  1651. # ('start_date', '{}-{}-{}T08:00:00-0800'.format(start_date.year, start_date.month, start_date.day)),
  1652. # ('end_date', '{}-{}-{}T08:00:00-0800'.format(end_date.year, end_date.month, end_date.day)),
  1653. # ('fields', 'all'),
  1654. # ('page_size', '500'),
  1655. # ('page', '1'),
  1656. # ('transaction_status', 'S')
  1657. # )
  1658. # order_list = PayPalService(PAYPAL_CRD['client_id'], PAYPAL_CRD['client_secret']).get_transactions(data)
  1659. thread = threading.Thread(target=CronComparedDataView.thread_compared_paypal_order,
  1660. args=(order_list, end_date))
  1661. thread.start() # 启动线程
  1662. return response.json(0)
  1663. except Exception as e:
  1664. LOGGER.info('CronComparedDataView.compared_paypal_order, errLine:{}, errMsg:{}'.format(
  1665. e.__traceback__.tb_lineno, repr(e)))
  1666. return response.json(500)
  1667. @staticmethod
  1668. def thread_compared_paypal_order(order_list, start_time):
  1669. try:
  1670. now_time = int(time.time())
  1671. timestamp = int(start_time.timestamp())
  1672. count = 0
  1673. total = 0
  1674. more_order_list = []
  1675. for item in order_list:
  1676. if 'custom_field' in item['transaction_info'] and item['transaction_info']['custom_field'] == 'Shopify':
  1677. continue
  1678. count += 1
  1679. total += float(item['transaction_info']['transaction_amount']['value'])
  1680. trade_no = item['transaction_info']['transaction_id']
  1681. if item['transaction_info']['transaction_event_code'] in ['T1106', 'T1107', 'T1202']: # 付款退款
  1682. trade_no = item['transaction_info']['paypal_reference_id']
  1683. transaction_subject = item['transaction_info'].get('transaction_subject', '')
  1684. agreement_id = item['transaction_info'].get('paypal_reference_id', '')
  1685. refund_order = False
  1686. if item['transaction_info']['transaction_event_code'] in ['T1106', 'T1107', 'T1201', 'T0114', 'T1108']:
  1687. agreement_id = ''
  1688. if item['transaction_info']['transaction_event_code'] in ['T0114']:
  1689. transaction_subject = '争议费'
  1690. elif item['transaction_info']['transaction_event_code'] in ['T1108']:
  1691. transaction_subject = 'Fee reversal'
  1692. else:
  1693. refund_order = True
  1694. transaction_subject = '退款费'
  1695. more_order_list.append(trade_no)
  1696. pay_time = int(datetime.datetime.strptime(item['transaction_info']['transaction_updated_date'],
  1697. "%Y-%m-%dT%H:%M:%S%z").timestamp())
  1698. order_qs = Order_Model.objects.filter(trade_no=trade_no, payType=1)
  1699. if not order_qs.exists():
  1700. order_dict = {
  1701. 'trade_no': trade_no,
  1702. 'agreement_id': agreement_id,
  1703. 'pay_time': pay_time,
  1704. 'username': item['payer_info'].get('email_address', ''),
  1705. 'price': item['transaction_info']['transaction_amount']['value'],
  1706. 'pay_type': 1,
  1707. 'upd_time': now_time,
  1708. 'status': 0,
  1709. 'meal_name': transaction_subject
  1710. }
  1711. if agreement_id:
  1712. order_dict['pay_type'] = 0
  1713. order_dict['meal_name'] = 'paypal_cycle'
  1714. order_dict['order_id'] = transaction_subject
  1715. params = {'trade_no': trade_no, 'pay_time': pay_time, 'refund_order': refund_order}
  1716. response = requests.get('https://www.zositeche.com/testApi/checkOrderExist', params=params)
  1717. if response.status_code != 200:
  1718. # 如果响应失败,记录在数据库
  1719. abnormal_qs = AbnormalOrder.objects.filter(trade_no=trade_no)
  1720. if not abnormal_qs.exists():
  1721. AbnormalOrder.objects.create(**order_dict)
  1722. continue
  1723. result = response.json()
  1724. if result['result_code'] != 0 or not result['result']['is_exist']:
  1725. # 如果响应结果为空,记录在数据库
  1726. abnormal_qs = AbnormalOrder.objects.filter(trade_no=trade_no)
  1727. if not abnormal_qs.exists():
  1728. AbnormalOrder.objects.create(**order_dict)
  1729. more_order_list.append(trade_no)
  1730. else:
  1731. if not refund_order:
  1732. order_qs.update(payTime=pay_time)
  1733. total = round(total, 2)
  1734. daily_reconciliation = DailyReconciliation.objects.filter(time=timestamp)
  1735. if daily_reconciliation.exists():
  1736. if daily_reconciliation.first().order_ids:
  1737. old_order_list = daily_reconciliation.first().order_ids.split(',')
  1738. more_order_list = list(set(old_order_list) | set(more_order_list))
  1739. order_ids = ','.join(set(more_order_list))
  1740. daily_reconciliation.update(paypal_num=count, paypal_total=total, upd_time=now_time,
  1741. order_ids=order_ids)
  1742. else:
  1743. order_ids = ','.join(set(more_order_list))
  1744. DailyReconciliation.objects.create(paypal_num=count, paypal_total=total, time=timestamp,
  1745. order_ids=order_ids, creat_time=now_time, upd_time=now_time)
  1746. except Exception as e:
  1747. LOGGER.info('paypal每日对账异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  1748. @staticmethod
  1749. def compared_wechat_order(response):
  1750. today = datetime.datetime.today()
  1751. start_date = today - datetime.timedelta(days=1)
  1752. start_date = start_date.strftime("%Y%m%d")
  1753. try:
  1754. order_list = WechatPayObject().download_bill(start_date)
  1755. thread = threading.Thread(target=CronComparedDataView.thread_compared_wechat_order,
  1756. args=(order_list,))
  1757. thread.start()
  1758. return response.json(0)
  1759. except Exception as e:
  1760. LOGGER.info('CronComparedDataView.compared_wechat_order, errLine:{}, errMsg:{}'.format(
  1761. e.__traceback__.tb_lineno, repr(e)))
  1762. return response.json(500)
  1763. @staticmethod
  1764. def thread_compared_wechat_order(order_list):
  1765. now_time = int(time.time())
  1766. for order in order_list:
  1767. if order['交易类型'] != '`APP':
  1768. continue
  1769. order_id = order['商户订单号'].replace('`', '')
  1770. order_qs = Order_Model.objects.filter(orderID=order_id)
  1771. if not order_qs.exists():
  1772. order_dict = {
  1773. 'trade_no': order['微信订单号'].replace('`', ''),
  1774. 'order_id': order_id,
  1775. 'pay_type': 3,
  1776. 'price': order['订单金额'].replace('`', ''),
  1777. 'pay_time': int(datetime.datetime.strptime(order['\ufeff交易时间'], "`%Y-%m-%d %H:%M:%S").timestamp()),
  1778. 'upd_time': now_time,
  1779. 'meal_name': order['商品名称'].replace('`', ''),
  1780. }
  1781. AbnormalOrder.objects.create(**order_dict)
  1782. @staticmethod
  1783. def compared_alipay_order(response):
  1784. today = datetime.datetime.today()
  1785. start_date = today - datetime.timedelta(days=1)
  1786. start_date = start_date.strftime("%Y-%m-%d")
  1787. try:
  1788. ali_pay_obj = AliPayObject()
  1789. alipay = ali_pay_obj.conf()
  1790. result = alipay.server_api(
  1791. api_name='alipay.data.dataservice.bill.downloadurl.query',
  1792. biz_content={'bill_type': 'trade',
  1793. 'bill_date': start_date,
  1794. }
  1795. )
  1796. res = requests.get(result['bill_download_url'])
  1797. zip_file = res.content
  1798. zip_data = io.BytesIO(zip_file)
  1799. data = []
  1800. with zipfile.ZipFile(zip_data, 'r') as zip_ref:
  1801. for file in zip_ref.namelist():
  1802. if '汇总' not in file.encode('cp437').decode('gbk'):
  1803. with zip_ref.open(file) as f:
  1804. reader = csv.reader(io.TextIOWrapper(f, 'gbk'))
  1805. for row in reader:
  1806. data.append(row)
  1807. key_list = data[4]
  1808. orders = data[5:-4]
  1809. order_list = []
  1810. for item in orders:
  1811. order_list.append(dict(zip(key_list, item)))
  1812. thread = threading.Thread(target=CronComparedDataView.thread_compared_alipay_order,
  1813. args=(order_list,))
  1814. thread.start()
  1815. return response.json(0)
  1816. except Exception as e:
  1817. LOGGER.info('CronComparedDataView.compared_alipay_order, errLine:{}, errMsg:{}'.format(
  1818. e.__traceback__.tb_lineno, repr(e)))
  1819. return response.json(500)
  1820. @staticmethod
  1821. def thread_compared_alipay_order(order_list):
  1822. now_time = int(time.time())
  1823. for order in order_list:
  1824. order_id = order['商户订单号'].replace('\t', '')
  1825. if len(order_id) != 20:
  1826. continue
  1827. order_qs = Order_Model.objects.filter(orderID=order_id)
  1828. if not order_qs.exists():
  1829. order_dict = {
  1830. 'trade_no': order['支付宝交易号'].replace('\t', ''),
  1831. 'order_id': order_id,
  1832. 'pay_type': 2,
  1833. 'price': order['订单金额(元)'].replace('\t', ''),
  1834. 'pay_time': int(datetime.datetime.strptime(order['完成时间'], "%Y-%m-%d %H:%M:%S").timestamp()),
  1835. 'upd_time': now_time,
  1836. 'meal_name': order['商品名称'].replace('\t', ''),
  1837. 'username': order['对方账户'].replace('\t', ''),
  1838. }
  1839. AbnormalOrder.objects.create(**order_dict)
  1840. @staticmethod
  1841. def compared_ansjer_order(request_dict, response):
  1842. start_date_stamp = request_dict.get('time', None)
  1843. end_time = request_dict.get('end_time', None)
  1844. if start_date_stamp:
  1845. start_date = datetime.datetime.fromtimestamp(int(start_date_stamp))
  1846. end_date = start_date + datetime.timedelta(days=1)
  1847. end_date_stamp = int(end_date.timestamp())
  1848. else:
  1849. today = datetime.datetime.today()
  1850. start_date = today - datetime.timedelta(days=1)
  1851. start_date = datetime.datetime(start_date.year, start_date.month, start_date.day)
  1852. end_date = datetime.datetime(today.year, today.month, today.day)
  1853. start_date_stamp = int(start_date.timestamp())
  1854. end_date_stamp = int(end_date.timestamp())
  1855. try:
  1856. if end_time:
  1857. end_date_stamp = end_time
  1858. order_qs = Order_Model.objects.filter(status__in=[1, 5, 6], payType=1, payTime__gte=start_date_stamp,
  1859. payTime__lt=end_date_stamp).values('trade_no', 'orderID', 'UID',
  1860. 'userID__username',
  1861. 'userID__NickName', 'channel',
  1862. 'desc', 'payType',
  1863. 'price', 'status',
  1864. 'refunded_amount', 'addTime',
  1865. 'updTime', 'app_type')
  1866. if CONFIG_INFO == CONFIG_EUR:
  1867. return response.json(0, list(order_qs))
  1868. thread = threading.Thread(target=CronComparedDataView.thread_compared_ansjer_order,
  1869. args=(list(order_qs), start_date))
  1870. thread.start() # 启动线程
  1871. return response.json(0)
  1872. except Exception as e:
  1873. LOGGER.info('CronComparedDataView.compared_ansjer_order, errLine:{}, errMsg:{}'.format(
  1874. e.__traceback__.tb_lineno, repr(e)))
  1875. return response.json(500)
  1876. @staticmethod
  1877. def thread_compared_ansjer_order(order_list, start_time):
  1878. while True:
  1879. response = requests.get('https://www.zositeche.com/cron/compared/AnsjerOrder',
  1880. params={'time': int(start_time.timestamp())})
  1881. if response.status_code == 200:
  1882. result = response.json()
  1883. if result['result_code'] == 0:
  1884. eur_order_list = result['result']
  1885. break
  1886. try:
  1887. begin_date = start_time - datetime.timedelta(days=15)
  1888. end_date = start_time + datetime.timedelta(days=15)
  1889. start_timestamp = int(start_time.timestamp())
  1890. now_time = int(time.time())
  1891. more_order_list = []
  1892. total = 0
  1893. all_order_list = order_list + eur_order_list
  1894. count = len(all_order_list)
  1895. zosi_paypal_api = paypalrestsdk.Api(PAYPAL_CRD['Zosi'])
  1896. vsees_paypal_api = paypalrestsdk.Api(PAYPAL_CRD['Vsees'])
  1897. for index, order in enumerate(all_order_list):
  1898. total += float(order['price'])
  1899. if not order['trade_no']:
  1900. more_order_list.append(order['orderID'])
  1901. continue
  1902. if all_order_list.index(order) != index:
  1903. more_order_list.append(order['orderID'])
  1904. continue
  1905. paypal_url = 'v1/reporting/transactions?start_date={}-{}-{}T00:00:00-0000&end_date={}-{}-{}T00:00:00-0000&transaction_id={}&fields=all&page_size=100&page=1'.format(
  1906. begin_date.year, begin_date.month, begin_date.day, end_date.year, end_date.month, end_date.day,
  1907. order['trade_no'])
  1908. if order['app_type'] == 1:
  1909. paypal_order_list = zosi_paypal_api.get(paypal_url)
  1910. elif order['app_type'] == 2:
  1911. paypal_order_list = vsees_paypal_api.get(paypal_url)
  1912. else:
  1913. continue
  1914. if not paypal_order_list['transaction_details']:
  1915. more_order_list.append(order['orderID'])
  1916. total = round(total, 2)
  1917. daily_reconciliation = DailyReconciliation.objects.filter(time=start_timestamp)
  1918. if daily_reconciliation.exists():
  1919. if daily_reconciliation.first().order_ids:
  1920. old_order_list = daily_reconciliation.first().order_ids.split(',')
  1921. more_order_list = list(set(old_order_list) | set(more_order_list))
  1922. order_ids = ','.join(set(more_order_list))
  1923. daily_reconciliation.update(ansjer_total=total, ansjer_num=count, order_ids=order_ids,
  1924. upd_time=now_time)
  1925. else:
  1926. order_ids = ','.join(more_order_list)
  1927. DailyReconciliation.objects.create(order_ids=order_ids, ansjer_total=total, ansjer_num=count,
  1928. time=start_timestamp, creat_time=now_time, upd_time=now_time)
  1929. except Exception as e:
  1930. LOGGER.info('后台每日对账异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))