Browse Source

完善根据iccid查询套餐信息

zhangdongming 3 years ago
parent
commit
9e32b8e244
2 changed files with 106 additions and 0 deletions
  1. 105 0
      Controller/UnicomCombo/UnicomComboController.py
  2. 1 0
      Model/models.py

+ 105 - 0
Controller/UnicomCombo/UnicomComboController.py

@@ -10,6 +10,7 @@ import datetime
 import json
 import logging
 import time
+from decimal import Decimal
 
 from django.db import transaction
 from django.http import HttpResponse, JsonResponse
@@ -70,6 +71,110 @@ class UnicomComboView(View):
                 return self.query_package_list(response)
             elif operation == 'get-device-info':
                 return self.get_device_info(request_dict, response)
+            elif operation == 'user-combo-query':
+                return self.user_combo_query(user_id, request_dict, response)
+
+    @classmethod
+    def user_combo_query(cls, user_id, request_dict, response):
+        """
+        查询套餐流量列表或者正在使用流量详情
+        @param user_id:
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        try:
+            q_type = request_dict.get('type', None)
+            iccid = request_dict.get('iccid', None)
+            if not all([q_type, iccid]):
+                return response.json(444)
+            unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
+            if not unicom_device_info_qs.exists():
+                return response.json(173)
+            if not unicom_device_info_qs[0].user_id:
+                unicom_device_info_qs.update(user_id=user_id)
+            unicom_api = UnicomObjeect()
+            today = datetime.datetime.today()
+            year = today.year
+            month = today.month
+            if q_type == 'combo':
+                cls.update_combo_order_sort(iccid)
+                combo_list = []
+                combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid, is_del=False) \
+                    .values('iccid', 'status', 'combo__combo_name', 'combo__flow_total',
+                            'combo__remark', 'expiration_days', 'expiration_type', 'flow_total_usage',
+                            'expire_time')
+                for item in combo_order_qs:
+                    combo_list.append({
+                        'iccid': iccid,
+                        'comboName': item['combo__combo_name'],
+                        'flowTotal': item['combo__flow_total'],
+                        'comboRemark': item['combo__remark'],
+                        'expirationDays': item['expiration_days'],
+                        'expirationType': item['expiration_type'],
+                        'flowTotalUsage': item['flow_total_usage'],
+                        'expireTime': item['expire_time'],
+                    })
+                return response.json(0, combo_list)
+
+            elif q_type == 'usage':
+                combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid, status=1, is_del=False) \
+                    .values('iccid', 'status', 'combo__status', 'combo__combo_name', 'combo__combo_type',
+                            'combo__flow_total', 'combo__remark', 'expiration_days', 'expiration_type',
+                            'year', 'month', 'flow_total_usage', 'expire_time')
+                if not combo_order_qs.exists():
+                    combo_order = combo_order_qs.first()
+                    flow_details = {
+                        'iccid': iccid,
+                        'comboName': combo_order['combo__combo_name'],
+                        'comboType': combo_order['combo__combo_type'],
+                        'flowTotal': combo_order['combo__flow_total'],
+                        'comboRemark': combo_order['combo__remark'],
+                        'expirationDays': combo_order['expiration_days'],
+                        'expirationType': combo_order['expiration_type'],
+                        'year': combo_order['year'],
+                        'month': combo_order['month'],
+                        'flowTotalUsage': combo_order['flow_total_usage'],
+                        'expireTime': combo_order['expire_time'],
+                    }
+                    if flow_details['year'] == year and flow_details['month'] == month:
+                        month_flow = unicom_api.get_flow_usage_total(year, month, iccid)
+                        month_flow = month_flow - float(flow_details['flowTotalUsage'])
+                        flow = flow_details['flowTotal'] - month_flow
+                        flow_details['usageFlow'] = flow
+                    else:
+                        now_month_flow = unicom_api.get_flow_usage_total(year, month, iccid)
+                        last_month_flow = unicom_api.get_flow_usage_total(flow_details['year'], flow_details['month'],
+                                                                          iccid)
+                        flow = now_month_flow + last_month_flow - float(flow_details['flowTotalUsage'])
+                        flow_details['usageFlow'] = flow_details['flowTotal'] - flow
+
+                    flow_details['usageFlow'] = \
+                        flow_details['flowTotal'] if flow_details['usageFlow'] <= 0 else flow_details['usageFlow']
+                    flow_details['usageFlow'] = Decimal(flow_details['usageFlow']).quantize(Decimal('0.00'))
+                    return response.json(0, flow_details)
+        except Exception as e:
+            print(e)
+            return response.json(177, repr(e))
+
+    @classmethod
+    def update_combo_order_sort(cls, iccd):
+        """
+        修改套餐排序
+        @param iccd: 联通20位ICCID
+        @return:
+        """
+        combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccd, is_del=False)
+        if combo_order_qs.exists():
+            unused_qs = combo_order_qs.filter(status=0)
+            if unused_qs.exists():
+                unused_qs.update(sort=50)
+            used_qs = combo_order_qs.filter(status=1)
+            if used_qs.exists():
+                used_qs.update(sort=1)
+            expire_qs = combo_order_qs.filter(status=2)
+            if expire_qs.exists():
+                expire_qs.update(sort=100)
 
     @classmethod
     def update_device_status(cls, request_dict, response):

+ 1 - 0
Model/models.py

@@ -2691,6 +2691,7 @@ class UnicomComboOrderInfo(models.Model):
     year = models.IntegerField(default=0, verbose_name='使用年')
     month = models.IntegerField(default=0, verbose_name='使用月')
     flow_total_usage = models.CharField(blank=True, default='', max_length=32, verbose_name=u'激活时当月已用流量')
+    sort = models.IntegerField(default=99, blank=True, verbose_name=u'排序')
     next_month_activate = models.BooleanField(blank=True, default=False, verbose_name=u'下月激活')
     activation_time = models.IntegerField(default=0, verbose_name='激活时间')
     expire_time = models.IntegerField(default=0, verbose_name='过期时间')