Parcourir la source

修改续费机制

chenjunkai il y a 6 ans
Parent
commit
df5814c416
4 fichiers modifiés avec 53 ajouts et 37 suppressions
  1. 1 0
      Ansjer/test/util/testss.py
  2. 45 34
      Controller/CloudVod.py
  3. 6 2
      Controller/Test.py
  4. 1 1
      Service/CommonService.py

+ 1 - 0
Ansjer/test/util/testss.py

@@ -12,6 +12,7 @@ import datetime
 # utc时间
 nowutc = (datetime.datetime.utcnow())
 print(nowutc)
+# dtimeArray = time.strftime(nowutc)
 # utc转换时间
 timenow = (datetime.datetime.utcnow() + datetime.timedelta(hours=8))
 print(timenow)

+ 45 - 34
Controller/CloudVod.py

@@ -14,6 +14,7 @@
 import datetime
 import json
 import time
+import math
 
 import oss2
 import paypalrestsdk
@@ -117,10 +118,11 @@ class CloudVodView(View):
         uidf = Device_Info.objects.filter(userID_id=userID)
         if did:
             uidf.filter(id=did)
-        uids = uidf.values_list("UID",flat=True)
+        uids = uidf.values_list("UID", flat=True)
         uid_list = list(uids)
-        dmqs = Device_Meal.objects.filter(uid__in=uid_list).values('rank', 'status', 'channel', 'add_time', 'update_time',
-                                                              'end_time', 'rank__title')
+        dmqs = Device_Meal.objects.filter(uid__in=uid_list).values('rank', 'status', 'channel', 'add_time',
+                                                                   'update_time',
+                                                                   'end_time', 'rank__title')
         res = CommonService.qs_to_list(dmqs)
         return response.json(0, res)
 
@@ -134,22 +136,26 @@ class CloudVodView(View):
         if not qs.exists():
             return response.json(13)
         uid = qs[0]['UID']
-        dmqs = Device_Meal.objects.filter(channel=channel, uid=uid).values("rank_id")
+        dmqs = Device_Meal.objects.filter(channel=channel, uid=uid).values("rank_id", "end_time")
         # 续费模式
         if dmqs.exists():
-            if rank < dmqs[0]['rank_id']:
-                return response.json(10,'不能降级套餐')
+            rankId = dmqs[0]['rank_id']
+            end_time = dmqs[0]['end_time']
+            now_time = timezone.localtime(timezone.now())
+            if now_time < end_time:
+                if rank < rankId:
+                    return response.json(10, '不能降级套餐')
         nowTime = int(time.time())
-        orderID = CommonService.createID()
-        smqs = Store_Meal.objects.filter(id=rank).values("currency", "price", "content","day")
+        orderID = CommonService.createOrderID()
+        smqs = Store_Meal.objects.filter(id=rank).values("currency", "price", "content", "day")
         if not smqs.exists():
             return response.json(10, '套餐不存在')
         currency = smqs[0]['currency']
         price = smqs[0]['price']
         content = smqs[0]['content']
         day = smqs[0]['day']
-        call_sub_url = "{SERVER_DOMAIN}cloudVod/payExecute?orderID={orderID}".format(SERVER_DOMAIN=SERVER_DOMAIN,
-                                                                                     orderID=orderID)
+        call_sub_url = "{SERVER_DOMAIN}cloudVod/payExecute?orderID={orderID}". \
+            format(SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
         # call_sub_url = "http://192.168.136.40:8077/cloudVod/payExecute?orderID={orderID}".format(
         #     SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
         call_clc_url = "http://192.168.136.40:8077/cloudVod/cancleorder"
@@ -168,7 +174,6 @@ class CloudVodView(View):
                 "amount": {"total": price, "currency": currency},
                 "description": content
             }]})
-
         if payment.create():
             print("Payment created successfully")
         else:
@@ -179,22 +184,13 @@ class CloudVodView(View):
             if link.rel == "approval_url":
                 approval_url = str(link.href)
                 print("Redirect for approval: %s" % (approval_url))
-                cqs = Order_Model.objects.create(
-                    orderID=orderID,
-                    UID=uid,
-                    channel=channel,
-                    userID_id=userID,
-                    desc=content,
-                    price=price,
-                    currency=currency,
-                    addTime=nowTime,
-                    updTime=nowTime,
-                    endTime=nowTime+int(day)*3600*24,
-                    rank_id=rank,
-                    paypal=approval_url
-                )
+                Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID, desc=content,
+                                           price=price, currency=currency, addTime=nowTime, updTime=nowTime,
+                                           endTime=nowTime + int(day) * 3600 * 24,
+                                           rank_id=rank,
+                                           paypal=approval_url
+                                           )
                 return response.json(0, {"redirectUrl": approval_url})
