user_and_agent_product_id.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # coding=utf-8
  2. import csv
  3. import sys
  4. import os
  5. from base import init_env
  6. from decimal import InvalidOperation
  7. env = sys.argv[1]
  8. os.environ.setdefault('DJANGO_SETTINGS_MODULE', env)
  9. init_env(interactive=False)
  10. from apps.web.helpers import get_wechat_user_manager_mp_proxy
  11. from apps.web.user.models import MyUser
  12. from apps.web.agent.models import Agent
  13. no_agent_user = list()
  14. error_user = list()
  15. agent_map = dict()
  16. wechat_map = dict()
  17. decimal_error = 0
  18. def get_agent(agentId):
  19. if agentId not in agent_map:
  20. try:
  21. agent = Agent.objects.get(id=agentId)
  22. except Exception as e:
  23. agent = None
  24. agent_map[agentId] = agent
  25. else:
  26. agent = agent_map.get(agentId)
  27. return agent
  28. def check_agent(agent):
  29. # 正常的no group 用户
  30. if hasattr(agent, "wechatUserManagerialApp") and agent.wechatUserManagerialApp:
  31. return True
  32. # 该用户的 agent 不是一级代理商
  33. else:
  34. return False
  35. def get_user_data(user):
  36. return {
  37. "userId": str(user.id),
  38. "balance": str(user.balance) if hasattr(user, "balance") else "",
  39. "gateway": str(user.gateway) if hasattr(user, "gateway") else "",
  40. "totalRecharge": str(user.total_recharged) if hasattr(user, "total_recharged") else "",
  41. "totalConsume": str(user.total_consumed) if hasattr(user, "total_consumed") else "",
  42. "addTime": str(user.dateTimeAdded)[:7] if hasattr(user, "dateTimeAdded") else "",
  43. }
  44. def check_user(user):
  45. agentId = user.agentId
  46. agent = get_agent(agentId)
  47. if not agent:
  48. data = get_user_data(user)
  49. data.update(
  50. {"agentId": agentId if agentId else ""}
  51. )
  52. return
  53. if not check_agent(agent):
  54. data = get_user_data(user)
  55. data.update({
  56. "agentId": str(user.agentId),
  57. "agentProductName": agent.productName if agent else ""
  58. })
  59. error_user.append(data)
  60. return
  61. def get_productId(agent):
  62. if str(agent.id) in wechat_map:
  63. return wechat_map.get(str(agent.id))
  64. productId = get_wechat_user_manager_mp_proxy(agent).occupantId
  65. wechat_map[str(agent.id)] = productId
  66. return productId
  67. def add_productId(user):
  68. agentId = user.agentId
  69. agent = get_agent(agentId)
  70. if not agent:
  71. return
  72. productId = get_productId(agent)
  73. user.productAgentId = productId
  74. try:
  75. user.save()
  76. except Exception as e:
  77. pass
  78. def write_csv(**kwargs):
  79. name = kwargs.keys()[0]
  80. user_lis = kwargs.get(name)
  81. if user_lis:
  82. fieldNames = user_lis[0].keys()
  83. with open("{}.csv".format(name), "w", ) as csvFile:
  84. writer = csv.DictWriter(csvFile, fieldnames=fieldNames,lineterminator='\n')
  85. writer.writeheader()
  86. writer.writerows(user_lis)
  87. def main():
  88. index = 0
  89. lens = 1000
  90. while True:
  91. users = MyUser.objects.filter(groupId="")[index: index+lens]
  92. if users:
  93. try:
  94. for user in users:
  95. if hasattr(user, "groupId") and not user.groupId:
  96. # check_user(user)
  97. add_productId(user)
  98. except Exception as e:
  99. print "error index: %s" % index
  100. print e
  101. index += lens
  102. else:
  103. break
  104. # write_csv(error_user=error_user)
  105. # write_csv(no_agent_user=no_agent_user)
  106. if __name__ == '__main__':
  107. main()