Skip to content

Commit e2902b1

Browse files
authored
Merge pull request #206 from FunD-StockProject/fix/stock-master-info
Fix 종목 마스터 데이터 업데이트 오류 수정2
2 parents fc510b1 + 40eac49 commit e2902b1

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

scripts/stocks_info/overseas_stock_code.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,16 @@ def get_overseas_master_dataframe(base_dir,val):
140140
df = pd.read_table(cod_file_path, sep='\t', encoding='cp949')
141141
df.columns = columns
142142

143-
# 엑셀 파일 저장 (선택사항)
144-
excel_path = os.path.join(base_dir, f'{val}_code.xlsx')
145-
df.to_excel(excel_path, index=False)
143+
# 엑셀 파일 저장 (선택사항) - openpyxl이 없으면 스킵
144+
try:
145+
excel_path = os.path.join(base_dir, f'{val}_code.xlsx')
146+
df.to_excel(excel_path, index=False)
147+
except ImportError:
148+
# openpyxl이 없으면 엑셀 저장 스킵
149+
pass
150+
except Exception as e:
151+
# 기타 에러는 무시
152+
print(f"Warning: Could not save Excel file: {e}")
146153

147154
return df
148155

src/main/java/com/fund/stockProject/stock/service/StockImportService.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,40 @@ public void importStocksFromJson(String jsonFilePath) {
153153
}
154154
}
155155

156-
// 새 종목 저장
156+
// 새 종목 저장 - 개별 처리로 중복 ID 방지
157157
if (!newStocks.isEmpty()) {
158-
try {
159-
entityManager.flush();
160-
stockRepository.saveAll(newStocks);
161-
entityManager.flush();
162-
log.info("Saved {} new stocks", newStocks.size());
163-
} catch (Exception e) {
164-
log.error("Error saving new stocks: {}", e.getMessage());
165-
throw e;
158+
int savedCount = 0;
159+
int skippedCount = 0;
160+
for (Stock stock : newStocks) {
161+
try {
162+
// symbol로 이미 존재하는지 다시 확인 (DB에서 직접 확인)
163+
Optional<Stock> existingStock = stockRepository.findBySymbol(stock.getSymbol());
164+
if (existingStock.isPresent()) {
165+
// 이미 존재하면 업데이트로 처리
166+
Stock existing = existingStock.get();
167+
existing.updateSymbolNameIfNull(stock.getSymbolName());
168+
existing.setValid(true);
169+
if (stock.getDomesticSector() != null) {
170+
existing.setDomesticSector(stock.getDomesticSector());
171+
}
172+
if (stock.getOverseasSector() != null) {
173+
existing.setOverseasSector(stock.getOverseasSector());
174+
}
175+
stockRepository.save(existing);
176+
skippedCount++;
177+
log.debug("Stock already exists, updated: {}", stock.getSymbol());
178+
} else {
179+
// 새 종목 저장
180+
stockRepository.save(stock);
181+
savedCount++;
182+
}
183+
} catch (Exception e) {
184+
log.warn("Failed to save stock: {} - {}", stock.getSymbol(), e.getMessage());
185+
skippedCount++;
186+
}
166187
}
188+
entityManager.flush();
189+
log.info("Saved {} new stocks, {} skipped (already exists or error)", savedCount, skippedCount);
167190
}
168191

169192
// 기존 종목 업데이트 - 개별 처리로 안전하게

0 commit comments

Comments
 (0)