|
@@ -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
|
|
|
|
|
@@ -53,6 +55,8 @@ class UnicomComboTaskView(View):
|
|
|
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):
|
|
@@ -342,3 +346,47 @@ class UnicomComboTaskView(View):
|
|
|
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)
|