batch customer event

This commit is contained in:
Timi
2026-08-03 17:30:33 +08:00
parent 6eedc06a07
commit 50d4bba045
4 changed files with 80 additions and 30 deletions
@@ -79,8 +79,8 @@ public class GaoRegistrationEventRecordController {
@RequestRateLimit @RequestRateLimit
@PostMapping("/create") @PostMapping("/create")
@JsonView(ResponseView.Public.class) @JsonView(ResponseView.Public.class)
public GaoRegistrationEventRecord create(@RequestBody GaoRegistrationEventRecordCreateReq req) { public List<GaoRegistrationEventRecord> create(@RequestBody GaoRegistrationEventRecordCreateReq req) {
return service.createRecord(req); return service.createRecords(req);
} }
@AOPLog @AOPLog
@@ -26,11 +26,11 @@ import java.util.List;
/// @since 2026-07-27 /// @since 2026-07-27
public interface GaoRegistrationEventRecordService extends BaseService<GaoRegistrationEventRecord, String> { public interface GaoRegistrationEventRecordService extends BaseService<GaoRegistrationEventRecord, String> {
/// 创建登记事件记录 /// 批量创建登记事件记录
/// ///
/// @param req 创建请求 /// @param req 批量创建请求
/// @return 创建后的登记事件记录 /// @return 创建后的登记事件记录列表
GaoRegistrationEventRecord createRecord(GaoRegistrationEventRecordCreateReq req); List<GaoRegistrationEventRecord> createRecords(GaoRegistrationEventRecordCreateReq req);
/// 快速录入客户并创建登记事件记录 /// 快速录入客户并创建登记事件记录
/// ///
@@ -53,6 +53,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
@@ -102,14 +103,24 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe
@Transactional(TimiServerDBConfig.ROLLBACKER) @Transactional(TimiServerDBConfig.ROLLBACKER)
@Override @Override
public GaoRegistrationEventRecord createRecord(GaoRegistrationEventRecordCreateReq req) { public List<GaoRegistrationEventRecord> createRecords(GaoRegistrationEventRecordCreateReq req) {
Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_RECORD_CREATE"); Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_RECORD_BATCH_CREATE");
try { try {
TimiException.required(req, "not found req");
TimiException.requiredTrue(req.getCustomerIdList() != null && !req.getCustomerIdList().isEmpty(), "customerIdList is empty");
logger.setContent(jackson.writeValueAsString(req)); logger.setContent(jackson.writeValueAsString(req));
GaoRegistrationEventRecord record = doCreateRecord(req); List<GaoRegistrationEventRecord> 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.setLevel(Logger.Level.INFO);
logger.setResult(record.getId()); logger.setResult(recordList.stream().map(GaoRegistrationEventRecord::getId).collect(Collectors.joining(",")));
return record; return recordList;
} catch (TimiException e) { } catch (TimiException e) {
logger.setLevel(Logger.Level.WARN); logger.setLevel(Logger.Level.WARN);
logger.setException(e.getMessage()); logger.setException(e.getMessage());
@@ -117,13 +128,59 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe
} catch (Exception e) { } catch (Exception e) {
logger.setLevel(Logger.Level.ERROR); logger.setLevel(Logger.Level.ERROR);
logger.setException(TimiJava.serializeThrowable(e)); logger.setException(TimiJava.serializeThrowable(e));
log.error("新增 GAO 登记记录失败", e); log.error("批量新增 GAO 登记记录失败", e);
throw new TimiException(TimiCode.ERROR, "新增 GAO 登记记录失败", e); throw new TimiException(TimiCode.ERROR, "批量新增 GAO 登记记录失败", e);
} finally { } finally {
loggerService.create(logger); loggerService.create(logger);
} }
} }
private List<String> duplicateTempFileList(List<String> sourceIdList) {
if (sourceIdList == null) {
return null;
}
List<String> 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<String> 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) @Transactional(TimiServerDBConfig.ROLLBACKER)
@Override @Override
public GaoRegistrationEventRecord quickRegister(GaoQuickRegistrationEventRecordReq req) { public GaoRegistrationEventRecord quickRegister(GaoQuickRegistrationEventRecordReq req) {
@@ -135,11 +192,11 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe
TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId"); TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId");
GaoCustomer customer = customerService.getOrQuickCreateByCode(req.getCustomerCode()); GaoCustomer customer = customerService.getOrQuickCreateByCode(req.getCustomerCode());
GaoRegistrationEventRecordCreateReq recordReq = new GaoRegistrationEventRecordCreateReq(); GaoRegistrationEventRecordCreateReq recordReq = new GaoRegistrationEventRecordCreateReq();
recordReq.setCustomerId(customer.getId()); recordReq.setCustomerIdList(List.of(customer.getId()));
recordReq.setRegistrationEventId(req.getRegistrationEventId()); recordReq.setRegistrationEventId(req.getRegistrationEventId());
recordReq.setRemark(req.getRemark()); recordReq.setRemark(req.getRemark());
recordReq.setAttachmentIdList(req.getAttachmentIdList()); recordReq.setAttachmentIdList(req.getAttachmentIdList());
GaoRegistrationEventRecord record = doCreateRecord(recordReq); GaoRegistrationEventRecord record = createRecords(recordReq).get(0);
logger.setLevel(Logger.Level.INFO); logger.setLevel(Logger.Level.INFO);
logger.setResult(record.getId()); logger.setResult(record.getId());
return record; return record;
@@ -344,20 +401,16 @@ public class GaoRegistrationEventRecordServiceImplement extends AbstractEntitySe
return list; return list;
} }
private String resolveCustomerId(GaoRegistrationEventRecordCreateReq req) { private String resolveCustomerId(String customerId) {
if (TimiJava.isNotEmpty(req.getCustomerId())) { TimiException.required(customerId, "not found customerId");
customerService.get(req.getCustomerId()); customerService.get(customerId);
return req.getCustomerId(); return customerId;
}
TimiException.required(req.getCustomerCode(), "not found req.customerCode");
GaoCustomer customer = customerService.getByCode(req.getCustomerCode());
return customer.getId();
} }
private GaoRegistrationEventRecord doCreateRecord(GaoRegistrationEventRecordCreateReq req) { private GaoRegistrationEventRecord doCreateRecord(GaoRegistrationEventRecordCreateReq req, String customerId) {
TimiException.required(req, "not found req"); TimiException.required(req, "not found req");
TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId"); TimiException.required(req.getRegistrationEventId(), "not found req.registrationEventId");
String customerId = resolveCustomerId(req); customerId = resolveCustomerId(customerId);
GaoRegistrationEvent registrationEvent = registrationEventService.get(req.getRegistrationEventId()); GaoRegistrationEvent registrationEvent = registrationEventService.get(req.getRegistrationEventId());
checkRegistrationEventPermission(registrationEvent); checkRegistrationEventPermission(registrationEvent);
long registeredAt = TimiJava.defaultIfNull(req.getRegisteredAt(), Time.now()); long registeredAt = TimiJava.defaultIfNull(req.getRegisteredAt(), Time.now());
@@ -13,11 +13,8 @@ import java.util.List;
@Data @Data
public class GaoRegistrationEventRecordCreateReq { public class GaoRegistrationEventRecordCreateReq {
/// 客户 ID /// 客户 ID 列表,批量登记时使用
private String customerId; private List<String> customerIdList = new ArrayList<>();
/// 客户编码,扫码登记可直接传这个
private String customerCode;
/// 登记事件 ID /// 登记事件 ID
private String registrationEventId; private String registrationEventId;