瑞利声电固定资产管理系统
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package zbooksoft.fams.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zbooksoft.fams.common.BusinessException;
import zbooksoft.fams.common.FormDataUtils;
import zbooksoft.fams.dao.AssetBaseDao;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
 
/**
 * 资产底座业务规则:类别层级、位置完整名称、资产编号与入库记录
 * @author ycq
 * @date 2026-10-16
 */
@Service
public class AssetBaseService {
 
    public static final int MAX_CATEGORY_LEVEL = 3;
    public static final String ENABLED = "启用";
    public static final String DEFAULT_ASSET_STATE = "在用";
    public static final String DEFAULT_DEVICE_TYPE = "普通设备";
    public static final String DEFAULT_SOURCE_TYPE = "手工新增";
    public static final String CHANGE_TYPE_INBOUND = "入库";
    public static final String LOCATION_SEPARATOR = "/";
    public static final String IMPORT_PENDING_PREFIX = "【导入待处理:";
    public static final String IMPORT_PENDING_SUFFIX = "】";
    public static final List<String> ASSET_STATES = Collections.unmodifiableList(Arrays.asList("在用", "闲置", "借用", "封存", "维修", "报废"));
    public static final List<String> DEVICE_TYPES = Collections.unmodifiableList(Arrays.asList("普通设备", "特种设备", "计量设备"));
    public static final List<String> RISK_LEVELS = Collections.unmodifiableList(Arrays.asList("低", "中", "高"));
 
    /** 单次导入后补全最多处理的卡片数,防止一次性加载过多数据 */
    private static final int IMPORT_BATCH_LIMIT = 5000;
    private static final List<String> IMPORT_UPDATE_COLUMNS = Collections.unmodifiableList(Arrays.asList(
            "ASSET_NO", "CATEGORY_ID", "CATEGORY_NAME", "TOP_CATEGORY_CODE", "DEVICE_TYPE", "ASSET_STATE", "SOURCE_TYPE",
            "LOCK_STATE", "PRINT_STATE", "USE_DEPT_ID", "USER_ID", "SAFETY_USER_ID", "LOCATION_ID", "LOCATION_NAME",
            "WARRANTY_EXPIRE_DATE", "NEXT_MAINTAIN_DATE", "REMARK"));
 
    private static final int ASSET_SN_DIGITS = 5;
    private static final int DATE_TEXT_LENGTH = 10;
    private static final Pattern CATEGORY_CODE_PATTERN = Pattern.compile("^[A-Z0-9]{1,10}$");
 
    @Autowired
    private AssetBaseDao assetBaseDao;
 
    /**
     * 资产类别保存前:校验上级与类别码,计算层级和所属一级类别
     * @param keyId 当前类别ID,新增时为0或null
     * @param formData 表单数据,计算结果直接写回
     */
    public void prepareCategory(Long keyId, Map<String, Object> formData) {
        long selfId = keyId == null ? 0L : keyId;
        Long parentId = FormDataUtils.getLong(formData, "PARENT_ID");
        if (parentId != null && parentId == selfId) {
            throw new BusinessException("上级类别不能选择自己");
        }
        if (selfId > 0) {
            checkParentChangeAllowed(selfId, parentId);
        }
        if (FormDataUtils.isBlank(formData, "ENABLE_STATE")) {
            FormDataUtils.put(formData, "ENABLE_STATE", ENABLED);
        }
        if (parentId == null) {
            prepareTopCategory(selfId, formData);
        } else {
            prepareSubCategory(parentId, formData);
        }
    }
 
    private void checkParentChangeAllowed(long selfId, Long newParentId) {
        Map<String, Object> current = assetBaseDao.findCategory(selfId);
        if (current == null) {
            return;
        }
        Long oldParentId = FormDataUtils.toLong(current.get("PARENT_ID"));
        boolean parentChanged = !normalizeParent(oldParentId).equals(normalizeParent(newParentId));
        if (parentChanged && assetBaseDao.hasChildCategory(selfId)) {
            throw new BusinessException("该类别下已有子类别,不能调整上级类别");
        }
    }
 
    private static Long normalizeParent(Long parentId) {
        return parentId == null ? 0L : parentId;
    }
 
