import tushare as ts
import MySQLdb
import math
import time

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 str2float( str ):
    try:
        val = float(str)
        if math.isinf(val) or math.isnan(val) :
            val = 0
        return val
    except Exception as e:
        return 0.0

def queryts( code ) :
    df = pro.fina_indicator(**{
        "ts_code": code,
        "ann_date": "",
        "start_date": 20220630,
        "end_date": 20230604,
        "period": "",
        "update_flag": "",
        "limit": "",
        "offset": ""
    }, fields=[
        "ts_code",
        "ann_date",
        "end_date",
        "eps",
        "dt_eps",
        "total_revenue_ps",
        "revenue_ps",
        "capital_rese_ps",
        "surplus_rese_ps",
        "undist_profit_ps",
        "extra_item",
        "profit_dedt",
        "gross_margin",
        "current_ratio",
        "quick_ratio",
        "cash_ratio",
        "invturn_days",
        "arturn_days",
        "inv_turn",
        "ar_turn",
        "ca_turn",
        "fa_turn",
        "assets_turn",
        "op_income",
        "valuechange_income",
        "interst_income",
        "daa",
        "ebit",
        "ebitda",
        "fcff",
        "fcfe",
        "current_exint",
        "noncurrent_exint",
        "interestdebt",
        "netdebt",
        "tangible_asset",
        "working_capital",
        "networking_capital",
        "invest_capital",
        "retained_earnings",
        "diluted2_eps",
        "bps",
        "ocfps",
        "retainedps",
        "cfps",
        "ebit_ps",
        "fcff_ps",
        "fcfe_ps",
        "netprofit_margin",
        "grossprofit_margin",
        "cogs_of_sales",
        "expense_of_sales",
        "profit_to_gr",
        "saleexp_to_gr",
        "adminexp_of_gr",
        "finaexp_of_gr",
        "impai_ttm",
        "gc_of_gr",
        "op_of_gr",
        "ebit_of_gr",
        "roe",
        "roe_waa",
        "roe_dt",
        "roa",
        "npta",
        "roic",
        "roe_yearly",
        "roa2_yearly",
        "roe_avg",
        "opincome_of_ebt",
        "investincome_of_ebt",
        "n_op_profit_of_ebt",
        "tax_to_ebt",
        "dtprofit_to_profit",
        "salescash_to_or",
        "ocf_to_or",
        "ocf_to_opincome",
        "capitalized_to_da",
        "debt_to_assets",
        "assets_to_eqt",
        "dp_assets_to_eqt",
        "ca_to_assets",
        "nca_to_assets",
        "tbassets_to_totalassets",
        "int_to_talcap",
        "eqt_to_talcapital",
        "currentdebt_to_debt",
        "longdeb_to_debt",
        "ocf_to_shortdebt",
        "debt_to_eqt",
        "eqt_to_debt",
        "eqt_to_interestdebt",
        "tangibleasset_to_debt",
        "tangasset_to_intdebt",
        "tangibleasset_to_netdebt",
        "ocf_to_debt",
        "ocf_to_interestdebt",
        "ocf_to_netdebt",
        "ebit_to_interest",
        "longdebt_to_workingcapital",
        "ebitda_to_debt",
        "turn_days",
        "roa_yearly",
        "roa_dp",
        "fixed_assets",
        "profit_prefin_exp",
        "non_op_profit",
        "op_to_ebt",
        "nop_to_ebt",
        "ocf_to_profit",
        "cash_to_liqdebt",
        "cash_to_liqdebt_withinterest",
        "op_to_liqdebt",
        "op_to_debt",
        "roic_yearly",
        "total_fa_trun",
        "profit_to_op",
        "q_opincome",
        "q_investincome",
        "q_dtprofit",
        "q_eps",
        "q_netprofit_margin",
        "q_gsprofit_margin",
        "q_exp_to_sales",
        "q_profit_to_gr",
        "q_saleexp_to_gr",
        "q_adminexp_to_gr",
        "q_finaexp_to_gr",
        "q_impair_to_gr_ttm",
        "q_gc_to_gr",
        "q_op_to_gr",
        "q_roe",
        "q_dt_roe",
        "q_npta",
        "q_opincome_to_ebt",
        "q_investincome_to_ebt",
        "q_dtprofit_to_profit",
        "q_salescash_to_or",
        "q_ocf_to_sales",
        "q_ocf_to_or",
        "basic_eps_yoy",
        "dt_eps_yoy",
        "cfps_yoy",
        "op_yoy",
        "ebt_yoy",
        "netprofit_yoy",
        "dt_netprofit_yoy",
        "ocf_yoy",
        "roe_yoy",
        "bps_yoy",
        "assets_yoy",
        "eqt_yoy",
        "tr_yoy",
        "or_yoy",
        "q_gr_yoy",
        "q_gr_qoq",
        "q_sales_yoy",
        "q_sales_qoq",
        "q_op_yoy",
        "q_op_qoq",
        "q_profit_yoy",
        "q_profit_qoq",
        "q_netprofit_yoy",
        "q_netprofit_qoq",
        "equity_yoy",
        "rd_exp",
        "update_flag"
    ])
    return df

