# Path: kquant_data/wind/wss.py
# def download_ipo_last_trade_trading(w, wind_codes):
#     # 黄金从20130625开始将最小变动价位从0.01调整成了0.05，但从万得上查出来还是完全一样，所以没有必要记录mfprice
#     # 郑商所在修改合约交易单位时都改了合约代码，所以没有必要记录contractmultiplier
#     w.asDateTime = asDateTime
#     w_wss_data = w.wss(wind_codes, "sec_name,ipo_date,lasttrade_date,lasttradingdate", "")
#     grid = w_wss_data.Data
# 
#     # T1803一类的会被当成时间，需要提前转置
#     new_grid = [[row[i] for row in grid] for i in range(len(grid[0]))]
# 
#     df = pd.DataFrame(new_grid)
#     df.columns = ['sec_name', 'ipo_date', 'lasttrade_date', 'lasttradingdate']
#     df.index = w_wss_data.Codes
#     df.index.name = 'wind_code'
# 
#     df['ipo_date'] = df['ipo_date'].apply(datetime_2_yyyyMMdd)
#     df['lasttrade_date'] = df['lasttrade_date'].apply(datetime_2_yyyyMMdd)
#     df['lasttradingdate'] = df['lasttradingdate'].apply(datetime_2_yyyyMMdd)
# 
#     df.replace(18991230, 0, inplace=True)
# 
#     return df
# 
# Path: kquant_data/xio/csv.py
# def write_data_dataframe(path, df, date_format='%Y-%m-%d'):
#     """
#     写入数据
#     :param path:
#     :param df:
#     :return:
#     """
#     df.to_csv(path, date_format=date_format, encoding='utf-8-sig')
# 
# def read_data_dataframe(path, sep=','):
#     """
#     读取季报的公告日
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     return df
# 
# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/wind/wset.py
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df


from WindPy import w
from kquant_data.wind.wss import download_ipo_last_trade_trading
from kquant_data.xio.csv import write_data_dataframe, read_data_dataframe
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.wind.wset import read_constituent
import sys
import os
import pandas as pd
    all_set = set(df.index)
    for dirpath, dirnames, filenames in os.walk(mydir):
        for filename in filenames:
            # 这个日期需要记得修改
            if filename < "2017-01-01.csv":
                continue

            filepath = os.path.join(dirpath, filename)

            df1 = read_constituent(filepath)
            # print(filepath)
            if df1 is None:
                continue
            if df1.empty:
                continue
            curr_set = set(df1['wind_code'])
            diff_set = curr_set - all_set
            if len(diff_set) == 0:
                continue

            print(filepath)
            df2 = download_ipo_last_trade_trading(w, list(diff_set))
            df = pd.concat([df, df2])
            all_set = set(df.index)
            # 出于安全考虑，还是每次都保存
            write_data_dataframe(myfile, df)

    df['wind_code'] = df.index
    df.sort_values(by=['ipo_date', 'wind_code'], inplace=True)
    del df['wind_code']

# Path: kquant_data/wind/wss.py
# def download_ipo_last_trade_trading(w, wind_codes):
#     # 黄金从20130625开始将最小变动价位从0.01调整成了0.05，但从万得上查出来还是完全一样，所以没有必要记录mfprice
#     # 郑商所在修改合约交易单位时都改了合约代码，所以没有必要记录contractmultiplier
#     w.asDateTime = asDateTime
#     w_wss_data = w.wss(wind_codes, "sec_name,ipo_date,lasttrade_date,lasttradingdate", "")
#     grid = w_wss_data.Data
# 
#     # T1803一类的会被当成时间，需要提前转置
#     new_grid = [[row[i] for row in grid] for i in range(len(grid[0]))]
# 
#     df = pd.DataFrame(new_grid)
#     df.columns = ['sec_name', 'ipo_date', 'lasttrade_date', 'lasttradingdate']
#     df.index = w_wss_data.Codes
#     df.index.name = 'wind_code'
# 
#     df['ipo_date'] = df['ipo_date'].apply(datetime_2_yyyyMMdd)
#     df['lasttrade_date'] = df['lasttrade_date'].apply(datetime_2_yyyyMMdd)
#     df['lasttradingdate'] = df['lasttradingdate'].apply(datetime_2_yyyyMMdd)
# 
#     df.replace(18991230, 0, inplace=True)
# 
#     return df
# 
# Path: kquant_data/xio/csv.py
# def write_data_dataframe(path, df, date_format='%Y-%m-%d'):
#     """
#     写入数据
#     :param path:
#     :param df:
#     :return:
#     """
#     df.to_csv(path, date_format=date_format, encoding='utf-8-sig')
# 
# def read_data_dataframe(path, sep=','):
#     """
#     读取季报的公告日
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     return df
# 
# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/wind/wset.py
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df


# Path: demo_future/D01_download_ipo_last_trade_trading.py
from WindPy import w
from kquant_data.wind.wss import download_ipo_last_trade_trading
from kquant_data.xio.csv import write_data_dataframe, read_data_dataframe
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.wind.wset import read_constituent
import sys
import os
import pandas as pd

    all_set = set(df.index)
    for dirpath, dirnames, filenames in os.walk(mydir):
        for filename in filenames:
            # 这个日期需要记得修改
            if filename < "2017-01-01.csv":
                continue

            filepath = os.path.join(dirpath, filename)

            df1 = read_constituent(filepath)
            # print(filepath)
            if df1 is None:
                continue
            if df1.empty:
                continue
            curr_set = set(df1['wind_code'])
            diff_set = curr_set - all_set
            if len(diff_set) == 0:
                continue

            print(filepath)
            df2 = download_ipo_last_trade_trading(w, list(diff_set))
            df = pd.concat([df, df2])
            all_set = set(df.index)
            # 出于安全考虑，还是每次都保存
            write_data_dataframe(myfile, df)

    df['wind_code'] = df.index
    df.sort_values(by=['ipo_date', 'wind_code'], inplace=True)
    del df['wind_code']

    write_data_dataframe(myfile, df)