|
@@ -0,0 +1,70 @@
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
+"""
|
|
|
+@File : TelecomService.py
|
|
|
+@Time : 2024/1/24 15:12
|
|
|
+@Author : stephen
|
|
|
+@Email : zhangdongming@asj6.wecom.work
|
|
|
+@Software: PyCharm
|
|
|
+"""
|
|
|
+import time
|
|
|
+from decimal import Decimal
|
|
|
+from Ansjer.config import LOGGER
|
|
|
+from Model.models import UnicomDeviceInfo
|
|
|
+from Object.RedisObject import RedisObject
|
|
|
+from Object.TelecomObject import TelecomObject
|
|
|
+
|
|
|
+
|
|
|
+class TelecomService:
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def query_total_usage_by_access_number(cls, iccid, access_number, key, expire=600):
|
|
|
+ """
|
|
|
+ 根据接入号码查询设备总流量使用量(实现缓存)
|
|
|
+ @param iccid: 20位ICCID
|
|
|
+ @param key: 缓存key
|
|
|
+ @param expire: 失效时间
|
|
|
+ @param access_number: 接入号码
|
|
|
+ @return: 查询流量结果
|
|
|
+ """
|
|
|
+ redis = RedisObject()
|
|
|
+ sim_flow_used_total = redis.get_data(key)
|
|
|
+ if sim_flow_used_total:
|
|
|
+ return Decimal(sim_flow_used_total).quantize(Decimal('0.00'))
|
|
|
+ else:
|
|
|
+ # 查询SIM卡信息
|
|
|
+ sim_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
|
|
|
+ if not sim_qs:
|
|
|
+ return None
|
|
|
+ sim_vo = sim_qs.first()
|
|
|
+
|
|
|
+ telecom = TelecomObject()
|
|
|
+ data = telecom.query_total_usage_by_date(access_number)
|
|
|
+ if data and data['SvcCont']['resultCode'] == '0' and 'dataCumulationTotal' in data['SvcCont']['result']:
|
|
|
+ cycle_total = data['SvcCont']['result'].get('dataCumulationTotal')
|
|
|
+ else:
|
|
|
+ LOGGER.info(f'query_total_usage_by_access_number查询流量异常,iccid:{iccid}')
|
|
|
+ return sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
|
|
|
+
|
|
|
+ cycle_total = Decimal(cycle_total).quantize(Decimal('0.00'))
|
|
|
+
|
|
|
+ n_time = int(time.time())
|
|
|
+
|
|
|
+ # 判断数据库周期流量用量 是否大于API查询出来的周期用量 如果是则判定进入了下一个周期
|
|
|
+ if sim_vo.sim_cycle_used_flow != 0 and sim_vo.sim_cycle_used_flow > cycle_total:
|
|
|
+ sim_used_flow = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
|
|
|
+ sim_qs.update(
|
|
|
+ sim_used_flow=sim_used_flow,
|
|
|
+ sim_cycle_used_flow=cycle_total,
|
|
|
+ updated_time=n_time
|
|
|
+ )
|
|
|
+ # 队列用量历史总量 + 上一个周期流量 + 当前周期流量 = 总消耗流量
|
|
|
+ sim_flow_used_total = sim_used_flow + cycle_total
|
|
|
+ elif cycle_total > sim_vo.sim_cycle_used_flow: # API周期用量大于当前数据库用量则更新记录
|
|
|
+ sim_qs.update(sim_cycle_used_flow=cycle_total, updated_time=n_time)
|
|
|
+ # 队列用量历史总量 + 当前周期流量 = 总消耗流量
|
|
|
+ sim_flow_used_total = sim_vo.sim_used_flow + cycle_total
|
|
|
+ else:
|
|
|
+ sim_flow_used_total = sim_vo.sim_used_flow + sim_vo.sim_cycle_used_flow
|
|
|
+ redis.CONN.setnx(key, str(sim_flow_used_total))
|
|
|
+ redis.CONN.expire(key, expire)
|
|
|
+ return sim_flow_used_total
|