diff --git a/src/main/java/com/ffii/fpsms/modules/deliveryOrder/service/DeliveryOrderService.kt b/src/main/java/com/ffii/fpsms/modules/deliveryOrder/service/DeliveryOrderService.kt index efd51fd..d1eaede 100644 --- a/src/main/java/com/ffii/fpsms/modules/deliveryOrder/service/DeliveryOrderService.kt +++ b/src/main/java/com/ffii/fpsms/modules/deliveryOrder/service/DeliveryOrderService.kt @@ -56,7 +56,7 @@ import net.sf.jasperreports.engine.JasperPrint import org.springframework.core.io.ClassPathResource import java.io.File import java.io.FileNotFoundException - +import com.ffii.core.support.JdbcDao; import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderRecord import com.ffii.fpsms.modules.deliveryOrder.entity.DoPickOrderRecordRepository import com.ffii.fpsms.modules.deliveryOrder.web.models.ExportDNLabelsRequest @@ -96,6 +96,7 @@ open class DeliveryOrderService( private val inventoryLotService: InventoryLotService, private val suggestedPickLotRepository: SuggestPickLotRepository, private val inventoryLotRepository: InventoryLotRepository, + private val jdbcDao: JdbcDao, ) { open fun findByM18DataLogId(m18DataLogId: Long): DeliveryOrder? { @@ -482,11 +483,59 @@ val truck = deliveryOrder.shop?.id?.let { shopId -> println("🔍 DEBUG: Looking for truck with shop ID: $shopId") val trucks = truckRepository.findByShopIdAndDeletedFalse(shopId) println("🔍 DEBUG: Found ${trucks.size} trucks for shop $shopId") - trucks.forEach { t -> - println("🔍 DEBUG: Truck ID: ${t.id}, DepartureTime: ${t.departureTime}") + + if (trucks.size <= 1) { + // 如果只有一个或没有 truck,直接返回 + return@let trucks.firstOrNull() + } + + // ✅ 分析 DO order lines 中的 items 分布 + val itemIds = deliveryOrder.deliveryOrderLines.mapNotNull { it.item?.id }.distinct() + println("🔍 DEBUG: Analyzing ${itemIds.size} unique items in DO order lines") + + // 使用 SQL 查询统计每个楼层的库存数量 + val inventoryQuery = """ + SELECT w.store_id as floor, COUNT(*) as inventory_count + FROM inventory_lot_line ill + JOIN inventory_lot il ON il.id = ill.inventoryLotId + JOIN warehouse w ON w.id = ill.warehouseId + WHERE il.itemId IN (${itemIds.joinToString(",")}) + AND ill.deleted = false + AND il.deleted = false + AND w.deleted = false + AND ill.inQty > ill.outQty + COALESCE(ill.holdQty, 0) + GROUP BY w.store_id + """.trimIndent() + + val inventoryResults = jdbcDao.queryForList(inventoryQuery) + val floorInventoryCount = mutableMapOf() + + inventoryResults.forEach { row: Map -> + val floor = row["floor"] as? String ?: "Other" + val count = (row["inventory_count"] as? Number)?.toInt() ?: 0 + floorInventoryCount[floor] = count + } + + println("🔍 DEBUG: Floor inventory distribution: $floorInventoryCount") + + + // 决定使用哪个楼层 + val preferredFloor = when { + floorInventoryCount["2F"] ?: 0 > floorInventoryCount["4F"] ?: 0 -> "2F" + floorInventoryCount["4F"] ?: 0 > floorInventoryCount["2F"] ?: 0 -> "4F" + else -> "2F" // 默认使用 2F + } + + println("🔍 DEBUG: Preferred floor based on inventory: $preferredFloor") + + // 根据楼层选择对应的 truck + val selectedTruck = when (preferredFloor) { + "2F" -> trucks.find { it.storeId == 2 } + "4F" -> trucks.find { it.storeId == 4 } + else -> trucks.minByOrNull { it.departureTime ?: LocalTime.MAX } } - val selectedTruck = trucks.minByOrNull { it.departureTime ?: LocalTime.MAX } - println("🔍 DEBUG: Selected truck: ID=${selectedTruck?.id}, DepartureTime=${selectedTruck?.departureTime}") + + println("🔍 DEBUG: Selected truck: ID=${selectedTruck?.id}, StoreId=${selectedTruck?.storeId}, DepartureTime=${selectedTruck?.departureTime}") selectedTruck } diff --git a/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryLotLineRepository.kt b/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryLotLineRepository.kt index 1379127..90fb34f 100644 --- a/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryLotLineRepository.kt +++ b/src/main/java/com/ffii/fpsms/modules/stock/entity/InventoryLotLineRepository.kt @@ -42,5 +42,5 @@ interface InventoryLotLineRepository : AbstractRepository - fun findAllByInventoryLotItemIdAndStatus(itemId: Long, status: InventoryLotLineStatus): List + fun findAllByInventoryLotItemIdAndStatus(itemId: Long, status: InventoryLotLineStatus): List } \ No newline at end of file diff --git a/src/main/resources/db/changelog/changes/20251014_01_enson/01_altertable_enson.sql b/src/main/resources/db/changelog/changes/20251014_01_enson/01_altertable_enson.sql new file mode 100644 index 0000000..df61bb5 --- /dev/null +++ b/src/main/resources/db/changelog/changes/20251014_01_enson/01_altertable_enson.sql @@ -0,0 +1,5 @@ +-- liquibase formatted sql +-- changeset enson:altertable_warehouse + +ALTER TABLE warehouse +ADD COLUMN `store_id` varchar(50) default null; \ No newline at end of file