瑞利声电固定资产管理系统
ycq
2026-10-16 dbbadb021c2fc171466cc82bd9a7a82cc47165f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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 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 AssetBaseServiceTest {
 
    @Mock
    private AssetBaseDao assetBaseDao;
 
    @InjectMocks
    private AssetBaseService service;
 
    private static Map<String, Object> map(Object... kv) {
        Map<String, Object> m = new HashMap<>();
        for (int i = 0; i < kv.length; i += 2) {
            m.put((String) kv[i], kv[i + 1]);
        }
        return m;
    }
 
    // ---------------- 资产类别 ----------------
 
    @Test
    void topCategoryRequiresCode() {
        Map<String, Object> form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "");
        BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form));
        assertTrue(e.getMessage().contains("类别码"));
    }
 
    @Test
    void topCategoryCodeIsUpperCasedAndLevelIsOne() {
        Map<String, Object> form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "bg");
        when(assetBaseDao.existsTopCategoryCode("BG", 0L)).thenReturn(false);
 
        service.prepareCategory(0L, form);
 
        assertEquals("BG", form.get("CATEGORY_CODE"));
        assertEquals(1, form.get("CATEGORY_LEVEL"));
        assertEquals("BG", form.get("TOP_CATEGORY_CODE"));
        assertEquals("", form.get("PARENT_NAME"));
        assertEquals("启用", form.get("ENABLE_STATE"));
    }
 
    @Test
    void duplicateTopCategoryCodeIsRejected() {
        Map<String, Object> form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "BG");
        when(assetBaseDao.existsTopCategoryCode("BG", 5L)).thenReturn(true);
 
        assertThrows(BusinessException.class, () -> service.prepareCategory(5L, form));
    }
 
    @Test
    void invalidCategoryCodeIsRejected() {
        Map<String, Object> form = map("PARENT_ID", "", "CATEGORY_NAME", "办公设备", "CATEGORY_CODE", "办公");
        assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form));
    }
 
    @Test
    void subCategoryInheritsTopFromLevelOneParent() {
        Map<String, Object> form = map("PARENT_ID", "10", "CATEGORY_NAME", "电脑", "DEVICE_TYPE", "");
        when(assetBaseDao.findCategory(10L)).thenReturn(map("ID", 10L, "CATEGORY_LEVEL", 1, "CATEGORY_CODE", "BG",
                "CATEGORY_NAME", "办公设备", "DEVICE_TYPE", "普通设备"));
 
        service.prepareCategory(0L, form);
 
        assertEquals(2, form.get("CATEGORY_LEVEL"));
        assertEquals(10L, form.get("TOP_CATEGORY_ID"));
        assertEquals("BG", form.get("TOP_CATEGORY_CODE"));
        assertEquals("办公设备", form.get("PARENT_NAME"));
        assertEquals("普通设备", form.get("DEVICE_TYPE"));
    }
 
    @Test
    void thirdLevelInheritsTopFromParentsTopFields() {
        Map<String, Object> form = map("PARENT_ID", "20", "CATEGORY_NAME", "笔记本");
        when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "CATEGORY_LEVEL", 2, "CATEGORY_NAME", "电脑",
                "TOP_CATEGORY_ID", 10L, "TOP_CATEGORY_CODE", "BG"));
 
        service.prepareCategory(0L, form);
 
        assertEquals(3, form.get("CATEGORY_LEVEL"));
        assertEquals(10L, form.get("TOP_CATEGORY_ID"));
        assertEquals("BG", form.get("TOP_CATEGORY_CODE"));
    }
 
    @Test
    void fourthLevelIsRejected() {
        Map<String, Object> form = map("PARENT_ID", "30", "CATEGORY_NAME", "x");
        when(assetBaseDao.findCategory(30L)).thenReturn(map("ID", 30L, "CATEGORY_LEVEL", 3, "CATEGORY_NAME", "笔记本"));
 
        BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCategory(0L, form));
        assertTrue(e.getMessage().contains("三级"));
    }
 
    @Test
    void categoryCannotBeItsOwnParent() {
        Map<String, Object> form = map("PARENT_ID", "7", "CATEGORY_NAME", "x");
        assertThrows(BusinessException.class, () -> service.prepareCategory(7L, form));
    }
 
    @Test
    void categoryWithChildrenCannotChangeParent() {
        Map<String, Object> form = map("PARENT_ID", "10", "CATEGORY_NAME", "电脑");
        when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "PARENT_ID", 11L, "CATEGORY_LEVEL", 2));
        when(assetBaseDao.hasChildCategory(20L)).thenReturn(true);
 
        assertThrows(BusinessException.class, () -> service.prepareCategory(20L, form));
    }
 
    @Test
    void syncTopCategoryOnlyForLevelOne() {
        when(assetBaseDao.findCategory(10L)).thenReturn(map("ID", 10L, "CATEGORY_LEVEL", 1, "CATEGORY_CODE", "BG"));
        service.syncCategoryTop(10L);
        verify(assetBaseDao).syncTopCategory(10L, "BG");
 
        when(assetBaseDao.findCategory(20L)).thenReturn(map("ID", 20L, "CATEGORY_LEVEL", 2));
        service.syncCategoryTop(20L);
        verify(assetBaseDao, never()).syncTopCategory(eq(20L), anyString());
    }
 
    // ---------------- 存放位置 ----------------
 
    @Test
    void rootLocationFullNameIsItsName() {
        Map<String, Object> form = map("PARENT_ID", "", "LOCATION_NAME", "1号楼");
        service.prepareLocation(0L, form);
        assertEquals("1号楼", form.get("FULL_NAME"));
        assertEquals("", form.get("PARENT_NAME"));
    }
 
    @Test
    void childLocationFullNameJoinsParent() {
        Map<String, Object> form = map("PARENT_ID", "1", "LOCATION_NAME", "3层");
        when(assetBaseDao.findLocation(1L)).thenReturn(map("ID", 1L, "LOCATION_NAME", "1号楼", "FULL_NAME", "1号楼"));
 
        service.prepareLocation(0L, form);
 
        assertEquals("1号楼/3层", form.get("FULL_NAME"));
        assertEquals("1号楼", form.get("PARENT_NAME"));
    }
 
    @Test
    void locationCannotBeItsOwnParent() {
        Map<String, Object> form = map("PARENT_ID", "1", "LOCATION_NAME", "x");
        assertThrows(BusinessException.class, () -> service.prepareLocation(1L, form));
    }
 
    @Test
    void refreshLocationDescendantsRewritesSubtreeAndCards() {
        when(assetBaseDao.findLocation(1L)).thenReturn(map("ID", 1L, "FULL_NAME", "A楼"));
        when(assetBaseDao.findChildLocations(1L)).thenReturn(Collections.singletonList(map("ID", 2L, "LOCATION_NAME", "3层")));
        when(assetBaseDao.findChildLocations(2L)).thenReturn(Collections.singletonList(map("ID", 3L, "LOCATION_NAME", "301")));
        when(assetBaseDao.findChildLocations(3L)).thenReturn(Collections.<Map<String, Object>>emptyList());
 
        service.refreshLocationDescendants(1L);
 
        verify(assetBaseDao).updateCardLocationName(1L, "A楼");
        verify(assetBaseDao).updateLocationFullName(2L, "A楼", "A楼/3层");
        verify(assetBaseDao).updateCardLocationName(2L, "A楼/3层");
        verify(assetBaseDao).updateLocationFullName(3L, "A楼/3层", "A楼/3层/301");
        verify(assetBaseDao).updateCardLocationName(3L, "A楼/3层/301");
    }
 
    // ---------------- 资产卡片 ----------------
 
    private Map<String, Object> leafCategory() {
        return map("ID", 30L, "CATEGORY_LEVEL", 3, "CATEGORY_NAME", "笔记本", "TOP_CATEGORY_CODE", "BG",
                "DEVICE_TYPE", "普通设备", "ENABLE_STATE", "启用", "WARRANTY_MONTHS", 36, "MAINTAIN_MONTHS", 12);
    }
 
    @Test
    void newCardGetsAssetNoAndDefaults() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "联想笔记本", "ASSET_NO", "",
                "ASSET_STATE", "", "DEVICE_TYPE", "", "PURCHASE_DATE", "2026-01-15", "ORIGINAL_VALUE", "5999.00");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
        when(assetBaseDao.nextSerialNo("BG")).thenReturn(7L);
 
        service.prepareCard(0L, form);
 
        assertEquals("BG00007", form.get("ASSET_NO"));
        assertEquals("笔记本", form.get("CATEGORY_NAME"));
        assertEquals("BG", form.get("TOP_CATEGORY_CODE"));
        assertEquals("普通设备", form.get("DEVICE_TYPE"));
        assertEquals("在用", form.get("ASSET_STATE"));
        assertEquals("手工新增", form.get("SOURCE_TYPE"));
        assertEquals(0, form.get("LOCK_STATE"));
        assertEquals(0, form.get("PRINT_STATE"));
        assertEquals("2029-01-15", form.get("WARRANTY_EXPIRE_DATE"));
        assertEquals("2027-01-15", form.get("NEXT_MAINTAIN_DATE"));
    }
 
    @Test
    void existingAssetNoIsKept() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001",
                "ASSET_STATE", "闲置", "WARRANTY_EXPIRE_DATE", "2030-01-01");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
 
        service.prepareCard(9L, form);
 
        assertEquals("BG00001", form.get("ASSET_NO"));
        assertEquals("闲置", form.get("ASSET_STATE"));
        assertEquals("2030-01-01", form.get("WARRANTY_EXPIRE_DATE"));
        assertFalse(form.containsKey("LOCK_STATE"));
        verify(assetBaseDao, never()).nextSerialNo(anyString());
    }
 
    @Test
    void cardWithoutCategoryIsRejected() {
        Map<String, Object> form = map("CATEGORY_ID", "", "ASSET_NAME", "x");
        assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
    }
 
    @Test
    void cardCategoryWithoutTopCodeIsRejected() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x");
        Map<String, Object> category = leafCategory();
        category.put("TOP_CATEGORY_CODE", null);
        when(assetBaseDao.findCategory(30L)).thenReturn(category);
 
        BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
        assertTrue(e.getMessage().contains("类别码"));
        verify(assetBaseDao, never()).nextSerialNo(anyString());
    }
 
    @Test
    void disabledCategoryIsRejected() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x");
        Map<String, Object> category = leafCategory();
        category.put("ENABLE_STATE", "停用");
        when(assetBaseDao.findCategory(30L)).thenReturn(category);
 
        assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
    }
 
    @Test
    void invalidOriginalValueIsRejectedBeforeNumbering() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ORIGINAL_VALUE", "abc");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
 
        assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
        verify(assetBaseDao, never()).nextSerialNo(anyString());
    }
 
    @Test
    void cardLocationNameComesFromLocationFullName() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001", "LOCATION_ID", "3");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
        when(assetBaseDao.findLocation(3L)).thenReturn(map("ID", 3L, "FULL_NAME", "1号楼/3层/301"));
 
        service.prepareCard(9L, form);
 
        assertEquals("1号楼/3层/301", form.get("LOCATION_NAME"));
    }
 
    @Test
    void inboundLogIsWrittenOnce() {
        Map<String, Object> form = map("ASSET_NO", "BG00007", "ASSET_NAME", "联想笔记本", "ASSET_STATE", "在用",
                "SOURCE_TYPE", "手工新增", "USE_DEPT_NAME", "信息中心", "USER_NAME", "张三", "LOCATION_NAME", "1号楼/3层/301");
        when(assetBaseDao.existsChangeLog(100L, "入库")).thenReturn(false);
 
        service.recordCardInbound(100L, form, 1L, "管理员");
 
        verify(assetBaseDao).insertChangeLog(argThat(row ->
                Long.valueOf(100L).equals(row.get("ASSET_ID"))
                        && "入库".equals(row.get("CHANGE_TYPE"))
                        && "在用".equals(row.get("AFTER_STATE"))
                        && "手工新增".equals(row.get("BILL_TYPE"))
                        && String.valueOf(row.get("AFTER_CONTENT")).contains("信息中心")
                        && "管理员".equals(row.get("OPERATE_USER_NAME"))));
 
        when(assetBaseDao.existsChangeLog(100L, "入库")).thenReturn(true);
        service.recordCardInbound(100L, form, 1L, "管理员");
        verify(assetBaseDao, times(1)).insertChangeLog(anyMap());
    }
 
    @Test
    void assetNoFormatPadsToFiveDigits() {
        assertEquals("JL00128", AssetBaseService.formatAssetNo("JL", 128));
        assertEquals("BG123456", AssetBaseService.formatAssetNo("BG", 123456));
    }
 
    @Test
    void generatedAssetNoSkipsNumbersAlreadyUsedByImportedAssets() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
        when(assetBaseDao.nextSerialNo("BG")).thenReturn(5L, 6L);
        when(assetBaseDao.existsAssetNo("BG00005")).thenReturn(true);
        when(assetBaseDao.existsAssetNo("BG00006")).thenReturn(false);
 
        service.prepareCard(0L, form);
 
        assertEquals("BG00006", form.get("ASSET_NO"));
    }
 
    @Test
    void invalidAssetStateIsRejected() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_STATE", "正常");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
 
        BusinessException e = assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
        assertTrue(e.getMessage().contains("资产状态"));
    }
 
    @Test
    void invalidDeviceTypeIsRejected() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "DEVICE_TYPE", "电梯");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
 
        assertThrows(BusinessException.class, () -> service.prepareCard(0L, form));
    }
 
    @Test
    void formSaveClearsImportPendingNote() {
        Map<String, Object> form = map("CATEGORY_ID", "30", "ASSET_NAME", "x", "ASSET_NO", "BG00001",
                "REMARK", AssetBaseService.IMPORT_PENDING_PREFIX + "类别「台式机」不存在】原备注");
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
 
        service.prepareCard(9L, form);
 
        assertEquals("原备注", form.get("REMARK"));
    }
 
    @Test
    void importedCardIsResolvedNumberedAndLogged() {
        Map<String, Object> row = map("ID", 100L, "ASSET_NAME", "联想笔记本", "CATEGORY_NAME", "笔记本",
                "USE_DEPT_NAME", "信息中心", "USER_NAME", "张三", "LOCATION_NAME", "1号楼/3层", "SAFETY_USER_NAME", "",
                "SOURCE_TYPE", "Excel导入", "PURCHASE_DATE", "2026-01-15 00:00:00.0", "REMARK", "旧备注");
        when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row));
        when(assetBaseDao.findCategoriesByName("笔记本")).thenReturn(Collections.singletonList(leafCategory()));
        when(assetBaseDao.findCategory(30L)).thenReturn(leafCategory());
        when(assetBaseDao.findDeptIdsByName("信息中心")).thenReturn(Collections.singletonList(200L));
        when(assetBaseDao.findUserIdsByTrueName("张三")).thenReturn(Collections.singletonList(300L));
        when(assetBaseDao.findLocationsByName("1号楼/3层")).thenReturn(Collections.singletonList(
                map("ID", 3L, "LOCATION_NAME", "3层", "FULL_NAME", "1号楼/3层")));
        when(assetBaseDao.findLocation(3L)).thenReturn(map("ID", 3L, "FULL_NAME", "1号楼/3层"));
        when(assetBaseDao.nextSerialNo("BG")).thenReturn(1L);
 
        int[] result = service.completeImportedCards(1L, "管理员");
 
        assertArrayEquals(new int[]{1, 0}, result);
        verify(assetBaseDao).updateCard(eq(100L), argThat(values ->
                "BG00001".equals(values.get("ASSET_NO"))
                        && Long.valueOf(30L).equals(values.get("CATEGORY_ID"))
                        && Long.valueOf(200L).equals(values.get("USE_DEPT_ID"))
                        && Long.valueOf(300L).equals(values.get("USER_ID"))
                        && Long.valueOf(3L).equals(values.get("LOCATION_ID"))
                        && "在用".equals(values.get("ASSET_STATE"))
                        && "2029-01-15".equals(values.get("WARRANTY_EXPIRE_DATE"))
                        && "旧备注".equals(values.get("REMARK"))));
        verify(assetBaseDao).insertChangeLog(argThat(log -> "BG00001".equals(log.get("ASSET_NO"))));
    }
 
    @Test
    void importedCardWithUnknownCategoryIsMarkedPending() {
        Map<String, Object> row = map("ID", 101L, "ASSET_NAME", "x", "CATEGORY_NAME", "台式机", "REMARK", "");
        when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row));
        when(assetBaseDao.findCategoriesByName("台式机")).thenReturn(Collections.<Map<String, Object>>emptyList());
 
        int[] result = service.completeImportedCards(1L, "管理员");
 
        assertArrayEquals(new int[]{0, 1}, result);
        verify(assetBaseDao).updateCard(eq(101L), argThat(values ->
                String.valueOf(values.get("REMARK")).startsWith(AssetBaseService.IMPORT_PENDING_PREFIX)
                        && String.valueOf(values.get("REMARK")).contains("台式机")
                        && !values.containsKey("ASSET_NO")));
        verify(assetBaseDao, never()).nextSerialNo(anyString());
        verify(assetBaseDao, never()).insertChangeLog(anyMap());
    }
 
    @Test
    void importedCardWithDuplicateUserNameIsMarkedPending() {
        Map<String, Object> row = map("ID", 102L, "ASSET_NAME", "x", "CATEGORY_NAME", "笔记本", "USER_NAME", "李明");
        when(assetBaseDao.findCardsWithoutAssetNo(anyInt())).thenReturn(Collections.singletonList(row));
        when(assetBaseDao.findCategoriesByName("笔记本")).thenReturn(Collections.singletonList(leafCategory()));
        when(assetBaseDao.findUserIdsByTrueName("李明")).thenReturn(Arrays.asList(1L, 2L));
 
        int[] result = service.completeImportedCards(1L, "管理员");
 
        assertArrayEquals(new int[]{0, 1}, result);
        verify(assetBaseDao).updateCard(eq(102L), argThat(values -> String.valueOf(values.get("REMARK")).contains("李明")));
    }
 
    @Test
    void lowerCaseFormKeysAreReadToo() {
        Map<String, Object> form = map("parent_id", "", "category_name", "办公设备", "category_code", "DZ");
        service.prepareCategory(0L, form);
        assertEquals(Arrays.asList(1, "DZ"), Arrays.asList(form.get("CATEGORY_LEVEL"), form.get("TOP_CATEGORY_CODE")));
    }
}