from py_jftech import read, to_tuple, where


@read
def get_index_list(index_ids=None, min_date=None, max_date=None, limit=None):
    limit_sql = f'limit {limit}' if limit else ''
    sqls = []
    if min_date:
        sqls.append(f"rid_date >= '{min_date}'")
    if max_date:
        sqls.append(f"rid_date <= '{max_date}'")
    return f'''
    select * from robo_index_datas 
    {where(*sqls, rid_index_id=to_tuple(index_ids))} order by rid_index_id, rid_date {limit_sql}
    '''


@read
def get_eco_list(eco_ids=None, min_date=None, max_date=None):
    sqls = []
    if min_date:
        sqls.append(f"red_date >= '{min_date}'")
    if max_date:
        sqls.append(f"red_date <= '{max_date}'")
    return f'''
    select * from robo_eco_datas 
    {where(*sqls, red_eco_id=to_tuple(eco_ids))} order by red_eco_id, red_date
    '''


@read
def get_fund_list(fund_ids=None, min_date=None, max_date=None, limit=None):
    limit_sql = f'limit {limit}' if limit else ''
    sqls = []
    if min_date:
        sqls.append(f"rfn_date >= '{min_date}'")
    if max_date:
        sqls.append(f"rfn_date <= '{max_date}'")
    return f'''
    select * from robo_fund_navs 
    {where(*sqls, rfn_fund_id=to_tuple(fund_ids))} order by rfn_fund_id, rfn_date {limit_sql}
    '''


@read
def get_base_info(ids=None):
    sqls = []
    return f"""
    SELECT rbd_id id,v_rbd_bloomberg_ticker ticker,v_rbd_type type, rbd_datas datas FROM `robo_base_datum`
    {where(*sqls, rbd_id=to_tuple(ids))}
    """