{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "4eeb08f90b26d53f"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-20T08:27:40.142050Z",
     "start_time": "2025-08-20T08:27:38.703087Z"
    }
   },
   "source": [
    "from datetime import datetime, timezone, timedelta, date, UTC\n",
    "import holidays\n",
    "from config import Config\n",
    "import psycopg2\n",
    "import pandas as pd\n",
    "import pymysql\n",
    "from api import API\n",
    "from log_config import configure_task_logger, configure_error_task_logger\n",
    "\n",
    "api_instance = API()\n",
    "# 获取已经配置好的常规日志记录器\n",
    "logger = configure_task_logger()\n",
    "\n",
    "# 获取已经配置好的错误任务日志记录器\n",
    "error_task_logger = configure_error_task_logger()\n",
    "\n",
    "\n",
    "def get_ngv_details():\n",
    "    \"\"\"\n",
    "    从固定的数据库中获取前几天的NGV明细。\n",
    "    参数 `days_back` 表示相对于今天的天数偏移量，默认为1（即前一天）。\n",
    "    返回包含NGV明细的pandas DataFrame。\n",
    "    \"\"\"\n",
    "    try:\n",
    "        # 获得连接\n",
    "        conn = psycopg2.connect(**Config.CONN_INFO)\n",
    "        cursor = conn.cursor()\n",
    "\n",
    "        # sql语句查询\n",
    "        sql = f\"\"\"\n",
    "        SELECT * FROM \"public\".\"saas_ngv_yesterday\";\n",
    "        \"\"\"\n",
    "\n",
    "        # 执行语句并获取结果集\n",
    "        cursor.execute(sql)\n",
    "        rows = cursor.fetchall()\n",
    "        all_fields = cursor.description\n",
    "\n",
    "        # 执行结果转化为dataframe\n",
    "        col = [i[0] for i in all_fields]\n",
    "        data_NGV = pd.DataFrame(rows, columns=col)\n",
    "\n",
    "        # 尝试自动解析日期时间字符串\n",
    "        time_format = \"%Y-%m-%d %H:%M:%S\"\n",
    "        if 'saas_create_time' in data_NGV.columns:\n",
    "            data_NGV['saas_create_time'] = pd.to_datetime(data_NGV['saas_create_time'], format=time_format,\n",
    "                                                          errors='coerce')\n",
    "            data_NGV['saas_create_time'] = data_NGV['saas_create_time'].dt.strftime('%Y-%m-%d')\n",
    "\n",
    "        # 关闭游标和连接\n",
    "        cursor.close()\n",
    "        conn.close()\n",
    "\n",
    "        return data_NGV\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Error occurred: {e}\")\n",
    "        return None\n",
    "\n",
    "df = get_ngv_details()\n",
    "df.to_csv(\"中石化ngv同步.csv\", index=False)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error occurred: relation \"public.saas_ngv_yesterday\" does not exist\n",
      "LINE 2:         SELECT * FROM \"public\".\"saas_ngv_yesterday\";\n",
      "                              ^\n",
      "\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'to_csv'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mAttributeError\u001B[39m                            Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[1]\u001B[39m\u001B[32m, line 63\u001B[39m\n\u001B[32m     60\u001B[39m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[32m     62\u001B[39m df = get_ngv_details()\n\u001B[32m---> \u001B[39m\u001B[32m63\u001B[39m df.to_csv(\u001B[33m\"\u001B[39m\u001B[33m中石化ngv同步.csv\u001B[39m\u001B[33m\"\u001B[39m, index=\u001B[38;5;28;01mFalse\u001B[39;00m)\n",
      "\u001B[31mAttributeError\u001B[39m: 'NoneType' object has no attribute 'to_csv'"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
