123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- # coding=utf-8
- from base import init_env
- import datetime
- init_env(True)
- from apps.web.agent.proxy import record_agent_withdraw_fee
- from apilib.monetary import RMB
- from apps.web.agent.models import Agent, AgentIncomeReport
- from apps.web.dealer.models import Dealer
- from apps.web.common.models import WithdrawRecord
- def get_records():
- """
- 获取经销商提现成功的 记录
- :return:
- :rtype:
- """
- # records = WithdrawRecord.objects.filter(postTime__gte=datetime.datetime(2020, 9, 8, 7, 0, 0), status=1, role="dealer").all()
- agent = Agent.objects.get(username="")
- dealers = Dealer.objects.get(agentId=str(agent.id))
- records = WithdrawRecord.objects.filter(ownerId__in=dealers).all()
- # 这一条测试执行了
- need_record = list()
- for record in records:
- if str(record.id) != "5f573cd4003048a623a02b29":
- need_record.append(record)
- record_ids = "\n".join([str(item.id) for item in need_record]) + "\n"
- write_data(record_ids)
- return need_record
- def get_record_partition(record):
- amount = record.amount
- dealer = Dealer.objects.get(id=record.ownerId)
- if not dealer:
- print("not dealer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- agent = Agent.objects.get(id=dealer.agentId)
- if not agent:
- print("noe agent !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- agentFeeRation = agent.withdrawFeeRatio
- managerFeeRatio = agent.withdrawFeeRatioCost
- dealerFeeRatio = dealer.withdrawFeeRatio
- # 计算服务费以及实际需要提现的费用
- serviceFee = amount * dealerFeeRatio.as_ratio
- under_little_service_fee = dealer.withdraw_little_fee(record.payType)
- serviceFee += under_little_service_fee
- actualPay = amount - serviceFee
- if dealerFeeRatio > managerFeeRatio:
- earned = amount * (dealerFeeRatio - managerFeeRatio).as_ratio
- if earned < RMB(0):
- earned = RMB(0)
- elif earned > serviceFee:
- earned = serviceFee
- # 分给代理商
- agentEarned = RMB(0)
- if dealerFeeRatio > agentFeeRation:
- agentEarned = amount * (dealerFeeRatio - agentFeeRation).as_ratio
- if agentEarned < RMB(0):
- agentEarned = RMB(0)
- elif agentEarned > earned:
- agentEarned = earned
- # 代理商分完如果还有 继续分给厂商
- managerEarned = (earned - agentEarned)
- if managerEarned < RMB(0):
- managerEarned = RMB(0)
- else:
- agentEarned = RMB(0)
- managerEarned = RMB(0)
- # 计算出了这个record真正 的record
- partition = [
- {
- 'role': "agent",
- 'id': str(agent.id),
- 'cost': agentFeeRation.mongo_amount,
- 'earned': agentEarned.mongo_amount
- },
- {
- 'role': "manager",
- 'id': str(agent.primary_agent.id),
- 'cost': managerFeeRatio.mongo_amount,
- 'earned': managerEarned.mongo_amount
- }
- ]
- return partition
- def repaired(record, new_partition):
- # 两个处理的方式 直系代理商ID 等于 主代理商ID
- dealer = Dealer.objects.get(id=record.ownerId)
- write_data("withdraw is <{}>, odl partition is {}, new partition is {}\n".format(record.id, record.partition,
- new_partition))
- _map = {}
- for item in new_partition:
- agent_id = item['id']
- if agent_id in _map:
- _map[agent_id]['earned'] += RMB(item['earned'])
- else:
- _map[agent_id] = {
- 'earned': RMB(item['earned'])
- }
- for agent_id, item in _map.iteritems():
- earned = item['earned']
- if earned <= RMB(0):
- continue
- detail = {
- 'recharge_record_id': record.id,
- 'name': dealer.nickname,
- 'sum_of_price': record.serviceFee,
- 'withdrawAmount': record.amount,
- 'withdrawFeeRatio': record.withdrawFeeRatio
- }
- # 这个地方不需要做判断 record是同一条 id不变的情况下 该函数内部已经做了是否建立起的
- if AgentIncomeReport.get_collection().find(
- {'detail.recharge_record_id': record.id, 'agentId': agent_id}).count() > 0:
- incomeRecord = AgentIncomeReport.objects.filter(detail__recharge_record_id=record.id,
- agentId=agent_id).first()
- oldAmount = incomeRecord.amount
- oldDetail = incomeRecord.detail
- incomeRecord.amount = earned
- incomeRecord.detail = detail
- incomeRecord.save()
- write_data(
- "AgentIncome <{}> has been repaired, old amount is <{}>, new amount is <{}>".format(incomeRecord.id,
- oldAmount, earned))
- write_data("AgentIncome <{}> has been repaired, old detail is {}, new detail is {}".format(incomeRecord.id,
- oldDetail,
- detail))
- # agent = Agent.objects.get(agent_id)
- # # 更正收益
- # agent.decr_income("dealer_withdraw_fee", record.source_key, oldAmount)
- # agent.incr_income("dealer_withdraw_fee", record.source_key, earned)
- # write_data("agent income has been repaired, old income is {}, new income is {}".format(oldAmount, oldDetail, earned))
- else:
- record_agent_withdraw_fee(agentId=agent_id, source_key=record.source_key, amount=earned, detail=detail)
- write_data(
- "new a AgentIncome detail.recharge_record_id is <{}>, agentId is <{}>\n".format(record.id, agent_id))
- write_data("agent income has been repaired! new_income is <{}>\n\n".format(earned))
- # 最后吧 withdraw 更正过来
- record.partition = new_partition
- record.save()
- def write_data(data):
- with open("repaired.log", "a") as f:
- f.write(data)
- def main():
- records = get_records()
- for record in records:
- new_p = get_record_partition(record)
- repaired(record, new_p)
- if __name__ == '__main__':
- main()
|