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.balancesheet(**{
        "ts_code": code,
        "ann_date": "",
        "f_ann_date": "",
        "start_date": 20220630,
        "end_date": 20230604,
        "period": "",
        "report_type": "",
        "comp_type": "",
        "end_type": "",
        "limit": "",
        "offset": ""
    }, fields=[
        "ts_code",
        "ann_date",
        "f_ann_date",
        "end_date",
        "report_type",
        "comp_type",
        "end_type",
        "total_share",
        "cap_rese",
        "undistr_porfit",
        "surplus_rese",
        "special_rese",
        "money_cap",
        "trad_asset",
        "notes_receiv",
        "accounts_receiv",
        "oth_receiv",
        "prepayment",
        "div_receiv",
        "int_receiv",
        "inventories",
        "amor_exp",
        "nca_within_1y",
        "sett_rsrv",
        "loanto_oth_bank_fi",
        "premium_receiv",
        "reinsur_receiv",
        "reinsur_res_receiv",
        "pur_resale_fa",
        "oth_cur_assets",
        "total_cur_assets",
        "fa_avail_for_sale",
        "htm_invest",
        "lt_eqt_invest",
        "invest_real_estate",
        "time_deposits",
        "oth_assets",
        "lt_rec",
        "fix_assets",
        "cip",
        "const_materials",
        "fixed_assets_disp",
        "produc_bio_assets",
        "oil_and_gas_assets",
        "intan_assets",
        "r_and_d",
        "goodwill",
        "lt_amor_exp",
        "defer_tax_assets",
        "decr_in_disbur",
        "oth_nca",
        "total_nca",
        "cash_reser_cb",
        "depos_in_oth_bfi",
        "prec_metals",
        "deriv_assets",
        "rr_reins_une_prem",
        "rr_reins_outstd_cla",
        "rr_reins_lins_liab",
        "rr_reins_lthins_liab",
        "refund_depos",
        "ph_pledge_loans",
        "refund_cap_depos",
        "indep_acct_assets",
        "client_depos",
        "client_prov",
        "transac_seat_fee",
        "invest_as_receiv",
        "total_assets",
        "lt_borr",
        "st_borr",
        "cb_borr",
        "depos_ib_deposits",
        "loan_oth_bank",
        "trading_fl",
        "notes_payable",
        "acct_payable",
        "adv_receipts",
        "sold_for_repur_fa",
        "comm_payable",
        "payroll_payable",
        "taxes_payable",
        "int_payable",
        "div_payable",
        "oth_payable",
        "acc_exp",
        "deferred_inc",
        "st_bonds_payable",
        "payable_to_reinsurer",
        "rsrv_insur_cont",
        "acting_trading_sec",
        "acting_uw_sec",
        "non_cur_liab_due_1y",
        "oth_cur_liab",
        "total_cur_liab",
        "bond_payable",
        "lt_payable",
        "specific_payables",
        "estimated_liab",
        "defer_tax_liab",
        "defer_inc_non_cur_liab",
        "oth_ncl",
        "total_ncl",
        "depos_oth_bfi",
        "deriv_liab",
        "depos",
        "agency_bus_liab",
        "oth_liab",
        "prem_receiv_adva",
        "depos_received",
        "ph_invest",
        "reser_une_prem",
        "reser_outstd_claims",
        "reser_lins_liab",
        "reser_lthins_liab",
        "indept_acc_liab",
        "pledge_borr",
        "indem_payable",
        "policy_div_payable",
        "total_liab",
        "treasury_share",
        "ordin_risk_reser",
        "forex_differ",
        "invest_loss_unconf",
        "minority_int",
        "total_hldr_eqy_exc_min_int",
        "total_hldr_eqy_inc_min_int",
        "total_liab_hldr_eqy",
        "lt_payroll_payable",
        "oth_comp_income",
        "oth_eqt_tools",
        "oth_eqt_tools_p_shr",
        "lending_funds",
        "acc_receivable",
        "st_fin_payable",
        "payables",
        "hfs_assets",
        "hfs_sales",
        "cost_fin_assets",
        "fair_value_fin_assets",
        "contract_assets",
        "contract_liab",
        "accounts_receiv_bill",
        "accounts_pay",
        "oth_rcv_total",
        "fix_assets_total",
        "cip_total",
        "oth_pay_total",
        "long_pay_total",
        "debt_invest",
        "oth_debt_invest",
        "oth_eq_invest",
        "oth_illiq_fin_assets",
        "oth_eq_ppbond",
        "receiv_financing",
        "use_right_assets",
        "lease_liab",
        "update_flag"
    ])
    return df

