# -*- coding: utf-8 -*- #!/usr/bin/env python import os import sys import datetime PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"}) import django django.setup() from apilib.utils_datetime import to_datetime from apps.web.constant import Const from apps.web.device.models import Device from apps.web.dealer.models import DealerRechargeRecord from apilib.monetary import RMB printMonths = [2,3,4] isPrintNoChargedDev = False#是否打印没有充值的设备清单 def print_month_stat(startTime,endTime,isPrintNoChargedDev=False): def record_value(statsDict,typeName,statKind): if u'洗衣' in typeName: statsDict['washer'][statKind] += 1 elif u'电' in typeName: statsDict['elector'][statKind] += 1 elif u'足' in typeName: statsDict['footer'][statKind] += 1 else: statsDict['other'][statKind] += 1 objs = DealerRechargeRecord.objects.filter(finishedTime__gte = startTime,finishedTime__lte = endTime,status = 'Paid') countAll,devCount,monthDevCount = 0,0,0 chargedDevNos = [] allAgentCost = 0 for obj in objs: needReduce = 0 monthDevCount += len(obj.items) if 'partition' not in obj.settleInfo: allAgentCost = allAgentCost + 2000*len(obj.items) continue for setInfo in obj.settleInfo['partition']: if setInfo['id'] == '6417d4456f29257125ebf705': continue needReduce += setInfo['earned'] allAgentCost = allAgentCost + (obj.totalFee - needReduce) statsDict = {'washer':{'expired':0,'charged':0,'uncharged':0},'elector':{'expired':0,'charged':0,'uncharged':0},'footer':{'expired':0,'charged':0,'uncharged':0},'other':{'expired':0,'charged':0,'uncharged':0}} # for obj in objs: # typeName = obj.devType.get('name','') # if u'洗衣' in typeName: # statsDict['washer']['charged'] += 1 # elif u'电' in typeName: # statsDict['elector']['charged'] += 1 # elif u'足' in typeName: # statsDict['footer']['charged'] += 1 # else: # statsDict['other']['charged'] += 1 objs = Device.objects.filter(simExpireDate__gte = startTime,simExpireDate__lte = endTime) allExpiredDevNos = [] expiredDevCount,unbindDevCount,blueDevCount,simCardUnknowCount = 0,0,0,0 for obj in objs: expiredDevCount += 1 if not obj.ownerId: unbindDevCount += 1 continue if 'B' in obj.logicalCode: blueDevCount += 1 continue typeName = obj.devType.get('name','') record_value(statsDict, typeName, 'expired') if obj.simStatus == 'chargedUnupdated': devCount += 1 record_value(statsDict, typeName, 'charged') elif obj.simStatus == 'updated': record_value(statsDict, typeName, 'uncharged') else: simCardUnknowCount += 1 allExpiredDevNos.append(obj.devNo) print startTime.strftime(Const.DATE_FMT) print u'过期总设备量:%s' % expiredDevCount print u'充值总设备量:%s' % devCount print u'解绑的设备量:%s' % unbindDevCount print u'蓝牙的设备量:%s' % blueDevCount print u'充值总金额(只统计当月充值数据):%s' % countAll print u'洗衣机%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['washer']['expired'],statsDict['washer']['charged'],float(statsDict['washer']['charged'])/float(statsDict['washer']['expired']),statsDict['washer']['uncharged']) print u'充电设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['elector']['expired'],statsDict['elector']['charged'],float(statsDict['elector']['charged'])/float(statsDict['elector']['expired']),statsDict['elector']['uncharged']) print u'足疗设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['footer']['expired'],statsDict['footer']['charged'],float(statsDict['footer']['charged'])/float(statsDict['footer']['expired']),statsDict['footer']['uncharged']) print u'其他设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['other']['expired'],statsDict['other']['charged'],float(statsDict['other']['charged'])/float(statsDict['other']['expired']),statsDict['other']['uncharged']) print u'实际代理商付出成本为%s' % allAgentCost print u'当月充值的设备总量%s' % monthDevCount if isPrintNoChargedDev: notChargedDevNos = list(set(allExpiredDevNos)-set(chargedDevNos)) objs = Device.objects.filter(devNo__in = notChargedDevNos) for obj in objs: print obj.devNo,obj.devType.get('name',''),obj.ownerId for month in printMonths: startTime = to_datetime('2022-%s-01 00:00:00' % month) endTime = startTime + datetime.timedelta(days = 31) print_month_stat(startTime, endTime,isPrintNoChargedDev) # startTime = to_datetime('2018-01-01 00:00:00') # endTime = to_datetime('2022-01-01 00:00:00') # objs = DealerRechargeRecord.objects.filter(finishedTime__gte = startTime,finishedTime__lte = endTime,status = 'Paid') # allPay = RMB(0.0) # devCount = 0 # chargedDevNos = [] # for obj in objs: # devCount += len(obj.items) # chargedDevNos.extend([item['devNo'] for item in obj.items]) # # for devNo in chargedDevNos: # try: # devObj = Device.objects.get(devNo = devNo) # if not devObj.trafficCardCos: # allPay += RMB(20) # else: # allPay += devObj.trafficCardCos # print devCount,allPay # except Exception,e: # continue