Prechádzať zdrojové kódy

更新流量使用情况,优化设备激活

zhangdongming 3 rokov pred
rodič
commit
0234361c0c

+ 7 - 5
Controller/UnicomCombo/UnicomComboController.py

@@ -72,6 +72,10 @@ class UnicomComboView(View):
             elif operation == 'get-device-info':
                 return self.get_device_info(request_dict, response)
 
+    @classmethod
+    def update_device_status(cls, request_dict, response):
+        pass
+
     @classmethod
     def get_device_info(cls, request_dict, response):
         """
@@ -120,6 +124,8 @@ class UnicomComboView(View):
                         params = {'iccid': iccid, 'serial_no': serial_no, 'updated_time': n_time,
                                   'created_time': n_time}
                         UnicomDeviceInfo.objects.create(**params)
+                        # 自动激活1G套餐
+                        cls.user_activate_flow(iccid)
                     return response.json(0)
                 else:
                     return response.json(173)
@@ -127,10 +133,6 @@ class UnicomComboView(View):
             print(e)
             return response.json(177, repr(e))
 
-
-
-
-
     @classmethod
     def user_activate_flow(cls, iccid):
         """
@@ -533,4 +535,4 @@ class UnicomComboView(View):
         zero_today, last_today = LocalDateTimeUtil.get_today_date(True)
         month_end = LocalDateTimeUtil.get_cur_month_end()
         start_time, month_end_time = LocalDateTimeUtil.get_start_and_end_time(month_end, '%Y-%m-%d')
-        return zero_today, month_end_time
+        return zero_today, month_end_time

+ 68 - 12
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -35,8 +35,14 @@ class UnicomComboTaskView(View):
         print(request)
         if operation == 'check-activate':
             return self.check_activate_combo(request_dict, response)
-        if operation == 'check-flow':
+        elif operation == 'check-flow':
             return self.check_flow_usage(response)
+        elif operation == 'check-expire':
+            today = datetime.datetime.today()
+            year = today.year
+            month = today.month
+            self.query_unused_combo_and_activate(request_dict.get('iccid'), year, month, '666')
+            return response.json(0)
 
     @classmethod
     def check_activate_combo(cls, request_dict, response):
@@ -114,6 +120,8 @@ class UnicomComboTaskView(View):
             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']
                 usage_flow = float(item['flow_total_usage']) if item['flow_total_usage'] else 0.0
@@ -122,22 +130,70 @@ class UnicomComboTaskView(View):
                 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)
+                    is_expire = False
                     if item['year'] == year and item['month'] == month:
-                        # 查询当前月用量历史
-                        month_usage_flow = UnicomObjeect.get_flow_usage_total(year, month, iccid)
-                        logger.info('账单年:{},账单月:{},实际使用流量{}'.format(year, month, month_usage_flow))
                         if month_usage_flow > 0:
                             # 初始套餐已使用流量 + 套餐总流量
                             flow = usage_flow + flow_total
-                            if flow <= month_usage_flow:
-                                logger.info('当前套餐{}已用完iccid:{}'.format(combo_qs['combo_name'], iccid))
-                                # 检查是否有当月未使用套餐 没有则停卡
-                                pass
+                            if month_usage_flow >= flow:
+                                is_expire = True
                     else:
-                        # 如自定义天数30天 涉及到跨月 把激活月已使用流量 加流量总值
-                        # 判断大于 (激活月已使用流量 + 跨月当月已用流量)则继续使用否则停卡或激活未使用套餐
-                        pass
+                        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
+                        # result = Decimal(surplus_flow).quantize(Decimal('0.00'))
+                        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:
+                            re_data = {"iccid": iccid, "status": 2}
+                            unicom_api.update_device_state(**re_data)
+
             return response.json(0)
         except Exception as e:
-            print(e)
+            logger.info('网关推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return response.json(177, repr(e))
+
+    @staticmethod
+    def query_unused_combo_and_activate(iccid, year, month, usage_flow):
+        """
+        查询未使用套餐并激活
+        @param iccid:
+        @param year:
+        @param month:
+        @param usage_flow:
+        @return:
+        """
+        try:
+            now_time = int(time.time())
+            combo_order_qs = UnicomComboOrderInfo.objects.filter(expire_time__gt=now_time, status=0,
+                                                                 iccid=iccid).order_by(
+                'created_time')
+            if not combo_order_qs.exists():
+                return False
+
+            combo_order = combo_order_qs.first()
+            upd_data = {
+                'status': 1,
+                'year': year,
+                'month': month,
+                'flow_total_usage': str(usage_flow),
+                'updated_time': now_time,
+            }
+            UnicomComboOrderInfo.objects.filter(id=combo_order.id).update(**upd_data)
+            return True
+        except Exception as e:
+            print(e)
+            return False