From db68b3b62ed137a98c3a8581923a7f7abd88c7c6 Mon Sep 17 00:00:00 2001
From: hxp <ale99527@vip.qq.com>
Date: 星期一, 18 十一月 2024 10:56:14 +0800
Subject: [PATCH] 1111 修复新服首次维护后战力榜战力数值异常bug;

---
 Tool/WebCenter/CouponCode/webapp.py |  122 ++++++++++++++++++++++++++++------------
 1 files changed, 84 insertions(+), 38 deletions(-)

diff --git a/Tool/WebCenter/CouponCode/webapp.py b/Tool/WebCenter/CouponCode/webapp.py
index 2628699..c827426 100644
--- a/Tool/WebCenter/CouponCode/webapp.py
+++ b/Tool/WebCenter/CouponCode/webapp.py
@@ -25,6 +25,7 @@
 import os.path
 import zipfile
 import time
+import traceback
 
 # get: request.query.username request.GET.get('username','')
 # post: request.forms.get('username')  request.POST.get('username')
@@ -45,12 +46,23 @@
 # GM绔彛
 PushPort = ConfigIO.GetValue("Coupon", "PushPort")
 PushKey = ConfigIO.GetValue("Coupon", "PushKey")
+PushTimeout = ConfigIO.GetInt("Coupon", "PushTimeout", 5)
 CommonCards = eval(ConfigIO.GetValue("Coupon", "CommonCards"))
 
+g_mailJsonDict = {} # {channel:MailJson, ...}
 # 鍙戞斁濂栧姳閭欢鍐呭json锛寀tf8缂栫爜
-f = open(os.getcwd() + "\\..\\Coupon\\CodeMail.json", "r")
-MailJson = eval(f.read())
-f.close()
+def GetMailJson(channel):
+    global g_mailJsonDict
+    if channel in g_mailJsonDict:
+        return g_mailJsonDict[channel]
+    jsonPath = os.getcwd() + ("\\..\\Coupon\\CodeMail_%s.json" % channel)
+    if not os.path.isfile(jsonPath):
+        jsonPath = os.getcwd() + "\\..\\Coupon\\CodeMail.json"
+    f = open(jsonPath, "r")
+    MailJson = eval(f.read())
+    f.close()
+    g_mailJsonDict[channel] = MailJson
+    return MailJson
 
 # 绀煎寘浣跨敤杩斿洖缁撴灉鐮佸畾涔�
 ErrCode_OK = 0 # 鎴愬姛锛岄潪0鐨勭爜鍧囦唬琛ㄩ敊璇�
@@ -83,8 +95,11 @@
     if msgEx:
         errmsg = "%s : %s" % (errmsg, msgEx)
     resData = {"errcode":errcode, "errmsg":errmsg}
-    mylog.debug("resData: %s" % str(resData))
-    #mylog.info("resData: %s" % str(resData))
+    if errcode != ErrCode_OK:
+        mylog.error("resError: %s" % str(resData))
+    else:
+        mylog.debug("resData: %s" % str(resData))
+        #mylog.info("resData: %s" % str(resData))
     return json.dumps(resData, ensure_ascii=False)
 
                 
@@ -218,18 +233,20 @@
         result, commondata = dbController.find_one(CouponDB.CouponCodeColName + "_Common", {"code":codeStr, "accid":accid})
         if commondata:
             # 宸蹭娇鐢�
-            return SendGm(ErrCode_Used, gmresult, pushurl)
+            return SendGm(ErrCode_Used, gmresult, pushurl)[1]
         
         commCheckStatus = CommCheck(dataDict, codeStr)
         if commCheckStatus != None:
-            return SendGm(commCheckStatus, gmresult, pushurl)
+            return SendGm(commCheckStatus, gmresult, pushurl)[1]
         
         gmresult['code'] = codeStr
         gmresult['coupontype'] = codeStr  # 缁熶竴鏍煎紡锛屾湇鍔″櫒涓嶄竴瀹氭湁鐢紝鍥哄畾鐮佹牴鎹� codeStr 鍙戝鍔�
         
-        dbController.insert(CouponDB.CouponCodeColName + "_Common", {"code":codeStr, "accid":accid})
-        mylog.debug("common ok")
-        return SendGm(ErrCode_OK, gmresult, pushurl)
+        retCode, retMsg = SendGm(ErrCode_OK, gmresult, pushurl)
+        if retCode == ErrCode_OK:
+            dbController.insert(CouponDB.CouponCodeColName + "_Common", {"code":codeStr, "accid":accid})
+            mylog.debug("common ok")
+        return retMsg
     
     #-----------寰俊鍟嗗煄----------------------
     if codeStr.startswith('wx'):
@@ -242,7 +259,7 @@
         
         if int(wxdata['status']) == 1:
             mylog.debug("wxcard used!")
