Selaa lähdekoodia

监控流量套餐剩余10%预警推送

zhangdongming 3 vuotta sitten
vanhempi
commit
7d704671d3
2 muutettua tiedostoa jossa 79 lisäystä ja 36 poistoa
  1. 78 36
      Controller/UnicomCombo/UnicomComboTaskController.py
  2. 1 0
      Model/models.py

+ 78 - 36
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -14,7 +14,7 @@ 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
+from Model.models import UnicomComboOrderInfo, UnicomCombo, Order_Model, UnicomDeviceInfo, UnicomFlowPush
 from Object.ResponseObject import ResponseObject
 from Object.UnicomObject import UnicomObjeect
 
@@ -106,59 +106,101 @@ class UnicomComboTaskView(View):
         logger = logging.getLogger('info')
         logger.info('--->进入监控流量使用情况')
         try:
+            unicom_api = UnicomObjeect()
             combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, is_del=False).values()
             if not combo_order_qs.exists():
                 return response.json(0)
             today = datetime.datetime.today()
             year = today.year
             month = today.month
-            unicom_api = UnicomObjeect()
             now_time = int(time.time())
             for item in combo_order_qs:
                 iccid = item['iccid']
+                u_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
+                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
                 combo_id = item['combo_id']
                 combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
-                if combo_qs.exists():
-                    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))
-                    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
-                    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
-                    # 检查是否有当月未使用套餐 没有则停卡
-                    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)
-                        if not activate_status:
-                            # 停用
-                            unicom_api.change_device_to_disable(iccid)
+                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))
+                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
+                    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)
+                    if not activate_status:
+                        # 停用
+                        unicom_api.change_device_to_disable(iccid)
 
             return response.json(0)
         except Exception as e:
             logger.info('检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return response.json(177, repr(e))
 
+    @staticmethod
+    def flow_warning_push(app_user_id, serial_no, combo_order_id, flow_total, flow_usage):
+        """
+        监控流量使用大于85%and小于96%进行消息推送提醒
+        @param app_user_id: app用户id
+        @param serial_no: 序列号
+        @param combo_order_id: 当前套餐订单id
+        @param flow_total: 流量总量
+        @param flow_usage: 已使用流量
+        @return:
+        """
+        logger = logging.getLogger('info')
+        try:
+            if not app_user_id:
+                return False
+            if 0 < flow_total and 0 < flow_usage < flow_total:
+                res = flow_usage / flow_total * 100
+                if 85 < res <= 95:
+                    flow_push = UnicomFlowPush.objects.filter(serial_no, combo_order_id)
+                    if not flow_push.exists():
+                        now_time = int(time.time())
+                        push_data = {'combo_order_id': str(combo_order_id), 'serial_no': serial_no,
+                                     'flow_total_usage': flow_usage, 'flow_total': flow_total, 'status': 0,
+                                     'updated_time': now_time,
+                                     'created_time': now_time, 'user_id': app_user_id}
+                        UnicomFlowPush.objects.create(**push_data)
+            return True
+        except Exception as e:
+            logger.info('检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
     @staticmethod
     def query_unused_combo_and_activate(iccid, year, month, usage_flow):
         """

+ 1 - 0
Model/models.py

@@ -2780,6 +2780,7 @@ class UnicomFlowPush(models.Model):
     user_id = models.CharField(default='', max_length=32, verbose_name=u'用户id')
     # 0: 剩余10%流量预警, 1: 流量到期
     type = models.SmallIntegerField(default=0, verbose_name='流量推送类型')
+    combo_order_id = models.CharField(max_length=32, default='', verbose_name='当前订单套餐id')
     serial_no = models.CharField(max_length=32, default='', verbose_name='序列号')
     flow_total_usage = models.IntegerField(default=0, verbose_name=u'当月实际流量用量 单位(MB)')
     flow_total = models.IntegerField(default=0, verbose_name=u'流量总量 单位(MB)')