| 
					
				 | 
			
			
				@@ -1,35 +1,33 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-from decimal import Decimal, InvalidOperation 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import xlrd 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import pymysql 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from datetime import datetime 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import xlrd 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from crossborder.utils.db_helper import DBHelper 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from crossborder.utils.parse_utils import convert_unit, parse_ratio 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 def get_upsert_sql(): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    """生成带更新条件的SQL语句""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    """使用命名占位符并正确使用VALUES函数的SQL""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    INSERT INTO t_yujin_crossborder_yearly_summary  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    (year, year_total, year_import, year_export, trade_balance,  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     yoy_import_export, yoy_import, yoy_export, create_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    ON DUPLICATE KEY UPDATE 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        year_total = VALUES(year_total), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        year_import = VALUES(year_import), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        year_export = VALUES(year_export), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        trade_balance = VALUES(trade_balance), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        yoy_import_export = VALUES(yoy_import_export), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        yoy_import = VALUES(yoy_import), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        yoy_export = VALUES(yoy_export), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        create_time = VALUES(create_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           INSERT INTO t_yujin_crossborder_yearly_summary 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           (year, year_total, year_import, year_export, trade_balance, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            yoy_import_export, yoy_import, yoy_export, create_time) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (:year, :year_total, :year_import, :year_export, :trade_balance, \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                   :yoy_import_export, :yoy_import, :yoy_export, :create_time) ON DUPLICATE KEY \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           UPDATE \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+               year_total = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (year_total), year_import = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (year_import), year_export = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (year_export), trade_balance = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (trade_balance), yoy_import_export = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (yoy_import_export), yoy_import = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (yoy_import), yoy_export = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (yoy_export), create_time = \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           VALUES (create_time) \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+           """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 def parse_year_table_excel(file): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     db_helper = DBHelper() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     # 读取Excel文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     try: 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -40,7 +38,6 @@ def parse_year_table_excel(file): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     sql = get_upsert_sql() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     params_list = [] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     for row_idx in range(5, sheet.nrows): 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -48,20 +45,19 @@ def parse_year_table_excel(file): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if not row[1]:  # 跳过空年份 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             continue 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        # 准备数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        params = ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            row[1],  # year 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            convert_unit(row[2]),  # year_total 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            convert_unit(row[4]),  # year_import 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            convert_unit(row[3]),  # year_export 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            convert_unit(row[5]),  # trade_balance 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            parse_ratio(row[6]),   # yoy_import_export 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            parse_ratio(row[7]),   # yoy_import 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            parse_ratio(row[8]),   # yoy_export 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            current_time           # create_time 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        params_list.append(params) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # 准备数据 - 使用字典 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        param_dict = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "year": row[1], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "year_total": convert_unit(row[2]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "year_import": convert_unit(row[4]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "year_export": convert_unit(row[3]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "trade_balance": convert_unit(row[5]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "yoy_import_export": parse_ratio(row[6]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "yoy_import": parse_ratio(row[7]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "yoy_export": parse_ratio(row[8]), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            "create_time": current_time 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        params_list.append(param_dict) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     # 使用 DBHelper 执行 SQL 插入 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     try: 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -69,7 +65,8 @@ def parse_year_table_excel(file): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         print(f"成功处理 {len(params_list)} 条数据,受影响行数:{affected_rows}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     except Exception as e: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         print(f"数据库操作失败: {e}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        raise 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 if __name__ == "__main__": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    parse_year_table_excel('../src/downloads/20250513/(1)2025年进出口商品总值表_A年度表_3月.xls') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    parse_year_table_excel(r'D:\pythonSpace\crossborder\downloads\total\2025\04\(1)2025年进出口商品总值表 A-年度表.xls') 
			 |