def getsqlstr(row):
    sqlstr = "insert into t_ods_hs_finance_indicator(ts_code, ann_date, end_date, eps, dt_eps, total_revenue_ps, revenue_ps, capital_rese_ps, surplus_rese_ps, undist_profit_ps, \
            extra_item, profit_dedt, gross_margin, current_ratio, quick_ratio, cash_ratio, invturn_days, arturn_days, inv_turn, ar_turn, ca_turn, fa_turn, assets_turn, op_income, \
            valuechange_income, interst_income, daa, ebit, ebitda, fcff, fcfe, current_exint, noncurrent_exint, interestdebt, netdebt, tangible_asset, working_capital, \
            networking_capital, invest_capital, retained_earnings, diluted2_eps, bps, ocfps, retainedps, cfps, ebit_ps, fcff_ps, fcfe_ps, netprofit_margin, grossprofit_margin, \
            cogs_of_sales, expense_of_sales, profit_to_gr, saleexp_to_gr, adminexp_of_gr, finaexp_of_gr, impai_ttm, gc_of_gr, op_of_gr, ebit_of_gr, roe, roe_waa, roe_dt, roa, npta, \
            roic, roe_yearly, roa2_yearly, roe_avg, opincome_of_ebt, investincome_of_ebt, n_op_profit_of_ebt, tax_to_ebt, dtprofit_to_profit, salescash_to_or, ocf_to_or, \
            ocf_to_opincome, capitalized_to_da, debt_to_assets, assets_to_eqt, dp_assets_to_eqt, ca_to_assets, nca_to_assets, tbassets_to_totalassets, int_to_talcap, \
            eqt_to_talcapital, currentdebt_to_debt, longdeb_to_debt, ocf_to_shortdebt, debt_to_eqt, eqt_to_debt, eqt_to_interestdebt, tangibleasset_to_debt, tangasset_to_intdebt, \
            tangibleasset_to_netdebt, ocf_to_debt, ocf_to_interestdebt, ocf_to_netdebt, ebit_to_interest, longdebt_to_workingcapital, ebitda_to_debt, turn_days, roa_yearly, \
            roa_dp, fixed_assets, profit_prefin_exp, non_op_profit, op_to_ebt, nop_to_ebt, ocf_to_profit, cash_to_liqdebt, cash_to_liqdebt_withinterest, op_to_liqdebt, op_to_debt, \
            roic_yearly, total_fa_trun, profit_to_op, q_opincome, q_investincome, q_dtprofit, q_eps, q_netprofit_margin, q_gsprofit_margin, q_exp_to_sales, q_profit_to_gr, \
            q_saleexp_to_gr, q_adminexp_to_gr, q_finaexp_to_gr, q_impair_to_gr_ttm, q_gc_to_gr, q_op_to_gr, q_roe, q_dt_roe, q_npta, q_opincome_to_ebt, q_investincome_to_ebt, \
            q_dtprofit_to_profit, q_salescash_to_or, q_ocf_to_sales, q_ocf_to_or, basic_eps_yoy, dt_eps_yoy, cfps_yoy, op_yoy, ebt_yoy, netprofit_yoy, dt_netprofit_yoy, ocf_yoy, \
            roe_yoy, bps_yoy, assets_yoy, eqt_yoy, tr_yoy, or_yoy, q_gr_yoy, q_gr_qoq, q_sales_yoy, q_sales_qoq, q_op_yoy, q_op_qoq, q_profit_yoy, q_profit_qoq, q_netprofit_yoy, \
            q_netprofit_qoq, equity_yoy, rd_exp, update_flag) \
            values('%s', '%s', '%s',  \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, \
            %f, %f, %f, '%s') \
            on duplicate key update  \
                eps=%f, dt_eps=%f, total_revenue_ps=%f, revenue_ps=%f, capital_rese_ps=%f, surplus_rese_ps=%f, undist_profit_ps=%f, extra_item=%f, profit_dedt=%f, gross_margin=%f, \
                current_ratio=%f, quick_ratio=%f, cash_ratio=%f, invturn_days=%f, arturn_days=%f, inv_turn=%f, ar_turn=%f, ca_turn=%f, fa_turn=%f, assets_turn=%f, op_income=%f, \
                valuechange_income=%f, interst_income=%f, daa=%f, ebit=%f, ebitda=%f, fcff=%f, fcfe=%f, current_exint=%f, noncurrent_exint=%f, interestdebt=%f, netdebt=%f, \
                tangible_asset=%f, working_capital=%f, networking_capital=%f, invest_capital=%f, retained_earnings=%f, diluted2_eps=%f, bps=%f, ocfps=%f, retainedps=%f, cfps=%f, \
                ebit_ps=%f, fcff_ps=%f, fcfe_ps=%f, netprofit_margin=%f, grossprofit_margin=%f, cogs_of_sales=%f, expense_of_sales=%f, profit_to_gr=%f, saleexp_to_gr=%f, \
                adminexp_of_gr=%f, finaexp_of_gr=%f, impai_ttm=%f, gc_of_gr=%f, op_of_gr=%f, ebit_of_gr=%f, roe=%f, roe_waa=%f, roe_dt=%f, roa=%f, npta=%f, roic=%f, roe_yearly=%f, \
                roa2_yearly=%f, roe_avg=%f, opincome_of_ebt=%f, investincome_of_ebt=%f, n_op_profit_of_ebt=%f, tax_to_ebt=%f, dtprofit_to_profit=%f, salescash_to_or=%f, ocf_to_or=%f, \
                ocf_to_opincome=%f, capitalized_to_da=%f, debt_to_assets=%f, assets_to_eqt=%f, dp_assets_to_eqt=%f, ca_to_assets=%f, nca_to_assets=%f, tbassets_to_totalassets=%f, \
                int_to_talcap=%f, eqt_to_talcapital=%f, currentdebt_to_debt=%f, longdeb_to_debt=%f, ocf_to_shortdebt=%f, debt_to_eqt=%f, eqt_to_debt=%f, eqt_to_interestdebt=%f, \
                tangibleasset_to_debt=%f, tangasset_to_intdebt=%f, tangibleasset_to_netdebt=%f, ocf_to_debt=%f, ocf_to_interestdebt=%f, ocf_to_netdebt=%f, ebit_to_interest=%f, \
                longdebt_to_workingcapital=%f, ebitda_to_debt=%f, turn_days=%f, roa_yearly=%f, roa_dp=%f, fixed_assets=%f, profit_prefin_exp=%f, non_op_profit=%f, op_to_ebt=%f, \
                nop_to_ebt=%f, ocf_to_profit=%f, cash_to_liqdebt=%f, cash_to_liqdebt_withinterest=%f, op_to_liqdebt=%f, op_to_debt=%f, roic_yearly=%f, total_fa_trun=%f, profit_to_op=%f, \
                q_opincome=%f, q_investincome=%f, q_dtprofit=%f, q_eps=%f, q_netprofit_margin=%f, q_gsprofit_margin=%f, q_exp_to_sales=%f, q_profit_to_gr=%f, q_saleexp_to_gr=%f, \
                q_adminexp_to_gr=%f, q_finaexp_to_gr=%f, q_impair_to_gr_ttm=%f, q_gc_to_gr=%f, q_op_to_gr=%f, q_roe=%f, q_dt_roe=%f, q_npta=%f, q_opincome_to_ebt=%f, \
                q_investincome_to_ebt=%f, q_dtprofit_to_profit=%f, q_salescash_to_or=%f, q_ocf_to_sales=%f, q_ocf_to_or=%f, basic_eps_yoy=%f, dt_eps_yoy=%f, cfps_yoy=%f, op_yoy=%f, \
                ebt_yoy=%f, netprofit_yoy=%f, dt_netprofit_yoy=%f, ocf_yoy=%f, roe_yoy=%f, bps_yoy=%f, assets_yoy=%f, eqt_yoy=%f, tr_yoy=%f, or_yoy=%f, q_gr_yoy=%f, q_gr_qoq=%f, \
                q_sales_yoy=%f, q_sales_qoq=%f, q_op_yoy=%f, q_op_qoq=%f, q_profit_yoy=%f, q_profit_qoq=%f, q_netprofit_yoy=%f, q_netprofit_qoq=%f, equity_yoy=%f, rd_exp=%f  " % (
                getattr(row, 'ts_code'), getattr(row, 'ann_date'), getattr(row, 'end_date'),  
                str2float(getattr(row, 'eps')), str2float(getattr(row, 'dt_eps')), str2float(getattr(row, 'total_revenue_ps')), str2float(getattr(row, 'revenue_ps')), 
                str2float(getattr(row, 'capital_rese_ps')), str2float(getattr(row, 'surplus_rese_ps')), str2float(getattr(row, 'undist_profit_ps')), 
                str2float(getattr(row, 'extra_item')), str2float(getattr(row, 'profit_dedt')), str2float(getattr(row, 'gross_margin')), str2float(getattr(row, 'current_ratio')), 
                str2float(getattr(row, 'quick_ratio')), str2float(getattr(row, 'cash_ratio')), str2float(getattr(row, 'invturn_days')), str2float(getattr(row, 'arturn_days')), 
                str2float(getattr(row, 'inv_turn')), str2float(getattr(row, 'ar_turn')), str2float(getattr(row, 'ca_turn')), str2float(getattr(row, 'fa_turn')), 
                str2float(getattr(row, 'assets_turn')), str2float(getattr(row, 'op_income')), str2float(getattr(row, 'valuechange_income')), str2float(getattr(row, 'interst_income')), 
                str2float(getattr(row, 'daa')), str2float(getattr(row, 'ebit')), str2float(getattr(row, 'ebitda')), str2float(getattr(row, 'fcff')), str2float(getattr(row, 'fcfe')), 
                str2float(getattr(row, 'current_exint')), str2float(getattr(row, 'noncurrent_exint')), str2float(getattr(row, 'interestdebt')), str2float(getattr(row, 'netdebt')), 
                str2float(getattr(row, 'tangible_asset')), str2float(getattr(row, 'working_capital')), str2float(getattr(row, 'networking_capital')), 
                str2float(getattr(row, 'invest_capital')), str2float(getattr(row, 'retained_earnings')), str2float(getattr(row, 'diluted2_eps')), str2float(getattr(row, 'bps')), 
                str2float(getattr(row, 'ocfps')), str2float(getattr(row, 'retainedps')), str2float(getattr(row, 'cfps')), str2float(getattr(row, 'ebit_ps')), 
                str2float(getattr(row, 'fcff_ps')), str2float(getattr(row, 'fcfe_ps')), str2float(getattr(row, 'netprofit_margin')), str2float(getattr(row, 'grossprofit_margin')), 
                str2float(getattr(row, 'cogs_of_sales')), str2float(getattr(row, 'expense_of_sales')), str2float(getattr(row, 'profit_to_gr')), str2float(getattr(row, 'saleexp_to_gr')), 
                str2float(getattr(row, 'adminexp_of_gr')), str2float(getattr(row, 'finaexp_of_gr')), str2float(getattr(row, 'impai_ttm')), str2float(getattr(row, 'gc_of_gr')), 
                str2float(getattr(row, 'op_of_gr')), str2float(getattr(row, 'ebit_of_gr')), str2float(getattr(row, 'roe')), str2float(getattr(row, 'roe_waa')), 
                str2float(getattr(row, 'roe_dt')), str2float(getattr(row, 'roa')), str2float(getattr(row, 'npta')), str2float(getattr(row, 'roic')), str2float(getattr(row, 'roe_yearly')), 
                str2float(getattr(row, 'roa2_yearly')), str2float(getattr(row, 'roe_avg')), str2float(getattr(row, 'opincome_of_ebt')), str2float(getattr(row, 'investincome_of_ebt')), 
                str2float(getattr(row, 'n_op_profit_of_ebt')), str2float(getattr(row, 'tax_to_ebt')), str2float(getattr(row, 'dtprofit_to_profit')), 
                str2float(getattr(row, 'salescash_to_or')), str2float(getattr(row, 'ocf_to_or')), str2float(getattr(row, 'ocf_to_opincome')), str2float(getattr(row, 'capitalized_to_da')), 
                str2float(getattr(row, 'debt_to_assets')), str2float(getattr(row, 'assets_to_eqt')), str2float(getattr(row, 'dp_assets_to_eqt')), str2float(getattr(row, 'ca_to_assets')), 
                str2float(getattr(row, 'nca_to_assets')), str2float(getattr(row, 'tbassets_to_totalassets')), str2float(getattr(row, 'int_to_talcap')), 
                str2float(getattr(row, 'eqt_to_talcapital')), str2float(getattr(row, 'currentdebt_to_debt')), str2float(getattr(row, 'longdeb_to_debt')), 
                str2float(getattr(row, 'ocf_to_shortdebt')), str2float(getattr(row, 'debt_to_eqt')), str2float(getattr(row, 'eqt_to_debt')), str2float(getattr(row, 'eqt_to_interestdebt')), 
                str2float(getattr(row, 'tangibleasset_to_debt')), str2float(getattr(row, 'tangasset_to_intdebt')), str2float(getattr(row, 'tangibleasset_to_netdebt')), 
                str2float(getattr(row, 'ocf_to_debt')), str2float(getattr(row, 'ocf_to_interestdebt')), str2float(getattr(row, 'ocf_to_netdebt')), 
                str2float(getattr(row, 'ebit_to_interest')), str2float(getattr(row, 'longdebt_to_workingcapital')), str2float(getattr(row, 'ebitda_to_debt')), 
                str2float(getattr(row, 'turn_days')), str2float(getattr(row, 'roa_yearly')), str2float(getattr(row, 'roa_dp')), str2float(getattr(row, 'fixed_assets')), 
                str2float(getattr(row, 'profit_prefin_exp')), str2float(getattr(row, 'non_op_profit')), str2float(getattr(row, 'op_to_ebt')), str2float(getattr(row, 'nop_to_ebt')), 
                str2float(getattr(row, 'ocf_to_profit')), str2float(getattr(row, 'cash_to_liqdebt')), str2float(getattr(row, 'cash_to_liqdebt_withinterest')), 
                str2float(getattr(row, 'op_to_liqdebt')), str2float(getattr(row, 'op_to_debt')), str2float(getattr(row, 'roic_yearly')), str2float(getattr(row, 'total_fa_trun')), 
                str2float(getattr(row, 'profit_to_op')), str2float(getattr(row, 'q_opincome')), str2float(getattr(row, 'q_investincome')), str2float(getattr(row, 'q_dtprofit')), 
                str2float(getattr(row, 'q_eps')), str2float(getattr(row, 'q_netprofit_margin')), str2float(getattr(row, 'q_gsprofit_margin')), str2float(getattr(row, 'q_exp_to_sales')), 
                str2float(getattr(row, 'q_profit_to_gr')), str2float(getattr(row, 'q_saleexp_to_gr')), str2float(getattr(row, 'q_adminexp_to_gr')), str2float(getattr(row, 'q_finaexp_to_gr')), 
                str2float(getattr(row, 'q_impair_to_gr_ttm')), str2float(getattr(row, 'q_gc_to_gr')), str2float(getattr(row, 'q_op_to_gr')), str2float(getattr(row, 'q_roe')), 
                str2float(getattr(row, 'q_dt_roe')), str2float(getattr(row, 'q_npta')), str2float(getattr(row, 'q_opincome_to_ebt')), str2float(getattr(row, 'q_investincome_to_ebt')), 
                str2float(getattr(row, 'q_dtprofit_to_profit')), str2float(getattr(row, 'q_salescash_to_or')), str2float(getattr(row, 'q_ocf_to_sales')), 
                str2float(getattr(row, 'q_ocf_to_or')), str2float(getattr(row, 'basic_eps_yoy')), str2float(getattr(row, 'dt_eps_yoy')), str2float(getattr(row, 'cfps_yoy')), 
                str2float(getattr(row, 'op_yoy')), str2float(getattr(row, 'ebt_yoy')), str2float(getattr(row, 'netprofit_yoy')), str2float(getattr(row, 'dt_netprofit_yoy')), 
                str2float(getattr(row, 'ocf_yoy')), str2float(getattr(row, 'roe_yoy')), str2float(getattr(row, 'bps_yoy')), str2float(getattr(row, 'assets_yoy')), 
                str2float(getattr(row, 'eqt_yoy')), str2float(getattr(row, 'tr_yoy')), str2float(getattr(row, 'or_yoy')), str2float(getattr(row, 'q_gr_yoy')), 
                str2float(getattr(row, 'q_gr_qoq')), str2float(getattr(row, 'q_sales_yoy')), str2float(getattr(row, 'q_sales_qoq')), str2float(getattr(row, 'q_op_yoy')), 
                str2float(getattr(row, 'q_op_qoq')), str2float(getattr(row, 'q_profit_yoy')), str2float(getattr(row, 'q_profit_qoq')), str2float(getattr(row, 'q_netprofit_yoy')), 
                str2float(getattr(row, 'q_netprofit_qoq')), str2float(getattr(row, 'equity_yoy')), str2float(getattr(row, 'rd_exp')), 
                getattr(row, 'update_flag'), 
                 
                str2float(getattr(row, 'eps')), str2float(getattr(row, 'dt_eps')), str2float(getattr(row, 'total_revenue_ps')), str2float(getattr(row, 'revenue_ps')), 
                str2float(getattr(row, 'capital_rese_ps')), str2float(getattr(row, 'surplus_rese_ps')), str2float(getattr(row, 'undist_profit_ps')), 
                str2float(getattr(row, 'extra_item')), str2float(getattr(row, 'profit_dedt')), str2float(getattr(row, 'gross_margin')), str2float(getattr(row, 'current_ratio')), 
                str2float(getattr(row, 'quick_ratio')), str2float(getattr(row, 'cash_ratio')), str2float(getattr(row, 'invturn_days')), str2float(getattr(row, 'arturn_days')), 
                str2float(getattr(row, 'inv_turn')), str2float(getattr(row, 'ar_turn')), str2float(getattr(row, 'ca_turn')), str2float(getattr(row, 'fa_turn')), 
                str2float(getattr(row, 'assets_turn')), str2float(getattr(row, 'op_income')), str2float(getattr(row, 'valuechange_income')), str2float(getattr(row, 'interst_income')), 
                str2float(getattr(row, 'daa')), str2float(getattr(row, 'ebit')), str2float(getattr(row, 'ebitda')), str2float(getattr(row, 'fcff')), str2float(getattr(row, 'fcfe')), 
                str2float(getattr(row, 'current_exint')), str2float(getattr(row, 'noncurrent_exint')), str2float(getattr(row, 'interestdebt')), str2float(getattr(row, 'netdebt')), 
                str2float(getattr(row, 'tangible_asset')), str2float(getattr(row, 'working_capital')), str2float(getattr(row, 'networking_capital')), 
                str2float(getattr(row, 'invest_capital')), str2float(getattr(row, 'retained_earnings')), str2float(getattr(row, 'diluted2_eps')), str2float(getattr(row, 'bps')), 
                str2float(getattr(row, 'ocfps')), str2float(getattr(row, 'retainedps')), str2float(getattr(row, 'cfps')), str2float(getattr(row, 'ebit_ps')), 
                str2float(getattr(row, 'fcff_ps')), str2float(getattr(row, 'fcfe_ps')), str2float(getattr(row, 'netprofit_margin')), str2float(getattr(row, 'grossprofit_margin')), 
                str2float(getattr(row, 'cogs_of_sales')), str2float(getattr(row, 'expense_of_sales')), str2float(getattr(row, 'profit_to_gr')), str2float(getattr(row, 'saleexp_to_gr')), 
                str2float(getattr(row, 'adminexp_of_gr')), str2float(getattr(row, 'finaexp_of_gr')), str2float(getattr(row, 'impai_ttm')), str2float(getattr(row, 'gc_of_gr')), 
                str2float(getattr(row, 'op_of_gr')), str2float(getattr(row, 'ebit_of_gr')), str2float(getattr(row, 'roe')), str2float(getattr(row, 'roe_waa')), 
                str2float(getattr(row, 'roe_dt')), str2float(getattr(row, 'roa')), str2float(getattr(row, 'npta')), str2float(getattr(row, 'roic')), str2float(getattr(row, 'roe_yearly')), 
                str2float(getattr(row, 'roa2_yearly')), str2float(getattr(row, 'roe_avg')), str2float(getattr(row, 'opincome_of_ebt')), str2float(getattr(row, 'investincome_of_ebt')), 
                str2float(getattr(row, 'n_op_profit_of_ebt')), str2float(getattr(row, 'tax_to_ebt')), str2float(getattr(row, 'dtprofit_to_profit')), 
                str2float(getattr(row, 'salescash_to_or')), str2float(getattr(row, 'ocf_to_or')), str2float(getattr(row, 'ocf_to_opincome')), str2float(getattr(row, 'capitalized_to_da')), 
                str2float(getattr(row, 'debt_to_assets')), str2float(getattr(row, 'assets_to_eqt')), str2float(getattr(row, 'dp_assets_to_eqt')), str2float(getattr(row, 'ca_to_assets')), 
                str2float(getattr(row, 'nca_to_assets')), str2float(getattr(row, 'tbassets_to_totalassets')), str2float(getattr(row, 'int_to_talcap')), 
                str2float(getattr(row, 'eqt_to_talcapital')), str2float(getattr(row, 'currentdebt_to_debt')), str2float(getattr(row, 'longdeb_to_debt')), 
                str2float(getattr(row, 'ocf_to_shortdebt')), str2float(getattr(row, 'debt_to_eqt')), str2float(getattr(row, 'eqt_to_debt')), str2float(getattr(row, 'eqt_to_interestdebt')), 
                str2float(getattr(row, 'tangibleasset_to_debt')), str2float(getattr(row, 'tangasset_to_intdebt')), str2float(getattr(row, 'tangibleasset_to_netdebt')), 
                str2float(getattr(row, 'ocf_to_debt')), str2float(getattr(row, 'ocf_to_interestdebt')), str2float(getattr(row, 'ocf_to_netdebt')), 
                str2float(getattr(row, 'ebit_to_interest')), str2float(getattr(row, 'longdebt_to_workingcapital')), str2float(getattr(row, 'ebitda_to_debt')), 
                str2float(getattr(row, 'turn_days')), str2float(getattr(row, 'roa_yearly')), str2float(getattr(row, 'roa_dp')), str2float(getattr(row, 'fixed_assets')), 
                str2float(getattr(row, 'profit_prefin_exp')), str2float(getattr(row, 'non_op_profit')), str2float(getattr(row, 'op_to_ebt')), str2float(getattr(row, 'nop_to_ebt')), 
                str2float(getattr(row, 'ocf_to_profit')), str2float(getattr(row, 'cash_to_liqdebt')), str2float(getattr(row, 'cash_to_liqdebt_withinterest')), 
                str2float(getattr(row, 'op_to_liqdebt')), str2float(getattr(row, 'op_to_debt')), str2float(getattr(row, 'roic_yearly')), str2float(getattr(row, 'total_fa_trun')), 
                str2float(getattr(row, 'profit_to_op')), str2float(getattr(row, 'q_opincome')), str2float(getattr(row, 'q_investincome')), str2float(getattr(row, 'q_dtprofit')), 
                str2float(getattr(row, 'q_eps')), str2float(getattr(row, 'q_netprofit_margin')), str2float(getattr(row, 'q_gsprofit_margin')), str2float(getattr(row, 'q_exp_to_sales')), 
                str2float(getattr(row, 'q_profit_to_gr')), str2float(getattr(row, 'q_saleexp_to_gr')), str2float(getattr(row, 'q_adminexp_to_gr')), str2float(getattr(row, 'q_finaexp_to_gr')), 
                str2float(getattr(row, 'q_impair_to_gr_ttm')), str2float(getattr(row, 'q_gc_to_gr')), str2float(getattr(row, 'q_op_to_gr')), str2float(getattr(row, 'q_roe')), 
                str2float(getattr(row, 'q_dt_roe')), str2float(getattr(row, 'q_npta')), str2float(getattr(row, 'q_opincome_to_ebt')), str2float(getattr(row, 'q_investincome_to_ebt')), 
                str2float(getattr(row, 'q_dtprofit_to_profit')), str2float(getattr(row, 'q_salescash_to_or')), str2float(getattr(row, 'q_ocf_to_sales')), 
                str2float(getattr(row, 'q_ocf_to_or')), str2float(getattr(row, 'basic_eps_yoy')), str2float(getattr(row, 'dt_eps_yoy')), str2float(getattr(row, 'cfps_yoy')), 
                str2float(getattr(row, 'op_yoy')), str2float(getattr(row, 'ebt_yoy')), str2float(getattr(row, 'netprofit_yoy')), str2float(getattr(row, 'dt_netprofit_yoy')), 
                str2float(getattr(row, 'ocf_yoy')), str2float(getattr(row, 'roe_yoy')), str2float(getattr(row, 'bps_yoy')), str2float(getattr(row, 'assets_yoy')), 
                str2float(getattr(row, 'eqt_yoy')), str2float(getattr(row, 'tr_yoy')), str2float(getattr(row, 'or_yoy')), str2float(getattr(row, 'q_gr_yoy')), 
                str2float(getattr(row, 'q_gr_qoq')), str2float(getattr(row, 'q_sales_yoy')), str2float(getattr(row, 'q_sales_qoq')), str2float(getattr(row, 'q_op_yoy')), 
                str2float(getattr(row, 'q_op_qoq')), str2float(getattr(row, 'q_profit_yoy')), str2float(getattr(row, 'q_profit_qoq')), str2float(getattr(row, 'q_netprofit_yoy')), 
                str2float(getattr(row, 'q_netprofit_qoq')), str2float(getattr(row, 'equity_yoy')), str2float(getattr(row, 'rd_exp'))
            )
    return sqlstr

