|
@@ -16,7 +16,9 @@ from django.db import transaction
|
|
|
from django.db.models import Q
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import UnicomComboOrderInfo, UnicomCombo, Order_Model, UnicomDeviceInfo, UnicomFlowPush
|
|
|
+from Model.models import UnicomComboOrderInfo, UnicomCombo, Order_Model, UnicomDeviceInfo, UnicomFlowPush, \
|
|
|
+ IotCardUsageHistory
|
|
|
+from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.UnicomObject import UnicomObjeect
|
|
|
|
|
@@ -50,6 +52,11 @@ class UnicomComboTaskView(View):
|
|
|
month = today.month
|
|
|
self.query_unused_combo_and_activate(request_dict.get('iccid'), year, month, '666')
|
|
|
return response.json(0)
|
|
|
+ elif operation == 'updateFlowUsed': # 更新流量使用
|
|
|
+ self.unicom_flow_used(request_dict, response)
|
|
|
+ return response.json(0)
|
|
|
+ elif operation == 'queryFlowUsedHistory':
|
|
|
+ return self.query_flow_used_history(response)
|
|
|
|
|
|
@classmethod
|
|
|
def check_activate_combo(cls, request_dict, response):
|
|
@@ -306,3 +313,79 @@ class UnicomComboTaskView(View):
|
|
|
UnicomFlowPush.objects.create(**push_data)
|
|
|
except Exception as e:
|
|
|
logger.info('-->出错了~,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def unicom_flow_used(request_dict, response):
|
|
|
+ """
|
|
|
+ 查询设备每张卡流量使用情况
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ page_size = int(request_dict.get('pageSize', 1))
|
|
|
+ device_count = UnicomDeviceInfo.objects.filter(card_type=0).count()
|
|
|
+ total_pages = device_count // page_size + (device_count % page_size > 0) # 计算总页数
|
|
|
+ for page_number in range(1, total_pages + 1):
|
|
|
+ u_device_qs = UnicomDeviceInfo.objects.filter(card_type=0).values('id', 'iccid', 'sim_used_flow').order_by(
|
|
|
+ '-created_time')[(page_number - 1) * page_size:page_number * page_size]
|
|
|
+ asy = threading.Thread(target=UnicomComboTaskView.thread_collect_flow_used, args=(u_device_qs,))
|
|
|
+ asy.start()
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def thread_collect_flow_used(u_device_qs):
|
|
|
+ for item in u_device_qs:
|
|
|
+ try:
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ n_time = int(time.time())
|
|
|
+ # 队列已使用总流量总量
|
|
|
+ flow_total_usage = unicom_api.get_flow_usage_total(item['iccid'])
|
|
|
+ UnicomDeviceInfo.objects.filter(id=item['id']).update(updated_time=n_time,
|
|
|
+ sim_used_flow=flow_total_usage)
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ continue
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def query_flow_used_history(cls, response):
|
|
|
+ # 获取符合条件的卡片对象查询集,并按创建时间升序排序
|
|
|
+ card_qs = UnicomDeviceInfo.objects.filter(card_type=0).values('iccid').order_by('created_time')
|
|
|
+ if not card_qs.exists():
|
|
|
+ return response.json(0)
|
|
|
+ asy = threading.Thread(target=UnicomComboTaskView.async_bulk_create_usage_history, args=(card_qs,))
|
|
|
+ asy.start()
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def async_bulk_create_usage_history(qs):
|
|
|
+ """
|
|
|
+ 异步批量创建流量用量历史记录
|
|
|
+ """
|
|
|
+ redis_obj = RedisObject()
|
|
|
+ current_time = int(time.time()) # 获取当前时间戳
|
|
|
+
|
|
|
+ for item in qs:
|
|
|
+ iccid = item['iccid']
|
|
|
+ key = 'monthly_flow_' + iccid
|
|
|
+
|
|
|
+ flow_dict = redis_obj.get_all_hash_data(key) # 获取Redis中指定键的哈希表数据
|
|
|
+ iot_card_list = [] # 创建一个空的列表,用于批量创建IotCardUsageHistory对象
|
|
|
+ for k, v in flow_dict.items():
|
|
|
+ try:
|
|
|
+ cycle = datetime.datetime.strptime(str(k.decode()), '%Y-%m') # 将字符串日期解析为datetime类型
|
|
|
+ flow = float(v)
|
|
|
+
|
|
|
+ iot_card_list.append(IotCardUsageHistory(
|
|
|
+ iccid=iccid,
|
|
|
+ card_type=1,
|
|
|
+ cycle=int(cycle.strftime('%Y%m')), # 将日期转换为整数形式,如202201
|
|
|
+ flow_total_usage=flow,
|
|
|
+ created_time=current_time,
|
|
|
+ updated_time=current_time
|
|
|
+ ))
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ continue
|
|
|
+ # 批量创建IotCardUsageHistory对象
|
|
|
+ if iot_card_list:
|
|
|
+ IotCardUsageHistory.objects.bulk_create(iot_card_list)
|