Эх сурвалжийг харах

修改查询流量调用实时队列套餐流量用量详情API

zhangdongming 3 жил өмнө
parent
commit
b2ca550932

+ 7 - 23
Controller/UnicomCombo/UnicomComboController.py

@@ -96,9 +96,6 @@ class UnicomComboView(View):
             if not unicom_device_info_qs[0].user_id:
                 unicom_device_info_qs.update(user_id=user_id)
             unicom_api = UnicomObjeect()
-            today = datetime.datetime.today()
-            year = today.year
-            month = today.month
             combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid, status=1, is_del=False) \
                 .values('iccid', 'status', 'combo__status', 'combo__combo_name', 'combo__combo_type',
                         'combo__flow_total', 'combo__remark', 'combo__expiration_days', 'combo__expiration_type',
@@ -122,18 +119,10 @@ class UnicomComboView(View):
                     'year': combo_order['year'],
                     'month': combo_order['month'],
                 }
-                if flow_details['year'] == year and flow_details['month'] == month:
-                    month_flow = unicom_api.get_flow_usage_total(year, month, iccid)
-                    # 当月流量减去 套餐激活时用量
-                    month_flow = month_flow - float(flow_details['flowTotalUsage'])
-                    flow = flow_details['flowTotal'] - month_flow
-                    flow_details['usableFlow'] = flow
-                else:
-                    now_month_flow = unicom_api.get_flow_usage_total(year, month, iccid)
-                    last_month_flow = unicom_api.get_flow_usage_total(flow_details['year'], flow_details['month'],
-                                                                      iccid)
-                    flow = now_month_flow + last_month_flow - float(flow_details['flowTotalUsage'])
-                    flow_details['usableFlow'] = flow_details['flowTotal'] - flow
+                activate_flow = float(flow_details['flowTotalUsage'])
+                flow_total_usage = unicom_api.get_flow_usage_total(iccid)
+                flow = flow_total_usage - activate_flow
+                flow_details['usableFlow'] = flow_details['flowTotal'] - flow
 
                 flow_details['usableFlow'] = \
                     0.00 if flow_details['usableFlow'] <= 0 else flow_details['usableFlow']
@@ -512,19 +501,14 @@ class UnicomComboView(View):
     @classmethod
     def query_device_usage_history(cls, request_dict, response):
         """
-        查询用量历史
+        查询实时已用总流量API
         @return:
         """
-        today = datetime.datetime.today()
-        year = today.year
-        month = today.month
-        year = request_dict.get('year', year)
-        month = request_dict.get('month', month)
         iccid = request_dict.get('iccid', None)
         if not iccid:
             return response.json(444)
         unicom_api = UnicomObjeect()
-        flow = unicom_api.get_flow_usage_total(int(year), int(month), iccid)
+        flow = unicom_api.get_flow_usage_total(iccid)
         return response.json(0, flow)
 
     @staticmethod
@@ -680,7 +664,7 @@ class UnicomComboView(View):
                     unicom_api = UnicomObjeect()
                     if status == 1:
                         unicom_api.change_device_to_activate(iccid)
-                        flow_total_usage = unicom_api.get_flow_usage_total(year, month, iccid)
+                        flow_total_usage = unicom_api.get_flow_usage_total(iccid)
                         if flow_total_usage > 0:
                             flow_total_usage = Decimal(flow_total_usage)
                             flow_total_usage = flow_total_usage.quantize(Decimal('0.00'))

+ 15 - 31
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -86,7 +86,7 @@ class UnicomComboTaskView(View):
                         if not combo_qs.exists():
                             continue
                         # 查询当月用量情况
-                        flow_total_usage = unicom_api.get_flow_usage_total(year, month, item['iccid'])
+                        flow_total_usage = unicom_api.get_flow_usage_total(item['iccid'])
                         flow_total_usage = Decimal(flow_total_usage).quantize(
                             Decimal('0.00')) if flow_total_usage > 0 else 0
                         flow_total_usage = str(flow_total_usage)
@@ -123,48 +123,32 @@ class UnicomComboTaskView(View):
                 if not u_device_info_qs.exists():
                     continue
                 u_device_info_qs = u_device_info_qs.first()
-                usage_flow = float(item['flow_total_usage']) if item['flow_total_usage'] else 0.0
+                activate_usage_flow = float(item['flow_total_usage']) if item['flow_total_usage'] else 0.0
                 combo_id = item['combo_id']
                 combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
                 if not combo_qs.exists():
                     continue
                 combo_qs = combo_qs.first()
                 flow_total = combo_qs['flow_total']
-                # 查询当前月用量历史
-                month_usage_flow = unicom_api.get_flow_usage_total(year, month, iccid)
-                logger.info('--->{}-{},iccid:{};套餐总值:{},激活时当月用量值:{},月已用量:{}'
-                            .format(year, month, iccid, flow_total, usage_flow, month_usage_flow))
+                # 队列已使用总流量总量
+                flow_total_usage = unicom_api.get_flow_usage_total(iccid)
+                logger.info('--->{}-{},iccid:{};套餐总值:{},激活时当月用量值:{},套餐队列已使用流量量:{}'
+                            .format(year, month, iccid, flow_total, activate_usage_flow, flow_total_usage))
                 is_expire = False
