3v324v23 commited on
Commit
6a2c6af
·
1 Parent(s): 27dac4c

Add detailed trade_date handling and error logging

Browse files
Files changed (1) hide show
  1. apps/api/routers/trade_hf.py +32 -23
apps/api/routers/trade_hf.py CHANGED
@@ -74,29 +74,38 @@ async def search_trade_records(
74
  records = result.scalars().all()
75
 
76
  # 转换为响应格式
77
- items = [
78
- TradeRecordResponse(
79
- record_id=r.record_id,
80
- source_record_id=r.source_record_id,
81
- source_country=r.source_country,
82
- trade_direction=r.trade_direction,
83
- trade_date=r.trade_date if r.trade_date else datetime(2020, 1, 1),
84
- importer_name=r.importer_name or "",
85
- exporter_name=r.exporter_name or "",
86
- hs_code=r.hs_code or "",
87
- product_name=r.product_name or "",
88
- amount=r.amount,
89
- currency=r.currency or "",
90
- weight=r.weight,
91
- weight_unit=r.weight_unit or "",
92
- origin_country=r.origin_country or "",
93
- destination_country=r.destination_country or "",
94
- departure_port=r.departure_port or "",
95
- arrival_port=r.arrival_port or "",
96
- transport_mode=r.transport_mode or ""
97
- )
98
- for r in records
99
- ]
 
 
 
 
 
 
 
 
 
100
 
101
  return PaginatedResponse(
102
  items=items,
 
74
  records = result.scalars().all()
75
 
76
  # 转换为响应格式
77
+ items = []
78
+ for r in records:
79
+ try:
80
+ # 处理trade_date:如果是空字符串或None,使用默认日期
81
+ if not r.trade_date or (isinstance(r.trade_date, str) and r.trade_date.strip() == ''):
82
+ trade_date = datetime(2020, 1, 1)
83
+ else:
84
+ trade_date = r.trade_date
85
+
86
+ items.append(TradeRecordResponse(
87
+ record_id=r.record_id,
88
+ source_record_id=r.source_record_id,
89
+ source_country=r.source_country,
90
+ trade_direction=r.trade_direction,
91
+ trade_date=trade_date,
92
+ importer_name=r.importer_name or "",
93
+ exporter_name=r.exporter_name or "",
94
+ hs_code=r.hs_code or "",
95
+ product_name=r.product_name or "",
96
+ amount=r.amount,
97
+ currency=r.currency or "",
98
+ weight=r.weight,
99
+ weight_unit=r.weight_unit or "",
100
+ origin_country=r.origin_country or "",
101
+ destination_country=r.destination_country or "",
102
+ departure_port=r.departure_port or "",
103
+ arrival_port=r.arrival_port or "",
104
+ transport_mode=r.transport_mode or ""
105
+ ))
106
+ except Exception as e:
107
+ print(f"Error processing record {r.record_id}: trade_date={repr(r.trade_date)}, error={e}")
108
+ raise
109
 
110
  return PaginatedResponse(
111
  items=items,