month_sim_charge.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os
  4. import sys
  5. import datetime
  6. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  7. sys.path.insert(0, PROJECT_ROOT)
  8. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
  9. import django
  10. django.setup()
  11. from apilib.utils_datetime import to_datetime
  12. from apps.web.constant import Const
  13. from apps.web.device.models import Device
  14. from apps.web.dealer.models import DealerRechargeRecord
  15. from apilib.monetary import RMB
  16. printMonths = [2,3,4]
  17. isPrintNoChargedDev = False#是否打印没有充值的设备清单
  18. def print_month_stat(startTime,endTime,isPrintNoChargedDev=False):
  19. def record_value(statsDict,typeName,statKind):
  20. if u'洗衣' in typeName:
  21. statsDict['washer'][statKind] += 1
  22. elif u'电' in typeName:
  23. statsDict['elector'][statKind] += 1
  24. elif u'足' in typeName:
  25. statsDict['footer'][statKind] += 1
  26. else:
  27. statsDict['other'][statKind] += 1
  28. objs = DealerRechargeRecord.objects.filter(finishedTime__gte = startTime,finishedTime__lte = endTime,status = 'Paid')
  29. countAll,devCount,monthDevCount = 0,0,0
  30. chargedDevNos = []
  31. allAgentCost = 0
  32. for obj in objs:
  33. needReduce = 0
  34. monthDevCount += len(obj.items)
  35. if 'partition' not in obj.settleInfo:
  36. allAgentCost = allAgentCost + 2000*len(obj.items)
  37. continue
  38. for setInfo in obj.settleInfo['partition']:
  39. if setInfo['id'] == '6417d4456f29257125ebf705':
  40. continue
  41. needReduce += setInfo['earned']
  42. allAgentCost = allAgentCost + (obj.totalFee - needReduce)
  43. 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}}
  44. # for obj in objs:
  45. # typeName = obj.devType.get('name','')
  46. # if u'洗衣' in typeName:
  47. # statsDict['washer']['charged'] += 1
  48. # elif u'电' in typeName:
  49. # statsDict['elector']['charged'] += 1
  50. # elif u'足' in typeName:
  51. # statsDict['footer']['charged'] += 1
  52. # else:
  53. # statsDict['other']['charged'] += 1
  54. objs = Device.objects.filter(simExpireDate__gte = startTime,simExpireDate__lte = endTime)
  55. allExpiredDevNos = []
  56. expiredDevCount,unbindDevCount,blueDevCount,simCardUnknowCount = 0,0,0,0
  57. for obj in objs:
  58. expiredDevCount += 1
  59. if not obj.ownerId:
  60. unbindDevCount += 1
  61. continue
  62. if 'B' in obj.logicalCode:
  63. blueDevCount += 1
  64. continue
  65. typeName = obj.devType.get('name','')
  66. record_value(statsDict, typeName, 'expired')
  67. if obj.simStatus == 'chargedUnupdated':
  68. devCount += 1
  69. record_value(statsDict, typeName, 'charged')
  70. elif obj.simStatus == 'updated':
  71. record_value(statsDict, typeName, 'uncharged')
  72. else:
  73. simCardUnknowCount += 1
  74. allExpiredDevNos.append(obj.devNo)
  75. print startTime.strftime(Const.DATE_FMT)
  76. print u'过期总设备量:%s' % expiredDevCount
  77. print u'充值总设备量:%s' % devCount
  78. print u'解绑的设备量:%s' % unbindDevCount
  79. print u'蓝牙的设备量:%s' % blueDevCount
  80. print u'充值总金额(只统计当月充值数据):%s' % countAll
  81. print u'洗衣机%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['washer']['expired'],statsDict['washer']['charged'],float(statsDict['washer']['charged'])/float(statsDict['washer']['expired']),statsDict['washer']['uncharged'])
  82. print u'充电设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['elector']['expired'],statsDict['elector']['charged'],float(statsDict['elector']['charged'])/float(statsDict['elector']['expired']),statsDict['elector']['uncharged'])
  83. print u'足疗设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['footer']['expired'],statsDict['footer']['charged'],float(statsDict['footer']['charged'])/float(statsDict['footer']['expired']),statsDict['footer']['uncharged'])
  84. print u'其他设备%s台过期,%s台充值,充值率:%s,还有%s台未充值' % (statsDict['other']['expired'],statsDict['other']['charged'],float(statsDict['other']['charged'])/float(statsDict['other']['expired']),statsDict['other']['uncharged'])
  85. print u'实际代理商付出成本为%s' % allAgentCost
  86. print u'当月充值的设备总量%s' % monthDevCount
  87. if isPrintNoChargedDev:
  88. notChargedDevNos = list(set(allExpiredDevNos)-set(chargedDevNos))
  89. objs = Device.objects.filter(devNo__in = notChargedDevNos)
  90. for obj in objs:
  91. print obj.devNo,obj.devType.get('name',''),obj.ownerId
  92. for month in printMonths:
  93. startTime = to_datetime('2022-%s-01 00:00:00' % month)
  94. endTime = startTime + datetime.timedelta(days = 31)
  95. print_month_stat(startTime, endTime,isPrintNoChargedDev)
  96. # startTime = to_datetime('2018-01-01 00:00:00')
  97. # endTime = to_datetime('2022-01-01 00:00:00')
  98. # objs = DealerRechargeRecord.objects.filter(finishedTime__gte = startTime,finishedTime__lte = endTime,status = 'Paid')
  99. # allPay = RMB(0.0)
  100. # devCount = 0
  101. # chargedDevNos = []
  102. # for obj in objs:
  103. # devCount += len(obj.items)
  104. # chargedDevNos.extend([item['devNo'] for item in obj.items])
  105. #
  106. # for devNo in chargedDevNos:
  107. # try:
  108. # devObj = Device.objects.get(devNo = devNo)
  109. # if not devObj.trafficCardCos:
  110. # allPay += RMB(20)
  111. # else:
  112. # allPay += devObj.trafficCardCos
  113. # print devCount,allPay
  114. # except Exception,e:
  115. # continue