Commit ecee542f authored by 白条—徐加哲 's avatar 白条—徐加哲

new_ms_transaction_map_term

parent 3b39e4e1
# -*- coding: utf-8 -*-
"""
新马上划款记录 和 新马上用户还款计划 映射
new_ms_transaction 新马上扣款流水
new_ms_transaction_with_term 新马上用户 还款计划 明细
A 还款计划
按照loadid, 期数
计算该还 = (funding_principle, funding_interest, funding_service_fee, qg_service_fee, qg_overdue_fee, qg_late_fee)
B 划款记录
按照 loadid, repay_date
处理同一个loadid
取得 aa bb
aleft = aa[0].还款总额, bleft.repay_amt
term_no = 1
不能a b 都到头了
如果一个到头了
谁left 0 谁自增 相加 both
如果a增, 当前期数+
尝试消耗 min(bleft, aleft)
left各自相减
增加记录 扣款流水号, load_id, 当前期数, 消耗金额
"""
import pandas as pd
from sqlalchemy import create_engine
engine_audit = create_engine('mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/audit?charset=utf8',
echo=True)
engine_local = create_engine('mysql+mysqldb://root@localhost:3306/heloword?charset=utf8',
echo=True)
sqla = """
SELECT
nntr_plan.*,
(funding_principle + funding_interest + funding_service_fee +
qg_service_fee +
qg_overdue_fee +
qg_late_fee) need_pay
FROM new_transaction.new_ms_transaction_with_term nntr_plan
ORDER BY loan_id, term_no;
"""
sqlb = """
SELECT *
FROM new_transaction.new_ms_transaction nntr
ORDER BY ABS(loan_id) , repay_date;
"""
Adf = pd.read_sql(sql=sqla, con=engine_audit)
Bdf = pd.read_sql(sql=sqlb, con=engine_audit)
Bdf['loan_id'] = Bdf['loan_id'].astype(int)
# Adf = Adf.set_index(['loan_id', 'repay_date'])
# Bdf = Bdf.set_index(['loan_id', 'term_no'])
def find_forward(df, i, key, leng):
length = 0
while (i + length) < leng and df.iloc[i + length].loan_id == key:
length += 1
return length
Ai = 0
Bi = 0
ALEN = len(Adf)
BLEN = len(Bdf)
records = []
goon = True
while Ai != ALEN and Bi != BLEN and goon:
# aa = Adf.ix[Adf.loan_id == loan_id] # 最好有索引
# bb = Bdf.ix[Bdf.loan_id == loan_id]
loan_id = Adf.iloc[Ai].loan_id
alen = find_forward(Adf, Ai, loan_id, ALEN)
blen = find_forward(Bdf, Bi, loan_id, BLEN)
aa = Adf.iloc[Ai: Ai + alen]
bb = Bdf.iloc[Bi: Bi + blen]
term_no = 0
ai = 0
aleft = 0
bi = 0
bleft = 0
Ai += alen
Bi += blen
if alen == 0 or blen == 0:
if alen == 0:
print "%s 划款没有对应的 计划 " % loan_id, alen, blen
break
continue
# bb = bb.ix[bb.repay_amt != 0]
# blen = len(bb)
while not (ai == alen and bi == blen):
if aleft == 0:
if ai == alen: # 有笔扣款没有相应的还款计划
# 如果 最后一笔扣款是0
if bb.iloc[bi:blen].repay_amt.sum() == 0.0:
# print '扣款 %s 结尾是0 ' % loan_id, blen-bi
break
else: # 否则 输出
# print '扣款 %s not map full ' % loan_id, ai, alen, bi, blen, blen-bi
goon = False
break
a = aa.iloc[ai]
aleft = a.need_pay
afee = { # 当前期剩余未还 各项费用
'repay_principle': a.funding_principle,
'repay_intrest': a.funding_interest,
'fund_service_fee': a.funding_service_fee,
'service_fee': a.qg_service_fee,
'late_fee': a.qg_overdue_fee,
'penalty_fee': a.qg_late_fee
}
term_no = a.term_no
ai += 1
if bleft == 0:
if bi == blen:
# print '计划 not map full ', loan_id, ai, alen, bi, blen
# goon = False
break
b = bb.iloc[bi]
bleft = b.repay_amt
bfee = {
'repay_principle': b.repay_principle,
'repay_intrest': b.repay_intrest,
'fund_service_fee': b.fund_service_fee,
'service_fee': b.service_fee,
'late_fee': b.late_fee,
'penalty_fee': b.penalty_fee
}
bi += 1
cost = min(bleft, aleft)
aleft -= cost
bleft -= cost
if aleft == 0.0:
fees = afee
bfee = {key: bfee[key] - afee[key] for key in afee.keys()}
else:
fees = bfee
afee = {key: afee[key] - bfee[key] for key in bfee.keys()}
new_record = {
'transfer_id': b.id,
'loan_id': loan_id,
'term_no': term_no,
'amt_for_term': cost,
'contract_no': b.contract_no,
'repay_amt': b.repay_amt,
'loan_paid_at': b.loan_paid_at,
'repay_date': b.repay_date
}
new_record.update(fees)
for key in fees:
if fees[key] < 0:
print fees
goon = False
break
if not goon:
break
records.append(new_record)
df = pd.DataFrame(records)
df.to_sql('new_ms_transaction_map_term', con=engine_local, if_exists='append', index=None, chunksize=10000)
path_out = u'/Users/xujiazhe/Desktop/'
df.to_excel(path_out + u'新马上扣款记录加上期数.xlsx', index=None)
# encoding: utf8
import pandas as pd
from sqlalchemy import create_engine
engine_audit = create_engine('mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/audit?charset=utf8',
echo=True)
# TODO 代码需要作者检查
"""
A
给资金方流水 按 时间排序
一笔id, 转账时间, fund_cop, 银行, 实还, 应还 , 流水差额;
一笔id2, 转账时间2, 银行, 实还2, 应还2 , 流水差额 ;
每笔加上 fund_cop_name
B
用户该还, 按照 天,资金方 来汇总和排序
天, 资金方, 总钱.
按照时间归并AB, 按照时间选出a,b
a,
a.transfer_time
a.bank
a.fund_cop
a.amount
此时该还的拿到 清0
算差异
b,
资金方欠款总钱+进去
"""
sqla = """
SELECT
date_format(transfer_time, '%%y-%%m-%%d') transfer_date,
altf.fund_corp,
altf.bank,
(CASE
WHEN (fc.funding_code IS NULL)
THEN (
CASE
WHEN (fund_corp = '洋钱罐(直投)') THEN 140
WHEN (fund_corp = '洋钱罐(债转)') THEN 141
WHEN (fund_corp = '夸客') THEN 100
WHEN (fund_corp = '橘子理财') THEN 250
WHEN (fund_corp IN ('李然', '张初阳', '姚晓蕾')) THEN -1 # 这里有问题的 !!!!
ELSE -2 END
)
ELSE fc.funding_code
END) funding_code,
transfer_amount pay_amount,
0.0 need_pay,
0.0 trans_diff
FROM audit.lkb_to_fund altf
LEFT JOIN audit.funding_corp fc ON altf.fund_corp = fc.name
ORDER BY transfer_date, funding_code;
"""
sqlb = """
#funding_repayment_record 用户还给资金方的钱 每日汇总 user_fund_daily
SELECT
(CASE
WHEN (date_format(repaid_at, '%H%i%S') = '000000')
THEN date_format(DATE_SUB(repaid_at, INTERVAL 1 SECOND), '%y-%m-%d')
ELSE date_format(repaid_at, '%y-%m-%d') END) repaid_date,
afrr.funding_code,
(sum(principle) +
sum(interest) +
sum(prepay_fee) +
sum(IFNULL(funding_service_fee, 0)) +
sum(IFNULL(punish_fee, 0)) +
sum(IFNULL(AIR, 0))
) AS need_pay
FROM audit.funding_repayment_record afrr
WHERE repaid_at IS NOT NULL
GROUP BY repaid_date, funding_code
ORDER BY repaid_date, funding_code;
"""
# a = engine_financial.execute(sql, params=('%Y-%m',)).fetchall()
# df = pd.DataFrame(values=a, columns=[u'类别', u'放款年', u'放款月', u'应还年', u'应还月', u'应收本金', u'应收利息'])
need_pay_dict = {}
Adf = pd.read_sql(sql=sqla, con=engine_audit)
Bdf = pd.read_sql(sql=sqlb, con=engine_audit)
bi = 0
blen = len(Bdf)
for ai, a in Adf.iterrows():
while bi < blen and Bdf.iloc[bi]['repaid_date'] < a['transfer_date']:
b = Bdf.iloc[bi]
need_pay_dict[b['funding_code']] = need_pay_dict.get(b['funding_code'], 0.0) + b['need_pay']
bi += 1
need_pay = need_pay_dict.setdefault(a['funding_code'], 0.0)
a['need_pay'] = need_pay
a['diff'] = a['pay_amount'] - need_pay
path_out = u'~/Desktop/'
Adf.to_excel(path_out + u'现金贷应收本金利息汇总', index=None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment