# encoding:utf8
import sys
import os
import math
import pandas as pd
pd.options.mode.chained_assignment = None
from api import db_connect
from decimal import *
reload(sys)
sys.setdefaultencoding('utf8')

'''
@author：王博
功能：
'''

#核查供货价
#文件的存储路径，按需修改
file_path = "E:/quantgroup/3c/check_511/"
files = os.listdir(file_path)

con = db_connect.db_connect('merchant', 'merchant').get_conn()

refund_sql = """
 select t.order_no from baitiao_order t where t.order_no IN %s AND t.`status`=5 ;
"""

merchant_sql = """
SELECT supplier_amount , %f , mall_order_no  FROM supplier_order WHERE mall_order_no='%s' and  supplier_amount!=%f
"""

for f in files:
    print file_path + f
    # Reading data
    pd_3c_df = pd.read_csv(file_path + f, header=0)
    pd_refund_df = pd.DataFrame()
    for index, row in pd_3c_df.iterrows():
        amount = Decimal(row[u"amount"])
        mall_order_no = row[u"order_no"]
        if math.isnan(amount):
            continue
        sql = merchant_sql % (amount, mall_order_no, amount)
        pd_refund_sql_gen = pd.read_sql(sql=merchant_sql % (amount, mall_order_no, amount), con=con, chunksize=1000)
        for value in pd_refund_sql_gen:
            pd_refund_df = pd_refund_df.append(value, ignore_index=True)

    pd_refund_df.to_csv(file_path + 'refund_order_no_amount_%s.csv' % f)