-            return SendGm(ErrCode_Used, gmresult, pushurl)
+            return SendGm(ErrCode_Used, gmresult, pushurl)[1]
         
         wxdata['status'] = 1
         wxdata['accid'] = dataDict.get("accid", "")     # 姝ゅ鐢ㄥ師濮嬭处鍙�
@@ -267,37 +284,52 @@
     if not data:
         #鏃犳鍗�
         mylog.debug("no card")
-        return SendGm(ErrCode_Invalid, gmresult, pushurl)
+        return SendGm(ErrCode_Invalid, gmresult, pushurl)[1]
     if int(data['status']) == 1:
         #宸蹭娇鐢紝鍚屽崱鍙疯褰曢粯璁ゅ彲鐢紝閬垮厤鏂嚎鍙戦�佸け璐ョ殑鎯呭喌锛堟湭杩斿洖缁撴灉楠岃瘉鎯呭喌涓嬶級
         mylog.debug("card used!")
-        return SendGm(ErrCode_Used, gmresult, pushurl)
-    
+        return SendGm(ErrCode_Used, gmresult, pushurl)[1]
+        
+    couponType = data["coupontype"]
     commCheckStatus = CommCheck(dataDict, data["coupontype"])
     if commCheckStatus != None:
-        return SendGm(commCheckStatus, gmresult, pushurl)
+        return SendGm(commCheckStatus, gmresult, pushurl)[1]
     
     result, data2 = dbController.find_one(CouponDB.CouponBatchColName, {"couponid":data["couponid"]})
     if not data2:
         #姝ゆ壒娆″崱宸插垹闄�
         mylog.debug("no couponid")
-        return SendGm(ErrCode_Invalid, gmresult, pushurl)
+        return SendGm(ErrCode_Invalid, gmresult, pushurl)[1]
     if int(data2['status']) == 1:
         #鏆傚仠浣跨敤璇ユ壒娆″崱
         mylog.debug("couponid frozed")
-        return SendGm(ErrCode_Invalid, gmresult, pushurl)
+        return SendGm(ErrCode_Invalid, gmresult, pushurl)[1]
     if (data2["minserverno"] != 0 and data2["maxserverno"] != 0):
         if int(data2["minserverno"]) > sid or int(data2["maxserverno"]) < sid:
             #涓嶅湪鎸囧畾鍖�
             mylog.debug("no sid")
-            return SendGm(ErrCode_Invalid, gmresult, pushurl)
+            return SendGm(ErrCode_Invalid, gmresult, pushurl)[1]
     if data2["expiretime"] != "" and datetime.datetime.today() > GetDateTimeByStr(data2["expiretime"]):
         #宸茶繃鏈�
         mylog.debug("time pass")
-        return SendGm(ErrCode_Invalid, gmresult, pushurl)
+        return SendGm(ErrCode_Invalid, gmresult, pushurl)[1]
     
+    maxUseCount = 0 # -1-鏃犻檺锛�0鎴�1-榛樿1娆★紱 >1-闄愬埗娆℃暟锛� 娌℃湁閰嶇疆鐨勯粯璁�0鍗抽粯璁�1娆�
+    prefix = "" # 鍥哄畾鍓嶇紑
+    MailJson = GetMailJson(agentName)
+    if couponType in MailJson:
+        mailInfo = MailJson[couponType]
+        prefix = mailInfo.get("Prefix", "")
+        maxUseCount = CommFunc.ToIntDef(mailInfo.get("MaxUseCount", 0))
+    elif ConfigIO.HasSection(couponType):
+        if ConfigIO.HasOption(couponType, "Prefix"):
+            prefix = ConfigIO.GetValue(couponType, "Prefix")
+            maxUseCount = ConfigIO.GetInt(couponType, "MaxUseCount")
+            
     # h 寮�澶寸殑鍗$被鍨嬪彲閲嶅浣跨敤
-    if not codeStr.startswith('h'):
+    #if not codeStr.startswith(prefix + 'h'):
+    #淇敼涓烘寜閰嶇疆鏈�澶ф鏁板垽鏂嵆鍙�
+    if maxUseCount in [0, 1]:
         #鍐嶆煡涓�娆℃槸鍚︾敤杩囨鎵规鐨勫叾浠栧崱, 
         result, data3 = dbController.find_one(CouponDB.CouponCodeColName + "_" + agentName, \
                                               {"coupontype":data["coupontype"], "accid":accid})
@@ -306,22 +338,32 @@
             gmresult['coupontype'] = data["coupontype"]
             gmresult['code'] = codeStr
             mylog.debug("use again")
