# -*- coding: utf-8 -*- # !/usr/bin/env python import datetime import os import sys PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) from script.base import init_env os.environ["DJANGO_SETTINGS_MODULE"] = "configs.production" init_env(False) from apps.web.core.db import copy_document_classes from apps.web.dealer.proxy import DealerIncomeProxy from apps.web.common.models import WithdrawRecord from apps.web.dealer.models import Dealer, DealerRechargeRecord from apilib.monetary import RMB begin = datetime.datetime(2017, 1, 1, 0, 0, 0) end = datetime.datetime(2089, 1, 1, 0, 0, 0) check_date = datetime.datetime(2022, 6, 1, 0, 0, 0) def calc_auto_sim(ownerId, begin_time, end_time): simAutoFee = 0.0 ds = DealerRechargeRecord.objects(dealerId=ownerId, status='Paid', name__contains=u'自动续费', createdTime__gte=begin_time, createdTime__lt=end_time) for _ in ds: simAutoFee += (float(_.totalFee) / 100) return simAutoFee def calc_withdraw(ownerId, begin_time, end_time): ws = WithdrawRecord.objects(ownerId=ownerId, status__nin=[0, 4, 5], postTime__gte=begin_time, postTime__lt=end_time) withdraw = 0.0 for _ in ws: if _.refunded: print('refunded') else: withdraw += float(str(_.amount)) # print('withdraw is: {}'.format(withdraw)) return withdraw def calc_dealer_income_proxy(ownerId, begin_time, end_time): if begin_time < check_date: his_model_cls = copy_document_classes(DealerIncomeProxy, '{}_his'.format(DealerIncomeProxy.__name__), 'report_his') his_data = his_model_cls.sum_by_dealer(dealerId=ownerId, **{'dateTimeAdded__gte': begin_time, 'dateTimeAdded__lt': check_date if check_date < end_time else end_time}) else: his_data = 0 if begin_time > check_date: now_data = DealerIncomeProxy.sum_by_dealer(dealerId=ownerId, **{'dateTimeAdded__gte': begin_time, 'dateTimeAdded__lt': end_time}) else: if check_date > end_time: now_data = 0 else: now_data = DealerIncomeProxy.sum_by_dealer(dealerId=ownerId, **{'dateTimeAdded__gte': check_date, 'dateTimeAdded__lt': end_time}) return float(str(his_data)), float(str(now_data)), float(str(his_data)) + float(str(now_data)) def total_balance(dealer): total = RMB(0) for income_type in dealer.INCOME_TYPE_LIST: total += dealer.sub_balance(income_type, only_ledger=False) return total def check_one_dealer(ownerId, begin_wallet, begin_time): end_time = datetime.datetime.now() dealer = Dealer.objects(id=ownerId).first() wallet = float(total_balance(dealer)) withdraw = calc_withdraw(ownerId, begin_time, end_time) auto_sim = calc_auto_sim(ownerId, begin_time, end_time) total = withdraw + wallet + auto_sim - begin_wallet his_income, now_income, total_income = calc_dealer_income_proxy(ownerId, begin_time, end_time) print '--------------------------------------------------------------------------------------------------------------------------' print '' print '对账结果如下({}):'.format(begin_time.strftime('%Y-%m-%d %H:%M:%S')) print 'begin_wallet = {}, wallet={}, withdraw={}, autoSim={}'.format(str(begin_wallet), str(wallet), str(withdraw), str(auto_sim)) print 'his = {}, now = {}, total income = {}, calc income = {}'.format(str(his_income), str(now_income), str(total_income), str(total)) print 'check result is: {}; id = {}'.format(round(total_income, 2) == round(total, 2), ownerId) print 'diff is: {}; id = {}'.format((round(total_income, 2) - round(total, 2)), ownerId) print '--------------------------------------------------------------------------------------------------------------------------' def calc_begin(ownerId, begin_time, end_time): # ws = WithdrawRecord.objects(ownerId=ownerId, postTime__gte=begin_time, postTime__lt=end_time).first() # if ws: # return float(ws.balance), ws.postTime # else: # raise Exception('no withdraw.') # sys.exit(0) return float(0), begin_time from bson import ObjectId done_list = [ ] if __name__ == '__main__': _list = [] ownerIds = sys.argv[1:] if ownerIds: for ownerId in ownerIds: _list.append(ownerId) else: _list = [str(item.id) for item in Dealer.objects(id__nin = done_list).all().only('id')] #print _list for ownerId in _list: try: check_one_dealer(ownerId, 0, begin) except Exception as e: print('id={}, e={}'.format(ownerId, str(e)))