From aabc82d0e6fb8fcb6330ea937d46521f8330faae Mon Sep 17 00:00:00 2001 From: "CANCERYS\\kw093" Date: Thu, 16 Apr 2026 10:59:50 +0800 Subject: [PATCH] update --- .../entity/InventoryLotLineRepository.kt | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) 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 7206d08..04a65f3 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 @@ -14,9 +14,45 @@ import com.ffii.fpsms.modules.stock.entity.enum.InventoryLotLineStatus import org.springframework.data.repository.query.Param import java.time.LocalDate import org.springframework.data.jpa.repository.EntityGraph +import java.math.BigDecimal @Repository interface InventoryLotLineRepository : AbstractRepository { + /** + * Workbench pick: one conditional atomic UPDATE — winner updates row, loser gets 0 rows. + * - Requires expected [BaseEntity.version] (optimistic row lock). + * - No-hold rule: pickable only if (in - out) >= delta (ignore holdQty). + * - Recomputes [InventoryLotLine.status] in SQL (same rules as [InventoryLotLineService.deriveInventoryLotLineStatus]). + * - Bumps version in SQL so no stale JPA save is needed on this row afterward. + */ + /* + @Modifying(clearAutomatically = true, flushAutomatically = true) + @Transactional + @Query( + value = """ + UPDATE inventory_lot_line ill + SET ill.outQty = COALESCE(ill.outQty, 0) + :delta, + ill.status = CASE + WHEN LOWER(COALESCE(ill.status, '')) = 'available' + AND (COALESCE(ill.inQty, 0) - (COALESCE(ill.outQty, 0) + :delta)) > 0 + THEN 'available' + ELSE 'unavailable' + END, + ill.version = ill.version + 1, + ill.modified = CURRENT_TIMESTAMP + WHERE ill.id = :id + AND ill.deleted = 0 + AND ill.version = :expectedVersion + AND (COALESCE(ill.inQty, 0) - COALESCE(ill.outQty, 0)) >= :delta + """, + nativeQuery = true + ) + fun incrementOutQtyIfAvailable( + @Param("id") id: Long, + @Param("delta") delta: BigDecimal, + @Param("expectedVersion") expectedVersion: Int, + ): Int +*/ fun findByIdAndDeletedIsFalse(id: Serializable): InventoryLotLine; fun findInventoryLotLineInfoByInventoryLotItemIdIn(ids: List): List @@ -96,7 +132,10 @@ interface InventoryLotLineRepository : AbstractRepository): List - + @EntityGraph( + type = EntityGraph.EntityGraphType.FETCH, + attributePaths = ["inventoryLot", "inventoryLot.item", "warehouse", "stockUom", "stockUom.uom"] + ) @Query(""" SELECT ill FROM InventoryLotLine ill WHERE ill.deleted = false