-            return SendGm(ErrCode_TypeUsed, gmresult, pushurl)
+            return SendGm(ErrCode_TypeUsed, gmresult, pushurl)[1]
+    elif maxUseCount > 1:
+        result, resultList = dbController.find(CouponDB.CouponCodeColName + "_" + agentName, {"coupontype":data["coupontype"], "accid":accid}, {"code":1})
+        if not result or len(resultList) >= maxUseCount:
+            gmresult['coupontype'] = data["coupontype"]
+            gmresult['code'] = codeStr
+            mylog.debug("use maxUseCount limit. %s" % maxUseCount)
+            return SendGm(ErrCode_TypeUsed, gmresult, pushurl)[1]
         
     data['status'] = 1
     data['accid'] = accid
     data['usetime'] = str(datetime.datetime.today())
     
-    dbController.update(CouponDB.CouponCodeColName + "_" + agentName, {"code":codeStr, "channel":agentName}, data)
     gmresult['code'] = codeStr
     gmresult['coupontype'] = data["coupontype"]
     
-    mylog.debug("card ok!")
-    return SendGm(ErrCode_OK, gmresult, pushurl)
+    retCode, retMsg = SendGm(ErrCode_OK, gmresult, pushurl)
+    if retCode == ErrCode_OK:
+        dbController.update(CouponDB.CouponCodeColName + "_" + agentName, {"code":codeStr, "channel":agentName}, data)
+        mylog.debug("card ok!")
+    return retMsg
 
 def CommCheck(dataDict, coupontype):
     ## 閫氱敤甯歌妫�鏌�
     
+    MailJson = GetMailJson(dataDict.get("channel", ""))
     if coupontype in MailJson:
         mailInfo = MailJson[coupontype]
         lvLimit = mailInfo.get("LV", 0)
@@ -354,9 +396,9 @@
         else:
             gmurl = "http://%s:%s/Server/Tool.php" % (pushurl, PushPort)            
         gmkey = PushKey
-        mylog.debug("SendGm:%s" % gmurl)
         if not gmkey or not PushPort:
-            return CouponCodeResponse(ErrCode_OtherErr, "no PushKey or not PushPort")
+            return ErrCode_OtherErr, CouponCodeResponse(ErrCode_OtherErr, "no PushKey or not PushPort")
+        mylog.debug("SendGm:%s" % gmurl)
         
         notifyMsg = ""
         if errcode in ErrCodeMsgInfo:
@@ -369,9 +411,10 @@
             SendGMTCMD(gmurl, gmtData, gmkey, "GMT_MediaCard")
             
         if errcode != ErrCode_OK:
-            return CouponCodeResponse(errcode)
+            return errcode, CouponCodeResponse(errcode)
         
         coupontype = gmresult["coupontype"]
+        MailJson = GetMailJson(gmresult["channel"])
         if coupontype in MailJson:
             mailInfo = MailJson[coupontype]
             mailTitle = mailInfo.get("MailTitle", "MailTitle")
@@ -402,14 +445,15 @@
             
         pack_data["Detail"] = "cardCode:" + gmresult["code"]
         
-        SendGMTCMD(gmurl, pack_data, gmkey, "GMT_AddPersonalCompensation")
-        return CouponCodeResponse(ErrCode_OK)
-    except Exception, e:
-        mylog.debug("SendGm error %s" % e)
-        return CouponCodeResponse(ErrCode_OtherErr, "gm error")
-    
-    return
-    
+        ResultType = SendGMTCMD(gmurl, pack_data, gmkey, "GMT_AddPersonalCompensation")
+        if ResultType == 0:
+            return ErrCode_OK, CouponCodeResponse(ErrCode_OK)
+        else:
+            return ErrCode_OtherErr, CouponCodeResponse(ErrCode_OtherErr, "ResultType error is %s" % ResultType)
+    except BaseException:
+        mylog.error("SendGm error %s" % traceback.format_exc())
+    return ErrCode_OtherErr, CouponCodeResponse(ErrCode_OtherErr, "gm error")
+
 def SendGMTCMD(gmurl, gmtData, gmkey, pack_type):
     ## 鍙戦�丟M宸ュ叿鍛戒护
     pack_data = {}
@@ -425,10 +469,12 @@
     post = {}
     post['pack'] = pack_data_dict;
     post['sign'] = sign;
-    result = urllib2.urlopen(gmurl, urllib.urlencode(post), 3)
-    #content = result.read()
+    result = urllib2.urlopen(gmurl, urllib.urlencode(post), PushTimeout)
+    retContent = result.read()
+    resultDict = json.loads(retContent)
     result.close()
-    return
+    ResultType = resultDict.get("ResultType", 5) # 5鏈煡閿欒
+    return ResultType
 
     
 def GetDateTimeByStr(timeStr, timeFomat="%Y-%m-%d"):

--
Gitblit v1.8.0