    private void prepareTopCategory(long selfId, Map<String, Object> formData) {
        String code = FormDataUtils.getString(formData, "CATEGORY_CODE").toUpperCase();
        if (code.isEmpty()) {
            throw new BusinessException("一级类别必须填写类别码(资产编号前缀,如 BG)");
        }
        if (!CATEGORY_CODE_PATTERN.matcher(code).matches()) {
            throw new BusinessException("类别码只能是1~10位字母或数字");
        }
        if (assetBaseDao.existsTopCategoryCode(code, selfId)) {
            throw new BusinessException("类别码 " + code + " 已被其他一级类别使用");
        }
        FormDataUtils.put(formData, "CATEGORY_CODE", code);
        FormDataUtils.put(formData, "CATEGORY_LEVEL", 1);
        FormDataUtils.put(formData, "TOP_CATEGORY_CODE", code);
        FormDataUtils.put(formData, "PARENT_ID", null);
        FormDataUtils.put(formData, "PARENT_NAME", "");
        // 新增时还没有主键,一级类别的TOP_CATEGORY_ID由保存后事件回填
        FormDataUtils.put(formData, "TOP_CATEGORY_ID", selfId > 0 ? selfId : null);
    }
 
    private void prepareSubCategory(Long parentId, Map<String, Object> formData) {
        Map<String, Object> parent = assetBaseDao.findCategory(parentId);
        if (parent == null) {
            throw new BusinessException("上级类别不存在,请重新选择");
        }
        Integer parentLevel = FormDataUtils.toInteger(parent.get("CATEGORY_LEVEL"));
        int level = (parentLevel == null ? 1 : parentLevel) + 1;
        if (level > MAX_CATEGORY_LEVEL) {
            throw new BusinessException("资产类别最多三级,不能在三级类别下再建子类别");
        }
        boolean parentIsTop = level == 2;
        Long topId = parentIsTop ? parentId : FormDataUtils.toLong(parent.get("TOP_CATEGORY_ID"));
        String topCode = FormDataUtils.toText(parentIsTop ? parent.get("CATEGORY_CODE") : parent.get("TOP_CATEGORY_CODE"));
        FormDataUtils.put(formData, "CATEGORY_LEVEL", level);
        FormDataUtils.put(formData, "TOP_CATEGORY_ID", topId);
        FormDataUtils.put(formData, "TOP_CATEGORY_CODE", topCode);
        FormDataUtils.put(formData, "PARENT_NAME", FormDataUtils.toText(parent.get("CATEGORY_NAME")));
        FormDataUtils.put(formData, "CATEGORY_CODE", FormDataUtils.getString(formData, "CATEGORY_CODE").toUpperCase());
        if (FormDataUtils.isBlank(formData, "DEVICE_TYPE")) {
            FormDataUtils.put(formData, "DEVICE_TYPE", FormDataUtils.toText(parent.get("DEVICE_TYPE")));
        }
    }
 
    /**
     * 资产类别保存后:一级类别回填自身ID并把类别码同步给下级
     * @param keyId 已保存的类别ID
     */
    public void syncCategoryTop(Long keyId) {
        Map<String, Object> category = assetBaseDao.findCategory(keyId);
        if (category == null || !Integer.valueOf(1).equals(FormDataUtils.toInteger(category.get("CATEGORY_LEVEL")))) {
            return;
        }
        assetBaseDao.syncTopCategory(keyId, FormDataUtils.toText(category.get("CATEGORY_CODE")));
    }
 
    /**
     * 存放位置保存前:计算上级名称与完整位置
     * @param keyId 当前位置ID,新增时为0或null
     * @param formData 表单数据,计算结果直接写回
     */
    public void prepareLocation(Long keyId, Map<String, Object> formData) {
        long selfId = keyId == null ? 0L : keyId;
        Long parentId = FormDataUtils.getLong(formData, "PARENT_ID");
        if (parentId != null && parentId == selfId) {
            throw new BusinessException("上级位置不能选择自己");
        }
        if (FormDataUtils.isBlank(formData, "ENABLE_STATE")) {
            FormDataUtils.put(formData, "ENABLE_STATE", ENABLED);
        }
        String name = FormDataUtils.getString(formData, "LOCATION_NAME");
        if (parentId == null) {
            FormDataUtils.put(formData, "PARENT_ID", null);
            FormDataUtils.put(formData, "PARENT_NAME", "");
            FormDataUtils.put(formData, "FULL_NAME", name);
            return;
        }
        Map<String, Object> parent = assetBaseDao.findLocation(parentId);
        if (parent == null) {
            throw new BusinessException("上级位置不存在,请重新选择");
        }
        String parentFullName = locationFullName(parent);
        FormDataUtils.put(formData, "PARENT_NAME", parentFullName);
        FormDataUtils.put(formData, "FULL_NAME", parentFullName + LOCATION_SEPARATOR + name);
    }
 
