From 50d4bba045a2ea1ae93ebfb46cac56f9aedd8527 Mon Sep 17 00:00:00 2001 From: Timi Date: Mon, 3 Aug 2026 17:30:33 +0800 Subject: [PATCH] batch customer event --- .../GaoRegistrationEventRecordController.java | 4 +- .../GaoRegistrationEventRecordService.java | 8 +- ...gistrationEventRecordServiceImplement.java | 91 +++++++++++++++---- .../GaoRegistrationEventRecordCreateReq.java | 7 +- 4 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventRecordController.java b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventRecordController.java index 1d83eef..a341448 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventRecordController.java +++ b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventRecordController.java @@ -79,8 +79,8 @@ public class GaoRegistrationEventRecordController { @RequestRateLimit @PostMapping("/create") @JsonView(ResponseView.Public.class) - public GaoRegistrationEventRecord create(@RequestBody GaoRegistrationEventRecordCreateReq req) { - return service.createRecord(req); + public List create(@RequestBody GaoRegistrationEventRecordCreateReq req) { + return service.createRecords(req); } @AOPLog diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventRecordService.java b/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventRecordService.java index b294147..923683e 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventRecordService.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventRecordService.java @@ -26,11 +26,11 @@ import java.util.List; /// @since 2026-07-27 public interface GaoRegistrationEventRecordService extends BaseService { - /// 创建登记事件记录 + /// 批量创建登记事件记录 /// - /// @param req 创建请求 - /// @return 创建后的登记事件记录 - GaoRegistrationEventRecord createRecord(GaoRegistrationEventRecordCreateReq req); + /// @param req 批量创建请求 + /// @return 创建后的登记事件记录列表 + List createRecords(GaoRegistrationEventRecordCreateReq req); /// 快速录入客户并创建登记事件记录 /// diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoRegistrationEventRecordServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoRegistrationEventRecordServiceImplement.java index 06457b6..f84af31 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoRegistrationEventRecordServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoRegistrationEventRecordServiceImplement.java @@ -53,6 +53,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.time.Instant; import java.time.LocalDate; @@ -102,14 +103,24 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe @Transactional(TimiServerDBConfig.ROLLBACKER) @Override - public GaoRegistrationEventRecord createRecord(GaoRegistrationEventRecordCreateReq req) { - Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_RECORD_CREATE"); + public List createRecords(GaoRegistrationEventRecordCreateReq req) { + Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_RECORD_BATCH_CREATE"); try { + TimiException.required(req, "not found req"); + TimiException.requiredTrue(req.getCustomerIdList() != null && !req.getCustomerIdList().isEmpty(), "customerIdList is empty"); logger.setContent(jackson.writeValueAsString(req)); - GaoRegistrationEventRecord record = doCreateRecord(req); + List recordList = new ArrayList<>(); + for (int index = 0; index < req.getCustomerIdList().size(); index++) { + String customerId = req.getCustomerIdList().get(index); + GaoRegistrationEventRecordCreateReq customerReq = copyCreateReq(req); + customerReq.setAttachmentIdList(0 == index && canUseSourceTempFileList(req.getAttachmentIdList()) + ? req.getAttachmentIdList() + : duplicateTempFileList(req.getAttachmentIdList())); + recordList.add(doCreateRecord(customerReq, customerId)); + } logger.setLevel(Logger.Level.INFO); - logger.setResult(record.getId()); - return record; + logger.setResult(recordList.stream().map(GaoRegistrationEventRecord::getId).collect(Collectors.joining(","))); + return recordList; } catch (TimiException e) { logger.setLevel(Logger.Level.WARN); logger.setException(e.getMessage()); @@ -117,13 +128,59 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe } catch (Exception e) { logger.setLevel(Logger.Level.ERROR); logger.setException(TimiJava.serializeThrowable(e)); - log.error("新增 GAO 登记记录失败", e); - throw new TimiException(TimiCode.ERROR, "新增 GAO 登记记录失败", e); + log.error("批量新增 GAO 登记记录失败", e); + throw new TimiException(TimiCode.ERROR, "批量新增 GAO 登记记录失败", e); } finally { loggerService.create(logger); } } + private List duplicateTempFileList(List sourceIdList) { + if (sourceIdList == null) { + return null; + } + List targetIdList = new ArrayList<>(); + for (String sourceId : sourceIdList) { + if (sourceId == null || sourceId.isBlank()) { + continue; + } + Attachment source = attachmentService.get(sourceId.trim()); + TimiException.required(source, "not found attachment"); + TimiException.required(source.getMongoId(), "not found attachment.mongoId"); + Attachment target = new Attachment(); + target.setBizType(Attachment.BizType.TEMP_FILE); + target.setName(source.getName()); + target.setInputStream(new ByteArrayInputStream(attachmentService.readAllByMongoId(source.getMongoId()))); + attachmentService.create(target); + targetIdList.add(target.getId()); + } + return targetIdList; + } + + private boolean canUseSourceTempFileList(List sourceIdList) { + if (sourceIdList == null || sourceIdList.isEmpty()) { + return true; + } + for (String sourceId : sourceIdList) { + if (sourceId == null || sourceId.isBlank()) { + continue; + } + Attachment source = attachmentService.get(sourceId.trim()); + if (source == null || source.getBizType() != Attachment.BizType.TEMP_FILE) { + return false; + } + } + return true; + } + + private GaoRegistrationEventRecordCreateReq copyCreateReq(GaoRegistrationEventRecordCreateReq source) { + GaoRegistrationEventRecordCreateReq target = new GaoRegistrationEventRecordCreateReq(); + target.setRegistrationEventId(source.getRegistrationEventId()); + target.setRemark(source.getRemark()); + target.setRegisteredAt(source.getRegisteredAt()); + return target; + } + @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public GaoRegistrationEventRecord quickRegister(GaoQuickRegistrationEventRecordReq req) { @@ -135,11 +192,11 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId"); GaoCustomer customer = customerService.getOrQuickCreateByCode(req.getCustomerCode()); GaoRegistrationEventRecordCreateReq recordReq = new GaoRegistrationEventRecordCreateReq(); - recordReq.setCustomerId(customer.getId()); + recordReq.setCustomerIdList(List.of(customer.getId())); recordReq.setRegistrationEventId(req.getRegistrationEventId()); recordReq.setRemark(req.getRemark()); recordReq.setAttachmentIdList(req.getAttachmentIdList()); - GaoRegistrationEventRecord record = doCreateRecord(recordReq); + GaoRegistrationEventRecord record = createRecords(recordReq).get(0); logger.setLevel(Logger.Level.INFO); logger.setResult(record.getId()); return record; @@ -344,20 +401,16 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe return list; } - private String resolveCustomerId(GaoRegistrationEventRecordCreateReq req) { - if (TimiJava.isNotEmpty(req.getCustomerId())) { - customerService.get(req.getCustomerId()); - return req.getCustomerId(); - } - TimiException.required(req.getCustomerCode(), "not found req.customerCode"); - GaoCustomer customer = customerService.getByCode(req.getCustomerCode()); - return customer.getId(); + private String resolveCustomerId(String customerId) { + TimiException.required(customerId, "not found customerId"); + customerService.get(customerId); + return customerId; } - private GaoRegistrationEventRecord doCreateRecord(GaoRegistrationEventRecordCreateReq req) { + private GaoRegistrationEventRecord doCreateRecord(GaoRegistrationEventRecordCreateReq req, String customerId) { TimiException.required(req, "not found req"); TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId"); - String customerId = resolveCustomerId(req); + customerId = resolveCustomerId(customerId); GaoRegistrationEvent registrationEvent = registrationEventService.get(req.getRegistrationEventId()); checkRegistrationEventPermission(registrationEvent); long registeredAt = TimiJava.defaultIfNull(req.getRegisteredAt(), Time.now()); diff --git a/src/main/java/com/imyeyu/api/modules/gao/vo/GaoRegistrationEventRecordCreateReq.java b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoRegistrationEventRecordCreateReq.java index 2e23ebe..72d8b81 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/vo/GaoRegistrationEventRecordCreateReq.java +++ b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoRegistrationEventRecordCreateReq.java @@ -13,11 +13,8 @@ import java.util.List; @Data public class GaoRegistrationEventRecordCreateReq { - /// 客户 ID - private String customerId; - - /// 客户编码,扫码登记可直接传这个 - private String customerCode; + /// 客户 ID 列表,批量登记时使用 + private List customerIdList = new ArrayList<>(); /// 登记事件 ID private String registrationEventId;