Procházet zdrojové kódy

优化4G套餐通知&长期无线流量取消通知

zhangdongming před 4 měsíci
rodič
revize
73e4167647
1 změnil soubory, kde provedl 56 přidání a 23 odebrání
  1. 56 23
      Controller/ComboCron/ComboCronPushController.py

+ 56 - 23
Controller/ComboCron/ComboCronPushController.py

@@ -13,11 +13,13 @@ import traceback
 from django.db.models import Q
 from django.db.models import Q
 from django.views import View
 from django.views import View
 
 
-from Model.models import UnicomComboOrderInfo, UnicomDeviceInfo, GatewayPush, SysMsgModel, UnicomFlowPush, Device_User
+from Model.models import UnicomComboOrderInfo, UnicomDeviceInfo, GatewayPush, SysMsgModel, UnicomFlowPush, Device_User, \
+    Device_Info
 from Object.AliyunSmsObject import AliyunSmsObject
 from Object.AliyunSmsObject import AliyunSmsObject
 from Object.ResponseObject import ResponseObject
 from Object.ResponseObject import ResponseObject
 from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
 from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
 from Service.PushService import PushObject
 from Service.PushService import PushObject
+from AnsjerPush.config import XM_PUSH_CHANNEL_ID
 
 
 
 
 class ComboCronPushView(View):
 class ComboCronPushView(View):
@@ -54,12 +56,12 @@ class ComboCronPushView(View):
             now_time = int(time.time())
             now_time = int(time.time())
             combo_order_qs = UnicomComboOrderInfo.objects.filter(
             combo_order_qs = UnicomComboOrderInfo.objects.filter(
                 ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 144) & Q(
                 ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 144) & Q(
-                    expire_time__lte=(now_time + 3600 * 168)), is_del=False).values()
+                    expire_time__lte=(now_time + 3600 * 168)), is_del=False).exclude(combo_id=33).values()
             if combo_order_qs.exists():
             if combo_order_qs.exists():
                 cls.phone_msg_push(combo_order_qs)
                 cls.phone_msg_push(combo_order_qs)
             combo_order_qs = UnicomComboOrderInfo.objects.filter(
             combo_order_qs = UnicomComboOrderInfo.objects.filter(
                 ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 48) & Q(
                 ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 48) & Q(
-                    expire_time__lte=(now_time + 3600 * 72)), is_del=False).values()
+                    expire_time__lte=(now_time + 3600 * 72)), is_del=False).exclude(combo_id=33).values()
             if combo_order_qs.exists():
             if combo_order_qs.exists():
                 cls.phone_msg_push(combo_order_qs)
                 cls.phone_msg_push(combo_order_qs)
             return response.json(0)
             return response.json(0)
@@ -138,7 +140,20 @@ class ComboCronPushView(View):
     @classmethod
     @classmethod
     def flow_warning_push(cls, response):
     def flow_warning_push(cls, response):
         """
         """
-        流量到期或者流量预警消息推送
+        处理流量到期或使用量预警的消息推送
+
+        流程说明:
+        1. 查询所有待处理的流量推送通知
+        2. 遍历每个通知,校验设备及用户信息
+        3. 计算流量使用情况,检查关联套餐
+        4. 生成推送消息内容,通过短信和系统消息推送
+        5. 更新通知状态,记录推送日志
+
+        Args:
+            response: 响应对象,用于返回处理结果
+
+        Returns:
+            response.json(0): 固定返回成功响应
         """
         """
         logger = logging.getLogger('info')
         logger = logging.getLogger('info')
         flow_push_qs = UnicomFlowPush.objects.filter(status=0)
         flow_push_qs = UnicomFlowPush.objects.filter(status=0)
@@ -149,32 +164,52 @@ class ComboCronPushView(View):
                 user_id = item.user_id
                 user_id = item.user_id
                 if not user_id:
                 if not user_id:
                     continue
                     continue
-                user_push_qs = GatewayPush.objects.filter(user_id=user_id)
-                if not user_push_qs:
+                device_info_qs = Device_Info.objects.filter(userID_id=user_id, serial_number=item.serial_no) \
+                    .values('userID__phone', 'Type')
+                if not device_info_qs.exists():
+                    logger.info('推送4G通知设备未被当前客户添加:{},serial:{}'.format(user_id, item.serial_no))
                     continue
                     continue
                 now_time = int(time.time())
                 now_time = int(time.time())
-                # 查询推送配置数据
-                push_qs = GatewayPush.objects.filter(user_id=user_id, logout=False). \
-                    values('user_id', 'app_bundle_id', 'app_type', 'push_type', 'token_val', 'm_code', 'lang', 'tz')
-                if not push_qs.exists():
-                    continue
+
                 usage = cls.flow_split(item.flow_total_usage)
                 usage = cls.flow_split(item.flow_total_usage)
                 total = cls.flow_split(item.flow_total)
                 total = cls.flow_split(item.flow_total)
                 usable = cls.flow_split(item.flow_total - item.flow_total_usage)
                 usable = cls.flow_split(item.flow_total - item.flow_total_usage)
