repaired_success.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # coding=utf-8
  2. import datetime
  3. import simplejson as json
  4. from base import init_env
  5. from collections import defaultdict
  6. init_env(True)
  7. from apps.web.user.models import RechargeRecord, RefundMoneyRecord
  8. from apps.web.core.payment import PaymentGateway
  9. from apps.web.common.transaction.pay import PayManager, PayNotifyAction
  10. from apps.web.user.transaction import post_pay
  11. from apps.web.dealer.proxy import DealerIncomeProxy
  12. from apps.web.device.models import Device
  13. from apps.web.constant import Const, USER_RECHARGE_TYPE
  14. from apps.web.device.models import Group
  15. from apps.web.report.ledger import Ledger
  16. REPAIR = False # false 的情况下 只输入记录 不对任何记录进行修改
  17. LEFT_TIME = datetime.datetime.strptime("2021-11-10 16:00:00", "%Y-%m-%d %H:%M:%S") # 修复记录的起始时间
  18. RIGHT_TIME = datetime.datetime.strptime("2021-11-11 00:00:00", "%Y-%m-%d %H:%M:%S") # 修复记录的结束时间
  19. RESULT = RechargeRecord.PayResult.SUCCESS # 订单的状态
  20. VIA = ["recharge", "chargeCard"] # 需要排除的充值模式 这个就不在数据库里进行了吧,反正是脚本 运行的时候排除
  21. RECHARGE_NO = None
  22. def get_records():
  23. filters = {
  24. "dateTimeAdded__gte": LEFT_TIME,
  25. "dateTimeAdded__lte": RIGHT_TIME,
  26. "result": RESULT,
  27. "via__in": VIA
  28. }
  29. if RECHARGE_NO:
  30. filters.update({"orderNo": RECHARGE_NO})
  31. records = RechargeRecord.objects.filter(
  32. **filters
  33. )
  34. return records
  35. def get_dealer_income(ref_id):
  36. return DealerIncomeProxy.objects.filter(ref_id=ref_id).first()
  37. def get_refund_order(record):
  38. return RefundMoneyRecord.objects.filter(rechargeObjId=record.id).first()
  39. def is_record_ledger(record):
  40. # 如果存在了分账信息 则一定进行过分账 则不需要处理
  41. if get_dealer_income(record.id):
  42. return True
  43. # 如果存在了退款信息 则一定是post_pay里面启动失败退换现金的情况 也不需要处理
  44. if get_refund_order(record):
  45. return True
  46. # 昌源系列的设备,不需要处理分账
  47. device = Device.get_dev(record.devNo)
  48. if device["devType"]["code"] in (
  49. Const.DEVICE_TYPE_CODE_CAR_CHARGING_CY,
  50. Const.DEVICE_TYPE_CODE_CHANGING_CY4,
  51. Const.DEVICE_TYPE_CODE_CAR_CHARGING_CY_V2,
  52. Const.DEVICE_TYPE_CODE_CHANGING_CY_POWER,
  53. Const.DEVICE_TYPE_CODE_DUIBIJI,
  54. Const.DEVICE_TYPE_CODE_CHARGING_CHANGYUAN_FIVE
  55. ):
  56. return True
  57. return False
  58. def record_to_dict(record):
  59. return {
  60. "orderNo": record.orderNo,
  61. "money": record.money.amount,
  62. "ownerId": record.ownerId,
  63. "openId": record.openId,
  64. "time": record.time
  65. }
  66. def run():
  67. records = get_records()
  68. # 拿到记录之后需要 对记录查询是否已经分账
  69. info = defaultdict(list)
  70. for record in records: # type: RechargeRecord
  71. try:
  72. # 表示没有分过帐
  73. if not is_record_ledger(record):
  74. tempData = record_to_dict(record)
  75. ownerId = tempData.get("ownerId")
  76. info[ownerId].append(tempData)
  77. # 修复待处理
  78. if REPAIR:
  79. do_repaired(record)
  80. except Exception as e:
  81. print e
  82. print record.orderNo
  83. continue
  84. with open("record_{}_{}_{}.json".format(LEFT_TIME, RIGHT_TIME, RESULT).replace(" ", "_").replace(":", "_"), "w") as f:
  85. json.dump(info, f)
  86. def do_ledger1():
  87. if not REPAIR:
  88. return
  89. fileName = raw_input("输入待处理的文件: ")
  90. with open(fileName, "r") as f:
  91. content = json.loads(f.read())
  92. for _dealerId, _array in content.items():
  93. for _record in _array:
  94. record = RechargeRecord.objects.get(orderNo=_record["orderNo"]) # type: RechargeRecord
  95. if is_record_ledger(record):
  96. print _record
  97. continue
  98. if record.via == USER_RECHARGE_TYPE.RECHARGE:
  99. source = USER_RECHARGE_TYPE.RECHARGE
  100. else:
  101. source = USER_RECHARGE_TYPE.RECHARGE_CARD
  102. Ledger(source=source, record=record, notify=False)
  103. if __name__ == '__main__':
  104. # ac = u"执行查询,并不对结果进行更改" if not REPAIR else u"当前执行后会对记录进行修改"
  105. #
  106. # s = raw_input(u"当前{} 筛选条件:\n订单编号: {}\n开始时间: {}\n结束时间: {}\n订单当前状态: {}\n查询范围: {}\n继续执行请输入yes\n".format(ac, RECHARGE_NO, LEFT_TIME, RIGHT_TIME, RESULT, VIA))
  107. # assert s == "yes", u"执行结束"
  108. #
  109. # run()
  110. do_ledger()