def process():
    stock_list = []
    cursor = db.cursor()
    total_stocks = 0
    total_rows = 0
    fd = open("./indicator.log", 'w+')
    # get stock list
    sqlstr = "select ts_code from t_ods_hs_base_list where list_status = 'L'  "
    try:
        cursor.execute(sqlstr)
        results = cursor.fetchall()
        for row in results :
            stock_list.append(row[0])
            total_stocks = total_stocks+1
    except Exception as ex:
        print (ex)
        print ("get stock list error!")
        db.close()
        return
    # get income data
    for st_code in stock_list :
        df = queryts(st_code)
        loop = 0
        for row in df.itertuples():
            sql = getsqlstr(row)
            try:
                cursor.execute(sql)
                db.commit()
                loop = loop+1
            except Exception as ex:
                print (ex)
                print ("get stock %s error!" % st_code )
                db.rollback()
                return
        print("get %s indicator data succ! total rows: %d" % (st_code, loop))
        fd.write("get %s indicator data succ! total rows: %d \n" % (st_code, loop))
        fd.flush()
        total_rows = total_rows+loop
        time.sleep(0.5)
    # get all data  
    print("All stock indicator data process succ! total stocks: %d, total rows: %d " % (total_stocks, total_rows))  
    fd.write("All stock indicator data process succ! total stocks: %d, total rows: %d \n" % (total_stocks, total_rows))   

    # close db and file
    fd.close()
    db.close()


process()