+                # 查询当前套餐并且套餐类型不等于长期无限流量
+                unicom_order_qs = UnicomComboOrderInfo.objects.filter(id=item.combo_order_id).exclude(combo_id=33) \
+                    .values('combo__combo_name')
+                if unicom_order_qs.exists():
+                    logger.info(f'推送4G通知套餐订单不存在或长期无限流量不通知:{item.combo_order_id}')
+                    UnicomFlowPush.objects.filter(id=item.id).update(status=1, updated_time=now_time)
+                    continue
+
+                # 查询推送配置数据
+                push_qs = GatewayPush.objects.filter(user_id=user_id, logout=False).values('user_id',
+                                                                                           'app_bundle_id',
+                                                                                           'app_type', 'push_type',
+                                                                                           'token_val', 'm_code',
+                                                                                           'lang', 'tz').order_by(
+                    '-id')[:5]
                 msg = False
                 msg = False
-                if item.type != 0 and item.type != 1:
-                    unicom_order_qs = UnicomComboOrderInfo.objects.filter(id=item.combo_order_id) \
-                        .values('combo__combo_name')
+                if item.type != 0 and item.type != 1:  # 不等于流量预警通知并且不等于流量到期
                     combo_name = unicom_order_qs.first()['combo__combo_name'] if unicom_order_qs.exists() else ''
                     combo_name = unicom_order_qs.first()['combo__combo_name'] if unicom_order_qs.exists() else ''
                     sys_msg = cls.get_sys_msg_text(item.serial_no, combo_name, item.type)
                     sys_msg = cls.get_sys_msg_text(item.serial_no, combo_name, item.type)
                     msg = True
                     msg = True
+
                 else:
                 else:
-                    user_qs = Device_User.objects.filter(userID=user_id).values('phone')
-                    if user_qs.exists() and user_qs.first()['phone']:
+                    if device_info_qs[0]['userID__phone']:
                         params = u'{"devname":"' + item.serial_no + '","usage":"流量' + usage + '","usable":"流量' + \
                         params = u'{"devname":"' + item.serial_no + '","usage":"流量' + usage + '","usable":"流量' + \
                                  usable + '","total":"流量共' + total + '"}'
                                  usable + '","total":"流量共' + total + '"}'
-                        cls.send_aliyun_sms(user_qs.first()['phone'], params, 'SMS_246100414')
-                    sys_msg = cls.get_msg_text(item.serial_no, push_qs[0]['lang'], total, usage, usable)
+                        cls.send_aliyun_sms(device_info_qs[0]['userID__phone'], params, 'SMS_246100414')
+                    lang = push_qs[0]['lang'] if push_qs.exists() else 'cn'
+                    sys_msg = cls.get_msg_text(item.serial_no, lang, total, usage, usable)
+
+                cls.sys_msg_save(user_id, item.serial_no, now_time, sys_msg)
+                # 修改推送状态
+                UnicomFlowPush.objects.filter(id=item.id).update(status=1, updated_time=now_time)
+                if not push_qs.exists():
+                    logger.info('推送4G通知APP未登录:{},serial:{}'.format(user_id, item.serial_no))
+                    continue
+
                 for push_vo in push_qs:
                 for push_vo in push_qs:
                     kwargs = {
                     kwargs = {
                         'n_time': now_time,
                         'n_time': now_time,
@@ -195,11 +230,9 @@ class ComboCronPushView(View):
                     kwargs['msg_text'] = sys_msg_text
                     kwargs['msg_text'] = sys_msg_text
                     if not cls.msg_push(push_type, **kwargs):
                     if not cls.msg_push(push_type, **kwargs):
                         continue
                         continue
-                cls.sys_msg_save(user_id, item.serial_no, now_time, sys_msg)
-                # 修改推送状态
-                UnicomFlowPush.objects.filter(id=item.id).update(status=1)
+
             except Exception as e:
             except Exception as e:
-                logger.info('出错了~4G流量推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+                logger.info('推送4G通知消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
                 continue
                 continue
         return response.json(0)
         return response.json(0)
 
 
@@ -238,7 +271,7 @@ class ComboCronPushView(View):
                 huawei_push_object.send_push_notify_message(**kwargs)
                 huawei_push_object.send_push_notify_message(**kwargs)
             # android 小米推送
             # android 小米推送
             elif push_type == 4:
             elif push_type == 4:
-                channel_id = 104552
+                channel_id = XM_PUSH_CHANNEL_ID['service_reminder']
                 PushObject.android_xmpush(channel_id=channel_id, **kwargs)
                 PushObject.android_xmpush(channel_id=channel_id, **kwargs)
             # android vivo推送
             # android vivo推送
             elif push_type == 5:
             elif push_type == 5: