#!/usr/bin/python
|
# -*- coding: utf-8 -*-
|
# @todo: 商城物品购买分析
|
|
import CommFunc
|
import ConfigParser
|
import mylog
|
import logging
|
|
cfg = ConfigParser.ConfigParser()
|
cfg.read("../../InterfaceConfig.php")
|
ServerPath = cfg.get("ServerInfo", "ServerPath")
|
|
Def_NeedKeyList = ["MoneyCount", "ShopType", "ShopItemIndex", "ClientBuyCount", "TotalItemList", "time"]
|
|
def queryBuyStoreItem(argvDict):
|
|
startDate = argvDict.get("startDate", "")
|
endDate = argvDict.get("endDate", "")
|
|
moneyType = CommFunc.toInt(argvDict.get("moneyType"), 1) # 货币类型
|
shopTypeList = [CommFunc.toInt(shopTypeStr) for shopTypeStr in argvDict.get("shopTypeInfo", "").split(",")]
|
timeGroupType = CommFunc.toInt(argvDict.get("timeGroupType"), 1)
|
|
|
# 查询中心备份的
|
if CommFunc.isQueryCenterbak(argvDict):
|
queryPlayerItemInfo_CenterBak(startDate, endDate, argvDict, moneyType, shopTypeList)
|
return
|
|
drList = []
|
needQueryCenterbak = CommFunc.loopMainServerDR(cfg, startDate, endDate, argvDict, checkDrFileNeedParseFunc,
|
parseLineFunc, drList, moneyType, shopTypeList)
|
|
if needQueryCenterbak:
|
bakDataList = CommFunc.queryBackupCenterDR(cfg, argvDict)
|
if bakDataList == None:
|
return
|
# 合并数据
|
drList = bakDataList + drList
|
|
drInfoDict = {} # 组合展示数据 {shopType:{dateKey:{shopItemIndex:[singleItemList, totalClientBuyCount], ...}, ...}, ...}
|
for drDict in drList:
|
shopType = drDict["ShopType"]
|
shopItemIndex = drDict["ShopItemIndex"]
|
clientBuyCount = drDict["ClientBuyCount"]
|
totalItemList = drDict["TotalItemList"]
|
moneyCount = drDict["MoneyCount"]
|
buyTimeStr = drDict["time"]
|
|
singleItemList = []
|
if clientBuyCount > 1:
|
for itemID, itemCount, isBind in totalItemList:
|
singleItemList.append([itemID, itemCount / clientBuyCount, isBind])
|
else:
|
singleItemList = totalItemList
|
|
dateYMD = buyTimeStr[:10]
|
dateHour = buyTimeStr[11:13]
|
|
# 按日期
|
if timeGroupType == 1:
|
dateKey = dateYMD
|
# 按限时抢购时段
|
elif timeGroupType == 2:
|
hourKey = dateHour
|
dateHourInt = int(dateHour)
|
for hourA, hourB in [[12, 14], [16, 18], [20, 22]]: # 时段列表
|
if hourA <= dateHourInt <= hourB:
|
hourKey = "%s~%s" % (hourA, hourB)
|
break
|
dateKey = "%s %s" % (dateYMD, hourKey)
|
# 不分组
|
else:
|
dateKey = "All" # 为保持结构一致,也做为key,只是key是空字符串
|
|
if shopType not in drInfoDict:
|
drInfoDict[shopType] = {}
|
shopTypeInfoDict = drInfoDict[shopType]
|
if dateKey not in shopTypeInfoDict:
|
shopTypeInfoDict[dateKey] = {}
|
itemInfoDict = shopTypeInfoDict[dateKey]
|
|
if shopItemIndex not in itemInfoDict:
|
itemInfoDict[shopItemIndex] = [singleItemList, 0, 0]
|
clientBuyCountTotal, moneyCountTotal = itemInfoDict[shopItemIndex][1:]
|
clientBuyCountTotal += clientBuyCount
|
moneyCountTotal += moneyCount
|
itemInfoDict[shopItemIndex] = [singleItemList, clientBuyCountTotal, moneyCountTotal]
|
|
|
itemNameDict = CommFunc.getCfgKeyNameDict("item", argvDict)
|
|
#drList = sorted(drList, key=lambda dr:dr["time"], reverse=False) # 按time升序排
|
|
printStr = "<center><p>%s</p></center>" % _(u"商城购买记录")
|
|
if startDate:
|
printStr += "%s: %s<br/>" % (_(u"开始日期"), startDate)
|
if endDate:
|
printStr += "%s: %s<br/>" % (_(u"结束日期"), endDate)
|
|
for shopType, ymdDict in drInfoDict.items():
|
printStr += "--- %s: 【%s】<br/>" % (_(u"商店类型"), shopType)
|
dateKeyList = ymdDict.keys()
|
dateKeyList.sort()
|
for dateKey in dateKeyList:
|
itemInfoDict = ymdDict[dateKey]
|
tableDRList = []
|
for shopItemIndex, itemInfo in itemInfoDict.items():
|
singleItemList, clientBuyCountTotal, moneyCountTotal = itemInfo
|
itemNameInfo = ""
|
for itemID, itemCount, isBind in singleItemList:
|
itemName = itemNameDict.get(str(itemID), "")
|
if itemName:
|
itemName = "%s(%s)" % (itemName, itemID)
|
else:
|
itemName = "<font color='red'>%s(%s)</font>" % (_(u"未知物品"), itemID)
|
itemNameInfo += "%s*%s%s" % (itemName, itemCount, " ")
|
|
tableDRList.append({"dateKey":dateKey, "shopType":shopType, "shopItemIndex":shopItemIndex,
|
"clientBuyCountTotal":clientBuyCountTotal, "moneyCountTotal":moneyCountTotal,
|
"itemNameInfo":itemNameInfo})
|
|
# 表格输出
|
printStr += CommFunc.editTableHtml(tableDRList, ["dateKey", "shopType", "shopItemIndex", "clientBuyCountTotal", "moneyCountTotal", "itemNameInfo"], "",
|
styleInfo={"dateKey":{"title":_(u"日期时间")},
|
"shopType":{"title":_(u"商城类型")},
|
"shopItemIndex":{"title":_(u"商品ID标识")},
|
"clientBuyCountTotal":{"title":_(u"总购买次数")},
|
"moneyCountTotal":{"title":_(u"货币总数")},
|
"itemNameInfo":{"align":"left", "title":_(u"物品信息")},
|
},
|
printCount=False)
|
printStr += "<br/>"
|
|
# 只会返回最后一个print的内容
|
print printStr
|
return
|
|
def queryPlayerItemInfo_CenterBak(startDate, endDate, argvDict, moneyType, shopTypeList):
|
drList = []
|
|
if not CommFunc.loopCenterbakRarDR(cfg, startDate, endDate, argvDict, checkDrFileNeedParseFunc,
|
parseLineFunc, drList, moneyType, shopTypeList):
|
return
|
|
return CommFunc.queryBackupCenterOK(drList)
|
|
def checkDrFileNeedParseFunc(drFileName, *parseArgs, **kv):
|
''' 检查流向是否需要处理
|
@param drFileName: 流向文件名 xxx_日期.txt
|
@param *parseArgs: 自定义参数
|
@return: isNeed, checkNeedParseRetInfo
|
'''
|
|
moneyType = parseArgs[1]
|
|
if moneyType == 1 and drFileName.startswith("UseGold_"):
|
return True, None
|
|
elif moneyType == 2 and drFileName.startswith("UseGoldPaper_"):
|
return True, None
|
|
elif moneyType == 3 and drFileName.startswith("UseSilver_"):
|
return True, None
|
|
elif drFileName.startswith("UseCurrency_%s_" % moneyType):
|
return True, None
|
|
return False, None
|
|
def parseLineFunc(drName, dateStr, checkNeedParseRetInfo, line, *parseArgs, **kv):
|
''' 解析流向行内容
|
@param drName: 流向名
|
@param dateStr: 对应日期字符串
|
@param checkNeedParseRetInfo: checkDrFileNeedParseFunc 返回的内容
|
@param line: 本行内容
|
@param *parseArgs: 自定义参数
|
'''
|
|
if "BuyStoreItem" not in line or "ShopType" not in line:
|
return
|
|
drList, moneyType, shopTypeList = parseArgs
|
|
drDict = eval(line)
|
|
if drDict["MoneyType"] != moneyType:
|
return
|
|
shopType = drDict["ShopType"]
|
if shopType not in shopTypeList:
|
return
|
|
drList.append({key:drDict[key] for key in Def_NeedKeyList})
|
return
|
|
def main():
|
CommFunc.setdefaultencoding()
|
argvDict = CommFunc.parse_args()
|
mylog.InitMyLog(argvDict.get("eventType", ""))
|
CommFunc.gettextInstall(argvDict.get("lang", ""))
|
queryBuyStoreItem(argvDict)
|
return
|
|
if __name__ == "__main__":
|
try:
|
main()
|
except:
|
CommFunc.printExceptionError()
|