TelecomService.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : TelecomService.py
  4. @Time : 2024/1/24 15:12
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import json
  10. import re
  11. import time
  12. from decimal import Decimal
  13. from Ansjer.config import LOGGER
  14. from Model.models import UnicomDeviceInfo, LogModel
  15. from Object.RedisObject import RedisObject
  16. from Object.TelecomObject import TelecomObject
  17. class TelecomService:
  18. @classmethod
  19. def query_total_usage_by_access_number(cls, iccid, key, expire=600):
  20. """
  21. 根据接入号码查询设备总流量使用量(实现缓存)
  22. @param iccid: 20位ICCID
  23. @param key: 缓存key
  24. @param expire: 失效时间
  25. @return: 查询流量结果
  26. """
  27. redis = RedisObject()
  28. sim_flow_used_total = redis.get_data(key)
  29. if sim_flow_used_total:
  30. return Decimal(sim_flow_used_total).quantize(Decimal('0.00'))
  31. else:
  32. # 查询SIM卡信息
  33. sim_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
  34. if not sim_qs:
  35. return None
  36. sim_vo = sim_qs.first()
  37. telecom = TelecomObject()
  38. # 查询电信当月流量用量
  39. data = telecom.query_total_usage_by_date(sim_vo.access_number)
  40. if data and data['SvcCont']['resultCode'] == '0' and 'dataCumulationTotal' in data['SvcCont']['result']:
  41. cycle_total = data['SvcCont']['result'].get('dataCumulationTotal')
  42. else:
  43. LOGGER.info(f'query_total_usage_by_access_number查询流量异常,iccid:{iccid}')
  44. return sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
  45. cycle_total = Decimal(cycle_total).quantize(Decimal('0.00'))
  46. n_time = int(time.time())
  47. # 判断数据库周期流量用量 是否大于API查询出来的周期用量 如果是则判定进入了下一个周期
  48. if sim_vo.sim_cycle_used_flow != 0 and sim_vo.sim_cycle_used_flow > cycle_total:
  49. sim_used_flow = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
  50. sim_qs.update(
  51. sim_used_flow=sim_used_flow,
  52. sim_cycle_used_flow=cycle_total,
  53. updated_time=n_time
  54. )
  55. # 队列用量历史总量 + 上一个周期流量 + 当前周期流量 = 总消耗流量
  56. sim_flow_used_total = sim_used_flow + cycle_total
  57. elif cycle_total > sim_vo.sim_cycle_used_flow: # API周期用量大于当前数据库用量则更新记录
  58. sim_qs.update(sim_cycle_used_flow=cycle_total, updated_time=n_time)
  59. # 队列用量历史总量 + 当前周期流量 = 总消耗流量
  60. sim_flow_used_total = sim_vo.sim_used_flow + cycle_total
  61. else:
  62. sim_flow_used_total = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
  63. # 缓存当月流量数据
  64. redis.CONN.setnx(key, str(sim_flow_used_total))
  65. redis.CONN.expire(key, expire)
  66. return sim_flow_used_total
  67. @classmethod
  68. def update_access_number_network(cls, access_number, action):
  69. """
  70. 根据接入号码修改卡网络状态
  71. @param access_number: 11位接入号码
  72. @param action: ADD:单独添加断网,DEL:单独恢复上网
  73. @return: True|False
  74. """
  75. try:
  76. result = TelecomObject().single_cut_net(access_number, action)
  77. url = 'update/access_number/network'
  78. ip = '127.0.0.1'
  79. params = {'access_number': access_number, 'action': action, 'result': result}
  80. if not result:
  81. explain = f'{access_number}单独断网或恢复异常'
  82. cls.create_operation_log(url, ip, params, explain)
  83. return None
  84. explain = f'{access_number}单独断网或恢复成功'
  85. cls.create_operation_log(url, ip, params, explain)
  86. return 'success'
  87. except Exception as e:
  88. LOGGER.info('***{} TelecomService.update_access_number_network:errLine:{}, errMsg:{}'
  89. .format(access_number, e.__traceback__.tb_lineno, repr(e)))
  90. return None
  91. @classmethod
  92. def create_operation_log(cls, url, ip, request_dict, describe):
  93. """
  94. 创建操作日志
  95. @param url: 请求路径
  96. @param describe: 描述
  97. @param ip: 当前IP
  98. @param request_dict: 请求参数
  99. @return: True | False
  100. """
  101. try:
  102. # 记录操作日志
  103. content = json.loads(json.dumps(request_dict))
  104. log = {
  105. 'ip': ip,
  106. 'user_id': 1,
  107. 'status': 200,
  108. 'time': int(time.time()),
  109. 'content': json.dumps(content),
  110. 'url': url,
  111. 'operation': describe,
  112. }
  113. LogModel.objects.create(**log)
  114. return True
  115. except Exception as e:
  116. print('日志异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  117. return False
  118. @classmethod
  119. def get_access_number_network_status(cls, access_number):
  120. """
  121. 根据access_number查询是否单独断网
  122. @param access_number: 11位接入号码
  123. @return: 当前网络数据
  124. """
  125. try:
  126. result = TelecomObject().query_product_information(access_number)
  127. if not result:
  128. return None
  129. if result and result['result_code']:
  130. # 获取产品信息
  131. prod_infos = result["result"]["SvcCont"]["result"]["prodInfos"]
  132. # 获取 funProdInfos 列表
  133. fun_prod_infos = prod_infos["funProdInfos"]
  134. # 遍历 funProdInfos 列表
  135. for prod_info in fun_prod_infos:
  136. # 获取是否已单独断网信息
  137. if isinstance(prod_info, dict) and "productName" in prod_info:
  138. is_disconnect = prod_info.get("productName")
  139. if "是否已单独断网" in is_disconnect:
  140. is_disconnect_value = re.search(r"(?<=:).+", is_disconnect).group(0)
  141. # 进行判断
  142. if is_disconnect_value == "否":
  143. return 'disconnect'
  144. else:
  145. return 'connect'
  146. return None
  147. except Exception as e:
  148. print('***{}get_access_number_network_status,errLine:{}, errMsg:{}'
  149. .format(access_number, e.__traceback__.tb_lineno, repr(e)))
  150. return None