فهرست منبع

检查流量套餐使用情况

zhangdongming 3 سال پیش
والد
کامیت
3944faf6a1
1فایلهای تغییر یافته به همراه73 افزوده شده و 22 حذف شده
  1. 73 22
      Controller/UnicomCombo/UnicomComboTaskController.py

+ 73 - 22
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -6,6 +6,7 @@
 @Email   : zhangdongming@asj6.wecom.work
 @Software: PyCharm
 """
+import datetime
 import logging
 import time
 
@@ -34,6 +35,8 @@ class UnicomComboTaskView(View):
         print(request)
         if operation == 'check-activate':
             return self.check_activate_combo(request_dict, response)
+        if operation == 'check-flow':
+            return self.check_flow_usage(response)
 
     @classmethod
     def check_activate_combo(cls, request_dict, response):
@@ -54,38 +57,86 @@ class UnicomComboTaskView(View):
             return response.json(0)
         try:
             now_time = int(time.time())
+            today = datetime.datetime.today()
+            year = today.year
+            month = today.month
             with transaction.atomic():
                 unicom_api = UnicomObjeect()
-
                 for item in combo_order_info_qs:
                     if item['order_id']:
                         order_id = item['order_id']
                         order_qs = Order_Model.objects.filter(orderID=order_id, status=1)
                         if not order_qs.exists():
-                            logger.info('当前已付款联通订单套餐不存在:orderId={}'.format(order_id))
+                            continue
+                        combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, iccid=item['iccid'])
+                        # 当前已有套餐正在使用则跳出当前循环
+                        if combo_order_qs.exists():
                             continue
                         combo_id = item['combo_id']
                         combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
-                        if combo_qs.exists():
-                            item['status'] = 1
-                            item['updated_time'] = now_time
-                            params = {'iccid': item['iccid']}
-                            result = unicom_api.verify_device(**params)
-                            result = unicom_api.get_text_dict(result)
-                            if result and result['success']:
-                                # 1:已激活;2:可激活;3:已停用;4:已失效;5:可测试;6:库存;7:已更换;8:已清除;
-                                status = result['data']['status']
-                                if status != 1:
-                                    up_data = {'iccid': item['iccid'], 'status': 1}
-                                    device_result = UnicomObjeect.update_device_state(**up_data)
-                                    device_result = UnicomObjeect.get_text_dict(device_result)
-                                    if device_result and device_result['success']:
-                                        UnicomComboOrderInfo.objects.filter(id=item['id']) \
-                                            .update(status=1, updated_time=now_time)
-                                else:
-                                    UnicomComboOrderInfo.objects.filter(id=item['id']) \
-                                        .update(status=1, updated_time=now_time)
-                                logger.info('激活成功,订单编号:{}'.format(order_id))
+                        if not combo_qs.exists():
+                            continue
+                        flow_total_usage = unicom_api.get_flow_usage_total(year, month, item['iccid'])
+                        flow_total_usage = str(flow_total_usage)
+
+                        params = {'iccid': item['iccid']}
+                        result = unicom_api.query_device_status(**params)
+                        res_dict = unicom_api.get_text_dict(result)
+                        # 状态不等于1(激活)时进行激活 1:激活;2:停用
+                        if res_dict['data']['status'] != 1:
+                            re_data = {"iccid": item['iccid'], "status": 1}
+                            unicom_api.update_device_state(**re_data)
+                            UnicomComboOrderInfo.objects.filter(id=item['id']) \
+                                .update(status=1, updated_time=now_time, year=year, month=month,
+                                        flow_total_usage=flow_total_usage)
+                        else:
+                            UnicomComboOrderInfo.objects.filter(id=item['id']) \
+                                .update(status=1, updated_time=now_time, year=year, month=month,
+                                        flow_total_usage=flow_total_usage)
+                        logger.info('激活成功,订单编号:{}'.format(order_id))
+            return response.json(0)
+        except Exception as e:
+            print(e)
+            return response.json(177, repr(e))
+
+    @classmethod
+    def check_flow_usage(cls, response):
+        """
+        检查流量使用情况
+        @return:
+        """
+        logger = logging.getLogger('info')
+        logger.info('进入检查流量使用情况')
+        try:
+            combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1).values()
+            if not combo_order_qs.exists():
+                return response.json(0)
+            today = datetime.datetime.today()
+            year = today.year
+            month = today.month
+            for item in combo_order_qs:
+                iccid = item['iccid']
+                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']
+                    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
+                    else:
+                        # 如自定义天数30天 涉及到跨月 把激活月已使用流量 加流量总值
+                        # 判断大于 (激活月已使用流量 + 跨月当月已用流量)则继续使用否则停卡或激活未使用套餐
+                        pass
             return response.json(0)
         except Exception as e:
             print(e)