perf(lastleg): batch appointment requirement generation reads
This commit is contained in:
+22
@@ -28,6 +28,28 @@ public interface InboundFbaAppointmentQueryMapper {
|
||||
" ORDER BY appointment_id DESC LIMIT 1001</script>"})
|
||||
List<Long> relatedBatchIds(@Param("sources") List<com.baiyue.lastleg.appointmentplanning.AppointmentCoverageService.SourceRef> sources);
|
||||
|
||||
record RequirementSource(int groupOrdinal,String customerId,String sourceKey,String destinationKey) { }
|
||||
record RequirementBatch(int groupOrdinal,long appointmentId) { }
|
||||
/** 按原需求组返回历史归属,失活明细也保留;每组第1001条用于检测超限。 */
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT group_ordinal,appointment_id FROM (
|
||||
SELECT matched.*,ROW_NUMBER() OVER(PARTITION BY group_ordinal ORDER BY appointment_id DESC) work_row FROM (
|
||||
SELECT DISTINCT requested.group_ordinal,i.appointment_id
|
||||
FROM ll_inbound_fba_appointment_item i JOIN (
|
||||
<foreach collection='sources' item='s' separator=' UNION ALL '>
|
||||
SELECT CAST(#{s.groupOrdinal} AS SIGNED) group_ordinal,
|
||||
CAST(#{s.customerId} AS CHAR(128)) customer_id,CAST(#{s.sourceKey} AS CHAR(160)) source_key,
|
||||
CAST(#{s.destinationKey} AS CHAR(160)) destination_key
|
||||
</foreach>
|
||||
) requested ON requested.customer_id=i.customer_id AND requested.source_key=i.source_key
|
||||
AND requested.destination_key=CONCAT('FBA:',i.fba_warehouse_code)
|
||||
) matched
|
||||
) ranked WHERE work_row <= 1001 ORDER BY group_ordinal,appointment_id DESC
|
||||
</script>
|
||||
""")
|
||||
List<RequirementBatch> requirementBatches(@Param("sources") List<RequirementSource> sources);
|
||||
|
||||
@Select("<script>SELECT COUNT(*) FROM ll_order o "+com.baiyue.lastleg.appointmentplanning.AppointmentCoverageMapper.TRANSFER_JOIN+
|
||||
" WHERE o.id IN <foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach> AND ("+
|
||||
com.baiyue.lastleg.appointmentplanning.AppointmentCoverageMapper.SHIPMENT_MISSING+") </script>")
|
||||
|
||||
+37
@@ -34,6 +34,7 @@ public class InboundFbaAppointmentService {
|
||||
@Resource private com.baiyue.lastleg.inboundplan.receiving.FbaReceivingStore receiving;
|
||||
@Resource private com.baiyue.lastleg.inboundplan.receiving.FbaAppointmentWorkPort receivingWork;
|
||||
@Resource private com.baiyue.lastleg.inboundplan.work.AppointmentWorkAppointmentChanges workChanges;
|
||||
@Resource private com.baiyue.lastleg.inboundplan.work.AppointmentRequirementReadRepository requirementReads;
|
||||
|
||||
@Transactional(readOnly=true)
|
||||
public List<FbaAppointmentResponse> calendar(LocalDate from,LocalDate to) {
|
||||
@@ -111,6 +112,42 @@ public class InboundFbaAppointmentService {
|
||||
if(ids.size()>1000) throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,"关联批次超过读取范围,请缩小原来源范围后继续核对");
|
||||
return ids.isEmpty()?List.of():responses(appointmentMapper.selectBatchIds(ids));
|
||||
}
|
||||
/** 同页原料只读一次,历史归属按原需求组保留;响应解析延后至原访问顺序。 */
|
||||
public java.util.function.IntFunction<List<FbaAppointmentResponse>> prepareRelatedBatchSnapshots(List<List<AppointmentCoverageService.SourceRef>> groups) {
|
||||
var errors=new HashMap<Integer,RuntimeException>();
|
||||
var requested=new ArrayList<InboundFbaAppointmentQueryMapper.RequirementSource>();
|
||||
for(int i=0;i<groups.size();i++) {
|
||||
var refs=groups.get(i);
|
||||
if(refs==null || refs.isEmpty()) continue;
|
||||
if(refs.size()>100 || refs.stream().anyMatch(ref->ref==null || clean(ref.customerId())==null || clean(ref.sourceKey())==null || clean(ref.destinationKey())==null)) {
|
||||
errors.put(i,bad("每次最多读取100个完整原来源的关联批次"));continue;
|
||||
}
|
||||
final int group=i;
|
||||
refs.stream().distinct().forEach(ref->requested.add(new InboundFbaAppointmentQueryMapper.RequirementSource(group,ref.customerId(),ref.sourceKey(),ref.destinationKey())));
|
||||
}
|
||||
var groupIds=new HashMap<Integer,Set<Long>>();
|
||||
for(int start=0;start<requested.size();start+=100) {
|
||||
for(var match:requirementReads.history(requested.subList(start,Math.min(start+100,requested.size())))) {
|
||||
if(match.groupOrdinal()<0 || match.groupOrdinal()>=groups.size()) throw new IllegalStateException("历史关联返回了请求外需求组");
|
||||
groupIds.computeIfAbsent(match.groupOrdinal(),ignored->new TreeSet<>()).add(match.appointmentId());
|
||||
}
|
||||
}
|
||||
groupIds.forEach((group,ids)->{if(ids.size()>1000) errors.put(group,new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,"关联批次超过读取范围,请缩小原来源范围后继续核对"));});
|
||||
var ids=groupIds.entrySet().stream().filter(e->!errors.containsKey(e.getKey())).flatMap(e->e.getValue().stream()).distinct().sorted().toList();
|
||||
var rows=new LinkedHashMap<Long,InboundFbaAppointment>();
|
||||
var items=new HashMap<Long,List<InboundFbaAppointmentItem>>();
|
||||
for(int start=0;start<ids.size();start+=200) {
|
||||
var batch=ids.subList(start,Math.min(start+200,ids.size()));
|
||||
requirementReads.appointments(batch).forEach(row->rows.put(row.getId(),row));
|
||||
requirementReads.items(batch).forEach(item->items.computeIfAbsent(item.getAppointmentId(),ignored->new ArrayList<>()).add(item));
|
||||
}
|
||||
return group->{
|
||||
if(group<0 || group>=groups.size()) throw new IllegalArgumentException("需求组不属于本次读取范围");
|
||||
if(errors.containsKey(group)) throw errors.get(group);
|
||||
var selected=groupIds.getOrDefault(group,Set.of());
|
||||
return rows.values().stream().filter(row->selected.contains(row.getId())).map(row->response(row,items.getOrDefault(row.getId(),List.of()))).toList();
|
||||
};
|
||||
}
|
||||
@Transactional(readOnly=true)
|
||||
public FbaAppointmentBatchContext batchContext(String key) {
|
||||
final long id;
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baiyue.lastleg.inboundplan.work;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baiyue.lastleg.inboundplan.entity.*;
|
||||
import com.baiyue.lastleg.inboundplan.mapper.*;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
/** 需求生成的本域原料读取,历史归属不能从活动响应明细反推。 */
|
||||
@Repository
|
||||
public class AppointmentRequirementReadRepository {
|
||||
private final AppointmentWorkSourceMapper mapping;
|
||||
private final InboundFbaAppointmentQueryMapper queries;
|
||||
private final InboundFbaAppointmentMapper appointments;
|
||||
private final InboundFbaAppointmentItemMapper items;
|
||||
/** 保留本域 Mapper 与客户来源查询边界。 */
|
||||
public AppointmentRequirementReadRepository(AppointmentWorkSourceMapper mapping,InboundFbaAppointmentQueryMapper queries,InboundFbaAppointmentMapper appointments,InboundFbaAppointmentItemMapper items) {
|
||||
this.mapping=mapping;this.queries=queries;this.appointments=appointments;this.items=items;
|
||||
}
|
||||
/** 每柜独立上限由既有窗口查询保持。 */
|
||||
public List<AppointmentWorkSourceMapper.ContainerSource> sources(List<String> containers) { return containers.isEmpty()?List.of():mapping.containerSourcesBatch(containers); }
|
||||
/** 分块只控制单条 SQL 输入,业务服务仍按需求组完整合并及核对上限。 */
|
||||
public List<InboundFbaAppointmentQueryMapper.RequirementBatch> history(List<InboundFbaAppointmentQueryMapper.RequirementSource> sources) { return sources.isEmpty()?List.of():queries.requirementBatches(sources); }
|
||||
/** 读取已确认关联的预约主记录。 */
|
||||
public List<InboundFbaAppointment> appointments(List<Long> ids) { return ids.isEmpty()?List.of():appointments.selectBatchIds(ids); }
|
||||
/** 包括失活明细以维持取消与替代历史;响应过滤由业务服务执行。 */
|
||||
public List<InboundFbaAppointmentItem> items(List<Long> ids) { return ids.isEmpty()?List.of():items.selectList(new LambdaQueryWrapper<InboundFbaAppointmentItem>().in(InboundFbaAppointmentItem::getAppointmentId,ids).orderByAsc(InboundFbaAppointmentItem::getId)); }
|
||||
}
|
||||
+75
-27
@@ -17,29 +17,85 @@ public class AppointmentWorkRequirementProvider implements AppointmentWorkFactGa
|
||||
private final AppointmentCargoVerificationService cargo;
|
||||
private final InboundFbaAppointmentService appointments;
|
||||
private final AppointmentWorkSourceMapper sourceMapping;
|
||||
@jakarta.annotation.Resource private AppointmentRequirementReadRepository reads;
|
||||
public AppointmentWorkRequirementProvider(AppointmentSourceService sources,AppointmentCargoVerificationService cargo,InboundFbaAppointmentService appointments,AppointmentWorkSourceMapper sourceMapping) {
|
||||
this.sources=sources; this.cargo=cargo; this.appointments=appointments; this.sourceMapping=sourceMapping;
|
||||
}
|
||||
/** 同次生成仅共享按需读取的目录;原业务数据继续逐需求核实。 */
|
||||
public RequirementReader openRequirements() {
|
||||
/** 本次生成共享原料,实例不会跨同步页或独立命令保存。 */
|
||||
public RequirementReader openRequirements(List<RequirementInput> inputs) {
|
||||
return new RequirementReader() {
|
||||
private AppointmentSourceService.ReadScope scope;
|
||||
/** 直送和未建立尾程来源的需求不创建目录读取依赖。 */
|
||||
private Prepared prepared;
|
||||
/** 原料提示不替代实际路线判断;未提示的真实需求按同一实现即时读取。 */
|
||||
public List<Requirement> read(ContainerSource container,Flow flow) {
|
||||
return requirements(container,flow,refs->{
|
||||
if(scope==null) scope=sources.openReadScope();
|
||||
return scope.resolveBatch(refs);
|
||||
});
|
||||
if(flow==Flow.DIRECT) return original(container);
|
||||
if(flow!=Flow.TRANSFER) throw conflict("需求清单必须明确有效作业路线");
|
||||
if(prepared==null) {
|
||||
var requested=new ArrayList<>(inputs);requested.add(new RequirementInput(container,flow));
|
||||
prepared=prepare(requested);
|
||||
}
|
||||
var data=prepared.containers.contains(container)?prepared:prepare(List.of(new RequirementInput(container,flow)));
|
||||
return requirements(container,flow,data);
|
||||
}
|
||||
};
|
||||
}
|
||||
/** 私人地址仍以原客户仓需求表示,直送不增加尾程读取依赖。 */
|
||||
private List<Requirement> original(ContainerSource container) {
|
||||
return container.requirements().stream().map(r->"PRIVATE".equals(r.destinationKind())?new Requirement(r.requirementKey(),r.batchKey(),r.batchNo(),"CUSTOMER",r.originName(),r.destinationName(),r.timezone(),r.references(),r.replacesRequirementKeys(),r.sources()):r).toList();
|
||||
}
|
||||
private record Group(ContainerSource container,String requirementKey) { }
|
||||
/** 只含本次原料及延后执行器,不缓存已经解析的业务响应。 */
|
||||
private static final class Prepared {
|
||||
private final Set<ContainerSource> containers=new LinkedHashSet<>();
|
||||
private final Map<String,List<AppointmentWorkSourceMapper.Source>> mapped=new HashMap<>();
|
||||
private final Map<Group,Integer> ordinals=new LinkedHashMap<>();
|
||||
private List<AppointmentSourceService.OrderGroup> sources=List.of();
|
||||
private RuntimeException mappingError,sourceError;
|
||||
private java.util.function.IntFunction<List<FbaAppointmentResponse>> history;
|
||||
private java.util.function.Function<AppointmentSourceService.Source,AppointmentCargoVerificationService.Context> inspection;
|
||||
}
|
||||
/** 先批量读取原料,组级业务错误在原需求访问位置抛出。 */
|
||||
private Prepared prepare(List<RequirementInput> inputs) {
|
||||
var data=new Prepared();
|
||||
inputs.stream().filter(i->i.flow()==Flow.TRANSFER).map(RequirementInput::source).forEach(data.containers::add);
|
||||
var ids=data.containers.stream().map(ContainerSource::containerBookingId).distinct().toList();
|
||||
try {
|
||||
for(int start=0;start<ids.size();start+=100) reads.sources(ids.subList(start,Math.min(start+100,ids.size())))
|
||||
.forEach(row->data.mapped.computeIfAbsent(row.containerBookingId(),ignored->new ArrayList<>()).add(row.source()));
|
||||
} catch(RuntimeException error) { data.mappingError=error;return data; }
|
||||
var groups=new ArrayList<List<AppointmentExternalSourceGateway.OrderRef>>();
|
||||
for(var container:data.containers) {
|
||||
var mapped=data.mapped.getOrDefault(container.containerBookingId(),List.of());
|
||||
if(mapped.size()>1000) continue;
|
||||
for(var requirement:original(container)) {
|
||||
if(!"FBA".equals(requirement.destinationKind())) continue;
|
||||
var inbound=requirement.requirementKey().startsWith("INBOUND:")?requirement.requirementKey().substring(8):null;
|
||||
var orders=mapped.stream().filter(row->Objects.equals(row.sourceInboundId(),inbound)).toList();
|
||||
if(orders.isEmpty()) continue;
|
||||
data.ordinals.put(new Group(container,requirement.requirementKey()),groups.size());
|
||||
groups.add(orders.stream().map(row->new AppointmentExternalSourceGateway.OrderRef(row.customerId(),row.id())).toList());
|
||||
}
|
||||
}
|
||||
if(groups.isEmpty()) return data;
|
||||
try { data.sources=sources.openReadScope().resolveOrderGroups(groups); }
|
||||
catch(RuntimeException error) { data.sourceError=error;return data; }
|
||||
var historyGroups=data.sources.stream().map(group->group.error()==null?group.sources().stream().map(source->new AppointmentCoverageService.SourceRef(source.customerId(),source.sourceKey(),source.destinationKey())).toList():List.<AppointmentCoverageService.SourceRef>of()).toList();
|
||||
var valid=data.sources.stream().filter(group->group.error()==null).flatMap(group->group.sources().stream()).toList();
|
||||
if(valid.isEmpty()) return data;
|
||||
try { data.history=appointments.prepareRelatedBatchSnapshots(historyGroups); }
|
||||
catch(RuntimeException error) { data.history=ignored->{throw error;}; }
|
||||
// 不提前解析货件,仍由原 history -> anyMatch 路径按实际访问顺序执行。
|
||||
try { data.inspection=cargo.prepareInspection(valid); }
|
||||
catch(RuntimeException error) { data.inspection=ignored->{throw error;}; }
|
||||
return data;
|
||||
}
|
||||
/** 按当前实物分配生成客户需求,保留已撤销批次身份以退役旧事项。 */
|
||||
private List<Requirement> requirements(ContainerSource container,Flow effectiveFlow,
|
||||
java.util.function.Function<List<AppointmentCoverageService.SourceRef>,List<AppointmentSourceService.Source>> resolver) {
|
||||
var original=container.requirements().stream().map(r->"PRIVATE".equals(r.destinationKind())?new Requirement(r.requirementKey(),r.batchKey(),r.batchNo(),"CUSTOMER",r.originName(),r.destinationName(),r.timezone(),r.references(),r.replacesRequirementKeys(),r.sources()):r).toList();
|
||||
Prepared data) {
|
||||
var original=original(container);
|
||||
if(effectiveFlow==Flow.DIRECT) return original;
|
||||
if(effectiveFlow!=Flow.TRANSFER) throw conflict("需求清单必须明确有效作业路线");
|
||||
var mapped=sourceMapping.containerSources(container.containerBookingId());
|
||||
if(data.mappingError!=null) throw data.mappingError;
|
||||
var mapped=data.mapped.getOrDefault(container.containerBookingId(),List.of());
|
||||
if(mapped.size()>1000) throw unavailable("柜次尾程来源超过完整读取上限");
|
||||
var result=new LinkedHashMap<String,Requirement>();
|
||||
var privateDispatches=original.stream().anyMatch(r->"CUSTOMER".equals(r.destinationKind()))?sourceMapping.privateDispatches(container.containerBookingId()):List.<AppointmentWorkSourceMapper.DispatchSource>of();
|
||||
@@ -50,9 +106,13 @@ public class AppointmentWorkRequirementProvider implements AppointmentWorkFactGa
|
||||
if(sourceOrders.isEmpty()) { result.put(requirement.requirementKey(),requirement); continue; }
|
||||
if("CUSTOMER".equals(requirement.destinationKind())) { customer(requirement,sourceOrders,privateDispatches,result);continue; }
|
||||
if(!"FBA".equals(requirement.destinationKind())) { result.put(requirement.requirementKey(),requirement); continue; }
|
||||
var refs=canonical(sourceOrders.stream().map(s->new SourceReference(s.customerId(),"ORDER:"+s.id(),null)).toList());
|
||||
if(sourceOrders.size()>100) throw unavailable("Firstleg需求缺少完整客户货源关系");
|
||||
if(data.sourceError!=null) throw data.sourceError;
|
||||
int ordinal=data.ordinals.get(new Group(container,requirement.requirementKey()));
|
||||
var group=data.sources.get(ordinal);
|
||||
var refs=group.references();
|
||||
List<AppointmentSourceService.Source> resolved;
|
||||
try { resolved=resolver.apply(refs); }
|
||||
try { resolved=group.requireSources(); }
|
||||
catch(org.springframework.web.server.ResponseStatusException error) {
|
||||
if(!AppointmentWorkSourceAvailability.unresolved(error)) throw error;
|
||||
// Keep the complete physical obligation and its canonical source identities. The fact
|
||||
@@ -63,10 +123,10 @@ public class AppointmentWorkRequirementProvider implements AppointmentWorkFactGa
|
||||
continue;
|
||||
}
|
||||
var complete=resolved.stream().map(s->new AppointmentCoverageService.SourceRef(s.customerId(),s.sourceKey(),s.destinationKey())).toList();
|
||||
var history=appointments.relatedBatchSnapshots(complete);
|
||||
var history=data.history.apply(ordinal);
|
||||
var active=history.stream().filter(AppointmentWorkRequirementProvider::active).toList();
|
||||
// Unknown remaining quantity is still work, never a zero or an absent requirement.
|
||||
boolean remainder=resolved.stream().map(cargo::inspect).anyMatch(c->c.availablePackages()==null || c.availablePackages()>0);
|
||||
boolean remainder=resolved.stream().map(data.inspection).anyMatch(c->c.availablePackages()==null || c.availablePackages()>0);
|
||||
var replaced=history.stream().filter(a->!active(a)).map(FbaAppointmentResponse::batchKey).toList();
|
||||
if(remainder) {
|
||||
var restored=new LinkedHashSet<>(replaced);
|
||||
@@ -128,18 +188,6 @@ public class AppointmentWorkRequirementProvider implements AppointmentWorkFactGa
|
||||
result.put(batch,new Requirement(batch,batch,all.get(0).dispatchNo(),"CUSTOMER",requirement.originName(),requirement.destinationName(),requirement.timezone(),refs.stream().distinct().toList(),List.copyOf(replacements),all.stream().map(d->new SourceReference(d.customerId(),d.sourceKey(),d.destinationKey())).distinct().toList()));
|
||||
}
|
||||
}
|
||||
private List<AppointmentCoverageService.SourceRef> canonical(List<SourceReference> input) {
|
||||
if(input==null || input.isEmpty() || input.size()>100) throw unavailable("Firstleg需求缺少完整客户货源关系");
|
||||
var orders=input.stream().filter(s->s.sourceKey()!=null && s.sourceKey().startsWith("ORDER:")).map(s->{
|
||||
try { return new AppointmentExternalSourceGateway.OrderRef(s.customerId(),Long.parseLong(s.sourceKey().substring(6))); }
|
||||
catch(RuntimeException error) { throw unavailable("Firstleg原订单关联无效",error); }
|
||||
}).toList();
|
||||
var result=new ArrayList<AppointmentCoverageService.SourceRef>();
|
||||
if(!orders.isEmpty()) result.addAll(sources.canonicalOrders(orders));
|
||||
input.stream().filter(s->s.sourceKey()!=null && s.sourceKey().startsWith("BOOKING:")).map(s->new AppointmentCoverageService.SourceRef(s.customerId(),s.sourceKey(),s.destinationKey())).forEach(result::add);
|
||||
if(result.size()!=input.size()) throw unavailable("来源键必须明确为ORDER或BOOKING");
|
||||
return result.stream().distinct().toList();
|
||||
}
|
||||
/** 已取消且没有当前实物分配的记录只属历史;取消但仍有货物的批次继续产生需求。 */
|
||||
private static boolean active(FbaAppointmentResponse batch) {
|
||||
if(batch.replacementBatchKeys()!=null && !batch.replacementBatchKeys().isEmpty()) return false;
|
||||
|
||||
Reference in New Issue
Block a user