-                if item['year'] == year and item['month'] == month:
-                    if month_usage_flow > 0:
-                        # 初始套餐已使用流量 + 套餐总流量
-                        flow = usage_flow + flow_total
-                        if month_usage_flow >= flow:
-                            is_expire = True
-                    usage = (month_usage_flow - usage_flow) if month_usage_flow > usage_flow else 0
-                    cls.flow_warning_push(u_device_info_qs.user_id, u_device_info_qs.serial_no, item['id'], flow_total,
-                                          usage)
-                else:
-                    activate_year = item['year']
-                    activate_month = item['month']
-                    # 上月使用流量
-                    last_usage_flow = unicom_api.get_flow_usage_total(activate_year, activate_month, iccid)
-                    # 上月套餐实际使用量
-                    actual_usage_flow = last_usage_flow - usage_flow
-                    # 剩余
-                    surplus_flow = flow_total - actual_usage_flow
-                    if month_usage_flow > 0:
-                        if month_usage_flow >= surplus_flow:
-                            is_expire = True
-                    usage = (month_usage_flow + last_usage_flow - usage_flow) \
-                        if (month_usage_flow + last_usage_flow) > usage_flow else 0
+                if flow_total_usage > 0:
+                    # 初始套餐已使用流量 + 套餐总流量
+                    flow = activate_usage_flow + flow_total
+                    if flow_total_usage >= flow:
+                        is_expire = True
+                    usage = (flow_total_usage - activate_usage_flow) if flow_total_usage > activate_usage_flow else 0
                     cls.flow_warning_push(u_device_info_qs.user_id, u_device_info_qs.serial_no, item['id'], flow_total,
                                           usage)
+
                 # 检查是否有当月未使用套餐 没有则停卡
                 if is_expire:
                     UnicomComboOrderInfo.objects.filter(id=item['id']).update(status=2, updated_time=now_time)
                     activate_status = cls.query_unused_combo_and_activate(iccid, year, month,
-                                                                          month_usage_flow)
+                                                                          flow_total_usage)
                     if not activate_status:
                         # 停用
                         unicom_api.change_device_to_disable(iccid)
@@ -281,7 +265,7 @@ class UnicomComboTaskView(View):
                                                                     is_del=False).values()
             if activate_combo_qs.exists():
                 continue
-            usage_flow = unicom_api.get_flow_usage_total(year, month, item)
+            usage_flow = unicom_api.get_flow_usage_total(item)
             result = cls.query_unused_combo_and_activate(item, year, month, usage_flow)
             if not result:
                 # 停用设备

+ 16 - 23
Object/UnicomObject.py

@@ -230,45 +230,38 @@ class UnicomObjeect:
         return None
 
     @staticmethod
-    def unicom_flow_usage_cache(key, expire=0, **usage_data):
+    def get_flow_total_usage(key, expire=0, **usage_data):
         """
-        流量用量历史优先查询缓存
+        设备当前队列用量详情(实现缓存)
         @param key: 缓存key
         @param expire: 失效时间
         @param usage_data: 查询参数
         @return: 返回结果dict
         """
         redis = RedisObject()
-        usage_history = redis.get_data(key)
-        if usage_history:
-            usage_history = json.loads(usage_history)
+        flow_usage_details = redis.get_data(key)
+        if flow_usage_details:
+            flow_usage_details = json.loads(flow_usage_details)
         else:
-            usage_history = UnicomObjeect().query_device_usage_history(**usage_data)
-            usage_history = UnicomObjeect().get_text_dict(usage_history)
-            redis.set_data(key=key, val=json.dumps(usage_history), expire=expire)
-        return usage_history
+            flow_usage_details = UnicomObjeect().query_current_renew_list_usage_details(**usage_data)
+            flow_usage_details = UnicomObjeect().get_text_dict(flow_usage_details)
+            redis.set_data(key=key, val=json.dumps(flow_usage_details), expire=expire)
+        return flow_usage_details
 
     @staticmethod
-    def get_flow_usage_total(usage_year, usage_month, iccid):
+    def get_flow_usage_total(iccid):
         """
-        查询当月用量历史
-        @param usage_year: 使用年
-        @param usage_month: 使用月
+        获取实时当前套餐队列总已用流量
         @param iccid: 联通id
-        @return: flow_total_usage 当月实际使用流量
+        @return: flow_total_usage 当前套餐总已使用流量
         """
         flow_key = 'ASJ:UNICOM:FLOW:{}'
-        usage_data = {'iccid': iccid}
-        usage_history = UnicomObjeect().unicom_flow_usage_cache(flow_key.format(iccid), (60 * 10 + 3), **usage_data)
+        usage_data = {'iccid': iccid, 'realTime': True}
+        flow_usage_details = UnicomObjeect().get_flow_total_usage(flow_key.format(iccid), (60 * 10 + 3), **usage_data)
         # 当月实际总使用流量
         flow_total_usage = 0
-        if usage_history and usage_history['success']:
-            device_usage_history_list = usage_history['data']['deviceUsageHistory']
-            if device_usage_history_list:
-                for item in device_usage_history_list:
-                    if item['year'] != usage_year or item['month'] != usage_month:
-                        continue
-                    flow_total_usage += item['flowTotalUsage']
+        if flow_usage_details and flow_usage_details['success']:
+            flow_total_usage = flow_usage_details['data']['flowTotalUsage']
         return flow_total_usage
 
     @staticmethod