@@ -124,6 +124,11 @@ open class TruckService(
for (i in START_ROW_INDEX..<sheet.lastRowNum) {
for (i in START_ROW_INDEX..<sheet.lastRowNum) {
val row = sheet.getRow(i)
val row = sheet.getRow(i)
if (row == null) {
logger.warn("Row ${i + 1}: Skipped - row is null or empty")
continue
}
try {
try {
val truckLanceCode = ExcelUtils.getStringValue(row.getCell(COLUMN_TRUCK_LANCE_CODE_INDEX)).trim()
val truckLanceCode = ExcelUtils.getStringValue(row.getCell(COLUMN_TRUCK_LANCE_CODE_INDEX)).trim()
val departureTimeStr = ExcelUtils.getStringValue(row.getCell(COLUMN_DEPARTURE_TIME_INDEX)).trim()
val departureTimeStr = ExcelUtils.getStringValue(row.getCell(COLUMN_DEPARTURE_TIME_INDEX)).trim()
@@ -144,12 +149,15 @@ open class TruckService(
}
}
val departureTime = parseDepartureTime(departureTimeStr)
val departureTime = parseDepartureTime(departureTimeStr)
if (departureTime == null) {
if (departureTime == null) {
logger.warn("Row ${i + 1}: Invalid departure time '$departureTimeStr', skipping ")
logger.warn("Row ${i + 1}: Skipped - invalid departure time '$departureTimeStr' ")
continue
continue
}
}
val normalizedShopCode = normalizeShopCode(shopCode)
val normalizedShopCode = normalizeShopCode(shopCode)
val shop = shopRepository.findAllByDeletedIsFalse().firstOrNull { it.code == normalizedShopCode }
val shop = shopRepository.findAllByDeletedIsFalse().firstOrNull { it.code == normalizedShopCode }
//println("shop: ${shop}")
if (shop == null) {
logger.warn("Row ${i + 1}: Skipped - no shop found for shopCode '$shopCode' (normalized: '$normalizedShopCode')")
continue
}
val existingTruck = truckRepository.findByShopCodeAndStoreId(shopCode, store_id)
val existingTruck = truckRepository.findByShopCodeAndStoreId(shopCode, store_id)
if (existingTruck != null) {
if (existingTruck != null) {
@@ -158,13 +166,14 @@ open class TruckService(
store_id = store_id,
store_id = store_id,
truckLanceCode = truckLanceCode ?: existingTruck.truckLanceCode ?: "",
truckLanceCode = truckLanceCode ?: existingTruck.truckLanceCode ?: "",
departureTime = departureTime,
departureTime = departureTime,
shopId = shop? .id!!,
shopId = shop.id!!,
shopName = shopName ?: "",
shopName = shopName ?: "",
shopCode = normalizedShopCode,
shopCode = normalizedShopCode,
loadingSequence = loadingSequence,
loadingSequence = loadingSequence,
remark = remark
remark = remark
)
)
saveTruck(truckRequest)
saveTruck(truckRequest)
logger.info("Row ${i + 1}: Updated truck id=${existingTruck.id}, truckLanceCode=$truckLanceCode, shopCode=$normalizedShopCode, store_id=$store_id")
} else {
} else {
val truckRequest = SaveTruckRequest(
val truckRequest = SaveTruckRequest(
@@ -172,16 +181,17 @@ open class TruckService(
store_id = store_id,
store_id = store_id,
truckLanceCode = truckLanceCode ?: "",
truckLanceCode = truckLanceCode ?: "",
departureTime = departureTime,
departureTime = departureTime,
shopId = shop? .id!!,
shopId = shop.id!!,
shopName = shopName ?: "",
shopName = shopName ?: "",
shopCode = normalizedShopCode,
shopCode = normalizedShopCode,
loadingSequence = loadingSequence,
loadingSequence = loadingSequence,
remark = remark
remark = remark
)
)
saveTruck(truckRequest)
saveTruck(truckRequest)
logger.info("Row ${i + 1}: Created truck truckLanceCode=$truckLanceCode, shopCode=$normalizedShopCode, store_id=$store_id")
}
}
} catch (e: Exception) {
} catch (e: Exception) {
logger.error("Import Error (Warehouse Error): ${e.message}" )
logger.error("Row ${i + 1}: Skipped - exception: ${e.message}", e )
}
}
}
}
logger.info("--------- End - Import Warehouse Excel -------")
logger.info("--------- End - Import Warehouse Excel -------")