def getsqlstr(row):
    sqlstr = "insert into t_ods_hs_finance_balance(ts_code, ann_date, f_ann_date, end_date, report_type, comp_type, \
            end_type, total_share, cap_rese, undistr_porfit, surplus_rese, special_rese, money_cap, trad_asset, notes_receiv, \
            accounts_receiv, oth_receiv, prepayment, div_receiv, int_receiv, inventories, amor_exp, nca_within_1y, sett_rsrv, \
            loanto_oth_bank_fi, premium_receiv, reinsur_receiv, reinsur_res_receiv, pur_resale_fa, oth_cur_assets, total_cur_assets, \
            fa_avail_for_sale, htm_invest, lt_eqt_invest, invest_real_estate, time_deposits, oth_assets, lt_rec, fix_assets, cip, \
            const_materials, fixed_assets_disp, produc_bio_assets, oil_and_gas_assets, intan_assets, r_and_d, goodwill, lt_amor_exp, \
            defer_tax_assets, decr_in_disbur, oth_nca, total_nca, cash_reser_cb, depos_in_oth_bfi, prec_metals, deriv_assets, rr_reins_une_prem, \
            rr_reins_outstd_cla, rr_reins_lins_liab, rr_reins_lthins_liab, refund_depos, ph_pledge_loans, refund_cap_depos, indep_acct_assets, \
            client_depos, client_prov, transac_seat_fee, invest_as_receiv, total_assets, lt_borr, st_borr, cb_borr, depos_ib_deposits, loan_oth_bank, \
            trading_fl, notes_payable, acct_payable, adv_receipts, sold_for_repur_fa, comm_payable, payroll_payable, taxes_payable, int_payable, \
            div_payable, oth_payable, acc_exp, deferred_inc, st_bonds_payable, payable_to_reinsurer, rsrv_insur_cont, acting_trading_sec, acting_uw_sec, \
            non_cur_liab_due_1y, oth_cur_liab, total_cur_liab, bond_payable, lt_payable, specific_payables, estimated_liab, defer_tax_liab, \
            defer_inc_non_cur_liab, oth_ncl, total_ncl, depos_oth_bfi, deriv_liab, depos, agency_bus_liab, oth_liab, prem_receiv_adva, depos_received, \
            ph_invest, reser_une_prem, reser_outstd_claims, reser_lins_liab, reser_lthins_liab, indept_acc_liab, pledge_borr, indem_payable, policy_div_payable, \
            total_liab, treasury_share, ordin_risk_reser, forex_differ, invest_loss_unconf, minority_int, total_hldr_eqy_exc_min_int, total_hldr_eqy_inc_min_int, \
            total_liab_hldr_eqy, lt_payroll_payable, oth_comp_income, oth_eqt_tools, oth_eqt_tools_p_shr, lending_funds, acc_receivable, st_fin_payable, \
            payables, hfs_assets, hfs_sales, cost_fin_assets, fair_value_fin_assets, contract_assets, contract_liab, accounts_receiv_bill, accounts_pay, \
            oth_rcv_total, fix_assets_total, cip_total, oth_pay_total, long_pay_total, debt_invest, oth_debt_invest, oth_eq_invest, oth_illiq_fin_assets, \
            oth_eq_ppbond, receiv_financing, use_right_assets, lease_liab, update_flag) \
            values('%s', '%s', '%s', '%s', '%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, '%s') \
            on duplicate key update report_type='%s', comp_type='%s', end_type='%s', \
                total_share=%f, cap_rese=%f, undistr_porfit=%f, surplus_rese=%f, special_rese=%f, money_cap=%f, trad_asset=%f, notes_receiv=%f, accounts_receiv=%f, \
                oth_receiv=%f, prepayment=%f, div_receiv=%f, int_receiv=%f, inventories=%f, amor_exp=%f, nca_within_1y=%f, sett_rsrv=%f, loanto_oth_bank_fi=%f, \
                premium_receiv=%f, reinsur_receiv=%f, reinsur_res_receiv=%f, pur_resale_fa=%f, oth_cur_assets=%f, total_cur_assets=%f, fa_avail_for_sale=%f, \
                htm_invest=%f, lt_eqt_invest=%f, invest_real_estate=%f, time_deposits=%f, oth_assets=%f, lt_rec=%f, fix_assets=%f, cip=%f, const_materials=%f, \
                fixed_assets_disp=%f, produc_bio_assets=%f, oil_and_gas_assets=%f, intan_assets=%f, r_and_d=%f, goodwill=%f, lt_amor_exp=%f, defer_tax_assets=%f, \
                decr_in_disbur=%f, oth_nca=%f, total_nca=%f, cash_reser_cb=%f, depos_in_oth_bfi=%f, prec_metals=%f, deriv_assets=%f, rr_reins_une_prem=%f, \
                rr_reins_outstd_cla=%f, rr_reins_lins_liab=%f, rr_reins_lthins_liab=%f, refund_depos=%f, ph_pledge_loans=%f, refund_cap_depos=%f, indep_acct_assets=%f, \
                client_depos=%f, client_prov=%f, transac_seat_fee=%f, invest_as_receiv=%f, total_assets=%f, lt_borr=%f, st_borr=%f, cb_borr=%f, depos_ib_deposits=%f, \
                loan_oth_bank=%f, trading_fl=%f, notes_payable=%f, acct_payable=%f, adv_receipts=%f, sold_for_repur_fa=%f, comm_payable=%f, payroll_payable=%f, \
                taxes_payable=%f, int_payable=%f, div_payable=%f, oth_payable=%f, acc_exp=%f, deferred_inc=%f, st_bonds_payable=%f, payable_to_reinsurer=%f, \
                rsrv_insur_cont=%f, acting_trading_sec=%f, acting_uw_sec=%f, non_cur_liab_due_1y=%f, oth_cur_liab=%f, total_cur_liab=%f, bond_payable=%f, lt_payable=%f, \
                specific_payables=%f, estimated_liab=%f, defer_tax_liab=%f, defer_inc_non_cur_liab=%f, oth_ncl=%f, total_ncl=%f, depos_oth_bfi=%f, deriv_liab=%f, depos=%f, \
                agency_bus_liab=%f, oth_liab=%f, prem_receiv_adva=%f, depos_received=%f, ph_invest=%f, reser_une_prem=%f, reser_outstd_claims=%f, reser_lins_liab=%f, \
                reser_lthins_liab=%f, indept_acc_liab=%f, pledge_borr=%f, indem_payable=%f, policy_div_payable=%f, total_liab=%f, treasury_share=%f, ordin_risk_reser=%f, \
                forex_differ=%f, invest_loss_unconf=%f, minority_int=%f, total_hldr_eqy_exc_min_int=%f, total_hldr_eqy_inc_min_int=%f, total_liab_hldr_eqy=%f, \
                lt_payroll_payable=%f, oth_comp_income=%f, oth_eqt_tools=%f, oth_eqt_tools_p_shr=%f, lending_funds=%f, acc_receivable=%f, st_fin_payable=%f, payables=%f, \
                hfs_assets=%f, hfs_sales=%f, cost_fin_assets=%f, fair_value_fin_assets=%f, contract_assets=%f, contract_liab=%f, accounts_receiv_bill=%f, accounts_pay=%f, \
                oth_rcv_total=%f, fix_assets_total=%f, cip_total=%f, oth_pay_total=%f, long_pay_total=%f, debt_invest=%f, oth_debt_invest=%f, oth_eq_invest=%f, \
                oth_illiq_fin_assets=%f, oth_eq_ppbond=%f, receiv_financing=%f, use_right_assets=%f, lease_liab=%f " % (
                getattr(row, 'ts_code'), getattr(row, 'ann_date'), getattr(row, 'f_ann_date'), getattr(row, 'end_date'), 
                getattr(row, 'report_type'), getattr(row, 'comp_type'), getattr(row, 'end_type'), 
                str2float(getattr(row, 'total_share')), str2float(getattr(row, 'cap_rese')), str2float(getattr(row, 'undistr_porfit')), 
                str2float(getattr(row, 'surplus_rese')), str2float(getattr(row, 'special_rese')), str2float(getattr(row, 'money_cap')), 
                str2float(getattr(row, 'trad_asset')), str2float(getattr(row, 'notes_receiv')), str2float(getattr(row, 'accounts_receiv')), 
                str2float(getattr(row, 'oth_receiv')), str2float(getattr(row, 'prepayment')), str2float(getattr(row, 'div_receiv')), 
                str2float(getattr(row, 'int_receiv')), str2float(getattr(row, 'inventories')), str2float(getattr(row, 'amor_exp')), 
                str2float(getattr(row, 'nca_within_1y')), str2float(getattr(row, 'sett_rsrv')), str2float(getattr(row, 'loanto_oth_bank_fi')), 
                str2float(getattr(row, 'premium_receiv')), str2float(getattr(row, 'reinsur_receiv')), str2float(getattr(row, 'reinsur_res_receiv')), 
                str2float(getattr(row, 'pur_resale_fa')), str2float(getattr(row, 'oth_cur_assets')), str2float(getattr(row, 'total_cur_assets')), 
                str2float(getattr(row, 'fa_avail_for_sale')), str2float(getattr(row, 'htm_invest')), str2float(getattr(row, 'lt_eqt_invest')), 
                str2float(getattr(row, 'invest_real_estate')), str2float(getattr(row, 'time_deposits')), str2float(getattr(row, 'oth_assets')), 
                str2float(getattr(row, 'lt_rec')), str2float(getattr(row, 'fix_assets')), str2float(getattr(row, 'cip')), 
                str2float(getattr(row, 'const_materials')), str2float(getattr(row, 'fixed_assets_disp')), str2float(getattr(row, 'produc_bio_assets')), 
                str2float(getattr(row, 'oil_and_gas_assets')), str2float(getattr(row, 'intan_assets')), str2float(getattr(row, 'r_and_d')), 
                str2float(getattr(row, 'goodwill')), str2float(getattr(row, 'lt_amor_exp')), str2float(getattr(row, 'defer_tax_assets')), 
                str2float(getattr(row, 'decr_in_disbur')), str2float(getattr(row, 'oth_nca')), str2float(getattr(row, 'total_nca')), 
                str2float(getattr(row, 'cash_reser_cb')), str2float(getattr(row, 'depos_in_oth_bfi')), str2float(getattr(row, 'prec_metals')), 
                str2float(getattr(row, 'deriv_assets')), str2float(getattr(row, 'rr_reins_une_prem')), str2float(getattr(row, 'rr_reins_outstd_cla')), 
                str2float(getattr(row, 'rr_reins_lins_liab')), str2float(getattr(row, 'rr_reins_lthins_liab')), str2float(getattr(row, 'refund_depos')), 
                str2float(getattr(row, 'ph_pledge_loans')), str2float(getattr(row, 'refund_cap_depos')), str2float(getattr(row, 'indep_acct_assets')), 
                str2float(getattr(row, 'client_depos')), str2float(getattr(row, 'client_prov')), str2float(getattr(row, 'transac_seat_fee')), 
                str2float(getattr(row, 'invest_as_receiv')), str2float(getattr(row, 'total_assets')), str2float(getattr(row, 'lt_borr')), 
                str2float(getattr(row, 'st_borr')), str2float(getattr(row, 'cb_borr')), str2float(getattr(row, 'depos_ib_deposits')), 
                str2float(getattr(row, 'loan_oth_bank')), str2float(getattr(row, 'trading_fl')), str2float(getattr(row, 'notes_payable')), 
                str2float(getattr(row, 'acct_payable')), str2float(getattr(row, 'adv_receipts')), str2float(getattr(row, 'sold_for_repur_fa')), 
                str2float(getattr(row, 'comm_payable')), str2float(getattr(row, 'payroll_payable')), str2float(getattr(row, 'taxes_payable')), 
                str2float(getattr(row, 'int_payable')), str2float(getattr(row, 'div_payable')), str2float(getattr(row, 'oth_payable')), 
                str2float(getattr(row, 'acc_exp')), str2float(getattr(row, 'deferred_inc')), str2float(getattr(row, 'st_bonds_payable')), 
                str2float(getattr(row, 'payable_to_reinsurer')), str2float(getattr(row, 'rsrv_insur_cont')), str2float(getattr(row, 'acting_trading_sec')), 
                str2float(getattr(row, 'acting_uw_sec')), str2float(getattr(row, 'non_cur_liab_due_1y')), str2float(getattr(row, 'oth_cur_liab')), 
                str2float(getattr(row, 'total_cur_liab')), str2float(getattr(row, 'bond_payable')), str2float(getattr(row, 'lt_payable')), 
                str2float(getattr(row, 'specific_payables')), str2float(getattr(row, 'estimated_liab')), str2float(getattr(row, 'defer_tax_liab')), 
                str2float(getattr(row, 'defer_inc_non_cur_liab')), str2float(getattr(row, 'oth_ncl')), str2float(getattr(row, 'total_ncl')), 
                str2float(getattr(row, 'depos_oth_bfi')), str2float(getattr(row, 'deriv_liab')), str2float(getattr(row, 'depos')), 
                str2float(getattr(row, 'agency_bus_liab')), str2float(getattr(row, 'oth_liab')), str2float(getattr(row, 'prem_receiv_adva')), 
                str2float(getattr(row, 'depos_received')), str2float(getattr(row, 'ph_invest')), str2float(getattr(row, 'reser_une_prem')), 
                str2float(getattr(row, 'reser_outstd_claims')), str2float(getattr(row, 'reser_lins_liab')), str2float(getattr(row, 'reser_lthins_liab')), 
                str2float(getattr(row, 'indept_acc_liab')), str2float(getattr(row, 'pledge_borr')), str2float(getattr(row, 'indem_payable')), 
                str2float(getattr(row, 'policy_div_payable')), str2float(getattr(row, 'total_liab')), str2float(getattr(row, 'treasury_share')), 
                str2float(getattr(row, 'ordin_risk_reser')), str2float(getattr(row, 'forex_differ')), str2float(getattr(row, 'invest_loss_unconf')), 
                str2float(getattr(row, 'minority_int')), str2float(getattr(row, 'total_hldr_eqy_exc_min_int')), str2float(getattr(row, 'total_hldr_eqy_inc_min_int')), 
                str2float(getattr(row, 'total_liab_hldr_eqy')), str2float(getattr(row, 'lt_payroll_payable')), str2float(getattr(row, 'oth_comp_income')), 
                str2float(getattr(row, 'oth_eqt_tools')), str2float(getattr(row, 'oth_eqt_tools_p_shr')), str2float(getattr(row, 'lending_funds')), 
                str2float(getattr(row, 'acc_receivable')), str2float(getattr(row, 'st_fin_payable')), str2float(getattr(row, 'payables')), 
                str2float(getattr(row, 'hfs_assets')), str2float(getattr(row, 'hfs_sales')), str2float(getattr(row, 'cost_fin_assets')), 
                str2float(getattr(row, 'fair_value_fin_assets')), str2float(getattr(row, 'contract_assets')), str2float(getattr(row, 'contract_liab')), 
                str2float(getattr(row, 'accounts_receiv_bill')), str2float(getattr(row, 'accounts_pay')), str2float(getattr(row, 'oth_rcv_total')), 
                str2float(getattr(row, 'fix_assets_total')), str2float(getattr(row, 'cip_total')), str2float(getattr(row, 'oth_pay_total')), 
                str2float(getattr(row, 'long_pay_total')), str2float(getattr(row, 'debt_invest')), str2float(getattr(row, 'oth_debt_invest')), 
                str2float(getattr(row, 'oth_eq_invest')), str2float(getattr(row, 'oth_illiq_fin_assets')), str2float(getattr(row, 'oth_eq_ppbond')), 
                str2float(getattr(row, 'receiv_financing')), str2float(getattr(row, 'use_right_assets')), str2float(getattr(row, 'lease_liab')), 
                getattr(row, 'update_flag'), 
                 
                getattr(row, 'report_type'), getattr(row, 'comp_type'), getattr(row, 'end_type'), 
                str2float(getattr(row, 'total_share')), str2float(getattr(row, 'cap_rese')), str2float(getattr(row, 'undistr_porfit')), 
                str2float(getattr(row, 'surplus_rese')), str2float(getattr(row, 'special_rese')), str2float(getattr(row, 'money_cap')), 
                str2float(getattr(row, 'trad_asset')), str2float(getattr(row, 'notes_receiv')), str2float(getattr(row, 'accounts_receiv')), 
                str2float(getattr(row, 'oth_receiv')), str2float(getattr(row, 'prepayment')), str2float(getattr(row, 'div_receiv')), 
                str2float(getattr(row, 'int_receiv')), str2float(getattr(row, 'inventories')), str2float(getattr(row, 'amor_exp')), 
                str2float(getattr(row, 'nca_within_1y')), str2float(getattr(row, 'sett_rsrv')), str2float(getattr(row, 'loanto_oth_bank_fi')), 
                str2float(getattr(row, 'premium_receiv')), str2float(getattr(row, 'reinsur_receiv')), str2float(getattr(row, 'reinsur_res_receiv')), 
                str2float(getattr(row, 'pur_resale_fa')), str2float(getattr(row, 'oth_cur_assets')), str2float(getattr(row, 'total_cur_assets')), 
                str2float(getattr(row, 'fa_avail_for_sale')), str2float(getattr(row, 'htm_invest')), str2float(getattr(row, 'lt_eqt_invest')), 
                str2float(getattr(row, 'invest_real_estate')), str2float(getattr(row, 'time_deposits')), str2float(getattr(row, 'oth_assets')), 
                str2float(getattr(row, 'lt_rec')), str2float(getattr(row, 'fix_assets')), str2float(getattr(row, 'cip')), 
                str2float(getattr(row, 'const_materials')), str2float(getattr(row, 'fixed_assets_disp')), str2float(getattr(row, 'produc_bio_assets')), 
                str2float(getattr(row, 'oil_and_gas_assets')), str2float(getattr(row, 'intan_assets')), str2float(getattr(row, 'r_and_d')), 
                str2float(getattr(row, 'goodwill')), str2float(getattr(row, 'lt_amor_exp')), str2float(getattr(row, 'defer_tax_assets')), 
                str2float(getattr(row, 'decr_in_disbur')), str2float(getattr(row, 'oth_nca')), str2float(getattr(row, 'total_nca')), 
                str2float(getattr(row, 'cash_reser_cb')), str2float(getattr(row, 'depos_in_oth_bfi')), str2float(getattr(row, 'prec_metals')), 
                str2float(getattr(row, 'deriv_assets')), str2float(getattr(row, 'rr_reins_une_prem')), str2float(getattr(row, 'rr_reins_outstd_cla')), 
                str2float(getattr(row, 'rr_reins_lins_liab')), str2float(getattr(row, 'rr_reins_lthins_liab')), str2float(getattr(row, 'refund_depos')), 
                str2float(getattr(row, 'ph_pledge_loans')), str2float(getattr(row, 'refund_cap_depos')), str2float(getattr(row, 'indep_acct_assets')), 
                str2float(getattr(row, 'client_depos')), str2float(getattr(row, 'client_prov')), str2float(getattr(row, 'transac_seat_fee')), 
                str2float(getattr(row, 'invest_as_receiv')), str2float(getattr(row, 'total_assets')), str2float(getattr(row, 'lt_borr')), 
                str2float(getattr(row, 'st_borr')), str2float(getattr(row, 'cb_borr')), str2float(getattr(row, 'depos_ib_deposits')), 
                str2float(getattr(row, 'loan_oth_bank')), str2float(getattr(row, 'trading_fl')), str2float(getattr(row, 'notes_payable')), 
                str2float(getattr(row, 'acct_payable')), str2float(getattr(row, 'adv_receipts')), str2float(getattr(row, 'sold_for_repur_fa')), 
                str2float(getattr(row, 'comm_payable')), str2float(getattr(row, 'payroll_payable')), str2float(getattr(row, 'taxes_payable')), 
                str2float(getattr(row, 'int_payable')), str2float(getattr(row, 'div_payable')), str2float(getattr(row, 'oth_payable')), 
                str2float(getattr(row, 'acc_exp')), str2float(getattr(row, 'deferred_inc')), str2float(getattr(row, 'st_bonds_payable')), 
                str2float(getattr(row, 'payable_to_reinsurer')), str2float(getattr(row, 'rsrv_insur_cont')), str2float(getattr(row, 'acting_trading_sec')), 
                str2float(getattr(row, 'acting_uw_sec')), str2float(getattr(row, 'non_cur_liab_due_1y')), str2float(getattr(row, 'oth_cur_liab')), 
                str2float(getattr(row, 'total_cur_liab')), str2float(getattr(row, 'bond_payable')), str2float(getattr(row, 'lt_payable')), 
                str2float(getattr(row, 'specific_payables')), str2float(getattr(row, 'estimated_liab')), str2float(getattr(row, 'defer_tax_liab')), 
                str2float(getattr(row, 'defer_inc_non_cur_liab')), str2float(getattr(row, 'oth_ncl')), str2float(getattr(row, 'total_ncl')), 
                str2float(getattr(row, 'depos_oth_bfi')), str2float(getattr(row, 'deriv_liab')), str2float(getattr(row, 'depos')), 
                str2float(getattr(row, 'agency_bus_liab')), str2float(getattr(row, 'oth_liab')), str2float(getattr(row, 'prem_receiv_adva')), 
                str2float(getattr(row, 'depos_received')), str2float(getattr(row, 'ph_invest')), str2float(getattr(row, 'reser_une_prem')), 
                str2float(getattr(row, 'reser_outstd_claims')), str2float(getattr(row, 'reser_lins_liab')), str2float(getattr(row, 'reser_lthins_liab')), 
                str2float(getattr(row, 'indept_acc_liab')), str2float(getattr(row, 'pledge_borr')), str2float(getattr(row, 'indem_payable')), 
                str2float(getattr(row, 'policy_div_payable')), str2float(getattr(row, 'total_liab')), str2float(getattr(row, 'treasury_share')), 
                str2float(getattr(row, 'ordin_risk_reser')), str2float(getattr(row, 'forex_differ')), str2float(getattr(row, 'invest_loss_unconf')), 
                str2float(getattr(row, 'minority_int')), str2float(getattr(row, 'total_hldr_eqy_exc_min_int')), str2float(getattr(row, 'total_hldr_eqy_inc_min_int')), 
                str2float(getattr(row, 'total_liab_hldr_eqy')), str2float(getattr(row, 'lt_payroll_payable')), str2float(getattr(row, 'oth_comp_income')), 
                str2float(getattr(row, 'oth_eqt_tools')), str2float(getattr(row, 'oth_eqt_tools_p_shr')), str2float(getattr(row, 'lending_funds')), 
                str2float(getattr(row, 'acc_receivable')), str2float(getattr(row, 'st_fin_payable')), str2float(getattr(row, 'payables')), 
                str2float(getattr(row, 'hfs_assets')), str2float(getattr(row, 'hfs_sales')), str2float(getattr(row, 'cost_fin_assets')), 
                str2float(getattr(row, 'fair_value_fin_assets')), str2float(getattr(row, 'contract_assets')), str2float(getattr(row, 'contract_liab')), 
                str2float(getattr(row, 'accounts_receiv_bill')), str2float(getattr(row, 'accounts_pay')), str2float(getattr(row, 'oth_rcv_total')), 
                str2float(getattr(row, 'fix_assets_total')), str2float(getattr(row, 'cip_total')), str2float(getattr(row, 'oth_pay_total')), 
                str2float(getattr(row, 'long_pay_total')), str2float(getattr(row, 'debt_invest')), str2float(getattr(row, 'oth_debt_invest')), 
                str2float(getattr(row, 'oth_eq_invest')), str2float(getattr(row, 'oth_illiq_fin_assets')), str2float(getattr(row, 'oth_eq_ppbond')), 
                str2float(getattr(row, 'receiv_financing')), str2float(getattr(row, 'use_right_assets')), str2float(getattr(row, 'lease_liab'))
            )
    return sqlstr

def process():
    stock_list = []
    cursor = db.cursor()
    total_stocks = 0
    total_rows = 0
    fd = open("./balance.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 balance data succ! total rows: %d" % (st_code, loop))
        fd.write("get %s balance 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 balance data process succ! total stocks: %d, total rows: %d " % (total_stocks, total_rows))  
    fd.write("All stock balance data process succ! total stocks: %d, total rows: %d \n" % (total_stocks, total_rows))   

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


process()