-                # return HttpResponseRedirect(approval_url)
         return response.json(10, 'generate_order_false')
 
     def do_getSts(self, request_dict, ip, response):
@@ -368,11 +364,12 @@ class CloudVodView(View):
         rank_id = order_list[0]['rank_id']
         UID = order_list[0]['UID']
         channel = order_list[0]['channel']
-        smqs = Store_Meal.objects.filter(id=rank_id).values("day")
+        smqs = Store_Meal.objects.filter(id=rank_id).values("day", "bucket__storeDay")
         if not smqs.exists():
             return response.json(0, '套餐已删除')
         add_days = smqs[0]["day"]
-        dmqs = Device_Meal.objects.filter(channel=channel, uid=UID).values("end_time")
+        dmqs = Device_Meal.objects.filter(channel=channel, uid=UID).values("end_time", "rank_id",
+                                                                           "rank__bucket__storeDay")
         # 续费模式
         if dmqs.exists():
             end_time = dmqs[0]["end_time"]
@@ -380,7 +377,21 @@ class CloudVodView(View):
             if now_time > end_time:
                 end_time = now_time + datetime.timedelta(days=int(add_days))
             else:
-                end_time = end_time + datetime.timedelta(days=int(add_days))
+                dmrankID = dmqs[0]['rank_id']
+                # 在同一个关联套餐下加
+                if rank_id == dmrankID:
+                    timedelta = datetime.timedelta(days=int(add_days))
+                    end_time = end_time + timedelta
+                else:
+                    origin_storeDay = int(dmqs[0]['rank__bucket__storeDay'])
+                    upgrade_storeDay = int(smqs[0]['bucket__storeDay'])
+                    now_time = timezone.localtime(timezone.now())
+                    endTime = time.mktime(end_time.timetuple())
+                    ctcTime = endTime - now_time
+                    multiple = math.ceil(upgrade_storeDay / origin_storeDay)
+                    print(multiple)
+                    timedelta = datetime.timedelta(seconds=ctcTime / multiple)
+                    end_time = end_time + timedelta
             dmqs.update(end_time=end_time, update_time=now_time)
             # return response.json(0)
         # 新增模式
@@ -430,11 +441,11 @@ class CloudVodView(View):
         dmqs.update(update_time=now_time, status=status)
         utko = UidTokenObject()
         utko.generate(data={'uid': UID, 'channel': channel})
-        uidTkUrl = "{SERVER_DOMAIN}cloudVod/getSts?uidToken={uidToken}".\
-            format(uidToken=utko.token,SERVER_DOMAIN=SERVER_DOMAIN)
-        storeHlsUrl = "{SERVER_DOMAIN}cloudVod/storeplaylist?uidToken={uidToken}".\
-            format(uidToken=utko.token,SERVER_DOMAIN=SERVER_DOMAIN)
-        return response.json(0, {'uidTkUrl': uidTkUrl,'storeHlsUrl':storeHlsUrl})
+        uidTkUrl = "{SERVER_DOMAIN}cloudVod/getSts?uidToken={uidToken}". \
+            format(uidToken=utko.token, SERVER_DOMAIN=SERVER_DOMAIN)
+        storeHlsUrl = "{SERVER_DOMAIN}cloudVod/storeplaylist?uidToken={uidToken}". \
+            format(uidToken=utko.token, SERVER_DOMAIN=SERVER_DOMAIN)
+        return response.json(0, {'uidTkUrl': uidTkUrl, 'storeHlsUrl': storeHlsUrl})
 
     def do_store_palylist(self, request_dict, response):
         uidToken = request_dict.get('uidToken', None)

+ 6 - 2
Controller/Test.py

@@ -33,7 +33,6 @@ from django.shortcuts import render_to_response, render, redirect
 import paypalrestsdk
 from django.http import HttpResponseRedirect, HttpResponse
 
-
 class Test(View):
     '''
     limit=10:指定返回记录的数量
@@ -54,7 +53,12 @@ class Test(View):
 
     @ratelimit(key='ip', rate='1/m')
     def get(self, request, *args, **kwargs):
-
+        from django.utils import timezone
+        import time
+        nowTime = timezone.localtime(timezone.now())
+        ans_time = time.mktime(nowTime.timetuple())
+        print(ans_time)
+        return JsonResponse({'time':ans_time})
         # 阿里云oss测试
         return self.ossCrd()
         # 七牛测试

+ 1 - 1
Service/CommonService.py

@@ -161,7 +161,7 @@ class CommonService:
 
     # 生成订单好
     @staticmethod
-    def createID():
+    def createOrderID():
         random_id = CommonService.RandomStr(6,True)
         order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random_id)
         print('orderID:')