zhangdongming 3 жил өмнө
parent
commit
4b185e6108

+ 18 - 39
Controller/UnicomCombo/UnicomComboController.py

@@ -37,8 +37,9 @@ class UnicomComboView(View):
         return self.validation(request.POST, request, operation)
 
     def validation(self, request_dict, request, operation):
-        if operation == 'buy-notify':
-            return self.package_callback_notify(request_dict, request)
+        response = ResponseObject('cn')
+        if operation == 'query-usage-history':
+            return self.query_device_usage_history(request_dict, response)
         elif operation == 'test-notify':
             order_id = request_dict.get('orderId', None)
             activate_type = request_dict.get('activateType', 0)
@@ -51,10 +52,8 @@ class UnicomComboView(View):
         elif operation == 'device-status-change':
             return self.device_status_change_push(request_dict, request)
         elif operation == 'device-bind':
-            response = ResponseObject('cn')
             return self.device_add(request_dict, response)
         elif operation == 'device-status':
-            response = ResponseObject('cn')
             return self.update_device_status(request_dict, response)
         else:
             token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
@@ -127,7 +126,7 @@ class UnicomComboView(View):
                             return response.json(173)
                         params = {'iccid': iccid, 'serial_no': serial_no, 'updated_time': n_time,
                                   'created_time': n_time}
-                        cls.is_activate(iccid)
+                        unicom_obj.change_device_to_activate(iccid)
                         UnicomDeviceInfo.objects.create(**params)
                     return response.json(0)
                 else:
@@ -156,13 +155,7 @@ class UnicomComboView(View):
                     return False
                 # 联通业务逻辑
                 unicom_api = UnicomObjeect()
-                re_data = {'iccid': iccid}
-                result = unicom_api.query_device_status(**re_data)
-                res_dict = unicom_api.get_text_dict(result)
-                # 状态不等于1(激活)时进行激活 1:激活;2:停用
-                if res_dict['data']['status'] != 1:
-                    re_data = {"iccid": iccid, "status": 1}
-                    unicom_api.update_device_state(**re_data)
+                unicom_api.change_device_to_activate(iccid)
                 # 查看是否体验过免费套餐
                 experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
                 if not experience_history_qs.exists():
@@ -344,11 +337,22 @@ class UnicomComboView(View):
         return rank_id, ai_rank_id
 
     @classmethod
-    def query_device_usage_history(cls):
+    def query_device_usage_history(cls, request_dict, response):
         """
         查询用量历史
         @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('month', None)
+        if not iccid:
+            return response.json(444)
+        unicom_api = UnicomObjeect()
+        flow = unicom_api.get_flow_usage_total(int(year), int(month), iccid)
+        return response.json(0, flow)
 
     @staticmethod
     def package_callback_notify(request_dict, request):
@@ -487,13 +491,7 @@ class UnicomComboView(View):
                         # 联通业务逻辑
                     unicom_api = UnicomObjeect()
                     if status == 1:
-                        re_data = {'iccid': iccid}
-                        result = unicom_api.query_device_status(**re_data)
-                        res_dict = unicom_api.get_text_dict(result)
-                        # 状态不等于1(激活)时进行激活 1:激活;2:停用
-                        if res_dict and res_dict['data']['status'] != 1:
-                            re_data = {"iccid": iccid, "status": 1}
-                            unicom_api.update_device_state(**re_data)
+                        unicom_api.change_device_to_activate(iccid)
                         flow_total_usage = unicom_api.get_flow_usage_total(year, month, iccid)
                         combo_order_data['flow_total_usage'] = str(flow_total_usage)
                     UnicomComboOrderInfo.objects.create(**combo_order_data)
@@ -540,25 +538,6 @@ class UnicomComboView(View):
         start_time, month_end_time = LocalDateTimeUtil.get_start_and_end_time(month_end, '%Y-%m-%d')
         return zero_today, month_end_time
 
-    @staticmethod
-    def is_activate(iccid):
-        """
-        根据iccid判断是否激活,未激活则修改为激活状态
-        @param iccid:
-        @return:
-        """
-        if iccid:
-            unicom_api = UnicomObjeect()
-            re_data = {'iccid': iccid}
-            result = unicom_api.query_device_status(**re_data)
-            res_dict = unicom_api.get_text_dict(result)
-            # 状态不等于1(激活)时进行激活 1:激活;2:停用
-            if res_dict['data']['status'] != 1:
-                re_data = {"iccid": iccid, "status": 1}
-                unicom_api.update_device_state(**re_data)
-                return True
-        return None
-
     @classmethod
     def get_test_sign(cls, request_dict, response):
         """

+ 8 - 17
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -84,21 +84,12 @@ class UnicomComboTaskView(View):
                             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)
+                        iccid = item['iccid']
+                        # 检查激活iccid
+                        unicom_api.change_device_to_activate(iccid)
+                        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:
@@ -158,8 +149,8 @@ class UnicomComboTaskView(View):
                         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)
+                            # 停用
+                            unicom_api.change_device_to_disable(iccid)
 
             return response.json(0)
         except Exception as e:

+ 38 - 2
Object/UnicomObject.py

@@ -271,6 +271,42 @@ class UnicomObjeect:
                         break
         return flow_total_usage
 
+    @staticmethod
+    def change_device_to_activate(iccid):
+        """
+        根据iccid判断是否激活,未激活则修改为激活状态
+        @param iccid:
+        @return:
+        """
+        if iccid:
+            re_data = {'iccid': iccid}
+            result = UnicomObjeect().query_device_status(**re_data)
+            res_dict = UnicomObjeect().get_text_dict(result)
+            # 状态不等于1(激活)时进行激活 1:激活;2:停用
+            if res_dict['data']['status'] != 1:
+                re_data = {"iccid": iccid, "status": 1}
+                UnicomObjeect().update_device_state(**re_data)
+            return True
+        return None
+
+    @staticmethod
+    def change_device_to_disable(iccid):
+        """
+        修改设备为停用,并查看是否修改成功
+        @param iccid:
+        @return:
+        """
+        if iccid:
+            re_data = {"iccid": iccid, "status": 2}
+            UnicomObjeect().update_device_state(**re_data)
+            # 查询是否停用成功
+            result = unicom_api.query_device_status(**re_data)
+            res_dict = UnicomObjeect().get_text_dict(result)
+            if res_dict['data']['status'] != 3:
+                unicom_api.update_device_state(**re_data)
+            return True
+        return None
+
 
 if __name__ == '__main__':
 
@@ -304,8 +340,8 @@ if __name__ == '__main__':
     # response = unicom_api.query_renewal_list(**params)
 
     if response.status_code == 200:
-        res_dict = json.loads(response.text)
-        print(res_dict)
+        res = json.loads(response.text)
+        print(res)
     response_json = {
         "success": True,
         "msg": "操作成功",