import tushare as ts
import MySQLdb
import math

from config import CONFIG_DB 
from config import CONFIG_TS

db = MySQLdb.connect(CONFIG_DB['host'], CONFIG_DB['user'], CONFIG_DB['password'], CONFIG_DB['database'], charset='utf8')
pro = ts.pro_api(CONFIG_TS['secret'])


def NanVal( val ):
    if (val is None) : 
        return 0.0 
    if (math.isnan(val)): 
        return 0.0
    return val

df = pro.daily_basic(**{
    "ts_code": "",
    "trade_date": 20230531,
    "start_date": "",
    "end_date": "",
    "limit": "",
    "offset": ""
}, fields=[
    "ts_code",
    "trade_date",
    "close",
    "turnover_rate",
    "turnover_rate_f",
    "volume_ratio",
    "pe",
    "pe_ttm",
    "pb",
    "ps",
    "ps_ttm",
    "dv_ratio",
    "dv_ttm",
    "total_share",
    "float_share",
    "free_share",
    "total_mv",
    "circ_mv",
    "limit_status"
])

cursor = db.cursor()
for row in df.itertuples():
    sql = "insert into t_ods_hs_daily_basic(ts_code, trade_date, close, turnover_rate, turnover_rate_f, volume_ratio, pe, \
            pe_ttm, pb, ps, ps_ttm, dv_ratio, dv_ttm, total_share, float_share, free_share, total_mv, circ_mv, limit_status) \
             values('%s', '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %d) \
             on duplicate key update close=%f, turnover_rate=%f, turnover_rate_f=%f, volume_ratio=%f, pe=%f, pe_ttm=%f, pb=%f, \
             ps=%f, ps_ttm=%f, dv_ratio=%f, dv_ttm=%f, total_share=%f, float_share=%f, free_share=%f, total_mv=%f, circ_mv=%f, \
             limit_status=%d " % \
             (getattr(row, 'ts_code'), getattr(row, 'trade_date'), 
             NanVal(getattr(row, 'close')), NanVal(getattr(row, 'turnover_rate')), NanVal(getattr(row, 'turnover_rate_f')), 
             NanVal(getattr(row, 'volume_ratio')), NanVal(getattr(row, 'pe')), NanVal(getattr(row, 'pe_ttm')), NanVal(getattr(row, 'pb')), 
             NanVal(getattr(row, 'ps')), NanVal(getattr(row, 'ps_ttm')), NanVal(getattr(row, 'dv_ratio')), NanVal(getattr(row, 'dv_ttm')), 
             NanVal(getattr(row, 'total_share')), NanVal(getattr(row, 'float_share')), NanVal(getattr(row, 'free_share')), 
             NanVal(getattr(row, 'total_mv')), NanVal(getattr(row, 'circ_mv')), NanVal(getattr(row, 'limit_status')),

             NanVal(getattr(row, 'close')), NanVal(getattr(row, 'turnover_rate')), NanVal(getattr(row, 'turnover_rate_f')), 
             NanVal(getattr(row, 'volume_ratio')), NanVal(getattr(row, 'pe')), NanVal(getattr(row, 'pe_ttm')), NanVal(getattr(row, 'pb')), 
             NanVal(getattr(row, 'ps')), NanVal(getattr(row, 'ps_ttm')), NanVal(getattr(row, 'dv_ratio')), NanVal(getattr(row, 'dv_ttm')), 
             NanVal(getattr(row, 'total_share')), NanVal(getattr(row, 'float_share')), NanVal(getattr(row, 'free_share')), 
             NanVal(getattr(row, 'total_mv')), NanVal(getattr(row, 'circ_mv')), NanVal(getattr(row, 'limit_status'))
             )
    try:
        cursor.execute(sql)
        db.commit()
    except Exception as ex:
        print(sql)
        print (ex)
        db.rollback()
        break


db.close()