    /**
     * 存放位置保存后:重算所有下级的完整位置,并同步资产卡片上的位置名称
     * @param keyId 已保存的位置ID
     */
    public void refreshLocationDescendants(Long keyId) {
        Map<String, Object> self = assetBaseDao.findLocation(keyId);
        if (self == null) {
            return;
        }
        String fullName = locationFullName(self);
        assetBaseDao.updateCardLocationName(keyId, fullName);
        refreshChildren(keyId, fullName);
    }
 
    private void refreshChildren(Long parentId, String parentFullName) {
        List<Map<String, Object>> children = assetBaseDao.findChildLocations(parentId);
        for (Map<String, Object> child : children) {
            Long childId = FormDataUtils.toLong(child.get("ID"));
            String childFullName = parentFullName + LOCATION_SEPARATOR + FormDataUtils.toText(child.get("LOCATION_NAME"));
            assetBaseDao.updateLocationFullName(childId, parentFullName, childFullName);
            assetBaseDao.updateCardLocationName(childId, childFullName);
            refreshChildren(childId, childFullName);
        }
    }
 
    private static String locationFullName(Map<String, Object> location) {
        String fullName = FormDataUtils.toText(location.get("FULL_NAME"));
        return fullName.isEmpty() ? FormDataUtils.toText(location.get("LOCATION_NAME")) : fullName;
    }
 
    /**
     * 资产卡片保存前:校验类别与原值,补默认值、到期日,最后生成资产编号
     * @param keyId 当前卡片ID,新增时为0或null
     * @param formData 表单数据,计算结果直接写回
     */
    public void prepareCard(Long keyId, Map<String, Object> formData) {
        prepareCard(formData, keyId == null || keyId <= 0);
    }
 
    /**
     * 程序生成新卡片前(如采购入库):按与表单保存相同的规则校验、补默认值并生成编号
     * @param card 卡片字段(大写列名),结果直接写回
     */
    public void prepareNewCard(Map<String, Object> card) {
        prepareCard(card, true);
    }
 
    private void prepareCard(Map<String, Object> formData, boolean isNew) {
        Map<String, Object> category = requireCardCategory(formData);
        validateOriginalValue(formData);
        String topCode = FormDataUtils.toText(category.get("TOP_CATEGORY_CODE"));
        if (topCode.isEmpty()) {
            throw new BusinessException("所选类别的一级类别还没有类别码,请先到资产类别里补上");
        }
        FormDataUtils.put(formData, "CATEGORY_NAME", FormDataUtils.toText(category.get("CATEGORY_NAME")));
        FormDataUtils.put(formData, "TOP_CATEGORY_CODE", topCode);
        fillCardDefaults(isNew, category, formData);
        validateOptions(formData, "ASSET_STATE", "资产状态", ASSET_STATES);
        validateOptions(formData, "DEVICE_TYPE", "设备类型", DEVICE_TYPES);
        validateOptions(formData, "RISK_LEVEL", "风险等级", RISK_LEVELS);
        fillLocationName(formData);
        fillExpireDates(category, formData);
        FormDataUtils.put(formData, "REMARK", stripImportPendingNote(FormDataUtils.getString(formData, "REMARK")));
        // 编号放在所有校验之后,避免校验失败白白消耗流水号
        if (FormDataUtils.isBlank(formData, "ASSET_NO")) {
            FormDataUtils.put(formData, "ASSET_NO", nextFreeAssetNo(topCode));
        }
    }
 
