
# -*- coding:utf-8 -*-
# Author: WuKong

import sys
import datetime
from urllib import quote_plus as urlquote
import itertools
import pandas as pd
from sqlalchemy import create_engine
import multiprocessing as mtp
import os

reload(sys)
sys.setdefaultencoding('utf8')

# 正式库，只能用途 读账户！！！
engine_urge_dispatcher = create_engine(
    'mysql+mysqldb://cuishou_pro_rw:%sUI@172.16.90.39:3306/urge_dispatcher?charset=utf8' % urlquote('%789uijhktyghn%'),
    echo=True)

engine_urge_xyqb = create_engine(
    'mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/xyqb?charset=utf8',
    echo=True)

# 查询催收库中 所有已还款数据
sql_query_dispathcer = u"""
SELECT debtor_user_id,repay_plan_id,loan_order_id,repay_status FROM debt_cash_loan_item WHERE repay_status=3
"""

df_dispatcher = pd.read_sql(sql=sql_query_dispathcer,con=engine_urge_dispatcher)
plan_id_list = df_dispatcher.repay_plan_id.astype(int).tolist()

# 查询xyqb中 未还款
sql_query_xyqb_plan = u"""
SELECT id FROM repayment_plan WHERE repayment_status!=3 AND  id in %s
"""
no_repay_ids = []
for i in range(0, len(plan_id_list), 5000):
    plan_ids = plan_id_list[i:i+5000]
    df_xyqb_no_repay = pd.read_sql(sql=sql_query_xyqb_plan % (str(tuple(plan_ids)).replace(',)', ')'),), con=engine_urge_xyqb)
    if len(df_xyqb_no_repay) > 0:
        no_repay_ids.extend(df_xyqb_no_repay.id.astype(int).tolist())

df_dispatcher.repay_plan_id = df_dispatcher.repay_plan_id.astype(int)
df_unormal_dispatcher = df_dispatcher.ix[df_dispatcher.repay_plan_id.isin(no_repay_ids)]
df_unormal_dispatcher = df_unormal_dispatcher[['repay_plan_id']]
df_unormal_dispatcher.to_excel('.\unomarl_data.xlsx', columns=df_unormal_dispatcher.columns, index=None, encoding='utf8')


