| 
					
				 | 
			
			
				@@ -6,6 +6,7 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Email   : zhangdongming@asj6.wecom.work 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Software: PyCharm 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import datetime 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import json 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import logging 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import time 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -15,13 +16,14 @@ from django.http import HttpResponse, JsonResponse 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from django.views.generic.base import View 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type, Order_Model, Store_Meal, AiStoreMeal, \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    UnicomComboOrderInfo 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    UnicomComboOrderInfo, UnicomComboExperienceHistory 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Object.ResponseObject import ResponseObject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Object.TokenObject import TokenObject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Object.UnicomObject import UnicomObjeect 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Object.utils import LocalDateTimeUtil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Object.utils.PayUtil import PayService 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from Service.CommonService import CommonService 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+from Object.RedisObject import RedisObject 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 class UnicomComboView(View): 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -52,6 +54,8 @@ class UnicomComboView(View): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         elif operation == 'device-bind': 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             response = ResponseObject('cn') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return self.device_add(request_dict, response) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        elif operation == 'device-status': 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return self.update_device_status(request_dict, ResponseObject(lang='cn')) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             token = TokenObject(request.META.get('HTTP_AUTHORIZATION')) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             lang = request_dict.get('lang', token.lang) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -123,6 +127,87 @@ class UnicomComboView(View): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             print(e) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return response.json(177, repr(e)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @classmethod 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    def update_device_status(cls, request_dict, response): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        修改设备状态 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param request_dict: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param response: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @return: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        iccid = request_dict.get('iccid', None) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if not iccid: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return response.json(444) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if cls.user_activate_flow(iccid): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return response.json(0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return response.json(177) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @classmethod 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    def user_activate_flow(cls, iccid): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        用户激活初始化流量套餐 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param iccid: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @return: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        logger = logging.getLogger('info') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            flow_key = 'asj:unicom:flow:{}' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            now_time = int(time.time()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            today = datetime.datetime.today() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            year = today.year 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            month = today.month 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if not unicom_device_info_qs.exists(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return False 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            unicom_device_info_qs = unicom_device_info_qs.first() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if unicom_device_info_qs.status != 1: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                logger.info('用户激活iccid={},业务系统状态为{}'.format(iccid, unicom_device_info_qs.status)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                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) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # 查看是否体验过免费套餐 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if not experience_history_qs.exists(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                logger.info('用户首次激活iccid={}'.format(iccid)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                combo_qs = UnicomCombo.objects.filter(combo_type=1, status=0, is_show=1, is_del=False) \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .values('id', 'expiration_type', 'expiration_days', 'combo_type') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if combo_qs.exists(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    combo_qs = combo_qs.first() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    # 保存体验记录 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    experience_history_vo = {'iccid': iccid, 'experience_type': 0, 'do_time': now_time} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    UnicomComboExperienceHistory.objects.create(**experience_history_vo) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    # 保存套餐激活信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    cls.create_combo_order_info('', 0, iccid, combo_qs['id']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            usage_data = {'iccid': iccid} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            usage_history = unicom_api.unicom_flow_usage_cache(flow_key.format(iccid), (60 * 10 + 3), **usage_data) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # 使用流量总历史 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            flow_total = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # 当月实际总使用流量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            flow_total_usage = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if usage_history and usage_history['success']: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                device_usage_history_list = usage_history['data']['deviceUsageHistory'] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if device_usage_history_list: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    for item in device_usage_history_list: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        flow_total += float(item['flowTotalUsage']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if item['year'] == year and item['month'] == month: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            flow_total_usage = item['flowTotalUsage'] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # 修改业务联通卡设备激活信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            UnicomDeviceInfo.objects.filter(iccid=iccid).update(status=1, updated_time=now_time, year=year, month=month, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                                                flow_total_usage=str(flow_total_usage), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                                                before_usage_history=str(flow_total)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return True 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            print(e) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return False 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @classmethod 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     def save_unicom_combo(cls, request_dict, response): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         """ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -376,6 +461,14 @@ class UnicomComboView(View): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @classmethod 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     def create_combo_order_info(cls, order_id, activate_type, iccid, combo_id): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        创建套餐生效记录 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param order_id: 订单id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param activate_type: 激活类型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param iccid: 联通20位iccid 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @param combo_id: 套餐id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @return: True Or False 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         logger = logging.getLogger('info') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         logger.info('创建联通订单套餐信息,订单id{}'.format(order_id)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try: 
			 |