    private String nextFreeAssetNo(String topCode) {
        // Excel可能带入历史编号,与流水号撞号时继续往后取
        String assetNo;
        do {
            assetNo = formatAssetNo(topCode, assetBaseDao.nextSerialNo(topCode));
        } while (assetBaseDao.existsAssetNo(assetNo));
        return assetNo;
    }
 
    private static void validateOptions(Map<String, Object> formData, String column, String title, List<String> options) {
        String value = FormDataUtils.getString(formData, column);
        if (!value.isEmpty() && !options.contains(value)) {
            throw new BusinessException(title + "「" + value + "」不正确,只能填:" + String.join("、", options));
        }
    }
 
    private static String stripImportPendingNote(String remark) {
        if (!remark.startsWith(IMPORT_PENDING_PREFIX)) {
            return remark;
        }
        int end = remark.indexOf(IMPORT_PENDING_SUFFIX);
        return end < 0 ? "" : remark.substring(end + IMPORT_PENDING_SUFFIX.length());
    }
 
    /**
     * Excel导入后补全:把还没有编号的卡片按名称匹配类别/部门/人员/位置,再按表单保存规则校验、编号并写入库记录;
     * 匹配或校验不通过的卡片不编号,在备注前标注原因,待用户在表单里改正后保存
     * @param operatorId 操作人ID
     * @param operatorName 操作人姓名
     * @return [成功编号数, 待处理数]
     */
    public int[] completeImportedCards(Long operatorId, String operatorName) {
        int success = 0;
        int pending = 0;
        for (Map<String, Object> row : assetBaseDao.findCardsWithoutAssetNo(IMPORT_BATCH_LIMIT)) {
            Long id = FormDataUtils.toLong(row.get("ID"));
            Map<String, Object> formData = new HashMap<>(row);
            try {
                resolveImportedNames(formData);
                prepareCard(formData, true);
            } catch (BusinessException e) {
                String remark = stripImportPendingNote(FormDataUtils.getString(row, "REMARK"));
                Map<String, Object> values = new HashMap<>();
                values.put("REMARK", IMPORT_PENDING_PREFIX + e.getMessage() + IMPORT_PENDING_SUFFIX + remark);
                assetBaseDao.updateCard(id, values);
                pending++;
                continue;
            }
            Map<String, Object> values = new HashMap<>();
            for (String column : IMPORT_UPDATE_COLUMNS) {
                values.put(column, formData.get(column));
            }
            assetBaseDao.updateCard(id, values);
            recordCardInbound(id, formData, operatorId, operatorName);
            success++;
        }
        return new int[]{success, pending};
    }
 
    private void resolveImportedNames(Map<String, Object> formData) {
        StringBuilder errors = new StringBuilder();
        resolveCategory(formData, errors);
        resolveUnique(formData, "USE_DEPT_ID", "USE_DEPT_NAME", "部门", errors);
        resolveUnique(formData, "USER_ID", "USER_NAME", "人员", errors);
        resolveUnique(formData, "SAFETY_USER_ID", "SAFETY_USER_NAME", "人员", errors);
        resolveLocation(formData, errors);
        if (errors.length() > 0) {
            throw new BusinessException(errors.toString());
        }
    }
 
    private void resolveCategory(Map<String, Object> formData, StringBuilder errors) {
        if (FormDataUtils.getLong(formData, "CATEGORY_ID") != null) {
            return;
        }
        String name = FormDataUtils.getString(formData, "CATEGORY_NAME");
        if (name.isEmpty()) {
            appendError(errors, "资产类别未填写");
            return;
        }
        List<Map<String, Object>> leaves = new ArrayList<>();
        for (Map<String, Object> category : assetBaseDao.findCategoriesByName(name)) {
            Integer childCount = FormDataUtils.toInteger(category.get("CHILD_COUNT"));
            if (childCount == null || childCount == 0) {
                leaves.add(category);
            }
        }
        if (leaves.size() == 1) {
            FormDataUtils.put(formData, "CATEGORY_ID", FormDataUtils.toLong(leaves.get(0).get("ID")));
        } else {
            appendError(errors, leaves.isEmpty() ? "末级类别「" + name + "」不存在" : "类别「" + name + "」有重名,请在表单中选择");
        }
    }
 
