withdraw_repair.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # coding=utf-8
  2. from base import init_env
  3. import datetime
  4. init_env(True)
  5. from apps.web.agent.proxy import record_agent_withdraw_fee
  6. from apilib.monetary import RMB
  7. from apps.web.agent.models import Agent, AgentIncomeReport
  8. from apps.web.dealer.models import Dealer
  9. from apps.web.common.models import WithdrawRecord
  10. def get_records():
  11. """
  12. 获取经销商提现成功的 记录
  13. :return:
  14. :rtype:
  15. """
  16. # records = WithdrawRecord.objects.filter(postTime__gte=datetime.datetime(2020, 9, 8, 7, 0, 0), status=1, role="dealer").all()
  17. agent = Agent.objects.get(username="")
  18. dealers = Dealer.objects.get(agentId=str(agent.id))
  19. records = WithdrawRecord.objects.filter(ownerId__in=dealers).all()
  20. # 这一条测试执行了
  21. need_record = list()
  22. for record in records:
  23. if str(record.id) != "5f573cd4003048a623a02b29":
  24. need_record.append(record)
  25. record_ids = "\n".join([str(item.id) for item in need_record]) + "\n"
  26. write_data(record_ids)
  27. return need_record
  28. def get_record_partition(record):
  29. amount = record.amount
  30. dealer = Dealer.objects.get(id=record.ownerId)
  31. if not dealer:
  32. print("not dealer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  33. agent = Agent.objects.get(id=dealer.agentId)
  34. if not agent:
  35. print("noe agent !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  36. agentFeeRation = agent.withdrawFeeRatio
  37. managerFeeRatio = agent.withdrawFeeRatioCost
  38. dealerFeeRatio = dealer.withdrawFeeRatio
  39. # 计算服务费以及实际需要提现的费用
  40. serviceFee = amount * dealerFeeRatio.as_ratio
  41. under_little_service_fee = dealer.withdraw_little_fee(record.payType)
  42. serviceFee += under_little_service_fee
  43. actualPay = amount - serviceFee
  44. if dealerFeeRatio > managerFeeRatio:
  45. earned = amount * (dealerFeeRatio - managerFeeRatio).as_ratio
  46. if earned < RMB(0):
  47. earned = RMB(0)
  48. elif earned > serviceFee:
  49. earned = serviceFee
  50. # 分给代理商
  51. agentEarned = RMB(0)
  52. if dealerFeeRatio > agentFeeRation:
  53. agentEarned = amount * (dealerFeeRatio - agentFeeRation).as_ratio
  54. if agentEarned < RMB(0):
  55. agentEarned = RMB(0)
  56. elif agentEarned > earned:
  57. agentEarned = earned
  58. # 代理商分完如果还有 继续分给厂商
  59. managerEarned = (earned - agentEarned)
  60. if managerEarned < RMB(0):
  61. managerEarned = RMB(0)
  62. else:
  63. agentEarned = RMB(0)
  64. managerEarned = RMB(0)
  65. # 计算出了这个record真正 的record
  66. partition = [
  67. {
  68. 'role': "agent",
  69. 'id': str(agent.id),
  70. 'cost': agentFeeRation.mongo_amount,
  71. 'earned': agentEarned.mongo_amount
  72. },
  73. {
  74. 'role': "manager",
  75. 'id': str(agent.primary_agent.id),
  76. 'cost': managerFeeRatio.mongo_amount,
  77. 'earned': managerEarned.mongo_amount
  78. }
  79. ]
  80. return partition
  81. def repaired(record, new_partition):
  82. # 两个处理的方式 直系代理商ID 等于 主代理商ID
  83. dealer = Dealer.objects.get(id=record.ownerId)
  84. write_data("withdraw is <{}>, odl partition is {}, new partition is {}\n".format(record.id, record.partition,
  85. new_partition))
  86. _map = {}
  87. for item in new_partition:
  88. agent_id = item['id']
  89. if agent_id in _map:
  90. _map[agent_id]['earned'] += RMB(item['earned'])
  91. else:
  92. _map[agent_id] = {
  93. 'earned': RMB(item['earned'])
  94. }
  95. for agent_id, item in _map.iteritems():
  96. earned = item['earned']
  97. if earned <= RMB(0):
  98. continue
  99. detail = {
  100. 'recharge_record_id': record.id,
  101. 'name': dealer.nickname,
  102. 'sum_of_price': record.serviceFee,
  103. 'withdrawAmount': record.amount,
  104. 'withdrawFeeRatio': record.withdrawFeeRatio
  105. }
  106. # 这个地方不需要做判断 record是同一条 id不变的情况下 该函数内部已经做了是否建立起的
  107. if AgentIncomeReport.get_collection().find(
  108. {'detail.recharge_record_id': record.id, 'agentId': agent_id}).count() > 0:
  109. incomeRecord = AgentIncomeReport.objects.filter(detail__recharge_record_id=record.id,
  110. agentId=agent_id).first()
  111. oldAmount = incomeRecord.amount
  112. oldDetail = incomeRecord.detail
  113. incomeRecord.amount = earned
  114. incomeRecord.detail = detail
  115. incomeRecord.save()
  116. write_data(
  117. "AgentIncome <{}> has been repaired, old amount is <{}>, new amount is <{}>".format(incomeRecord.id,
  118. oldAmount, earned))
  119. write_data("AgentIncome <{}> has been repaired, old detail is {}, new detail is {}".format(incomeRecord.id,
  120. oldDetail,
  121. detail))
  122. # agent = Agent.objects.get(agent_id)
  123. # # 更正收益
  124. # agent.decr_income("dealer_withdraw_fee", record.source_key, oldAmount)
  125. # agent.incr_income("dealer_withdraw_fee", record.source_key, earned)
  126. # write_data("agent income has been repaired, old income is {}, new income is {}".format(oldAmount, oldDetail, earned))
  127. else:
  128. record_agent_withdraw_fee(agentId=agent_id, source_key=record.source_key, amount=earned, detail=detail)
  129. write_data(
  130. "new a AgentIncome detail.recharge_record_id is <{}>, agentId is <{}>\n".format(record.id, agent_id))
  131. write_data("agent income has been repaired! new_income is <{}>\n\n".format(earned))
  132. # 最后吧 withdraw 更正过来
  133. record.partition = new_partition
  134. record.save()
  135. def write_data(data):
  136. with open("repaired.log", "a") as f:
  137. f.write(data)
  138. def main():
  139. records = get_records()
  140. for record in records:
  141. new_p = get_record_partition(record)
  142. repaired(record, new_p)
  143. if __name__ == '__main__':
  144. main()