123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- # coding=utf-8
- import datetime
- import simplejson as json
- from base import init_env
- from collections import defaultdict
- init_env(True)
- from apps.web.user.models import RechargeRecord, RefundMoneyRecord
- from apps.web.core.payment import PaymentGateway
- from apps.web.common.transaction.pay import PayManager, PayNotifyAction
- from apps.web.user.transaction import post_pay
- from apps.web.dealer.proxy import DealerIncomeProxy
- from apps.web.device.models import Device
- from apps.web.constant import Const, USER_RECHARGE_TYPE
- from apps.web.device.models import Group
- from apps.web.report.ledger import Ledger
- REPAIR = False # false 的情况下 只输入记录 不对任何记录进行修改
- LEFT_TIME = datetime.datetime.strptime("2021-11-10 16:00:00", "%Y-%m-%d %H:%M:%S") # 修复记录的起始时间
- RIGHT_TIME = datetime.datetime.strptime("2021-11-11 00:00:00", "%Y-%m-%d %H:%M:%S") # 修复记录的结束时间
- RESULT = RechargeRecord.PayResult.SUCCESS # 订单的状态
- VIA = ["recharge", "chargeCard"] # 需要排除的充值模式 这个就不在数据库里进行了吧,反正是脚本 运行的时候排除
- RECHARGE_NO = None
- def get_records():
- filters = {
- "dateTimeAdded__gte": LEFT_TIME,
- "dateTimeAdded__lte": RIGHT_TIME,
- "result": RESULT,
- "via__in": VIA
- }
- if RECHARGE_NO:
- filters.update({"orderNo": RECHARGE_NO})
- records = RechargeRecord.objects.filter(
- **filters
- )
- return records
- def get_dealer_income(ref_id):
- return DealerIncomeProxy.objects.filter(ref_id=ref_id).first()
- def get_refund_order(record):
- return RefundMoneyRecord.objects.filter(rechargeObjId=record.id).first()
- def is_record_ledger(record):
- # 如果存在了分账信息 则一定进行过分账 则不需要处理
- if get_dealer_income(record.id):
- return True
- # 如果存在了退款信息 则一定是post_pay里面启动失败退换现金的情况 也不需要处理
- if get_refund_order(record):
- return True
- # 昌源系列的设备,不需要处理分账
- device = Device.get_dev(record.devNo)
- if device["devType"]["code"] in (
- Const.DEVICE_TYPE_CODE_CAR_CHARGING_CY,
- Const.DEVICE_TYPE_CODE_CHANGING_CY4,
- Const.DEVICE_TYPE_CODE_CAR_CHARGING_CY_V2,
- Const.DEVICE_TYPE_CODE_CHANGING_CY_POWER,
- Const.DEVICE_TYPE_CODE_DUIBIJI,
- Const.DEVICE_TYPE_CODE_CHARGING_CHANGYUAN_FIVE
- ):
- return True
- return False
- def record_to_dict(record):
- return {
- "orderNo": record.orderNo,
- "money": record.money.amount,
- "ownerId": record.ownerId,
- "openId": record.openId,
- "time": record.time
- }
- def run():
- records = get_records()
- # 拿到记录之后需要 对记录查询是否已经分账
- info = defaultdict(list)
- for record in records: # type: RechargeRecord
- try:
- # 表示没有分过帐
- if not is_record_ledger(record):
- tempData = record_to_dict(record)
- ownerId = tempData.get("ownerId")
- info[ownerId].append(tempData)
- # 修复待处理
- if REPAIR:
- do_repaired(record)
- except Exception as e:
- print e
- print record.orderNo
- continue
- with open("record_{}_{}_{}.json".format(LEFT_TIME, RIGHT_TIME, RESULT).replace(" ", "_").replace(":", "_"), "w") as f:
- json.dump(info, f)
- def do_ledger1():
- if not REPAIR:
- return
- fileName = raw_input("输入待处理的文件: ")
- with open(fileName, "r") as f:
- content = json.loads(f.read())
- for _dealerId, _array in content.items():
- for _record in _array:
- record = RechargeRecord.objects.get(orderNo=_record["orderNo"]) # type: RechargeRecord
- if is_record_ledger(record):
- print _record
- continue
- if record.via == USER_RECHARGE_TYPE.RECHARGE:
- source = USER_RECHARGE_TYPE.RECHARGE
- else:
- source = USER_RECHARGE_TYPE.RECHARGE_CARD
- Ledger(source=source, record=record, notify=False)
- if __name__ == '__main__':
- # ac = u"执行查询,并不对结果进行更改" if not REPAIR else u"当前执行后会对记录进行修改"
- #
- # s = raw_input(u"当前{} 筛选条件:\n订单编号: {}\n开始时间: {}\n结束时间: {}\n订单当前状态: {}\n查询范围: {}\n继续执行请输入yes\n".format(ac, RECHARGE_NO, LEFT_TIME, RIGHT_TIME, RESULT, VIA))
- # assert s == "yes", u"执行结束"
- #
- # run()
- do_ledger()
|