    private void resolveUnique(Map<String, Object> formData, String idColumn, String nameColumn, String title, StringBuilder errors) {
        String name = FormDataUtils.getString(formData, nameColumn);
        if (name.isEmpty() || FormDataUtils.getLong(formData, idColumn) != null) {
            return;
        }
        List<Long> ids = "部门".equals(title) ? assetBaseDao.findDeptIdsByName(name) : assetBaseDao.findUserIdsByTrueName(name);
        if (ids.size() == 1) {
            FormDataUtils.put(formData, idColumn, ids.get(0));
        } else {
            appendError(errors, ids.isEmpty() ? title + "「" + name + "」不存在" : title + "「" + name + "」有重名,请在表单中选择");
        }
    }
 
    private void resolveLocation(Map<String, Object> formData, StringBuilder errors) {
        String name = FormDataUtils.getString(formData, "LOCATION_NAME");
        if (name.isEmpty() || FormDataUtils.getLong(formData, "LOCATION_ID") != null) {
            return;
        }
        List<Map<String, Object>> matches = assetBaseDao.findLocationsByName(name);
        List<Map<String, Object>> exactFullName = new ArrayList<>();
        for (Map<String, Object> location : matches) {
            if (name.equals(FormDataUtils.toText(location.get("FULL_NAME")))) {
                exactFullName.add(location);
            }
        }
        List<Map<String, Object>> candidates = exactFullName.isEmpty() ? matches : exactFullName;
        if (candidates.size() == 1) {
            FormDataUtils.put(formData, "LOCATION_ID", FormDataUtils.toLong(candidates.get(0).get("ID")));
        } else {
            appendError(errors, candidates.isEmpty() ? "位置「" + name + "」不存在" : "位置「" + name + "」有重名,请填写完整位置");
        }
    }
 
    private static void appendError(StringBuilder errors, String message) {
        if (errors.length() > 0) {
            errors.append(";");
        }
        errors.append(message);
    }
 
    private Map<String, Object> requireCardCategory(Map<String, Object> formData) {
        Long categoryId = FormDataUtils.getLong(formData, "CATEGORY_ID");
        if (categoryId == null) {
            throw new BusinessException("请选择资产类别");
        }
        Map<String, Object> category = assetBaseDao.findCategory(categoryId);
        if (category == null) {
            throw new BusinessException("所选资产类别不存在,请重新选择");
        }
        String state = FormDataUtils.toText(category.get("ENABLE_STATE"));
        if (!state.isEmpty() && !ENABLED.equals(state)) {
            throw new BusinessException("资产类别「" + FormDataUtils.toText(category.get("CATEGORY_NAME")) + "」已停用,请选择其他类别");
        }
        return category;
    }
 
    private static void validateOriginalValue(Map<String, Object> formData) {
        String value = FormDataUtils.getString(formData, "ORIGINAL_VALUE");
        if (value.isEmpty()) {
            return;
        }
        try {
            if (new BigDecimal(value).signum() < 0) {
                throw new BusinessException("原值不能为负数");
            }
        } catch (NumberFormatException e) {
            throw new BusinessException("原值必须是数字");
        }
    }
 
    private static void fillCardDefaults(boolean isNew, Map<String, Object> category, Map<String, Object> formData) {
        if (FormDataUtils.isBlank(formData, "DEVICE_TYPE")) {
            String categoryType = FormDataUtils.toText(category.get("DEVICE_TYPE"));
            FormDataUtils.put(formData, "DEVICE_TYPE", categoryType.isEmpty() ? DEFAULT_DEVICE_TYPE : categoryType);
        }
        if (FormDataUtils.isBlank(formData, "ASSET_STATE")) {
            FormDataUtils.put(formData, "ASSET_STATE", DEFAULT_ASSET_STATE);
        }
        if (isNew) {
            if (FormDataUtils.isBlank(formData, "SOURCE_TYPE")) {
                FormDataUtils.put(formData, "SOURCE_TYPE", DEFAULT_SOURCE_TYPE);
            }
            FormDataUtils.put(formData, "LOCK_STATE", 0);
            FormDataUtils.put(formData, "PRINT_STATE", 0);
        }
    }
 
    private void fillLocationName(Map<String, Object> formData) {
        Long locationId = FormDataUtils.getLong(formData, "LOCATION_ID");
        if (locationId == null) {
            return;
        }
        Map<String, Object> location = assetBaseDao.findLocation(locationId);
        if (location != null) {
            FormDataUtils.put(formData, "LOCATION_NAME", locationFullName(location));
        }
    }
 
