1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/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, "&nbsp;&nbsp;")
            
                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()