'''
写一个方法：
1. 接收日期范围
2. 根据日期范围从表：xwlb_daily 查询数据：news_days, daily_sub_id, news_improve, news_title
    按news_days desc, daily_sub_id asc 排序
3. 调用mysqlHandle.py 里的查询方法查询数据(仔细阅读video/mysqlHandle)
'''
# 调用mysqlHandle中的查询方法
try:
    from .mysqlHandle import MySQLDB
except (ImportError, SystemError):  
    from mysqlHandle import MySQLDB
import pandas as pd
def get_xwlb(start_date, end_date):
    """
    根据日期范围查询新闻联播数据

    Args:
        start_date: 开始日期
        end_date: 结束日期

    Returns:
        查询结果列表
    """
    # SQL注入警告：使用参数化查询防止SQL注入
    sql = "xwlb_daily"
    columns = "news_days, daily_sub_id, news_improve, news_title"
    where = "news_days >= %s AND news_days <=%s order by news_days desc, daily_sub_id asc"
    params = (start_date, end_date)
    try:
        db = MySQLDB()
        result = db.query_data(sql, columns, where, params)
        print(f"查询到 {len(result)} 条记录")
    finally:
        # 关闭连接
        db.close()
    # 转换为pandas DataFrame
    
    df = pd.DataFrame(result)

    return df


def get_xwlb_fine(start_date, end_date):
    """
    根据日期范围查询新闻联播数据

    Args:
        start_date: 开始日期
        end_date: 结束日期

    Returns:
        查询结果列表
    """
    # SQL注入警告：使用参数化查询防止SQL注入
    sql = "xwlb_daily_ext"
    columns = "news_date as news_days, sub_id as daily_sub_id, news_content as news_improve, news_title"
    where = "news_date >= %s AND news_date <=%s order by news_date desc, sub_id asc"
    params = (start_date, end_date)
    try:
        db = MySQLDB()
        result = db.query_data(sql, columns, where, params)
        print(f"查询到 {len(result)} 条记录")
    finally:
        # 关闭连接
        db.close()
    # 转换为pandas DataFrame
    
    df = pd.DataFrame(result)

    return df

if __name__ == "__main__":
    # 测试代码
    start_date = "2025-01-01"
    end_date = "2025-01-31"
    result = get_xwlb(start_date, end_date)
    print("查询结果：")
    print(result.head())
    print(f"总记录数：{len(result)}")