package zbooksoft.fams.service; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import zbooksoft.fams.common.BusinessException; import zbooksoft.fams.dao.AssetBaseDao; import zbooksoft.fams.dao.PurchaseDao; import java.math.BigDecimal; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class PurchaseServiceTest { @Mock private PurchaseDao purchaseDao; @Mock private AssetBaseDao assetBaseDao; @Mock private AssetBaseService assetBaseService; @InjectMocks private PurchaseService service; private static Map map(Object... kv) { Map m = new HashMap<>(); for (int i = 0; i < kv.length; i += 2) { m.put((String) kv[i], kv[i + 1]); } return m; } // ---------------- 采购申请 ---------------- @Test void newPurchaseApplyGetsNumberAndApplicant() { Map form = map("PURPOSE", "新员工配电脑", "BILL_NO", ""); when(assetBaseDao.nextSerialNo(startsWith("CG"))).thenReturn(1L); service.preparePurchase(0L, form, 7L, "张三", 8L, "信息中心"); assertTrue(String.valueOf(form.get("BILL_NO")).matches("CG\\d{8}001")); assertEquals("草稿", form.get("BILL_STATE")); assertEquals("张三", form.get("APPLY_USER_NAME")); } @Test void purchaseApplyRequiresPurpose() { assertThrows(BusinessException.class, () -> service.preparePurchase(0L, map("PURPOSE", ""), 7L, "张三", 8L, "信息中心")); } @Test void existingPurchaseRefreshesTotalsFromItems() { Map form = map("PURPOSE", "x", "BILL_NO", "CG1"); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_STATE", "草稿")); when(purchaseDao.findPurchaseItems(5L)).thenReturn(Arrays.asList( map("QUANTITY", 2, "BUDGET_PRICE", new BigDecimal("5000")), map("QUANTITY", 1, "BUDGET_PRICE", null))); service.preparePurchase(5L, form, 7L, "张三", 8L, "信息中心"); assertEquals(2, form.get("ITEM_COUNT")); assertEquals(new BigDecimal("10000.00"), form.get("TOTAL_BUDGET")); } @Test void purchaseItemComputesAmountAndNeedsDraftParent() { Map item = map("APPLY_ID", "5", "ITEM_NAME", "笔记本", "QUANTITY", "3", "BUDGET_PRICE", "4999.5", "CATEGORY_ID", ""); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_STATE", "草稿")); service.preparePurchaseItem(0L, item); assertEquals(new BigDecimal("14998.50"), item.get("BUDGET_AMOUNT")); assertEquals(3, item.get("QUANTITY")); } @Test void purchaseItemWithoutParentIsRejected() { Map item = map("APPLY_ID", "", "ITEM_NAME", "笔记本", "QUANTITY", "1"); BusinessException e = assertThrows(BusinessException.class, () -> service.preparePurchaseItem(0L, item)); assertTrue(e.getMessage().contains("采购申请")); } @Test void purchaseItemCannotChangeAfterSubmit() { Map item = map("APPLY_ID", "5", "ITEM_NAME", "笔记本", "QUANTITY", "1"); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_NO", "CG1", "BILL_STATE", "审批中")); assertThrows(BusinessException.class, () -> service.preparePurchaseItem(0L, item)); } @Test void purchaseItemQuantityMustBePositiveInteger() { Map item = map("APPLY_ID", "5", "ITEM_NAME", "笔记本", "QUANTITY", "1.5"); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_STATE", "草稿")); assertThrows(BusinessException.class, () -> service.preparePurchaseItem(0L, item)); } @Test void submitPurchaseNeedsItems() { when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_NO", "CG1", "BILL_STATE", "草稿")); when(purchaseDao.findPurchaseItems(5L)).thenReturn(Collections.>emptyList()); BusinessException e = assertThrows(BusinessException.class, () -> service.submitPurchase(5L)); assertTrue(e.getMessage().contains("明细")); } @Test void approvePurchaseMarksNotReceived() { when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_NO", "CG1", "BILL_STATE", "审批中")); service.approvePurchase(5L); verify(purchaseDao).updatePurchase(eq(5L), argThat(v -> "已通过".equals(v.get("BILL_STATE")) && "未到货".equals(v.get("RECEIVE_STATE")))); } // ---------------- 到货入库 ---------------- @Test void newReceiptRequiresApprovedPurchase() { Map form = map("PURCHASE_ID", "5", "SUPPLIER", "联想"); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_NO", "CG1", "BILL_STATE", "审批中")); assertThrows(BusinessException.class, () -> service.prepareReceipt(0L, form, 7L, "张三")); } @Test void newReceiptGetsNumberAndDefaults() { Map form = map("PURCHASE_ID", "5", "SUPPLIER", "联想", "RECEIPT_DATE", "", "BILL_NO", ""); when(purchaseDao.findPurchase(5L)).thenReturn(map("ID", 5L, "BILL_NO", "CG1", "BILL_STATE", "已通过", "RECEIVE_STATE", "未到货")); when(assetBaseDao.nextSerialNo(startsWith("RK"))).thenReturn(2L); service.prepareReceipt(0L, form, 7L, "张三"); assertTrue(String.valueOf(form.get("BILL_NO")).matches("RK\\d{8}002")); assertEquals("CG1", form.get("PURCHASE_NO")); assertEquals("草稿", form.get("BILL_STATE")); assertEquals("张三", form.get("ACCEPT_USER_NAME")); assertFalse(String.valueOf(form.get("RECEIPT_DATE")).isEmpty()); } @Test void confirmedReceiptCannotBeEdited() { Map form = map("PURCHASE_ID", "5", "SUPPLIER", "联想"); when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "已入库", "PURCHASE_ID", 5L)); assertThrows(BusinessException.class, () -> service.prepareReceipt(9L, form, 7L, "张三")); } @Test void receiptItemsAreCopiedFromRemainingPurchaseItems() { when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "草稿", "PURCHASE_ID", 5L)); when(purchaseDao.countReceiptItems(9L)).thenReturn(0); when(purchaseDao.findPurchaseItems(5L)).thenReturn(Arrays.asList( map("ID", 51L, "ITEM_NAME", "笔记本", "QUANTITY", 3, "RECEIVED_QTY", 1, "BUDGET_PRICE", new BigDecimal("5000"), "CATEGORY_ID", 30L, "CATEGORY_NAME", "笔记本"), map("ID", 52L, "ITEM_NAME", "打印机", "QUANTITY", 1, "RECEIVED_QTY", 1))); service.afterSaveReceipt(9L); verify(purchaseDao).insertReceiptItem(argThat(row -> Long.valueOf(51L).equals(row.get("PURCHASE_ITEM_ID")) && Integer.valueOf(2).equals(row.get("QUANTITY")) && "合格".equals(row.get("ACCEPT_RESULT")) && new BigDecimal("10000.00").equals(row.get("AMOUNT")))); verify(purchaseDao, times(1)).insertReceiptItem(anyMap()); } @Test void receiptItemQuantityCannotExceedRemaining() { Map item = map("RECEIPT_ID", "9", "PURCHASE_ITEM_ID", "51", "ITEM_NAME", "笔记本", "CATEGORY_ID", "30", "QUANTITY", "3", "UNIT_PRICE", "5000", "ACCEPT_RESULT", "合格"); when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_STATE", "草稿")); when(purchaseDao.findPurchaseItem(51L)).thenReturn(map("ID", 51L, "QUANTITY", 3, "RECEIVED_QTY", 1)); BusinessException e = assertThrows(BusinessException.class, () -> service.prepareReceiptItem(0L, item)); assertTrue(e.getMessage().contains("2")); } @Test void confirmReceiptCreatesOneCardPerAcceptedUnit() { when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "草稿", "PURCHASE_ID", 5L, "SUPPLIER", "联想", "CONTRACT_NO", "HT-1", "RECEIPT_DATE", "2026-10-16 00:00:00.0", "USE_DEPT_ID", 8L, "USE_DEPT_NAME", "信息中心", "USER_ID", null, "USER_NAME", "", "LOCATION_ID", 3L, "LOCATION_NAME", "1号楼")); when(purchaseDao.findReceiptItems(9L)).thenReturn(Arrays.asList( map("ID", 91L, "PURCHASE_ITEM_ID", 51L, "ITEM_NAME", "笔记本", "SPEC_MODEL", "T14", "CATEGORY_ID", 30L, "BRAND", "联想", "UNIT", "台", "QUANTITY", 2, "UNIT_PRICE", new BigDecimal("5000"), "ACCEPT_RESULT", "合格"), map("ID", 92L, "PURCHASE_ITEM_ID", 52L, "ITEM_NAME", "坏打印机", "CATEGORY_ID", 31L, "QUANTITY", 1, "ACCEPT_RESULT", "不合格"))); doAnswer(inv -> { Map card = inv.getArgument(0); card.put("ASSET_NO", "BG00001"); return null; }).when(assetBaseService).prepareNewCard(anyMap()); when(purchaseDao.findPurchaseItems(5L)).thenReturn(Arrays.asList( map("ID", 51L, "QUANTITY", 2, "RECEIVED_QTY", 2), map("ID", 52L, "QUANTITY", 1, "RECEIVED_QTY", 0))); int created = service.confirmReceipts(Collections.singletonList(9L), 1L, "管理员"); assertEquals(2, created); verify(assetBaseDao, times(2)).insertCard(argThat(card -> "笔记本".equals(card.get("ASSET_NAME")) && "采购入库".equals(card.get("SOURCE_TYPE")) && "RK1".equals(card.get("SOURCE_BILL_NO")) && "闲置".equals(card.get("ASSET_STATE")) && Long.valueOf(8L).equals(card.get("USE_DEPT_ID")) && "2026-10-16".equals(card.get("PURCHASE_DATE")) && new BigDecimal("5000").equals(card.get("ORIGINAL_VALUE")))); verify(assetBaseService, times(2)).recordCardInbound(anyLong(), anyMap(), eq(1L), eq("管理员")); verify(purchaseDao).addReceivedQty(51L, 2); verify(purchaseDao, never()).addReceivedQty(eq(52L), anyInt()); verify(purchaseDao).updatePurchase(eq(5L), argThat(v -> "部分到货".equals(v.get("RECEIVE_STATE")))); verify(purchaseDao).updateReceipt(eq(9L), argThat(v -> "已入库".equals(v.get("BILL_STATE")) && Integer.valueOf(2).equals(v.get("ASSET_COUNT")))); } @Test void cardWithUserIsInUse() { when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "草稿", "PURCHASE_ID", 5L, "RECEIPT_DATE", "2026-10-16", "USER_ID", 20L, "USER_NAME", "李四")); when(purchaseDao.findReceiptItems(9L)).thenReturn(Collections.singletonList( map("ID", 91L, "ITEM_NAME", "笔记本", "CATEGORY_ID", 30L, "QUANTITY", 1, "ACCEPT_RESULT", "合格"))); when(purchaseDao.findPurchaseItems(5L)).thenReturn(Collections.>emptyList()); service.confirmReceipts(Collections.singletonList(9L), 1L, "管理员"); verify(assetBaseDao).insertCard(argThat(card -> "在用".equals(card.get("ASSET_STATE")) && "2026-10-16".equals(card.get("START_USE_DATE")))); verify(purchaseDao).updatePurchase(eq(5L), argThat(v -> "已到货".equals(v.get("RECEIVE_STATE")))); } @Test void confirmedReceiptCannotBeConfirmedAgain() { when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "已入库")); assertThrows(BusinessException.class, () -> service.confirmReceipts(Collections.singletonList(9L), 1L, "管理员")); verify(assetBaseDao, never()).insertCard(anyMap()); } @Test void receiptWithoutAcceptedItemsCannotBeConfirmed() { when(purchaseDao.findReceipt(9L)).thenReturn(map("ID", 9L, "BILL_NO", "RK1", "BILL_STATE", "草稿", "PURCHASE_ID", 5L)); when(purchaseDao.findReceiptItems(9L)).thenReturn(Collections.singletonList( map("ID", 92L, "ITEM_NAME", "坏打印机", "CATEGORY_ID", 31L, "QUANTITY", 1, "ACCEPT_RESULT", "不合格"))); assertThrows(BusinessException.class, () -> service.confirmReceipts(Collections.singletonList(9L), 1L, "管理员")); } }