# -*- coding: utf-8 -*-
import os
import sys

import pandas as pd

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


'联动文件合并，第一行+最后两行剔除'
def um_merge(file_tag='baitiao'):
    if file_tag == 'baitiao':
        filepath = 'E:/financial-system-data/umpay/baitiao/'
        out_file = open(filepath + 'um_bt.txt', 'w+')
    else:
        filepath = 'E:/financial-system-data/umpay/xjd/'
        out_file = open(filepath + 'um_xjd.txt', 'w+')
    out_file.writelines('TRADEDETAIL-START,3661,20170501,v4.0,0000,生成对账文件成功')
    for file in os.listdir(filepath):
        if (file == 'um_bt.txt') | (file == 'um_xjd.txt') :
            continue
        in_file=open(filepath+file,'r')
        while True:
            new_line = in_file.next()
            if new_line.startswith('TRADEDETAIL-START'):
                continue
            elif new_line.startswith('TRADEDETAIL-END'):
                break
            else:
                out_file.writelines(new_line)
        in_file.close()
    out_file.close()


um_merge(file_tag='xjd')
um_merge(file_tag='baitiao')


'微信合并'
def wx_merge(file_tag = 'baitiao'):
    if file_tag == 'baitiao' :
        filepath = 'E:/financial-system-data/weichat/baitiao/'
        out_file = open(filepath + 'wx_bt.txt', 'w+')
    else:
        filepath = 'E:/financial-system-data/weichat/xjd/'
        out_file = open(filepath + 'wx_xjd.txt', 'w+')
    out_file.writelines('交易时间,公众账号ID,商户号,子商户号,设备号,微信订单号,商户订单号,用户标识,交易类型,交易状态,付款银行,货币种类,总金额,企业红包金额,微信退款单号,商户退款单号,退款金额,企业红包退款金额,退款类型,退款状态,商品名称,商户数据包,手续费,费率')
    for file in os.listdir(filepath):
        if (file == 'wx_bt.txt') | (file == 'wx_xjd.txt'):
            continue
        in_file=open(filepath+file,'r')
        while True:
            new_line = in_file.next()
            if new_line.startswith('交易时间'):
                continue
            elif new_line.startswith('总交易单数'):
                break
            else:
                out_file.writelines(new_line)
        in_file.close()
    out_file.close()

wx_merge(file_tag = 'baitiao')
wx_merge(file_tag = 'xjd')