    private static void fillExpireDates(Map<String, Object> category, Map<String, Object> formData) {
        LocalDate purchaseDate = parseDate(FormDataUtils.getString(formData, "PURCHASE_DATE"), "购置日期");
        LocalDate startDate = parseDate(FormDataUtils.getString(formData, "START_USE_DATE"), "启用日期");
        Integer warrantyMonths = FormDataUtils.toInteger(category.get("WARRANTY_MONTHS"));
        Integer maintainMonths = FormDataUtils.toInteger(category.get("MAINTAIN_MONTHS"));
        if (FormDataUtils.isBlank(formData, "WARRANTY_EXPIRE_DATE") && purchaseDate != null && isPositive(warrantyMonths)) {
            FormDataUtils.put(formData, "WARRANTY_EXPIRE_DATE", purchaseDate.plusMonths(warrantyMonths).toString());
        }
        LocalDate maintainBase = startDate != null ? startDate : purchaseDate;
        if (FormDataUtils.isBlank(formData, "NEXT_MAINTAIN_DATE") && maintainBase != null && isPositive(maintainMonths)) {
            FormDataUtils.put(formData, "NEXT_MAINTAIN_DATE", maintainBase.plusMonths(maintainMonths).toString());
        }
    }
 
    private static boolean isPositive(Integer value) {
        return value != null && value > 0;
    }
 
    private static LocalDate parseDate(String text, String title) {
        if (text.isEmpty()) {
            return null;
        }
        try {
            return LocalDate.parse(text.length() > DATE_TEXT_LENGTH ? text.substring(0, DATE_TEXT_LENGTH) : text);
        } catch (DateTimeParseException e) {
            throw new BusinessException(title + "格式不正确,应为 yyyy-MM-dd");
        }
    }
 
    /**
     * 拼资产编号:一级类别码 + 至少5位流水号
     * @param topCategoryCode 一级类别码
     * @param serialNo 流水号
     * @return 资产编号,如 BG00001
     */
    public static String formatAssetNo(String topCategoryCode, long serialNo) {
        return topCategoryCode + String.format("%0" + ASSET_SN_DIGITS + "d", serialNo);
    }
 
    /**
     * 资产卡片保存后:首次保存时写一条“入库”变动记录,重复保存不重复写
     * @param assetId 已保存的卡片ID
     * @param formData 表单数据
     * @param operatorId 操作人ID
     * @param operatorName 操作人姓名
     */
    public void recordCardInbound(Long assetId, Map<String, Object> formData, Long operatorId, String operatorName) {
        if (assetId == null || assetBaseDao.existsChangeLog(assetId, CHANGE_TYPE_INBOUND)) {
            return;
        }
        Map<String, Object> row = new HashMap<>();
        row.put("ASSET_ID", assetId);
        row.put("ASSET_NO", FormDataUtils.getString(formData, "ASSET_NO"));
        row.put("ASSET_NAME", FormDataUtils.getString(formData, "ASSET_NAME"));
        row.put("CHANGE_TYPE", CHANGE_TYPE_INBOUND);
        row.put("BILL_TYPE", FormDataUtils.getString(formData, "SOURCE_TYPE"));
        row.put("BILL_ID", FormDataUtils.getLong(formData, "SOURCE_BILL_ID"));
        row.put("BILL_NO", FormDataUtils.getString(formData, "SOURCE_BILL_NO"));
        row.put("BEFORE_STATE", "");
        row.put("AFTER_STATE", FormDataUtils.getString(formData, "ASSET_STATE"));
        row.put("BEFORE_CONTENT", "");
        row.put("AFTER_CONTENT", "部门:" + FormDataUtils.getString(formData, "USE_DEPT_NAME")
                + ";使用人:" + FormDataUtils.getString(formData, "USER_NAME")
                + ";位置:" + FormDataUtils.getString(formData, "LOCATION_NAME"));
        row.put("OPERATE_USER_ID", operatorId);
        row.put("OPERATE_USER_NAME", operatorName);
        assetBaseDao.insertChangeLog(row);
    }
}