فهرست منبع

新增联通定时更新周期流量历史接口

zhangdongming 2 سال پیش
والد
کامیت
1ccbf0843e
1فایلهای تغییر یافته به همراه55 افزوده شده و 0 حذف شده
  1. 55 0
      Controller/UnicomCombo/UnicomComboTaskController.py

+ 55 - 0
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -59,6 +59,10 @@ class UnicomComboTaskView(View):
             return self.query_flow_used_history(response)
         elif operation == 'queryFlowCache':
             return self.query_flow_cache(response)
+        elif operation == 'getDeviceUsageHistory':
+            return self.get_device_usage_history(response)
+        else:
+            return response.json(414)
 
     @classmethod
     def check_activate_combo(cls, request_dict, response):
@@ -422,3 +426,54 @@ class UnicomComboTaskView(View):
         except Exception as e:
             logger.info('出错了~次月激活套餐异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return response.json(177, repr(e))
+
+    @staticmethod
+    def get_device_usage_history(response):
+        """
+        查询联通设备历史用量
+        """
+        card_qs = UnicomDeviceInfo.objects.filter(card_type=0).values('iccid')
+        if not card_qs.exists():
+            return response.json(0)
+        asy = threading.Thread(target=UnicomComboTaskView.async_update_device_usage_history, args=(card_qs,))
+        asy.start()
+        return response.json(0)
+
+    @staticmethod
+    def async_update_device_usage_history(qs):
+        """
+        异步更新设备用量历史
+        @param qs: 联通iccid集合
+        """
+        try:
+            u_service = UnicomObjeect()
+            iot_card_list = []
+            now_time = int(time.time())
+            # 获取当前时间
+            current_date = datetime.datetime.now()
+            # 计算上个月的时间
+            last_month_date = current_date - datetime.timedelta(days=current_date.day)
+            # 例格式化日期为 "202307"
+            formatted_date = last_month_date.strftime('%Y%m')
+            for item in qs:
+                params = {'iccid': item['iccid']}
+                result = u_service.query_device_usage_history(**params)
+                res_dict = u_service.get_text_dict(result)
+                if res_dict['code'] == 0 and res_dict['data']:
+                    for cycle in res_dict['data']:
+                        if cycle['flowTotalUsage'] <= 0 or cycle['cycle'] != int(formatted_date):
+                            continue
+                        iot_card_list.append(IotCardUsageHistory(
+                            iccid=item['iccid'],
+                            card_type=1,
+                            cycle=cycle['cycle'],
+                            flow_total_usage=cycle['flowTotalUsage'],
+                            created_time=now_time,
+                            updated_time=now_time
+                        ))
+            if not iot_card_list:
+                return None
+            IotCardUsageHistory.objects.bulk_create(iot_card_list)
+        except Exception as e:
+            logger.info('查询账期流量异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return None