diff --git a/.gitignore b/.gitignore index dc4b3792c..f0ffe02b8 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ reports/ uploads/ .codex-temp* .codex-tmp* +.runtime-*/ .codex-*.out .codex-*.err diff --git a/common-event-contract/AGENTS.md b/common-event-contract/AGENTS.md index bf9e87321..6e1702ccc 100644 --- a/common-event-contract/AGENTS.md +++ b/common-event-contract/AGENTS.md @@ -17,6 +17,7 @@ | 头程装柜完成 V2(当前生产) | `FirstLegLoadingPlanCompletedV2.java`;订单级冻结 `destinationType=FBA|OVERSEAS`,FBA 可按箱数接收,OVERSEAS 逐明细冻结与实装箱数一致的 SKU/箱号;同时冻结来源/根订单与入仓标识、客户、业务类型、目的仓/备注/托盘及一键代发托盘 SKU 快照;国内生产者见 `../firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java`,海外消费者见 `../lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java` | | 头程装柜完成 V1 解析模型 | `FirstLegLoadingPlanCompletedV1.java`;用于消费端冻结字段映射,新增生产不得回退为 V1 | | 海外卸柜完成 V1 | `OverseasContainerUnloadedV1.java`;按来源柜预订 ID 做柜级聚合/幂等,同一柜只发布一次;`unloadingCompletedAt` 必须是全柜 complete 时刻,不得用签收拍照时刻代替;海外生产者见 `../lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundUnloadCompletedOutboxService.java`,国内消费者见 `../firstleg/src/main/java/com/baiyue/firstleg/service/OverseasContainerEventConsumer.java` | +| 海外入仓单暂扣变更 V1 | `OverseasInboundOrderDetainedChangedV1.java`;聚合键固定为来源头程入仓单 ID,暂扣与解除暂扣均由尾程 Outbox 回传,国内按入仓单绑定、客户归属及聚合版本单调消费 | | 国内船节点记录 V1 | `FirstLegContainerShippingNodeRecordedV1.java`;只接受十节点中带实际时间和完整供应商审计字段的事实,国内生产者 `../firstleg/src/main/java/com/baiyue/firstleg/portschedule/service/CarrierScheduleNodeOutboxService.java`,海外消费者 `../lastleg-service/src/main/java/com/baiyue/lastleg/shipping/service/ContainerShippingNodeConsumer.java` | | IAM 安全事件 V1 | `IamSecurityEventsV1.java`;账号、授权快照等安全事件的冻结模型 | | 契约说明 | `package-info.java` | diff --git a/common-event-contract/src/main/java/com/baiyue/common/event/EventTypes.java b/common-event-contract/src/main/java/com/baiyue/common/event/EventTypes.java index 029056ada..eac3a335a 100644 --- a/common-event-contract/src/main/java/com/baiyue/common/event/EventTypes.java +++ b/common-event-contract/src/main/java/com/baiyue/common/event/EventTypes.java @@ -13,6 +13,7 @@ public final class EventTypes { public static final String FIRST_LEG_CONTAINER_SHIPPING_NODE_RECORDED = "FirstLegContainerShippingNodeRecorded"; public static final String OVERSEAS_CONTAINER_UNLOADED = "OverseasContainerUnloaded"; + public static final String OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED = "OverseasInboundOrderDetainedChanged"; public static final String IAM_USER_STATUS_CHANGED = "IamUserStatusChanged"; public static final String IAM_TENANT_STATUS_CHANGED = "IamTenantStatusChanged"; public static final String IAM_PERMISSION_VERSION_CHANGED = "IamPermissionVersionChanged"; diff --git a/common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java b/common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java index b83991f26..34ae1509a 100644 --- a/common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java +++ b/common-event-contract/src/main/java/com/baiyue/common/event/FirstLegLoadingPlanCompletedV2.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.math.BigDecimal; import java.util.List; -/** 装柜完成交接快照,包含三类逐货物备注及一键代发托盘快照。 */ +/** 装柜完成交接快照,包含三类逐货物备注、推荐托数和可复用的头程托盘编号快照。 */ public record FirstLegLoadingPlanCompletedV2( String loadingPlanId, String planNo, @JsonProperty("sourceContainerBookingId") String containerBookingId, @@ -117,7 +117,7 @@ public record FirstLegLoadingPlanCompletedV2( } } - /** 一键代发入仓单的托盘冻结快照。 */ + /** 头程托盘冻结快照;普通入仓用于复用打印编号,一键代发还冻结 SKU/箱唛库存。 */ public record PalletSnapshot(String palletNo, List skuPackages) {} /** 托盘下 SKU、箱数、数量及箱号冻结快照;cartonMarks 中每个箱号都对应 skuCode。 */ diff --git a/common-event-contract/src/main/java/com/baiyue/common/event/OverseasInboundOrderDetainedChangedV1.java b/common-event-contract/src/main/java/com/baiyue/common/event/OverseasInboundOrderDetainedChangedV1.java new file mode 100644 index 000000000..88cf9681b --- /dev/null +++ b/common-event-contract/src/main/java/com/baiyue/common/event/OverseasInboundOrderDetainedChangedV1.java @@ -0,0 +1,17 @@ +package com.baiyue.common.event; + +import java.time.Instant; + +/** 海外尾程按来源入仓单回传的暂扣状态事实。 */ +public record OverseasInboundOrderDetainedChangedV1( + String sourceInboundId, + String inboundCode, + String lastlegOrderId, + String lastlegOrderNo, + boolean detained, + String reason, + String operatorId, + String operatorName, + Instant changedAt +) { +} diff --git a/config/nacos/local/README.md b/config/nacos/local/README.md index 3585527bb..ce6d3e445 100644 --- a/config/nacos/local/README.md +++ b/config/nacos/local/README.md @@ -10,8 +10,19 @@ powershell -ExecutionPolicy Bypass -File D:\baiyue-firstleg\scripts\publish-local-nacos-config.ps1 ``` +也可以使用统一入口;`local` 会被强制限制在 `public` namespace: + +```powershell +powershell -ExecutionPolicy Bypass -File D:\baiyue-firstleg\scripts\publish-nacos-config.ps1 -Environment local +``` + 配置发布成功后,各服务仍由自身的 Nacos Discovery 配置在启动时自动注册,不需要手工创建服务实例。 +本地海外链路也发布在同一 `public / BAIYUE` 作用域,包括 +`gateway-overseas.yml`、`lastleg-service.yml` 和 +`warehouse-overseas-service.yml`。这些配置默认通过 `127.0.0.1` 连接本机 +Auth、国内仓库、文件和海外服务;需要跨主机运行时使用对应环境变量覆盖。 + ## MyBatis SQL 日志 本地公共配置使用 MyBatis `Slf4jImpl`,数据库服务的 Mapper 日志级别默认为 `DEBUG`。执行数据库操作时,控制台会打印 SQL 模板、参数和影响行数。 diff --git a/config/nacos/local/event-bridge-service.yml b/config/nacos/local/event-bridge-service.yml index 2f3d5dd51..e70f98f7f 100644 --- a/config/nacos/local/event-bridge-service.yml +++ b/config/nacos/local/event-bridge-service.yml @@ -5,5 +5,7 @@ spring.datasource: password: ${EVENT_BRIDGE_DB_PASSWORD:123456} baiyue: region: ${REGION_CODE:CN} + event-bridge: + accept-any-region: ${EVENT_BRIDGE_ACCEPT_ANY_REGION:true} deployment-cell: ${DEPLOYMENT_CELL:local-cn} data-residency: ${DATA_RESIDENCY:CN} diff --git a/config/nacos/local/gateway-overseas.yml b/config/nacos/local/gateway-overseas.yml new file mode 100644 index 000000000..ea2ef8cb5 --- /dev/null +++ b/config/nacos/local/gateway-overseas.yml @@ -0,0 +1,16 @@ +springdoc: + api-docs.enabled: ${SWAGGER_ENABLED:true} + swagger-ui.enabled: ${SWAGGER_ENABLED:true} +baiyue: + security: + issuer: ${JWT_ISSUER:auth-service} + audience: ${JWT_AUDIENCE:lastleg-api} + jwk-set-uri: ${JWT_JWK_SET_URI:http://127.0.0.1:9100/api/auth/.well-known/jwks.json} + pda-session-validation: + auth-url: ${AUTH_SESSION_VALIDATION_URL:http://127.0.0.1:9100} + internal-service-token: ${INTERNAL_SERVICE_TOKEN:phase1-local-internal-token-at-least-32-bytes} + timeout: ${PDA_SESSION_VALIDATION_TIMEOUT:2s} + overseas-projection: + service-url: ${LASTLEG_SECURITY_SERVICE_URL:http://127.0.0.1:9303} + internal-service-token: ${INTERNAL_SERVICE_TOKEN:phase1-local-internal-token-at-least-32-bytes} + timeout: ${LASTLEG_SECURITY_VALIDATION_TIMEOUT:2s} diff --git a/config/nacos/local/lastleg-service.yml b/config/nacos/local/lastleg-service.yml new file mode 100644 index 000000000..259c7e72e --- /dev/null +++ b/config/nacos/local/lastleg-service.yml @@ -0,0 +1,29 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: ${LASTLEG_DB_URL:jdbc:mysql://127.0.0.1:3306/baiyue_lastleg?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true} + username: ${LASTLEG_DB_USERNAME:root} + password: ${LASTLEG_DB_PASSWORD:123456} +lastleg: + dispatch-query-api.enabled: ${LASTLEG_DISPATCH_QUERY_API_ENABLED:true} + smart-dispatch-api.enabled: ${LASTLEG_SMART_DISPATCH_API_ENABLED:true} +baiyue: + clients: + domestic-warehouse: + url: ${DOMESTIC_WAREHOUSE_URL:http://127.0.0.1:9205} + cache-ttl: ${DOMESTIC_WAREHOUSE_CACHE_TTL:30m} + file: + url: ${FILE_SERVICE_URL:http://127.0.0.1:9206} + event-bridge: + url: ${CN_EVENT_BRIDGE_URL:${FIRSTLEG_EVENT_BRIDGE_URL:http://127.0.0.1:9312}} + warehouse: + base-url: ${WAREHOUSE_OVERSEAS_SERVICE_URL:http://127.0.0.1:9305} + firstleg: + event-bridge-url: ${CN_EVENT_BRIDGE_URL:${FIRSTLEG_EVENT_BRIDGE_URL:http://127.0.0.1:9312}} + security: + internal-service-token: ${INTERNAL_SERVICE_TOKEN:phase1-local-internal-token-at-least-32-bytes} + jwt-mode: jwks + session-mode: claims + issuer: ${JWT_ISSUER:auth-service} + audience: ${JWT_AUDIENCE:lastleg-api} + jwk-set-uri: ${JWT_JWK_SET_URI:http://127.0.0.1:9100/api/auth/.well-known/jwks.json} diff --git a/config/nacos/local/warehouse-overseas-service.yml b/config/nacos/local/warehouse-overseas-service.yml new file mode 100644 index 000000000..685994b87 --- /dev/null +++ b/config/nacos/local/warehouse-overseas-service.yml @@ -0,0 +1,19 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: ${WAREHOUSE_DB_URL:jdbc:mysql://127.0.0.1:3306/baiyue_lastlag_warehouse?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true} + username: ${WAREHOUSE_DB_USERNAME:root} + password: ${WAREHOUSE_DB_PASSWORD:123456} +baiyue: + clients: + domestic-warehouse: + url: ${DOMESTIC_WAREHOUSE_URL:http://127.0.0.1:9205} + cache-ttl: ${DOMESTIC_WAREHOUSE_CACHE_TTL:30m} + internal-service: + token: ${INTERNAL_SERVICE_TOKEN:phase1-local-internal-token-at-least-32-bytes} + security: + jwt-mode: jwks + session-mode: claims + issuer: ${JWT_ISSUER:auth-service} + audience: ${JWT_AUDIENCE:lastleg-api} + jwk-set-uri: ${JWT_JWK_SET_URI:http://127.0.0.1:9100/api/auth/.well-known/jwks.json} diff --git a/config/nacos/overseas/README.md b/config/nacos/overseas/README.md index 95d89c508..71866504e 100644 --- a/config/nacos/overseas/README.md +++ b/config/nacos/overseas/README.md @@ -1,5 +1,19 @@ # 海外 Nacos 配置 -将本目录文件按原文件名作为 Data ID 发布到 `prod-overseas` 命名空间和 `BAIYUE` 分组。配置只引用环境变量,不保存密码或访问密钥。Gateway 必须同时启用 `overseas` Profile。 +本目录是海外远程部署模板,由 dev/development、test 和 production 共用。配置只引用环境变量,不保存密码或访问密钥;三个环境必须发布到各自独立的 Nacos namespace,Gateway 必须同时启用 `overseas` Profile。 海外入口与仓储 Data ID 分别为 `gateway-overseas.yml`、`warehouse-overseas-service.yml`。`print-service.yml` 对应头程仓库保留的共用打印服务,不包含尾程旧 Print 模块。 + +发布示例: + +```powershell +# 开发环境 +powershell -ExecutionPolicy Bypass -File D:\baiyue-firstleg\scripts\publish-nacos-config.ps1 ` + -Environment development -Region overseas -ServerAddr -Namespace + +# 测试环境 +powershell -ExecutionPolicy Bypass -File D:\baiyue-firstleg\scripts\publish-nacos-config.ps1 ` + -Environment test -Region overseas -ServerAddr -Namespace +``` + +远程环境禁止使用 `public` namespace。数据库、Redis、JWKS、内部服务令牌和跨区服务地址继续由对应 Jenkins Secret file 提供;不要把真实值写入本目录。 diff --git a/deploy/maven/settings-cn.xml b/deploy/maven/settings-cn.xml index d3516a762..332010ae1 100644 --- a/deploy/maven/settings-cn.xml +++ b/deploy/maven/settings-cn.xml @@ -4,54 +4,36 @@ xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd"> - baiyue-nexus-public - 柏悦 Nexus Maven 聚合仓库 - http://8.163.30.128:18081/repository/maven-public/ - external:* + aliyun-public + 阿里云 Maven 公共镜像 + https://maven.aliyun.com/repository/public + central - - - baiyue-nexus-public - ${env.NEXUS_USERNAME} - ${env.NEXUS_PASSWORD} - - - baiyue-nexus-releases - ${env.NEXUS_USERNAME} - ${env.NEXUS_PASSWORD} - - - baiyue-nexus-snapshots - ${env.NEXUS_USERNAME} - ${env.NEXUS_PASSWORD} - - - - 柏悦 Nexus + 国内公共镜像 - baiyue-nexus-public - http://8.163.30.128:18081/repository/maven-public/ + central + https://repo.maven.apache.org/maven2 true - true + false - baiyue-nexus-public - http://8.163.30.128:18081/repository/maven-public/ + central + https://repo.maven.apache.org/maven2 true - true + false - 柏悦 Nexus + 国内公共镜像 diff --git a/deploy/sql/头程数据库统一增量-MySQL8.sql b/deploy/sql/头程数据库统一增量-MySQL8.sql index a34799f81..a8af86c29 100644 --- a/deploy/sql/头程数据库统一增量-MySQL8.sql +++ b/deploy/sql/头程数据库统一增量-MySQL8.sql @@ -2502,6 +2502,7 @@ CREATE TABLE IF NOT EXISTS fl_inbound_order ( destination_name VARCHAR(128) NULL COMMENT '目的仓名称快照', hold_flag TINYINT(1) NOT NULL DEFAULT 0 COMMENT '当前是否扣货', current_hold_reason VARCHAR(500) NULL COMMENT '当前扣货理由快照', + last_overseas_hold_version BIGINT UNSIGNED NULL COMMENT '海外暂扣事件最新聚合版本,用于阻止乱序覆盖', total_package_count INT UNSIGNED NULL COMMENT '确认后的总箱数', actual_gross_weight_kg DECIMAL(18,3) NULL COMMENT '确认后的实际总毛重kg', chargeable_weight_kg DECIMAL(18,3) NULL COMMENT '当前费用快照的计费重量kg', @@ -2791,6 +2792,7 @@ CREATE TABLE IF NOT EXISTS fl_inbound_carton_label ( inbound_detail_id BIGINT UNSIGNED NOT NULL COMMENT '订单货物明细ID,不建立跨库外键', cargo_item_no INT UNSIGNED NOT NULL COMMENT '订单货物明细序号快照', cargo_name VARCHAR(255) NULL COMMENT '货物名称快照', + sku_code VARCHAR(128) NULL COMMENT '箱唛绑定的SKU快照;二维码内容不变', detail_package_count INT UNSIGNED NOT NULL COMMENT '该订单货物明细箱数快照', inbound_code VARCHAR(40) NOT NULL COMMENT '入仓码快照', box_code VARCHAR(80) NOT NULL COMMENT '固定二维码内容:入仓码#总箱数-箱下标', @@ -2828,6 +2830,7 @@ BEGIN END$$ DELIMITER ; CALL unified_carton_label_add_column('label_version', 'INT UNSIGNED NOT NULL DEFAULT 1 COMMENT ''内部兼容批次字段,固定为1且不进入箱码'' AFTER carton_index'); +CALL unified_carton_label_add_column('sku_code', 'VARCHAR(128) NULL COMMENT ''箱唛绑定的SKU快照;二维码内容不变'' AFTER cargo_name'); CALL unified_carton_label_add_column('batch_status', 'VARCHAR(16) NOT NULL DEFAULT ''ACTIVE'' COMMENT ''批次状态:ACTIVE有效、VOID已作废'' AFTER label_version'); CALL unified_carton_label_add_column('cargo_fingerprint', 'CHAR(64) NULL COMMENT ''影响标签字段的SHA-256指纹'' AFTER batch_status'); CALL unified_carton_label_add_column('voided_at', 'DATETIME(3) NULL COMMENT ''批次作废时间'' AFTER cargo_fingerprint'); @@ -5429,6 +5432,66 @@ CREATE TABLE IF NOT EXISTS fl_loading_pick_history ( CONSTRAINT fk_loading_pick_history_item FOREIGN KEY (pick_item_id) REFERENCES fl_loading_pick_item (id) ) ENGINE=InnoDB COMMENT='PDA拣货幂等和操作审计历史'; +CREATE TABLE IF NOT EXISTS fl_loading_work_pallet ( + id BIGINT UNSIGNED NOT NULL COMMENT '一件代发现场作业托盘ID', + pick_task_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA装柜拣货任务ID', + loading_plan_id BIGINT UNSIGNED NOT NULL COMMENT '装柜计划ID快照', + warehouse_id BIGINT UNSIGNED NOT NULL COMMENT '作业仓库ID', + warehouse_pallet_id BIGINT UNSIGNED NULL COMMENT '仓库实物托盘ID;未绑定仓库资源时为空', + pallet_code VARCHAR(64) NOT NULL COMMENT '现场托盘号', + open_operation_id VARCHAR(128) NOT NULL COMMENT '开托幂等操作号', + seal_operation_id VARCHAR(128) NULL COMMENT '封托幂等操作号', + pallet_status VARCHAR(16) NOT NULL DEFAULT 'OPEN' COMMENT '托盘状态:OPEN打托中、SEALED已封托、CANCELLED已取消', + created_by BIGINT UNSIGNED NOT NULL COMMENT '创建操作人ID', + created_by_name VARCHAR(64) NULL COMMENT '创建操作人名称快照', + sealed_by BIGINT UNSIGNED NULL COMMENT '封托操作人ID', + sealed_by_name VARCHAR(64) NULL COMMENT '封托操作人名称快照', + sealed_at DATETIME(3) NULL COMMENT '封托时间', + version INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '乐观锁版本号', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY (id), + UNIQUE KEY uk_loading_work_pallet_code (pick_task_id, pallet_code), + UNIQUE KEY uk_loading_work_pallet_open_operation (open_operation_id), + UNIQUE KEY uk_loading_work_pallet_seal_operation (seal_operation_id), + KEY idx_loading_work_pallet_plan (loading_plan_id, pallet_status, id), + CONSTRAINT fk_loading_work_pallet_task FOREIGN KEY (pick_task_id) REFERENCES fl_loading_pick_task (id), + CONSTRAINT fk_loading_work_pallet_plan FOREIGN KEY (loading_plan_id) REFERENCES fl_loading_plan (id), + CONSTRAINT chk_loading_work_pallet_status CHECK (pallet_status IN ('OPEN','SEALED','CANCELLED')) +) ENGINE=InnoDB COMMENT='一件代发PDA现场实际托盘;允许同托盘承载多个客户订单'; + +CREATE TABLE IF NOT EXISTS fl_loading_work_pallet_carton ( + id BIGINT UNSIGNED NOT NULL COMMENT '一件代发现场托盘箱唛绑定ID', + pick_task_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA装柜拣货任务ID', + work_pallet_id BIGINT UNSIGNED NOT NULL COMMENT '现场作业托盘ID', + loading_plan_item_id BIGINT UNSIGNED NOT NULL COMMENT '装柜计划货物明细ID', + pick_item_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA拣货明细ID', + tenant_id BIGINT UNSIGNED NOT NULL COMMENT '箱唛所属客户ID', + inbound_id BIGINT UNSIGNED NOT NULL COMMENT '来源入仓单ID', + inbound_detail_id BIGINT UNSIGNED NOT NULL COMMENT '来源入仓货物明细ID', + carton_label_id BIGINT UNSIGNED NOT NULL COMMENT '稳定箱唛记录ID', + box_code VARCHAR(80) NOT NULL COMMENT '稳定箱唛箱码快照', + sku_code VARCHAR(128) NOT NULL COMMENT 'SKU编码快照', + units_per_carton INT UNSIGNED NULL COMMENT '每箱产品数量快照', + bound_by BIGINT UNSIGNED NOT NULL COMMENT '最后绑定操作人ID', + bound_by_name VARCHAR(64) NULL COMMENT '最后绑定操作人名称快照', + operation_id VARCHAR(128) NOT NULL COMMENT '最近一次绑定或移托幂等操作号', + version INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '乐观锁版本号', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '首次上托时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '最后移托时间', + PRIMARY KEY (id), + UNIQUE KEY uk_loading_work_carton_task_label (pick_task_id, carton_label_id), + UNIQUE KEY uk_loading_work_carton_operation (operation_id), + KEY idx_loading_work_carton_pallet (work_pallet_id, sku_code, id), + KEY idx_loading_work_carton_plan_item (loading_plan_item_id, id), + CONSTRAINT fk_loading_work_carton_task FOREIGN KEY (pick_task_id) REFERENCES fl_loading_pick_task (id), + CONSTRAINT fk_loading_work_carton_pallet FOREIGN KEY (work_pallet_id) REFERENCES fl_loading_work_pallet (id), + CONSTRAINT fk_loading_work_carton_plan_item FOREIGN KEY (loading_plan_item_id) REFERENCES fl_loading_plan_item (id), + CONSTRAINT fk_loading_work_carton_pick_item FOREIGN KEY (pick_item_id) REFERENCES fl_loading_pick_item (id), + CONSTRAINT fk_loading_work_carton_label FOREIGN KEY (carton_label_id) REFERENCES fl_inbound_carton_label (id), + CONSTRAINT chk_loading_work_carton_units CHECK (units_per_carton IS NULL OR units_per_carton > 0) +) ENGINE=InnoDB COMMENT='一件代发PDA逐箱上托事实;客户订单不合并但实际托盘可混托'; + DROP PROCEDURE IF EXISTS repair_phase3_loading_pick_task_foreign_keys; DELIMITER $$ CREATE PROCEDURE repair_phase3_loading_pick_task_foreign_keys() @@ -13196,6 +13259,7 @@ CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'created_at', ' CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'created_by', '创建人ID'); CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'created_by_name', '创建人名称快照'); CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'current_hold_reason', '当前扣货理由快照'); +CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'last_overseas_hold_version', '海外暂扣事件最新聚合版本,用于阻止乱序覆盖'); CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'customer_id', '客户逻辑ID'); CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'customer_name', '客户名称快照'); CALL queue_column_comment('baiyue_firstleg', 'fl_inbound_order', 'customs_inspection', '是否商检'); @@ -16011,12 +16075,35 @@ DROP PROCEDURE IF EXISTS add_container_empty_report_fields; CREATE TABLE IF NOT EXISTS fl_container_event_inbox ( event_id VARCHAR(64) NOT NULL COMMENT '跨区事件全局唯一ID', event_type VARCHAR(64) NOT NULL COMMENT '事件类型', - event_version INT UNSIGNED NOT NULL COMMENT '事件契约版本', aggregate_id VARCHAR(64) NOT NULL COMMENT '货柜聚合ID', - aggregate_version BIGINT UNSIGNED NOT NULL COMMENT '货柜聚合版本', payload_hash VARCHAR(80) NOT NULL COMMENT '规范化负载SHA-256摘要', + event_version INT UNSIGNED NOT NULL COMMENT '事件契约版本', aggregate_id VARCHAR(64) NOT NULL COMMENT '跨区业务聚合ID', + aggregate_version BIGINT UNSIGNED NOT NULL COMMENT '跨区业务聚合版本', payload_hash VARCHAR(80) NOT NULL COMMENT '规范化负载SHA-256摘要', payload_json JSON NOT NULL COMMENT '原始事件负载', status VARCHAR(16) NOT NULL COMMENT 'RECEIVED或PROCESSED', received_at DATETIME(3) NOT NULL COMMENT '接收时间(UTC)', processed_at DATETIME(3) NULL COMMENT '处理完成时间(UTC)', PRIMARY KEY (event_id), KEY idx_container_event_aggregate (aggregate_id,aggregate_version) -) ENGINE=InnoDB COMMENT='海外货柜事件消费Inbox'; +) ENGINE=InnoDB COMMENT='海外到国内业务事件消费Inbox'; + +DROP PROCEDURE IF EXISTS add_inbound_overseas_hold_version; +DELIMITER $$ +CREATE PROCEDURE add_inbound_overseas_hold_version() +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema=DATABASE() AND table_name='fl_inbound_order' + AND column_name='last_overseas_hold_version' + ) THEN + ALTER TABLE fl_inbound_order + ADD COLUMN last_overseas_hold_version BIGINT UNSIGNED NULL + COMMENT '海外暂扣事件最新聚合版本,用于阻止乱序覆盖' + AFTER current_hold_reason; + END IF; + ALTER TABLE fl_container_event_inbox + MODIFY COLUMN aggregate_id VARCHAR(64) NOT NULL COMMENT '跨区业务聚合ID', + MODIFY COLUMN aggregate_version BIGINT UNSIGNED NOT NULL COMMENT '跨区业务聚合版本', + COMMENT='海外到国内业务事件消费Inbox'; +END$$ +DELIMITER ; +CALL add_inbound_overseas_hold_version(); +DROP PROCEDURE IF EXISTS add_inbound_overseas_hold_version; -- 标准跨区事件摘要包含 sha256: 前缀,共 71 字符;兼容既有 Finance 64 位摘要。 ALTER TABLE fl_inbound_outbox @@ -16063,6 +16150,9 @@ VALUES (5107061,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:create','创建海外库位',NULL,'POST',761,'ACTIVE',1,0), (5107062,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:edit','编辑海外库位',NULL,'PUT',762,'ACTIVE',1,0), (5107063,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:delete','删除海外库位',NULL,'DELETE',763,'ACTIVE',1,0), + (5107090,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:outbound:list','海外出库单','/overseas-warehouse/outbounds','GET',765,'ACTIVE',1,0), + (5107091,0,'PLATFORM','BUTTON',5107090,'platform:overseas-warehouse:outbound:create','创建海外出库单',NULL,'POST',766,'ACTIVE',1,0), + (5107092,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:delivery:list','海外派送单','/overseas-warehouse/delivery-orders','GET',767,'ACTIVE',1,0), (5107100,0,'PLATFORM','MENU',0,'platform:overseas-business:purchase:list','海外采购订单','/overseas-business/purchases','GET',800,'ACTIVE',1,0), (5107101,0,'PLATFORM','BUTTON',5107100,'platform:overseas-business:purchase:edit','编辑海外采购订单',NULL,'PATCH',801,'ACTIVE',1,0), (5107102,0,'PLATFORM','BUTTON',5107100,'platform:overseas-business:purchase:review','审核海外采购订单',NULL,'POST',802,'ACTIVE',1,0), @@ -16237,6 +16327,152 @@ WHERE role.tenant_id=0 AND role.app_code='PLATFORM' AND UPPER(role.role_code) IN ('PLATFORM_ADMIN','PLATFORM_CUSTOM_ADMIN') AND role.status='ACTIVE' AND role.deleted=0 AND permission.status='ACTIVE' AND permission.deleted=0; +-- PLATFORM_CUSTOM_ADMIN needs access to the overseas outbound and delivery lists shown in the admin navigation. +INSERT IGNORE INTO baiyue_iam.iam_role_permission (tenant_id, role_id, permission_id, created_at) +SELECT 0, role.id, permission.id, NOW(3) +FROM baiyue_iam.iam_role role +JOIN baiyue_iam.iam_permission permission ON permission.tenant_id=0 AND permission.app_code='PLATFORM' + AND permission.permission_code IN ( + 'platform:overseas-warehouse:outbound:list', + 'platform:overseas-warehouse:delivery:list' + ) +WHERE role.tenant_id=0 AND role.app_code='PLATFORM' + AND UPPER(role.role_code)='PLATFORM_CUSTOM_ADMIN' + AND role.status='ACTIVE' AND role.deleted=0 AND permission.status='ACTIVE' AND permission.deleted=0; + +-- 海外智能排车草稿锁:拖入即锁定,移除或关闭草稿即释放。 +CREATE DATABASE IF NOT EXISTS baiyue_lastleg + DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; +USE baiyue_lastleg; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='source_warehouse_id'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN source_warehouse_id VARCHAR(128) NULL COMMENT ''来源国内装柜仓 ID 快照'' AFTER customer_name')); +PREPARE lastleg_order_source_warehouse_stmt FROM @ddl; EXECUTE lastleg_order_source_warehouse_stmt; DEALLOCATE PREPARE lastleg_order_source_warehouse_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='source_warehouse_name'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN source_warehouse_name VARCHAR(255) NULL COMMENT ''来源国内装柜仓名称快照'' AFTER source_warehouse_id')); +PREPARE lastleg_order_source_warehouse_stmt FROM @ddl; EXECUTE lastleg_order_source_warehouse_stmt; DEALLOCATE PREPARE lastleg_order_source_warehouse_stmt; +CREATE TABLE IF NOT EXISTS ll_dispatch_pallet_lock ( + id BIGINT NOT NULL COMMENT '主键ID', + customer_id VARCHAR(128) NOT NULL COMMENT '客户租户ID', + source_pallet_key VARCHAR(160) NOT NULL COMMENT '来源托盘业务键', + active_key VARCHAR(160) NULL COMMENT '生效中的资源排他键', + plan_id BIGINT NULL COMMENT '入仓计划ID', + plan_pallet_id BIGINT NULL COMMENT '计划托盘ID', + warehouse_pallet_id BIGINT NULL COMMENT '仓库实物托盘ID', + dispatch_order_id BIGINT NULL COMMENT '正式派车单ID,草稿锁阶段为空', + draft_token VARCHAR(64) NULL COMMENT '智能排车草稿随机标识', + operator_id VARCHAR(128) NULL COMMENT '草稿锁操作人ID', + status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE' COMMENT '业务状态', + locked_at DATETIME(3) NOT NULL COMMENT '锁定时间', + expires_at DATETIME(3) NULL COMMENT '草稿锁失效时间,正式派车锁为空', + released_at DATETIME(3) NULL COMMENT '释放时间', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY (id), + UNIQUE KEY uk_ll_dispatch_active_pallet (customer_id,active_key), + KEY idx_ll_dispatch_pallet_lock_order (customer_id,dispatch_order_id,status), + KEY idx_ll_dispatch_pallet_lock_source (customer_id,source_pallet_key,status) +) ENGINE=InnoDB COMMENT='派车托盘排他锁'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_dispatch_pallet_lock' AND column_name='draft_token'), 'SELECT 1', 'ALTER TABLE ll_dispatch_pallet_lock ADD COLUMN draft_token VARCHAR(64) NULL COMMENT ''智能排车草稿随机标识'' AFTER dispatch_order_id')); +PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_dispatch_pallet_lock' AND column_name='operator_id'), 'SELECT 1', 'ALTER TABLE ll_dispatch_pallet_lock ADD COLUMN operator_id VARCHAR(128) NULL COMMENT ''草稿锁操作人ID'' AFTER draft_token')); +PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_dispatch_pallet_lock' AND column_name='expires_at'), 'SELECT 1', 'ALTER TABLE ll_dispatch_pallet_lock ADD COLUMN expires_at DATETIME(3) NULL COMMENT ''草稿锁失效时间,正式派车锁为空'' AFTER locked_at')); +PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_dispatch_pallet_lock' AND column_name='dispatch_order_id' AND is_nullable='YES'), 'SELECT 1', 'ALTER TABLE ll_dispatch_pallet_lock MODIFY COLUMN dispatch_order_id BIGINT NULL COMMENT ''正式派车单ID,草稿锁阶段为空''')); +PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +-- 尾程库存余额按客户入仓单与海外目的仓汇总箱数,SKU仅保留为快照。 +SET @ddl = (SELECT CASE + WHEN EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema=DATABASE() + AND table_name='ll_inventory_balance' AND index_name='uk_ll_inventory_dimension' AND column_name='sku_code') + THEN 'ALTER TABLE ll_inventory_balance DROP INDEX uk_ll_inventory_dimension, ADD UNIQUE KEY uk_ll_inventory_dimension (customer_id,overseas_warehouse_id,destination_warehouse_code,inbound_order_id,inbound_code)' + WHEN NOT EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema=DATABASE() + AND table_name='ll_inventory_balance' AND index_name='uk_ll_inventory_dimension') + THEN 'ALTER TABLE ll_inventory_balance ADD UNIQUE KEY uk_ll_inventory_dimension (customer_id,overseas_warehouse_id,destination_warehouse_code,inbound_order_id,inbound_code)' + ELSE 'SELECT 1' END); +PREPARE lastleg_inventory_dimension_stmt FROM @ddl; EXECUTE lastleg_inventory_dimension_stmt; DEALLOCATE PREPARE lastleg_inventory_dimension_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() + AND table_name='ll_inventory_balance' AND column_name='sku_code'), + 'ALTER TABLE ll_inventory_balance MODIFY COLUMN sku_code VARCHAR(128) NOT NULL DEFAULT ''UNSPECIFIED'' COMMENT ''SKU快照;多SKU汇总时为MIXED,不参与当前库存唯一维度''', + 'SELECT 1')); +PREPARE lastleg_inventory_dimension_stmt FROM @ddl; EXECUTE lastleg_inventory_dimension_stmt; DEALLOCATE PREPARE lastleg_inventory_dimension_stmt; +ALTER TABLE ll_inventory_balance COMMENT='客户入仓单与海外目的仓维度实时库存余额'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() + AND table_name='ll_local_looseboard_order' AND column_name='lastleg_order_id'), 'SELECT 1', + 'ALTER TABLE ll_local_looseboard_order ADD COLUMN lastleg_order_id BIGINT NULL COMMENT ''关联尾程主订单ID'' AFTER customer_id')); +PREPARE lastleg_looseboard_tail_stmt FROM @ddl; EXECUTE lastleg_looseboard_tail_stmt; DEALLOCATE PREPARE lastleg_looseboard_tail_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema=DATABASE() + AND table_name='ll_local_looseboard_order' AND index_name='uk_ll_local_looseboard_lastleg'), 'SELECT 1', + 'ALTER TABLE ll_local_looseboard_order ADD UNIQUE KEY uk_ll_local_looseboard_lastleg(lastleg_order_id)')); +PREPARE lastleg_looseboard_tail_stmt FROM @ddl; EXECUTE lastleg_looseboard_tail_stmt; DEALLOCATE PREPARE lastleg_looseboard_tail_stmt; + +-- 派送单列表与详情快照、转派送费用及操作记录(当前上下文为尾程库)。 +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='overseas_warehouse_name'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN overseas_warehouse_name VARCHAR(255) NULL COMMENT ''海外仓名称快照'', ADD COLUMN customer_name VARCHAR(255) NULL COMMENT ''客户名称快照'', ADD COLUMN waybill_no VARCHAR(128) NULL COMMENT ''承运运单号'', ADD COLUMN delivery_method VARCHAR(32) NULL COMMENT ''派送方式'', ADD COLUMN carrier_name VARCHAR(255) NULL COMMENT ''派送承运商'', ADD COLUMN pickup_method VARCHAR(64) NULL COMMENT ''提货方式'', ADD COLUMN loading_type VARCHAR(64) NULL COMMENT ''装车类型'', ADD COLUMN vehicle_type VARCHAR(64) NULL COMMENT ''车辆类型'', ADD COLUMN vehicle_size VARCHAR(64) NULL COMMENT ''车辆大小'', ADD COLUMN vehicle_plate VARCHAR(64) NULL COMMENT ''车牌号'', ADD COLUMN driver_name VARCHAR(128) NULL COMMENT ''司机或车队名称'', ADD COLUMN driver_phone VARCHAR(64) NULL COMMENT ''车队联系电话'', ADD COLUMN appointment_account VARCHAR(128) NULL COMMENT ''预约账号'', ADD COLUMN seller_name VARCHAR(128) NULL COMMENT ''卖家姓名'', ADD COLUMN total_weight_kg DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总重量(千克)'', ADD COLUMN total_volume_cbm DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总体积(立方米)'', ADD COLUMN planned_delivery_at DATETIME(3) NULL COMMENT ''预计派送时间'', ADD COLUMN dispatched_at DATETIME(3) NULL COMMENT ''实际派送时间'', ADD COLUMN expected_arrival_at DATETIME(3) NULL COMMENT ''预计到达时间'', ADD COLUMN signed_at DATETIME(3) NULL COMMENT ''签收时间'', ADD COLUMN remark VARCHAR(1000) NULL COMMENT ''派送备注''')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +CREATE TABLE IF NOT EXISTS ll_delivery_fee (id BIGINT NOT NULL COMMENT '派送费用主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', fee_direction VARCHAR(16) NOT NULL DEFAULT 'PAYABLE' COMMENT '应收应付方向', fee_type VARCHAR(64) NOT NULL COMMENT '费用类型', currency VARCHAR(8) NOT NULL COMMENT '币种', amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', remark VARCHAR(500) NULL COMMENT '费用备注', created_by VARCHAR(128) NULL COMMENT '创建人ID', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', PRIMARY KEY(id), KEY idx_ll_delivery_fee_order(customer_id,delivery_order_id,id)) ENGINE=InnoDB COMMENT='派送单费用明细'; +CREATE TABLE IF NOT EXISTS ll_delivery_operation (id BIGINT NOT NULL COMMENT '派送操作主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', operator_id VARCHAR(128) NULL COMMENT '操作人ID', operator_name VARCHAR(128) NULL COMMENT '操作人名称', result VARCHAR(500) NOT NULL COMMENT '处理结果', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', PRIMARY KEY(id), KEY idx_ll_delivery_operation_order(customer_id,delivery_order_id,created_at,id)) ENGINE=InnoDB COMMENT='派送单操作记录'; + +CREATE TABLE IF NOT EXISTS ll_exception_order ( + id BIGINT NOT NULL COMMENT '异常订单ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', exception_no VARCHAR(64) NOT NULL COMMENT '异常订单号', + exception_type VARCHAR(32) NOT NULL COMMENT '异常类型', status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '异常处理状态', reason VARCHAR(1000) NOT NULL COMMENT '异常原因', + last_mile_order_id BIGINT NULL COMMENT '关联尾程订单ID', last_mile_order_no VARCHAR(64) NULL COMMENT '关联尾程订单号', inbound_order_id BIGINT NULL COMMENT '关联入仓单ID', inbound_code VARCHAR(128) NULL COMMENT '关联入仓码', + delivery_order_id BIGINT NULL COMMENT '关联派送单ID', delivery_no VARCHAR(64) NULL COMMENT '关联派送单号', outbound_no VARCHAR(64) NULL COMMENT '关联出库单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', overseas_warehouse_id VARCHAR(128) NULL COMMENT '海外仓ID快照', overseas_warehouse_name VARCHAR(255) NULL COMMENT '海外仓名称快照', + sku_code VARCHAR(128) NULL COMMENT '异常货物SKU', package_count INT NULL COMMENT '异常箱数', return_direction VARCHAR(32) NULL COMMENT '退回方向', return_logistics_no VARCHAR(128) NULL COMMENT '退回物流单号', + handling_action VARCHAR(500) NULL COMMENT '异常处理措施', handler_id VARCHAR(128) NULL COMMENT '处理人ID', handler_name VARCHAR(128) NULL COMMENT '处理人名称', + source_type VARCHAR(32) NOT NULL DEFAULT 'MANUAL' COMMENT '异常来源类型', created_by VARCHAR(128) NULL COMMENT '创建人ID', + exception_at DATETIME(3) NOT NULL COMMENT '异常发生时间', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_exception_no(customer_id,exception_no), + UNIQUE KEY uk_ll_exception_delivery_return(customer_id,delivery_order_id,exception_type), + KEY idx_ll_exception_list(customer_id,status,exception_at), KEY idx_ll_exception_tail(customer_id,last_mile_order_no), + KEY idx_ll_exception_inbound(customer_id,inbound_code) +) ENGINE=InnoDB COMMENT='海外仓异常订单'; + +CREATE TABLE IF NOT EXISTS ll_customer_visit ( + id BIGINT NOT NULL COMMENT '客户回访记录ID', + customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + delivery_order_id BIGINT NOT NULL COMMENT '关联派送单ID', + delivery_no VARCHAR(64) NOT NULL COMMENT '关联派送单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', + contact_name VARCHAR(128) NULL COMMENT '客户联系人姓名快照', + contact_phone VARCHAR(64) NULL COMMENT '客户联系人电话快照', + destination VARCHAR(500) NULL COMMENT '派送目的仓编码汇总快照', + delivery_completed_at DATETIME(3) NOT NULL COMMENT '派送完成时间', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '回访状态:PENDING待回访或COMPLETED已回访', + selected_last_mile_order_id BIGINT NULL COMMENT '本次回访选择的尾程订单ID', + selected_last_mile_order_no VARCHAR(64) NULL COMMENT '本次回访选择的尾程订单号', + selected_inbound_order_id BIGINT NULL COMMENT '本次回访选择的入仓单ID', + selected_inbound_code VARCHAR(128) NULL COMMENT '本次回访选择的入仓码', + visit_result VARCHAR(32) NULL COMMENT '最近一次回访结果', + customer_feedback VARCHAR(2000) NULL COMMENT '最近一次客户反馈', + remark VARCHAR(1000) NULL COMMENT '最近一次回访备注', + attachment_json JSON NULL COMMENT '当前回访资料文件元数据列表', + visited_by VARCHAR(128) NULL COMMENT '最近一次回访操作人ID', + visited_at DATETIME(3) NULL COMMENT '最近一次回访时间', + visit_count INT NOT NULL DEFAULT 0 COMMENT '累计保存回访次数', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), + UNIQUE KEY uk_ll_customer_visit_delivery(customer_id,delivery_order_id), + KEY idx_ll_customer_visit_list(customer_id,status,delivery_completed_at), + KEY idx_ll_customer_visit_delivery_no(customer_id,delivery_no) +) ENGINE=InnoDB COMMENT='派送单维度客户回访记录'; + + +INSERT INTO baiyue_iam.iam_permission + (id,tenant_id,app_code,permission_type,parent_id,permission_code,permission_name,route_path,http_method,sort_no,status,built_in,deleted) +VALUES + (5107070,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:exception-order:list','海外异常订单','/overseas-warehouse/exception-orders','GET',770,'ACTIVE',1,0), + (5107071,0,'PLATFORM','BUTTON',5107070,'platform:overseas-warehouse:exception-order:create','新增海外异常订单',NULL,'POST',771,'ACTIVE',1,0), + (5107072,0,'PLATFORM','BUTTON',5107070,'platform:overseas-warehouse:delivery:return','派送单标记退货',NULL,'POST',772,'ACTIVE',1,0) + ,(5107080,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:customer-visit:list','客户回访','/overseas-warehouse/customer-visits','GET',780,'ACTIVE',1,0) + ,(5107081,0,'PLATFORM','BUTTON',5107080,'platform:overseas-warehouse:customer-visit:edit','保存客户回访',NULL,'PUT',781,'ACTIVE',1,0) + ,(5107082,0,'PLATFORM','BUTTON',5107080,'platform:overseas-warehouse:delivery:status','编辑派送单状态',NULL,'PUT',782,'ACTIVE',1,0) +ON DUPLICATE KEY UPDATE permission_name=VALUES(permission_name),route_path=VALUES(route_path),http_method=VALUES(http_method),sort_no=VALUES(sort_no),status='ACTIVE',built_in=1,deleted=0; +INSERT IGNORE INTO baiyue_iam.iam_role_permission (tenant_id,role_id,permission_id,created_at) +SELECT 0,role.id,permission.id,NOW(3) FROM baiyue_iam.iam_role role +JOIN baiyue_iam.iam_permission permission ON permission.id BETWEEN 5107070 AND 5107082 AND permission.tenant_id=0 +WHERE role.tenant_id=0 AND role.app_code='PLATFORM' AND role.role_code='PLATFORM_ADMIN' AND role.status='ACTIVE' AND role.deleted=0; + -- IAM 客户提交即成功,默认尺寸通过持久化任务后台初始化;已有客户按历史事实标记 READY。 USE baiyue_iam; SET @customer_dimension_status_ddl = (SELECT IF( @@ -16269,6 +16505,16 @@ CREATE TABLE IF NOT EXISTS iam_customer_dimension_initialization_task ( KEY idx_customer_dimension_task_diagnosis (status,updated_at) ) ENGINE=InnoDB COMMENT='IAM客户默认尺寸可靠后台初始化任务及诊断状态'; +-- 兼容已存在的旧表:CREATE TABLE IF NOT EXISTS 不会为旧表补充新字段。 +SET @customer_dimension_lease_ddl = (SELECT IF( + EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() + AND table_name='iam_customer_dimension_initialization_task' AND column_name='lease_until'), + 'SELECT 1', + 'ALTER TABLE iam_customer_dimension_initialization_task ADD COLUMN lease_until DATETIME(3) NULL COMMENT ''当前任务租约截止时间'' AFTER lease_owner')); +PREPARE customer_dimension_lease_stmt FROM @customer_dimension_lease_ddl; +EXECUTE customer_dimension_lease_stmt; +DEALLOCATE PREPARE customer_dimension_lease_stmt; + USE baiyue_firstleg; ALTER TABLE fl_loading_pick_task MODIFY COLUMN task_status VARCHAR(24) NOT NULL DEFAULT 'LOADING' COMMENT 'PDA任务状态:LOADING、COMPLETING、COMPLETION_FAILED、COMPLETED'; diff --git a/deploy/sql/海外区域统一增量-MySQL8.sql b/deploy/sql/海外区域统一增量-MySQL8.sql index 5f9812428..4483acc53 100644 --- a/deploy/sql/海外区域统一增量-MySQL8.sql +++ b/deploy/sql/海外区域统一增量-MySQL8.sql @@ -195,7 +195,7 @@ CREATE TABLE IF NOT EXISTS ll_overseas_procurement_order ( purchase_quantity BIGINT NOT NULL COMMENT '采购数量', budget_unit_price DECIMAL(18,2) NULL COMMENT '预算单价', currency CHAR(3) NOT NULL DEFAULT 'USD' COMMENT '币种', receiving_address VARCHAR(1000) NULL COMMENT '收货地址', contact_name VARCHAR(128) NOT NULL COMMENT '联系人姓名', contact_phone VARCHAR(64) NOT NULL COMMENT '联系人电话', - purchase_warehouse_id VARCHAR(128) NULL COMMENT '采购入仓仓库ID', purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', + purchase_warehouse_id VARCHAR(128) NULL COMMENT '采购入仓仓库ID', purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', outbound_details JSON NULL COMMENT '出库明细', inbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '采购单据累计入库数量', domestic_carrier VARCHAR(128) NULL COMMENT '境内承运商', domestic_tracking_no VARCHAR(128) NULL COMMENT '境内物流单号', overseas_carrier VARCHAR(128) NULL COMMENT '海外承运商', overseas_tracking_no VARCHAR(128) NULL COMMENT '海外物流单号', estimated_arrival_at DATETIME(3) NULL COMMENT '预计到达时间', @@ -211,6 +211,8 @@ CREATE TABLE IF NOT EXISTS ll_overseas_procurement_order ( KEY idx_ll_overseas_procurement_list(customer_id,status,created_at), KEY idx_ll_overseas_procurement_warehouse(customer_id,purchase_warehouse_id,created_at) ) ENGINE=InnoDB COMMENT='海外采购订单'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_overseas_procurement_order' AND column_name='outbound_details'), 'SELECT 1', 'ALTER TABLE ll_overseas_procurement_order ADD COLUMN outbound_details JSON NULL COMMENT ''出库明细'' AFTER inbound_quantity')); +PREPARE procurement_outbound_stmt FROM @ddl; EXECUTE procurement_outbound_stmt; DEALLOCATE PREPARE procurement_outbound_stmt; SET @ddl = (SELECT IF(EXISTS ( SELECT 1 FROM information_schema.columns @@ -1073,7 +1075,57 @@ DELIMITER ; CALL assert_phase4_pda_operations_ready(); DROP PROCEDURE assert_phase4_pda_operations_ready; +CREATE TABLE IF NOT EXISTS ll_order_fee ( + id BIGINT NOT NULL COMMENT '费用明细主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', fee_item VARCHAR(128) NOT NULL COMMENT '费用项目', + currency VARCHAR(3) NOT NULL COMMENT '币种代码', amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING_REVIEW' COMMENT '费用状态:PENDING_REVIEW待审核、APPROVED审核通过、REJECTED已驳回', + supplier_description VARCHAR(500) NULL COMMENT '供应商或费用说明', created_by VARCHAR(128) NOT NULL COMMENT '创建操作人', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), KEY idx_ll_order_fee_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单费用明细'; + +CREATE TABLE IF NOT EXISTS ll_order_operation_log ( + id BIGINT NOT NULL COMMENT '操作记录主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', + operator_name VARCHAR(128) NOT NULL COMMENT '操作人名称', result VARCHAR(500) NOT NULL COMMENT '处理结果', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', + PRIMARY KEY(id), KEY idx_ll_order_operation_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单操作记录'; + +CREATE TABLE IF NOT EXISTS ll_outbound_pda_task ( + id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', outbound_order_id BIGINT NOT NULL COMMENT '出库单ID', + task_no VARCHAR(64) NOT NULL COMMENT 'PDA任务号', status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态', + pushed_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '推送时间', completed_at DATETIME(3) NULL COMMENT '完成时间', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_outbound_pda_task_order(customer_id,outbound_order_id), UNIQUE KEY uk_ll_outbound_pda_task_no(customer_id,task_no) +) ENGINE=InnoDB COMMENT='一件代发出库PDA拣货任务'; + +CREATE TABLE IF NOT EXISTS ll_outbound_pda_pick ( + id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', task_id BIGINT NOT NULL COMMENT 'PDA任务ID', + outbound_pallet_item_id BIGINT NOT NULL COMMENT '出库托盘明细ID', inventory_position_id BIGINT NOT NULL COMMENT '库存仓位ID', packages BIGINT NOT NULL COMMENT '拣货箱数', + idempotency_key VARCHAR(255) NOT NULL COMMENT '拣货幂等键', picked_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '拣货时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_outbound_pda_pick_key(customer_id,idempotency_key) +) ENGINE=InnoDB COMMENT='一件代发出库PDA托盘拣货记录'; + USE baiyue_lastleg; +SET @ddl = (SELECT IF(EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema=DATABASE() AND table_name='ll_inbound_plan' AND column_name='version' +), 'SELECT 1', 'ALTER TABLE ll_inbound_plan ADD COLUMN version INT NOT NULL DEFAULT 0 COMMENT ''入库计划乐观锁版本'' AFTER updated_at')); +PREPARE ll_inbound_plan_version_ddl FROM @ddl; +EXECUTE ll_inbound_plan_version_ddl; +DEALLOCATE PREPARE ll_inbound_plan_version_ddl; + +SET @ddl = (SELECT IF(EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='pallet_merge_batch_id' +), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN pallet_merge_batch_id VARCHAR(64) NULL COMMENT ''平台预标记合托分组号'' AFTER pallet_merge_flag')); +PREPARE ll_order_merge_batch_ddl FROM @ddl; +EXECUTE ll_order_merge_batch_ddl; +DEALLOCATE PREPARE ll_order_merge_batch_ddl; + CREATE TABLE IF NOT EXISTS ll_security_event_inbox ( event_id VARCHAR(64) NOT NULL COMMENT '跨区安全事件ID', event_type VARCHAR(96) NOT NULL COMMENT 'IAM安全事件类型', payload_hash CHAR(64) NOT NULL COMMENT '规范化载荷SHA-256哈希', payload_json JSON NOT NULL COMMENT '冻结事件载荷JSON', diff --git a/docs/数据库环境切换说明.md b/docs/数据库环境切换说明.md index 4a985c6f7..7828dadcf 100644 --- a/docs/数据库环境切换说明.md +++ b/docs/数据库环境切换说明.md @@ -1,66 +1,62 @@ -# 数据库环境切换说明 +# Nacos 环境切换说明 -## 环境定义 +数据库、Redis 和服务地址均由所选环境的 Nacos 配置决定。本地启动时只切换 +Nacos 地址与 namespace,不再通过 `application-test.yml` 单独切换数据库。 -- `dev`:本地开发环境,连接 `127.0.0.1:3306`,默认账号为 `root/123456`。 -- `test`:联调测试环境,连接 `8.163.30.128:3306`,各微服务仍使用独立数据库。 +## 环境映射 -所有服务默认使用 `dev`: +| Maven Profile | Nacos 地址 | Namespace | +| --- | --- | --- | +| `local` | `127.0.0.1:8848` | `public` | +| `dev` | `dev-nacos.buggoglobal.com:8848` | `DEV_NACOS_NAMESPACE` | +| `test` | `8.163.30.128:8848` | `d99d7998-5bb0-4ffe-a2a0-1a564ce14463` | -```yaml -spring: - profiles: - active: ${SPRING_PROFILES_ACTIVE:dev} +本地 Nacos 使用 `nacos/nacos`。开发和测试密码不写入 Git,需在启动 IDEA +之前设置一次用户环境变量: + +```powershell +[Environment]::SetEnvironmentVariable('DEV_NACOS_PASSWORD', '<开发 Nacos 密码>', 'User') +[Environment]::SetEnvironmentVariable('TEST_NACOS_PASSWORD', '<测试 Nacos 密码>', 'User') +[Environment]::SetEnvironmentVariable('DEV_NACOS_NAMESPACE', '<开发海外 namespace>', 'User') ``` -建议不要直接修改配置文件。通过 IDEA 的启动配置设置 `Active profiles` 即可切换: +设置后完全退出并重新打开 IDEA,使 Maven 能读取新环境变量。 -- 本地数据库:`dev` -- 测试数据库:`test` +## IDEA 中切换 -也可以使用启动参数: +1. 在 Maven 面板的 `Profiles` 中,`local`、`dev`、`test` 只勾选一个。 +2. 重新加载 Maven 项目。 +3. 在目标服务的 Maven `Plugins > spring-boot > spring-boot:run` 启动。 -```text ---spring.profiles.active=test +例如海外查询链路需要分别启动 `gateway-overseas`、`lastleg-service` 和 +`warehouse-overseas-service`。已经运行的进程不会自动切换环境,切换 Profile +后必须停止并重新启动。 + +IDEA 普通绿色 Run 按钮不执行 Maven,因此不会读取 Maven Profile。需要继续使用 +绿色 Run 时,请在 Run Configuration 的 Environment variables 中设置 +`NACOS_SERVER_ADDR`、`NACOS_NAMESPACE`、`NACOS_USERNAME` 和 `NACOS_PASSWORD`。 +`gateway-overseas` 已默认启用 `overseas` Spring Profile。 + +## PowerShell 中切换 + +也可以在当前终端加载同一套变量,然后从同一终端启动服务: + +```powershell +. .\scripts\use-nacos-environment.ps1 -Environment local -Region overseas +. .\scripts\use-nacos-environment.ps1 -Environment dev -Region overseas +. .\scripts\use-nacos-environment.ps1 -Environment test -Region overseas ``` -## 测试数据库密码 +脚本读取 `DEV_NACOS_PASSWORD`、`TEST_NACOS_PASSWORD`,namespace 优先读取 +`*_OVERSEAS_NACOS_NAMESPACE`,未设置时读取 `*_NACOS_NAMESPACE`。命令前面的点和 +空格不能省略,否则变量只存在于脚本子进程中。 -测试数据库密码不写入 Git。启动 `test` 环境前,在 IDEA 启动配置的环境变量中设置: +## 当前限制 -```text -TEST_DB_PASSWORD=测试数据库密码 -``` - -公共测试连接参数支持覆盖: - -```text -TEST_DB_HOST=8.163.30.128 -TEST_DB_PORT=3306 -TEST_DB_USERNAME=root -TEST_DB_PASSWORD=测试数据库密码 -``` - -每个服务还支持使用自己的 `*_TEST_DB_URL`、`*_TEST_DB_USERNAME` 和 `*_TEST_DB_PASSWORD` 覆盖公共参数。 - -## 分库映射 - -| 服务 | 测试数据库 | -| --- | --- | -| 认证服务 | `baiyue_auth` | -| 权限服务 | `baiyue_iam` | -| 订单服务 | `baiyue_order` | -| 仓库服务 | `baiyue_warehouse` | -| 头程履约服务 | `baiyue_firstleg` | -| 文件服务 | `baiyue_file` | -| 审计服务 | `baiyue_audit` | -| 事件桥接服务 | `baiyue_event_bridge` | - -网关、打印和集成服务没有业务数据库,但同样支持 `dev/test` Profile。网关在两种环境下都会自动加载 `cn` 路由配置。 - -## 注意事项 - -- `test` 只切换数据库,Nacos 和 Redis 默认仍连接本机。 -- 环境变量优先级高于配置文件,可用于临时覆盖单个服务。 -- 修改 IDEA 环境变量后需要重启对应服务。 -- 不要把测试数据库密码提交到配置文件、启动脚本或 Git。 +- 测试 Nacos 的 `8848` 客户端端口已确认可连接。 +- 开发 Nacos 网页控制台可访问,但 `dev-nacos.buggoglobal.com:8848` 当前从本机 + 无法连接。需要开放 Nacos 客户端端口以及 Nacos 2.x/3.x 所需的 gRPC 端口, + 或提供一个客户端可访问的内部地址。 +- 国内和海外存在同名 Data ID,远程环境必须使用各自独立 namespace。开发海外 + namespace 尚未确认,因此通过 `DEV_NACOS_NAMESPACE` 在本机配置。 +- 不要同时勾选多个环境 Profile;Maven 对同名属性的最终取值不直观,容易连错环境。 diff --git a/event-bridge-service/src/main/java/com/baiyue/eventbridge/metrics/EventBridgeMetrics.java b/event-bridge-service/src/main/java/com/baiyue/eventbridge/metrics/EventBridgeMetrics.java index 85756ae0e..156b7b28a 100644 --- a/event-bridge-service/src/main/java/com/baiyue/eventbridge/metrics/EventBridgeMetrics.java +++ b/event-bridge-service/src/main/java/com/baiyue/eventbridge/metrics/EventBridgeMetrics.java @@ -30,6 +30,7 @@ public class EventBridgeMetrics { EventTypes.FIRST_LEG_LOADING_PLAN_COMPLETED, EventTypes.FIRST_LEG_CONTAINER_SHIPPING_NODE_RECORDED, EventTypes.OVERSEAS_CONTAINER_UNLOADED, + EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED, EventTypes.IAM_USER_STATUS_CHANGED, EventTypes.IAM_TENANT_STATUS_CHANGED, EventTypes.IAM_PERMISSION_VERSION_CHANGED, diff --git a/event-bridge-service/src/main/java/com/baiyue/eventbridge/service/EventInboxService.java b/event-bridge-service/src/main/java/com/baiyue/eventbridge/service/EventInboxService.java index c130ae023..1a0538a19 100644 --- a/event-bridge-service/src/main/java/com/baiyue/eventbridge/service/EventInboxService.java +++ b/event-bridge-service/src/main/java/com/baiyue/eventbridge/service/EventInboxService.java @@ -31,10 +31,14 @@ public class EventInboxService { @Value("${baiyue.region:CN}") private String localRegion; + /** Local single-bridge mode may carry both regional envelopes; production keeps strict routing. */ + @Value("${baiyue.event-bridge.accept-any-region:false}") + private boolean acceptAnyRegion; + @Transactional public EventReceiveResult receive(EventEnvelope envelope) { envelope.requireValid(); - if (!envelope.targetRegion().name().equalsIgnoreCase(localRegion)) { + if (!acceptAnyRegion && !envelope.targetRegion().name().equalsIgnoreCase(localRegion)) { throw new IllegalArgumentException("事件目标区域与当前事件桥区域不一致"); } if (!EventPayloadHash.matches( diff --git a/event-bridge-service/src/main/resources/application.yml b/event-bridge-service/src/main/resources/application.yml index b20743a41..8b2c46a43 100644 --- a/event-bridge-service/src/main/resources/application.yml +++ b/event-bridge-service/src/main/resources/application.yml @@ -29,7 +29,7 @@ spring: namespace: ${NACOS_NAMESPACE:} baiyue: - region: ${REGION_CODE:OVERSEAS} + region: ${REGION_CODE:CN} event-dispatcher: consumer-url: ${EVENT_CONSUMER_URL:${LASTLEG_CONSUMER_URL:http://127.0.0.1:9303/internal/lastleg/events}} firstleg-consumer-url: ${FIRSTLEG_CONTAINER_EVENT_CONSUMER_URL:http://127.0.0.1:9202/internal/firstleg/v1/container-events} diff --git a/event-bridge-service/src/test/java/com/baiyue/eventbridge/service/EventInboxServiceTest.java b/event-bridge-service/src/test/java/com/baiyue/eventbridge/service/EventInboxServiceTest.java index 4bd350a39..3639dd0e5 100644 --- a/event-bridge-service/src/test/java/com/baiyue/eventbridge/service/EventInboxServiceTest.java +++ b/event-bridge-service/src/test/java/com/baiyue/eventbridge/service/EventInboxServiceTest.java @@ -151,6 +151,15 @@ class EventInboxServiceTest { .hasMessageContaining("目标区域"); } + @Test + void 本地单实例模式允许接收不同目标区域事件() { + EventInboxService localService = service("CN", true); + EventEnvelope envelope = withHash(envelope("sha256:placeholder")); + + assertThat(localService.receive(envelope).result()).isEqualTo("已接收"); + verify(store).insert(any(BridgeInbox.class)); + } + private EventEnvelope envelope(String hash) { return new EventEnvelope("550e8400-e29b-41d4-a716-446655440000", "OrderApproved", 1, Instant.parse("2026-07-29T08:30:00Z"), RegionCode.CN, RegionCode.OVERSEAS, @@ -159,11 +168,16 @@ class EventInboxServiceTest { } private EventInboxService service(String region) { + return service(region, false); + } + + private EventInboxService service(String region, boolean acceptAnyRegion) { EventInboxService service = new EventInboxService(); ReflectionTestUtils.setField(service, "inboxStore", store); ReflectionTestUtils.setField(service, "objectMapper", objectMapper); ReflectionTestUtils.setField(service, "metrics", metrics); ReflectionTestUtils.setField(service, "localRegion", region); + ReflectionTestUtils.setField(service, "acceptAnyRegion", acceptAnyRegion); return service; } diff --git a/firstleg/AGENTS.md b/firstleg/AGENTS.md index 3ceb946fd..5aaa6edd5 100644 --- a/firstleg/AGENTS.md +++ b/firstleg/AGENTS.md @@ -15,7 +15,7 @@ | 履约任务/分配 | `controller/PlatformFulfillmentAssignmentController.java`、`service/FulfillmentTaskService.java` | | 入库单查询/聚合 | `controller/PlatformInboundOrderController.java`、`service/InboundOrderQueryService.java` | | 平台测量/OCR | `controller/PlatformInboundMeasurementController.java`、`service/InboundMeasurementService.java`、`service/CartonMeasurementService.java`;手工单箱测量也从该 Controller 与 `CartonMeasurementService` 进入 | -| 箱唛箱码生成、打印与测量绑定 | 审核建单生成钩子 `controller/InternalInboundOrderController.java`;生成/解析 `service/InboundCartonLabelService.java`、`repository/InboundCartonLabelRepository.java`;平台与客户打印 `controller/PlatformInboundOrderController.java`、`MerchantInboundCartonLabelController.java`、`service/InboundCartonLabelPrintService.java`;基线/升级 SQL 为 `src/main/resources/db/phase2-inbound-carton-label.sql`、`phase2-carton-label-versioning-migration.sql` | +| 箱唛箱码生成、打印与测量绑定 | 审核建单生成钩子 `controller/InternalInboundOrderController.java`;生成/解析 `service/InboundCartonLabelService.java`、`repository/InboundCartonLabelRepository.java`;平台与客户打印 `controller/PlatformInboundOrderController.java`、`MerchantInboundCartonLabelController.java`、`service/InboundCartonLabelPrintService.java`。`fl_inbound_carton_label.sku_code` 是箱唛的 SKU 冻结快照,生成时经 `client/OrderCargoClient.java` 获取;既有记录按 `inbound_detail_id` 回填。`ONE_CLICK_DROPSHIP` 缺 SKU 时禁止生成箱唛。`box_code`、二维码内容和现有打印版式不携带 SKU,不能因该绑定改变。基线/升级 SQL 为 `src/main/resources/db/phase2-inbound-carton-label.sql`、`phase2-carton-label-versioning-migration.sql`、`../deploy/sql/头程数据库统一增量-MySQL8.sql` | | 墨家设备测量上传 | `controller/OpenApiInboundMeasurementController.java`、`dto/MojiaMeasurementDtos.java`、`service/MojiaMeasurementOpenApiService.java`、`security/MeasurementRequestAuthenticator.java`;接口契约见 `src/main/resources/openapi/firstleg-v1.yaml` 与 `../../docs/墨家设备测量上传接口说明-V3.md` | | 审核与初始计价 | `controller/PlatformInboundMeasurementReviewController.java`、`service/MeasurementReviewService.java`、`service/InitialInboundPricingService.java`、`dto/MeasurementApprovalResponse.java` | | FBA 主数据/计费 | `controller/PlatformFbaMasterDataController.java`、`PlatformFbaPricingController.java`、`service/FbaPricingService.java` | @@ -35,6 +35,7 @@ | PDA 拣货与装柜 | `controller/PlatformLoadingPdaController.java`、`loading/controller/PlatformLoadingDeliveryController.java` | | 装柜完成跨区事件 | `service/LoadingCompletedOutboxService.java`、`LoadingCompletedOutboxRelay.java`、`repository/LoadingCompletedOutboxRepository.java`、`mapper/LoadingCartonManifestMapper.java`、`client/EventBridgeClient.java`;从 `fl_inbound_order.destination_type` 冻结 V2 `orders[].destinationType=FBA|OVERSEAS`,OVERSEAS 按最终实装与稳定箱码冻结,FBA 可仅冻结箱数;真实生产验收 `src/test/java/com/baiyue/firstleg/service/TaillegRealProducerAcceptanceTest.java`;事件模型见 `../common-event-contract/AGENTS.md` | | 海外卸柜完成事件回写 | 内部入口 `controller/InternalOverseasContainerEventController.java`、消费者 `service/OverseasContainerEventConsumer.java`、Inbox `entity/ContainerEventInboxEntity.java` 与 `mapper/ContainerEventInboxMapper.java`;事件模型见 `../common-event-contract/AGENTS.md` | +| 海外暂扣状态回写 | 同一内部事件入口、消费者 `service/OverseasInboundOrderDetainedConsumer.java`;按来源入仓单 ID 更新 `fl_inbound_order.hold_flag/current_hold_reason`,复用跨区 Inbox 并写 `fl_inbound_hold_record` | | 三期海运订舱、关联候选与出仓前船期资料 | `oceanbooking/controller/PlatformOceanBookingController.java`、`oceanbooking/service/OceanBookingService.java`、`oceanbooking/mapper/OceanBookingMapper.java`、`portschedule/controller/PlatformPortScheduleController.java`;关联候选入口 `/api/platform/v1/ocean-bookings/link-candidates`,订舱契约见 `src/main/resources/openapi/ocean-bookings-v1.yaml`,表结构见 `src/main/resources/db/phase3-ocean-booking-schema.sql` | | 四期国内出仓后船节点定时查询 | `outbound/dispatch/service/OutboundDispatchService.java` 在确认出仓事务内幂等创建现有 `CarrierScheduleQuery`;编排与租约 `portschedule/service/CarrierScheduleQueryScheduler.java`,任务/十节点事实/异常事实/不可变 attempt `CarrierScheduleQueryService.java`,平台查询与人工补录入口 `PlatformCarrierScheduleQueryController.java` 的 `/{queryId}/manual-nodes`;停止只由实际 `EMPTY_RETURNED` 节点触发,不提供无节点停止入口;外部船司/轨迹调用经 `integration-service/`;标准见 `../../docs/四期/船节点标准化契约.md` | | 船节点跨区交接事件 | `portschedule/service/CarrierScheduleNodeOutboxService.java`;实际节点落库后发布 `FIRST_LEG_CONTAINER_SHIPPING_NODE_RECORDED` V1,复用 `LoadingCompletedOutboxRepository`/Relay 投 Event Bridge;海外消费见 `../lastleg-service/src/main/java/com/baiyue/lastleg/shipping/service/ContainerShippingNodeConsumer.java` | @@ -55,6 +56,7 @@ - 价格簿查询消费者依赖 `PriceBookQueryUseCase` 或稳定查询端口,不得反向依赖持久化 Mapper;移动旧查询类时同步更新生产调用、测试、Mock 与 import,避免同一职责在根 `repository/mapper` 和 `pricebookquery/` 双写。 - 第三方回传必须签名、时间窗、防重放、请求哈希,并绑定订单货物和 File 证据;不得信任外部客户字段。 - 稳定箱码映射以入库快照和 Order 货物明细为事实;订单审核创建入库单时必须幂等生成,打印接口只复用或补齐同一映射,用户是否下载箱唛不得决定箱码有效性。手工测量、墨家设备和 PDA 扫码统一通过 `InboundCartonLabelService` 校验客户、入库单、货物明细、总箱数/下标和有效批次,不得在测量时临时伪造箱码或接受跨客户、跨入库箱码。 +- 一件代发以 `fl_inbound_carton_label.sku_code` 作为不可变 SKU 快照;PDA 仍只扫描稳定 `box_code`,不得将 SKU 塞入二维码或以 SKU 替代箱码。实际打托后的查询必须能按“现场托盘 -> 客户 -> SKU -> 箱数”聚合;历史箱唛仅能在同客户、同入库单范围内按 `inbound_detail_id` 回填,禁止按货名、箱规或箱号猜测 SKU。修改本链路时同步检查 `OrderCargoClient`、Order 内部货物接口及 PDA 打托实现。 - 测量审核通过后直接进入 `WAIT_SPLIT`,费用仅作为独立初算和人工维护结果,不得阻塞或回退订单主流程;计费量、单价和附加费调整必须版本化并由服务端重算金额。状态或 DTO 变更时逐个检查 Order/Warehouse/File/Integration 消费者。 - 报关、订舱和拖车引用的供应商必须通过 `WarehouseSupplierSnapshotService` 按 ID 向 Warehouse 校验:报关使用 `CUSTOMS`,订舱使用 `BOOKING`(不区分国内/国外 scope),拖车使用 `TRUCKING`;供应商必须为 `ACTIVE`。请求中的名称只能作为一致性提示,持久化名称必须来自 Warehouse 快照,禁止用自由文本绕过 ID、类别或启用状态校验。修改任一供应商引用 DTO、OpenAPI 或内部客户端时,必须同步检查三条业务链、Warehouse `supplier-v1.yaml`、数据库快照字段和对应契约测试。 - 订舱状态固定为 `DRAFT → RELEASED`:草稿允许资料不完整并可替换或删除,放舱时校验完整持久化聚合,放舱后永久只读。创建草稿和放舱都只维护订舱聚合、三类参与方及柜型需求,不创建真实 `containerNo`、货柜行或分配关系;真实货柜只在后续提柜关联时占用已放舱柜型需求。 diff --git a/firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java b/firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java index f3a5683a5..ac30d0298 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java @@ -63,7 +63,12 @@ public interface OrderCargoClient { } record CargoLabelBinding(Long cargoId, Long orderId, Integer itemNo, String cargoName, - Integer packageCount, BigDecimal lengthCm, BigDecimal widthCm, + String skuNo, Integer packageCount, BigDecimal lengthCm, BigDecimal widthCm, BigDecimal heightCm) { + public CargoLabelBinding(Long cargoId, Long orderId, Integer itemNo, String cargoName, + Integer packageCount, BigDecimal lengthCm, BigDecimal widthCm, + BigDecimal heightCm) { + this(cargoId, orderId, itemNo, cargoName, null, packageCount, lengthCm, widthCm, heightCm); + } } } diff --git a/firstleg/src/main/java/com/baiyue/firstleg/controller/InternalOverseasContainerEventController.java b/firstleg/src/main/java/com/baiyue/firstleg/controller/InternalOverseasContainerEventController.java index 54f1a67be..095e376c8 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/controller/InternalOverseasContainerEventController.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/controller/InternalOverseasContainerEventController.java @@ -4,8 +4,9 @@ import com.baiyue.common.core.api.ApiResponse; import com.baiyue.common.core.api.ResultCode; import com.baiyue.common.core.exception.BizException; import com.baiyue.common.event.EventEnvelope; -import com.baiyue.firstleg.entity.ContainerBookingEntity; +import com.baiyue.common.event.EventTypes; import com.baiyue.firstleg.service.OverseasContainerEventConsumer; +import com.baiyue.firstleg.service.OverseasInboundOrderDetainedConsumer; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; @@ -20,14 +21,17 @@ import java.security.MessageDigest; @RequiredArgsConstructor public class InternalOverseasContainerEventController { private final OverseasContainerEventConsumer consumer; + private final OverseasInboundOrderDetainedConsumer detainedConsumer; @Value("${baiyue.security.internal-service-token}") private String expectedToken; @PostMapping("/internal/firstleg/v1/container-events") - public ApiResponse receive( + public ApiResponse receive( @RequestHeader(value = "X-Internal-Service-Token", required = false) String token, @RequestBody EventEnvelope event) { authenticate(token); - return ApiResponse.success(consumer.consume(event)); + Object result = EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED.equals(event.eventType()) + ? detainedConsumer.consume(event) : consumer.consume(event); + return ApiResponse.success(result); } private void authenticate(String token) { diff --git a/firstleg/src/main/java/com/baiyue/firstleg/controller/PlatformLoadingPdaController.java b/firstleg/src/main/java/com/baiyue/firstleg/controller/PlatformLoadingPdaController.java index 52d842093..0449991c8 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/controller/PlatformLoadingPdaController.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/controller/PlatformLoadingPdaController.java @@ -24,6 +24,11 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import jakarta.validation.constraints.PositiveOrZero; +import com.baiyue.firstleg.service.LoadingWorkPalletService; +import com.baiyue.firstleg.dto.LoadingPalletDtos.OpenRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.ScanRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.SealRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.Pallet; import jakarta.annotation.Resource; import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; @@ -51,6 +56,8 @@ public class PlatformLoadingPdaController { private LoadingPickService service; @Resource private CabinetLoadingEvidenceService evidenceService; + @Resource + private LoadingWorkPalletService palletService; @GetMapping("/tasks") @PreAuthorize("hasAuthority('platform:firstleg:pda:view')") @@ -218,4 +225,45 @@ public class PlatformLoadingPdaController { FirstlegScopeGuard.platform(user); return ApiResponse.success(service.operations(taskId)); } + + @GetMapping("/tasks/{taskId}/pallets") + @PreAuthorize("hasAuthority('platform:firstleg:pda:view')") + public ApiResponse> pallets(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable @Positive long taskId, + @RequestParam @Positive long warehouseId) { + FirstlegScopeGuard.platform(user); + return ApiResponse.success(palletService.list(taskId, warehouseId)); + } + + @PostMapping("/tasks/{taskId}/pallets/open") + @PreAuthorize("hasAuthority('platform:firstleg:pda:work')") + public ApiResponse openPallet(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable @Positive long taskId, + @Valid @RequestBody OpenRequest request) { + FirstlegScopeGuard.platform(user); + return ApiResponse.success(palletService.open(taskId, request, user.userId(), user.username())); + } + + /** 扫到其他未封托托盘上的箱唛时,同时完成按箱移托。 */ + @PostMapping("/tasks/{taskId}/pallets/{palletId}/cartons/scan") + @PreAuthorize("hasAuthority('platform:firstleg:pda:work')") + public ApiResponse scanCarton(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable @Positive long taskId, + @PathVariable @Positive long palletId, + @Valid @RequestBody ScanRequest request) { + FirstlegScopeGuard.platform(user); + return ApiResponse.success(palletService.scan(taskId, palletId, request, + user.userId(), user.username())); + } + + @PostMapping("/tasks/{taskId}/pallets/{palletId}/seal") + @PreAuthorize("hasAuthority('platform:firstleg:pda:work')") + public ApiResponse sealPallet(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable @Positive long taskId, + @PathVariable @Positive long palletId, + @Valid @RequestBody SealRequest request) { + FirstlegScopeGuard.platform(user); + return ApiResponse.success(palletService.seal(taskId, palletId, request, + user.userId(), user.username())); + } } diff --git a/firstleg/src/main/java/com/baiyue/firstleg/dto/LoadingPalletDtos.java b/firstleg/src/main/java/com/baiyue/firstleg/dto/LoadingPalletDtos.java new file mode 100644 index 000000000..3bb836838 --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/dto/LoadingPalletDtos.java @@ -0,0 +1,41 @@ +package com.baiyue.firstleg.dto; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import java.util.List; + +/** 一件代发PDA现场打托接口模型。 */ +public final class LoadingPalletDtos { + private LoadingPalletDtos() {} + + public record OpenRequest( + @NotNull @Min(1) Long warehouseId, + @Min(1) Long warehousePalletId, + @NotBlank @Size(max = 64) String palletCode, + @NotBlank @Size(max = 128) String operationId + ) {} + + public record ScanRequest( + @NotNull @Min(1) Long warehouseId, + @NotBlank @Size(max = 80) String boxCode, + @NotBlank @Size(max = 128) String operationId + ) {} + + public record SealRequest( + @NotNull @Min(1) Long warehouseId, + @NotNull @Min(0) Integer expectedVersion, + @NotBlank @Size(max = 128) String operationId + ) {} + + public record Carton(Long id, String boxCode, String skuCode, Long tenantId, + Long inboundId, Long inboundDetailId, Integer unitsPerCarton) {} + + public record SkuSummary(String skuCode, Integer cartonCount, Long productQuantity) {} + + public record Pallet(Long id, String palletCode, Long warehousePalletId, String status, + Integer version, Integer cartonCount, Integer skuCount, + Integer customerCount, List skus, List cartons) {} +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundCartonLabelEntity.java b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundCartonLabelEntity.java index bf0674011..6e9d7d252 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundCartonLabelEntity.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundCartonLabelEntity.java @@ -22,6 +22,7 @@ public class InboundCartonLabelEntity { private Long inboundDetailId; private Integer cargoItemNo; private String cargoName; + private String skuCode; private Integer detailPackageCount; private String inboundCode; private String boxCode; diff --git a/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundHoldRecordEntity.java b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundHoldRecordEntity.java new file mode 100644 index 000000000..931ac782f --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundHoldRecordEntity.java @@ -0,0 +1,24 @@ +package com.baiyue.firstleg.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +@TableName("fl_inbound_hold_record") +public class InboundHoldRecordEntity { + @TableId(type = IdType.INPUT) private Long id; + private Long tenantId; + private Long inboundId; + private String holdAction; + private String reason; + private Long evidenceFileId; + private Long operatorId; + private String operatorName; + private LocalDateTime operatedAt; +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundOrderEntity.java b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundOrderEntity.java index 75f165e1b..816a28e11 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundOrderEntity.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/entity/InboundOrderEntity.java @@ -53,6 +53,7 @@ public class InboundOrderEntity extends BaseEntity { private String destinationName; private Boolean holdFlag; private String currentHoldReason; + private Long lastOverseasHoldVersion; private Integer totalPackageCount; private BigDecimal actualGrossWeightKg; private BigDecimal chargeableWeightKg; diff --git a/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletCartonEntity.java b/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletCartonEntity.java new file mode 100644 index 000000000..a0072c7bc --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletCartonEntity.java @@ -0,0 +1,37 @@ +package com.baiyue.firstleg.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.Version; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +/** 一件代发箱唛与现场实际托盘的当前绑定事实。 */ +@Getter +@Setter +@TableName("fl_loading_work_pallet_carton") +public class LoadingWorkPalletCartonEntity { + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private Long pickTaskId; + private Long workPalletId; + private Long loadingPlanItemId; + private Long pickItemId; + private Long tenantId; + private Long inboundId; + private Long inboundDetailId; + private Long cartonLabelId; + private String boxCode; + private String skuCode; + private Integer unitsPerCarton; + private Long boundBy; + private String boundByName; + private String operationId; + @Version + private Integer version; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletEntity.java b/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletEntity.java new file mode 100644 index 000000000..169f984e2 --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/entity/LoadingWorkPalletEntity.java @@ -0,0 +1,36 @@ +package com.baiyue.firstleg.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.Version; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +/** 一件代发装柜现场的实际托盘。 */ +@Getter +@Setter +@TableName("fl_loading_work_pallet") +public class LoadingWorkPalletEntity { + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private Long pickTaskId; + private Long loadingPlanId; + private Long warehouseId; + private Long warehousePalletId; + private String palletCode; + private String openOperationId; + private String sealOperationId; + private String palletStatus; + private Long createdBy; + private String createdByName; + private Long sealedBy; + private String sealedByName; + private LocalDateTime sealedAt; + @Version + private Integer version; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundHoldRecordMapper.java b/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundHoldRecordMapper.java new file mode 100644 index 000000000..690bac3c6 --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundHoldRecordMapper.java @@ -0,0 +1,9 @@ +package com.baiyue.firstleg.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.firstleg.entity.InboundHoldRecordEntity; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface InboundHoldRecordMapper extends BaseMapper { +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundOrderMapper.java b/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundOrderMapper.java index c5663cf46..7ec6a1a22 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundOrderMapper.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/mapper/InboundOrderMapper.java @@ -5,10 +5,25 @@ import com.baiyue.firstleg.entity.InboundOrderEntity; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; +import org.apache.ibatis.annotations.Select; /** 入库聚合根条件更新 Mapper,所有推进均同时校验租户、前置状态和版本。 */ @Mapper public interface InboundOrderMapper extends BaseMapper { + @Select("SELECT * FROM fl_inbound_order WHERE id=#{inboundId} AND deleted=0 FOR UPDATE") + InboundOrderEntity selectActiveByIdForUpdate(@Param("inboundId") long inboundId); + + @Update(""" + UPDATE fl_inbound_order + SET hold_flag=#{detained}, current_hold_reason=#{reason}, + last_overseas_hold_version=#{sourceVersion}, updated_by=#{operatorId}, version=version+1 + WHERE id=#{inboundId} AND tenant_id=#{tenantId} AND deleted=0 + AND (last_overseas_hold_version IS NULL OR last_overseas_hold_version < #{sourceVersion}) + """) + int applyOverseasHold(@Param("tenantId") long tenantId, @Param("inboundId") long inboundId, + @Param("detained") boolean detained, @Param("reason") String reason, + @Param("sourceVersion") long sourceVersion, @Param("operatorId") long operatorId); + @Update(""" UPDATE fl_inbound_order SET process_status = 'WAIT_SPLIT', measure_review_status = 'APPROVED', diff --git a/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingCompletedEventMapper.java b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingCompletedEventMapper.java index 8760d0ce1..b263c306d 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingCompletedEventMapper.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingCompletedEventMapper.java @@ -50,11 +50,11 @@ public interface LoadingCompletedEventMapper { """) List rows(@Param("planId") long planId); - /** 读取一键代发托盘及其箱唛快照;普通订单不会使用该结果。 */ + /** 读取头程托盘及箱唛快照;普通订单向尾程交接打印编号,一键代发同时冻结托盘库存。 */ @Select(""" SELECT i.account_id, i.inbound_id AS source_inbound_id, a.pallet_id, a.reservation_id, a.pallet_code, a.allocation_no, - a.picked_package_count AS allocated_package_count, + 0 AS actual_binding, a.picked_package_count AS allocated_package_count, i.inbound_detail_id AS source_inbound_detail_id, i.sku_code, i.product_name_zh, i.package_count, i.product_quantity, label.box_code @@ -80,6 +80,30 @@ public interface LoadingCompletedEventMapper { """) List palletRows(@Param("planId") long planId); + /** 一件代发现场逐箱扫描结果;同一实物托盘可以包含多个客户,但行仍保留客户与入仓单归属。 */ + @Select(""" + SELECT carton.tenant_id AS account_id,carton.inbound_id AS source_inbound_id, + pallet.id AS pallet_id,1 AS actual_binding, + pallet.pallet_code,NULL AS allocation_no, + COUNT(*) OVER (PARTITION BY pallet.id,carton.tenant_id,carton.inbound_id) AS allocated_package_count, + carton.inbound_detail_id AS source_inbound_detail_id,carton.sku_code, + item.product_name_zh,item.package_count,item.product_quantity,carton.box_code + FROM fl_loading_work_pallet pallet + JOIN fl_loading_work_pallet_carton carton ON carton.work_pallet_id=pallet.id + JOIN fl_loading_plan_item item ON item.id=carton.loading_plan_item_id AND item.deleted=0 + JOIN fl_loading_pick_task task ON task.id=pallet.pick_task_id + AND task.loading_plan_id=pallet.loading_plan_id + WHERE pallet.loading_plan_id=#{planId} AND pallet.pallet_status='SEALED' + ORDER BY carton.tenant_id,carton.inbound_id,pallet.id,item.inbound_detail_id,carton.id + """) + List actualPalletRows(@Param("planId") long planId); + + @Select("SELECT COUNT(*) FROM fl_loading_work_pallet WHERE loading_plan_id=#{planId} AND pallet_status<>'CANCELLED'") + int workPalletCount(@Param("planId") long planId); + + @Select("SELECT COUNT(*) FROM fl_loading_work_pallet WHERE loading_plan_id=#{planId} AND pallet_status='OPEN'") + int openWorkPalletCount(@Param("planId") long planId); + @Getter @Setter class Row { private Long loadingPlanId; private String planNo; private Long containerBookingId; @@ -105,6 +129,7 @@ public interface LoadingCompletedEventMapper { class PalletRow { private Long accountId; private Long sourceInboundId; private Long palletId; private Long reservationId; private String palletCode; + private Boolean actualBinding; private Integer allocationNo; private Integer allocatedPackageCount; private Long sourceInboundDetailId; private String skuCode; private String productNameZh; private Integer packageCount; private Integer productQuantity; private String boxCode; diff --git a/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletCartonMapper.java b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletCartonMapper.java new file mode 100644 index 000000000..d8caf8f89 --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletCartonMapper.java @@ -0,0 +1,90 @@ +package com.baiyue.firstleg.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.firstleg.entity.LoadingWorkPalletCartonEntity; +import lombok.Getter; +import lombok.Setter; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +@Mapper +public interface LoadingWorkPalletCartonMapper extends BaseMapper { + @Select(""" + SELECT label.id carton_label_id,label.tenant_id,label.inbound_id,label.inbound_detail_id, + label.box_code,plan_item.id loading_plan_item_id,pick_item.id pick_item_id, + plan_item.sku_code,plan_item.units_per_carton + FROM fl_inbound_carton_label label + JOIN fl_loading_plan_item plan_item + ON plan_item.account_id=label.tenant_id AND plan_item.inbound_id=label.inbound_id + AND plan_item.inbound_detail_id=label.inbound_detail_id AND plan_item.deleted=0 + JOIN fl_loading_pick_item pick_item ON pick_item.loading_plan_item_id=plan_item.id + JOIN fl_loading_pick_task task ON task.id=pick_item.pick_task_id + AND task.loading_plan_id=plan_item.loading_plan_id + JOIN fl_loading_plan plan ON plan.id=task.loading_plan_id + JOIN fl_container_booking booking ON booking.id=plan.container_booking_id + JOIN fl_inbound_order inbound_order ON inbound_order.id=plan_item.inbound_id + AND inbound_order.tenant_id=plan_item.account_id AND inbound_order.deleted=0 + WHERE task.id=#{taskId} AND task.task_status='LOADING' AND booking.warehouse_id=#{warehouseId} + AND label.box_code=#{boxCode} AND label.batch_status='ACTIVE' + AND inbound_order.fulfillment_type='ONE_CLICK_DROPSHIP' + AND pick_item.item_status<>'SKIPPED' + ORDER BY label.tenant_id,label.id + """) + List selectCandidates(@Param("taskId") long taskId, + @Param("warehouseId") long warehouseId, + @Param("boxCode") String boxCode); + + @Select("SELECT * FROM fl_loading_work_pallet_carton WHERE pick_task_id=#{taskId} AND carton_label_id=#{labelId}") + LoadingWorkPalletCartonEntity selectBinding(@Param("taskId") long taskId, + @Param("labelId") long labelId); + + @Select("SELECT * FROM fl_loading_work_pallet_carton WHERE operation_id=#{operationId}") + LoadingWorkPalletCartonEntity selectByOperation(@Param("operationId") String operationId); + + @Select("SELECT * FROM fl_loading_work_pallet_carton WHERE work_pallet_id=#{palletId} ORDER BY id") + List selectPalletCartons(@Param("palletId") long palletId); + + @Select("SELECT COUNT(*) FROM fl_loading_work_pallet_carton WHERE work_pallet_id=#{palletId}") + int countPalletCartons(@Param("palletId") long palletId); + + @Select(""" + SELECT COUNT(*) FROM ( + SELECT carton.pick_item_id + FROM fl_loading_work_pallet_carton carton + JOIN fl_loading_pick_item pick_item ON pick_item.id=carton.pick_item_id + WHERE carton.pick_task_id=#{taskId} + GROUP BY carton.pick_item_id,pick_item.picked_package_count + HAVING COUNT(*)>pick_item.picked_package_count + ) unreconciled + """) + int countUnreconciled(@Param("taskId") long taskId); + + @Update(""" + UPDATE fl_loading_work_pallet_carton + SET work_pallet_id=#{targetPalletId},bound_by=#{operatorId},bound_by_name=#{operatorName}, + operation_id=#{operationId},version=version+1 + WHERE id=#{id} AND version=#{version} + """) + int move(@Param("id") long id, @Param("version") int version, + @Param("targetPalletId") long targetPalletId, + @Param("operatorId") long operatorId, @Param("operatorName") String operatorName, + @Param("operationId") String operationId); + + @Getter + @Setter + class CartonCandidate { + private Long cartonLabelId; + private Long tenantId; + private Long inboundId; + private Long inboundDetailId; + private String boxCode; + private Long loadingPlanItemId; + private Long pickItemId; + private String skuCode; + private Integer unitsPerCarton; + } +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletMapper.java b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletMapper.java new file mode 100644 index 000000000..e1b6ba90c --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/mapper/LoadingWorkPalletMapper.java @@ -0,0 +1,77 @@ +package com.baiyue.firstleg.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.firstleg.entity.LoadingWorkPalletEntity; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +@Mapper +public interface LoadingWorkPalletMapper extends BaseMapper { + @Select(""" + SELECT task.id task_id,task.loading_plan_id,task.task_status,booking.warehouse_id, + assignment.assignee_id, + SUM(CASE WHEN inbound_order.fulfillment_type='ONE_CLICK_DROPSHIP' THEN 1 ELSE 0 END) dropship_item_count + FROM fl_loading_pick_task task + JOIN fl_loading_plan plan ON plan.id=task.loading_plan_id + JOIN fl_container_booking booking ON booking.id=plan.container_booking_id + JOIN fl_loading_pick_item pick_item ON pick_item.pick_task_id=task.id + JOIN fl_loading_plan_item plan_item ON plan_item.id=pick_item.loading_plan_item_id AND plan_item.deleted=0 + JOIN fl_inbound_order inbound_order ON inbound_order.id=plan_item.inbound_id + AND inbound_order.tenant_id=plan_item.account_id AND inbound_order.deleted=0 + LEFT JOIN fl_loading_pick_assignment assignment ON assignment.pick_task_id=task.id + WHERE task.id=#{taskId} AND booking.warehouse_id=#{warehouseId} + GROUP BY task.id,task.loading_plan_id,task.task_status,booking.warehouse_id,assignment.assignee_id + """) + TaskContext selectTaskContext(@Param("taskId") long taskId, @Param("warehouseId") long warehouseId); + + @Select("SELECT * FROM fl_loading_work_pallet WHERE open_operation_id=#{operationId}") + LoadingWorkPalletEntity selectByOpenOperation(@Param("operationId") String operationId); + + @Select("SELECT * FROM fl_loading_work_pallet WHERE seal_operation_id=#{operationId}") + LoadingWorkPalletEntity selectBySealOperation(@Param("operationId") String operationId); + + @Select(""" + SELECT pallet.* + FROM fl_loading_work_pallet pallet + JOIN fl_loading_pick_task task ON task.id=pallet.pick_task_id + JOIN fl_loading_plan plan ON plan.id=task.loading_plan_id + JOIN fl_container_booking booking ON booking.id=plan.container_booking_id + WHERE pallet.pick_task_id=#{taskId} AND pallet.id=#{palletId} + AND booking.warehouse_id=#{warehouseId} + """) + LoadingWorkPalletEntity selectScoped(@Param("taskId") long taskId, + @Param("palletId") long palletId, + @Param("warehouseId") long warehouseId); + + @Select(""" + SELECT pallet.* + FROM fl_loading_work_pallet pallet + JOIN fl_loading_pick_task task ON task.id=pallet.pick_task_id + JOIN fl_loading_plan plan ON plan.id=task.loading_plan_id + JOIN fl_container_booking booking ON booking.id=plan.container_booking_id + WHERE pallet.pick_task_id=#{taskId} AND booking.warehouse_id=#{warehouseId} + AND pallet.pallet_status<>'CANCELLED' + ORDER BY pallet.id + """) + List selectTaskPallets(@Param("taskId") long taskId, + @Param("warehouseId") long warehouseId); + + @Update(""" + UPDATE fl_loading_work_pallet + SET pallet_status='SEALED',seal_operation_id=#{operationId},sealed_by=#{operatorId}, + sealed_by_name=#{operatorName},sealed_at=CURRENT_TIMESTAMP(3),version=version+1 + WHERE id=#{palletId} AND pick_task_id=#{taskId} AND warehouse_id=#{warehouseId} + AND pallet_status='OPEN' AND version=#{expectedVersion} + """) + int seal(@Param("taskId") long taskId, @Param("palletId") long palletId, + @Param("warehouseId") long warehouseId, @Param("expectedVersion") int expectedVersion, + @Param("operationId") String operationId, @Param("operatorId") long operatorId, + @Param("operatorName") String operatorName); + + record TaskContext(Long taskId, Long loadingPlanId, String taskStatus, Long warehouseId, + Long assigneeId, Integer dropshipItemCount) {} +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/repository/InboundCartonLabelRepository.java b/firstleg/src/main/java/com/baiyue/firstleg/repository/InboundCartonLabelRepository.java index 7cac757ec..1528937ca 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/repository/InboundCartonLabelRepository.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/repository/InboundCartonLabelRepository.java @@ -62,6 +62,16 @@ public class InboundCartonLabelRepository { .set(InboundCartonLabelEntity::getCargoFingerprint, fingerprint)); } + public void initializeLegacySku(long tenantId, long inboundId, long inboundDetailId, String skuCode) { + if (skuCode == null || skuCode.isBlank()) return; + labels.update(null, new LambdaUpdateWrapper() + .eq(InboundCartonLabelEntity::getTenantId, tenantId) + .eq(InboundCartonLabelEntity::getInboundId, inboundId) + .eq(InboundCartonLabelEntity::getInboundDetailId, inboundDetailId) + .isNull(InboundCartonLabelEntity::getSkuCode) + .set(InboundCartonLabelEntity::getSkuCode, skuCode.trim())); + } + public boolean initializeOriginalPackageCount(long tenantId, long inboundId, int packageCount) { return inbounds.update(null, new LambdaUpdateWrapper() .eq(InboundOrderEntity::getTenantId, tenantId) diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/InboundCartonLabelService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/InboundCartonLabelService.java index 71794fc5b..a54d57828 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/service/InboundCartonLabelService.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/InboundCartonLabelService.java @@ -50,6 +50,14 @@ public class InboundCartonLabelService implements CartonLabelBindingResolver { throw new BizException(ResultCode.CONFLICT, "当前入库单状态不允许首次生成箱号"); } List cargoes = loadCargoes(inbound); + if ("ONE_CLICK_DROPSHIP".equals(inbound.getFulfillmentType()) + && cargoes.stream().anyMatch(cargo -> cargo.skuNo() == null || cargo.skuNo().isBlank())) { + throw new BizException(ResultCode.VALIDATION_FAILED, "一件代发货物明细缺少SKU,不能生成箱唛"); + } + existing.forEach(label -> cargoes.stream() + .filter(cargo -> java.util.Objects.equals(cargo.cargoId(), label.getInboundDetailId())) + .findFirst().ifPresent(cargo -> repository.initializeLegacySku(tenantId, inboundId, + label.getInboundDetailId(), cargo.skuNo()))); String fingerprint = fingerprint(cargoes); if (!existing.isEmpty()) { String existingFingerprint = consistent.get(0).getCargoFingerprint(); @@ -202,6 +210,7 @@ public class InboundCartonLabelService implements CartonLabelBindingResolver { label.setInboundDetailId(cargo.cargoId()); label.setCargoItemNo(cargo.itemNo()); label.setCargoName(cargo.cargoName()); + label.setSkuCode(cargo.skuNo()); label.setDetailPackageCount(cargo.packageCount()); label.setInboundCode(inbound.getInboundCode()); label.setBoxCode(inbound.getInboundCode() + "#" + total + "-" + index); @@ -256,7 +265,8 @@ public class InboundCartonLabelService implements CartonLabelBindingResolver { private String fingerprint(List cargoes) { String source = cargoes.stream().map(cargo -> String.join("|", String.valueOf(cargo.itemNo()), - cargo.cargoName() == null ? "" : cargo.cargoName().trim(), String.valueOf(cargo.packageCount()), + cargo.cargoName() == null ? "" : cargo.cargoName().trim(), + cargo.skuNo() == null ? "" : cargo.skuNo().trim(), String.valueOf(cargo.packageCount()), decimalOrEmpty(cargo.lengthCm()), decimalOrEmpty(cargo.widthCm()), decimalOrEmpty(cargo.heightCm()))) .collect(java.util.stream.Collectors.joining("\n")); try { diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java index 93a24bab2..aabcf8567 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingCompletedOutboxService.java @@ -53,13 +53,33 @@ public class LoadingCompletedOutboxService { validateContainerIdentity(rows); validateCustomerSnapshots(rows); Map> cartonMarks = cartonLabelPools(rows); - List palletRows = snapshotMapper.palletRows(planId); + List palletRows = palletRows(planId); rows.stream().collect(java.util.stream.Collectors.groupingBy(Row::getAccountId, java.util.TreeMap::new, java.util.stream.Collectors.toList())) .forEach((accountId, accountRows) -> insert(envelope(accountId, accountRows, palletRows.stream().filter(item -> accountId.equals(item.getAccountId())).toList(), cartonMarks))); } + /** 已开展逐箱打托的入仓单使用现场事实;未开展的存量入仓单继续兼容原托盘分配快照。 */ + private List palletRows(long planId) { + int workPalletCount = snapshotMapper.workPalletCount(planId); + if (snapshotMapper.openWorkPalletCount(planId) > 0) { + throw new BizException(ResultCode.CONFLICT, "一件代发存在未封托的现场托盘,不能完成装柜交接"); + } + List actual = snapshotMapper.actualPalletRows(planId); + if (workPalletCount > 0 && actual.isEmpty()) { + throw new BizException(ResultCode.CONFLICT, "一件代发现场托盘没有有效箱唛,不能完成装柜交接"); + } + if (actual.isEmpty()) return snapshotMapper.palletRows(planId); + Set actualInbounds = actual.stream().map(row -> + new InboundPalletKey(row.getAccountId(), row.getSourceInboundId())).collect(java.util.stream.Collectors.toSet()); + List result = new ArrayList<>(actual); + snapshotMapper.palletRows(planId).stream() + .filter(row -> !actualInbounds.contains(new InboundPalletKey(row.getAccountId(), row.getSourceInboundId()))) + .forEach(result::add); + return result; + } + private EventEnvelope envelope(long accountId, List rows, List palletRows, Map> cartonMarks) { Row first = rows.get(0); @@ -213,9 +233,13 @@ public class LoadingCompletedOutboxService { } private record LabelPoolKey(long accountId, long inboundId) { } + private record InboundPalletKey(long accountId, long inboundId) { } private List palletSnapshots(List values, List source) { if (source.isEmpty()) return List.of(); + if (source.stream().anyMatch(item -> Boolean.TRUE.equals(item.getActualBinding()))) { + return actualPalletSnapshots(values, source); + } Map> byPallet = source.stream().collect(java.util.stream.Collectors.groupingBy( p -> (p.getAllocationNo() == null ? "" : p.getAllocationNo()) + "|" + p.getPalletCode(), LinkedHashMap::new, java.util.stream.Collectors.toList())); @@ -266,6 +290,43 @@ public class LoadingCompletedOutboxService { return result; } + /** 现场逐箱扫描已给出精确箱到托关系,不再使用旧的按托盘容量顺序推算。 */ + private List actualPalletSnapshots( + List values, List source) { + Map details = values.stream().collect(java.util.stream.Collectors.toMap( + Row::getSourceInboundDetailId, value -> value, (left, right) -> left, LinkedHashMap::new)); + Map> byPallet = source.stream().collect(java.util.stream.Collectors.groupingBy( + PalletRow::getPalletCode, LinkedHashMap::new, java.util.stream.Collectors.toList())); + List result = new ArrayList<>(); + int actualPackages = 0; + for (Map.Entry> pallet : byPallet.entrySet()) { + Map> byDetail = pallet.getValue().stream().collect( + java.util.stream.Collectors.groupingBy(PalletRow::getSourceInboundDetailId, + LinkedHashMap::new, java.util.stream.Collectors.toList())); + List skus = new ArrayList<>(); + for (Map.Entry> detailRows : byDetail.entrySet()) { + Row detail = details.get(detailRows.getKey()); + if (detail == null) throw new BizException(ResultCode.CONFLICT, "现场托盘箱唛无法匹配装柜货物明细"); + List marks = detailRows.getValue().stream().map(PalletRow::getBoxCode) + .filter(Objects::nonNull).distinct().toList(); + if (marks.size() != detailRows.getValue().size()) { + throw new BizException(ResultCode.CONFLICT, "现场托盘包含空箱唛或重复箱唛"); + } + int quantity = detail.getProductQuantity() == null ? marks.size() + : (int) Math.round((double) detail.getProductQuantity() * marks.size() + / Math.max(1, detail.getPackageCount())); + skus.add(new FirstLegLoadingPlanCompletedV2.SkuPackageSnapshot(detail.getSkuCode(), + detail.getProductNameZh(), marks.size(), quantity, marks)); + actualPackages += marks.size(); + } + result.add(new FirstLegLoadingPlanCompletedV2.PalletSnapshot(pallet.getKey(), skus)); + } + if (actualPackages != sumInt(values, Row::getPackageCount)) { + throw new BizException(ResultCode.CONFLICT, "一件代发现场托盘箱唛数与实装箱数不一致"); + } + return result; + } + private static boolean dropship(String value) { return "ONE_CLICK_DROPSHIP".equals(value); } diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingWorkPalletService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingWorkPalletService.java new file mode 100644 index 000000000..4557f92ce --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/LoadingWorkPalletService.java @@ -0,0 +1,206 @@ +package com.baiyue.firstleg.service; + +import com.baiyue.common.core.api.ResultCode; +import com.baiyue.common.core.exception.BizException; +import com.baiyue.firstleg.dto.LoadingPalletDtos.Carton; +import com.baiyue.firstleg.dto.LoadingPalletDtos.OpenRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.Pallet; +import com.baiyue.firstleg.dto.LoadingPalletDtos.ScanRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.SealRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.SkuSummary; +import com.baiyue.firstleg.entity.LoadingWorkPalletCartonEntity; +import com.baiyue.firstleg.entity.LoadingWorkPalletEntity; +import com.baiyue.firstleg.mapper.LoadingWorkPalletCartonMapper; +import com.baiyue.firstleg.mapper.LoadingWorkPalletCartonMapper.CartonCandidate; +import com.baiyue.firstleg.mapper.LoadingWorkPalletMapper; +import com.baiyue.firstleg.mapper.LoadingWorkPalletMapper.TaskContext; +import jakarta.annotation.Resource; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** 一件代发现场打托旁路;不改变既有拣货数量和仓库资源结算。 */ +@Service +public class LoadingWorkPalletService { + @Resource private LoadingWorkPalletMapper pallets; + @Resource private LoadingWorkPalletCartonMapper cartons; + + @Transactional + public Pallet open(long taskId, OpenRequest request, long operatorId, String operatorName) { + TaskContext context = requireWorkTask(taskId, request.warehouseId(), operatorId); + String code = request.palletCode().trim(); + String operationId = request.operationId().trim(); + LoadingWorkPalletEntity replay = pallets.selectByOpenOperation(operationId); + if (replay != null) { + if (replay.getPickTaskId() != taskId || !replay.getPalletCode().equals(code)) { + throw conflict("开托幂等号已用于其他请求"); + } + return view(replay); + } + LoadingWorkPalletEntity value = new LoadingWorkPalletEntity(); + value.setPickTaskId(taskId); + value.setLoadingPlanId(context.loadingPlanId()); + value.setWarehouseId(request.warehouseId()); + value.setWarehousePalletId(request.warehousePalletId()); + value.setPalletCode(code); + value.setOpenOperationId(operationId); + value.setPalletStatus("OPEN"); + value.setCreatedBy(operatorId); + value.setCreatedByName(operatorName); + value.setVersion(0); + try { + pallets.insert(value); + } catch (DuplicateKeyException exception) { + replay = pallets.selectByOpenOperation(operationId); + if (replay == null || replay.getPickTaskId() != taskId || !replay.getPalletCode().equals(code)) { + throw conflict("托盘号已存在或开托操作重复"); + } + value = replay; + } + return view(value); + } + + @Transactional + public Pallet scan(long taskId, long palletId, ScanRequest request, + long operatorId, String operatorName) { + requireWorkTask(taskId, request.warehouseId(), operatorId); + LoadingWorkPalletEntity target = requireOpenPallet(taskId, palletId, request.warehouseId()); + String operationId = request.operationId().trim(); + LoadingWorkPalletCartonEntity replay = cartons.selectByOperation(operationId); + if (replay != null) { + if (replay.getPickTaskId() != taskId || replay.getWorkPalletId() != palletId + || !replay.getBoxCode().equals(request.boxCode().trim())) { + throw conflict("箱唛操作幂等号已用于其他请求"); + } + return view(target); + } + List candidates = cartons.selectCandidates(taskId, request.warehouseId(), + request.boxCode().trim()); + if (candidates.isEmpty()) throw new BizException(ResultCode.NOT_FOUND, "箱唛不属于当前任务的一件代发货物"); + if (candidates.size() > 1) throw conflict("箱唛在当前多客户任务中不唯一,请检查箱唛数据"); + CartonCandidate candidate = candidates.get(0); + if (candidate.getSkuCode() == null || candidate.getSkuCode().isBlank()) { + throw conflict("一件代发箱唛缺少SKU,不能上托"); + } + LoadingWorkPalletCartonEntity current = cartons.selectBinding(taskId, candidate.getCartonLabelId()); + if (current == null) { + current = binding(taskId, palletId, candidate, operationId, operatorId, operatorName); + try { + cartons.insert(current); + } catch (DuplicateKeyException exception) { + LoadingWorkPalletCartonEntity concurrent = cartons.selectBinding(taskId, candidate.getCartonLabelId()); + if (concurrent == null || concurrent.getWorkPalletId() != palletId) { + throw conflict("箱唛已被其他托盘绑定"); + } + } + return view(target); + } + if (current.getWorkPalletId() == palletId) return view(target); + LoadingWorkPalletEntity source = pallets.selectScoped(taskId, current.getWorkPalletId(), request.warehouseId()); + if (source == null || !"OPEN".equals(source.getPalletStatus())) { + throw conflict("来源托盘已封托,不能直接移箱"); + } + if (cartons.move(current.getId(), current.getVersion(), palletId, operatorId, operatorName, operationId) != 1) { + throw conflict("箱唛绑定已变化,请刷新后重试"); + } + return view(target); + } + + @Transactional + public Pallet seal(long taskId, long palletId, SealRequest request, + long operatorId, String operatorName) { + requireWorkTask(taskId, request.warehouseId(), operatorId); + LoadingWorkPalletEntity replay = pallets.selectBySealOperation(request.operationId().trim()); + if (replay != null) { + if (replay.getId() != palletId || replay.getPickTaskId() != taskId) { + throw conflict("封托幂等号已用于其他请求"); + } + return view(replay); + } + requireOpenPallet(taskId, palletId, request.warehouseId()); + if (cartons.countPalletCartons(palletId) == 0) throw conflict("空托盘不能封托"); + if (cartons.countUnreconciled(taskId) > 0) { + throw conflict("现场上托箱数超过已提交拣货箱数,请先完成原PDA拣货提交"); + } + if (pallets.seal(taskId, palletId, request.warehouseId(), request.expectedVersion(), + request.operationId().trim(), operatorId, operatorName) != 1) { + throw conflict("托盘状态或版本已变化,请刷新后重试"); + } + return view(pallets.selectScoped(taskId, palletId, request.warehouseId())); + } + + @Transactional(readOnly = true) + public List list(long taskId, long warehouseId) { + TaskContext context = pallets.selectTaskContext(taskId, warehouseId); + if (context == null) throw new BizException(ResultCode.NOT_FOUND, "未找到当前仓库的PDA任务"); + return pallets.selectTaskPallets(taskId, warehouseId).stream().map(this::view).toList(); + } + + private TaskContext requireWorkTask(long taskId, long warehouseId, long operatorId) { + TaskContext context = pallets.selectTaskContext(taskId, warehouseId); + if (context == null) throw new BizException(ResultCode.NOT_FOUND, "未找到当前仓库的PDA任务"); + if (!"LOADING".equals(context.taskStatus())) throw conflict("PDA任务已完成,不能继续打托"); + if (context.dropshipItemCount() == null || context.dropshipItemCount() == 0) { + throw conflict("当前任务没有一件代发货物"); + } + if (context.assigneeId() == null || context.assigneeId() != operatorId) { + throw conflict("请先领取PDA任务后再执行打托"); + } + return context; + } + + private LoadingWorkPalletEntity requireOpenPallet(long taskId, long palletId, long warehouseId) { + LoadingWorkPalletEntity pallet = pallets.selectScoped(taskId, palletId, warehouseId); + if (pallet == null) throw new BizException(ResultCode.NOT_FOUND, "现场托盘不存在"); + if (!"OPEN".equals(pallet.getPalletStatus())) throw conflict("托盘不是打托中状态"); + return pallet; + } + + private static LoadingWorkPalletCartonEntity binding(long taskId, long palletId, + CartonCandidate source, String operationId, + long operatorId, String operatorName) { + LoadingWorkPalletCartonEntity value = new LoadingWorkPalletCartonEntity(); + value.setPickTaskId(taskId); + value.setWorkPalletId(palletId); + value.setLoadingPlanItemId(source.getLoadingPlanItemId()); + value.setPickItemId(source.getPickItemId()); + value.setTenantId(source.getTenantId()); + value.setInboundId(source.getInboundId()); + value.setInboundDetailId(source.getInboundDetailId()); + value.setCartonLabelId(source.getCartonLabelId()); + value.setBoxCode(source.getBoxCode()); + value.setSkuCode(source.getSkuCode()); + value.setUnitsPerCarton(source.getUnitsPerCarton()); + value.setBoundBy(operatorId); + value.setBoundByName(operatorName); + value.setOperationId(operationId); + value.setVersion(0); + return value; + } + + private Pallet view(LoadingWorkPalletEntity pallet) { + List values = cartons.selectPalletCartons(pallet.getId()); + Map> bySku = values.stream().collect( + java.util.stream.Collectors.groupingBy(LoadingWorkPalletCartonEntity::getSkuCode, + LinkedHashMap::new, java.util.stream.Collectors.toList())); + List skus = bySku.entrySet().stream().map(entry -> new SkuSummary(entry.getKey(), + entry.getValue().size(), entry.getValue().stream().map(LoadingWorkPalletCartonEntity::getUnitsPerCarton) + .filter(Objects::nonNull).mapToLong(Integer::longValue).sum())).toList(); + List cartonViews = values.stream().map(value -> new Carton(value.getId(), value.getBoxCode(), + value.getSkuCode(), value.getTenantId(), value.getInboundId(), value.getInboundDetailId(), + value.getUnitsPerCarton())).toList(); + return new Pallet(pallet.getId(), pallet.getPalletCode(), pallet.getWarehousePalletId(), + pallet.getPalletStatus(), pallet.getVersion(), values.size(), bySku.size(), + (int) values.stream().map(LoadingWorkPalletCartonEntity::getTenantId).distinct().count(), + skus, cartonViews); + } + + private static BizException conflict(String message) { + return new BizException(ResultCode.CONFLICT, message); + } +} diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumer.java b/firstleg/src/main/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumer.java new file mode 100644 index 000000000..df37c939f --- /dev/null +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumer.java @@ -0,0 +1,135 @@ +package com.baiyue.firstleg.service; + +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baiyue.common.event.EventEnvelope; +import com.baiyue.common.event.EventPayloadCanonicalizer; +import com.baiyue.common.event.EventPayloadHash; +import com.baiyue.common.event.EventTypes; +import com.baiyue.common.event.OverseasInboundOrderDetainedChangedV1; +import com.baiyue.common.event.RegionCode; +import com.baiyue.firstleg.entity.ContainerEventInboxEntity; +import com.baiyue.firstleg.entity.InboundHoldRecordEntity; +import com.baiyue.firstleg.entity.InboundOrderEntity; +import com.baiyue.firstleg.mapper.ContainerEventInboxMapper; +import com.baiyue.firstleg.mapper.InboundHoldRecordMapper; +import com.baiyue.firstleg.mapper.InboundOrderMapper; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Locale; +import java.util.Objects; + +/** 将海外暂扣事实按来源入仓单绑定回写到国内头程入仓单。 */ +@Service +@RequiredArgsConstructor +public class OverseasInboundOrderDetainedConsumer { + private final InboundOrderMapper inboundOrders; + private final InboundHoldRecordMapper holdRecords; + private final ContainerEventInboxMapper inbox; + private final ObjectMapper objectMapper; + + @Transactional + public InboundOrderEntity consume(EventEnvelope event) { + validateEnvelope(event); + OverseasInboundOrderDetainedChangedV1 payload = payload(event); + long inboundId = parsePositive(payload.sourceInboundId(), "来源入仓单 ID 无效"); + long tenantId = parsePositive(event.tenantId(), "客户 ID 无效"); + if (!payload.sourceInboundId().equals(event.aggregateId())) { + throw new IllegalArgumentException("事件 aggregateId 与来源入仓单 ID 不一致"); + } + if (payload.changedAt() == null || payload.inboundCode() == null || payload.inboundCode().isBlank() + || payload.reason() == null || payload.reason().isBlank()) { + throw new IllegalArgumentException("暂扣事件缺少入仓码、原因或变更时间"); + } + + ContainerEventInboxEntity existing = inbox.selectById(event.eventId()); + if (existing != null) { + if (!existing.getPayloadHash().equalsIgnoreCase(event.payloadHash())) { + throw new IllegalArgumentException("相同 eventId 对应的事件摘要冲突"); + } + return requiredInbound(inboundId, tenantId, payload.inboundCode()); + } + ContainerEventInboxEntity inboxRow = inbox(event); + try { + inbox.insert(inboxRow); + } catch (DuplicateKeyException duplicate) { + ContainerEventInboxEntity raced = inbox.selectById(event.eventId()); + if (raced == null || !raced.getPayloadHash().equalsIgnoreCase(event.payloadHash())) throw duplicate; + return requiredInbound(inboundId, tenantId, payload.inboundCode()); + } + + InboundOrderEntity inbound = requiredInbound(inboundId, tenantId, payload.inboundCode()); + if (inbound.getLastOverseasHoldVersion() == null + || event.aggregateVersion() > inbound.getLastOverseasHoldVersion()) { + int updated = inboundOrders.applyOverseasHold(tenantId, inboundId, payload.detained(), + payload.detained() ? payload.reason() : null, event.aggregateVersion(), + parseOperatorId(payload.operatorId())); + if (updated == 1) insertHistory(event, payload, tenantId, inboundId); + } + inboxRow.setStatus("PROCESSED"); + inboxRow.setProcessedAt(LocalDateTime.now(ZoneOffset.UTC)); + inbox.updateById(inboxRow); + return requiredInbound(inboundId, tenantId, payload.inboundCode()); + } + + private void validateEnvelope(EventEnvelope event) { + event.requireValid(); + if (!EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED.equals(event.eventType()) + || event.eventVersion() != 1 || event.sourceRegion() != RegionCode.OVERSEAS + || event.targetRegion() != RegionCode.CN || !"INBOUND_ORDER".equals(event.aggregateType())) { + throw new IllegalArgumentException("不支持的海外入仓单暂扣事件"); + } + if (!EventPayloadHash.matches(EventPayloadCanonicalizer.canonicalize(event.payload()), event.payloadHash())) { + throw new IllegalArgumentException("payloadHash 与事件负载不一致"); + } + } + + private OverseasInboundOrderDetainedChangedV1 payload(EventEnvelope event) { + try { return objectMapper.treeToValue(event.payload(), OverseasInboundOrderDetainedChangedV1.class); } + catch (Exception exception) { throw new IllegalArgumentException("海外入仓单暂扣事件负载无效", exception); } + } + + private InboundOrderEntity requiredInbound(long inboundId, long tenantId, String inboundCode) { + InboundOrderEntity value = inboundOrders.selectActiveByIdForUpdate(inboundId); + if (value == null) throw new IllegalArgumentException("来源头程入仓单不存在"); + if (!Objects.equals(value.getTenantId(), tenantId) || !Objects.equals(value.getInboundCode(), inboundCode)) { + throw new IllegalArgumentException("事件与来源头程入仓单归属不一致"); + } + return value; + } + + private void insertHistory(EventEnvelope event, OverseasInboundOrderDetainedChangedV1 payload, + long tenantId, long inboundId) { + InboundHoldRecordEntity row = new InboundHoldRecordEntity(); + row.setId(IdWorker.getId()); row.setTenantId(tenantId); row.setInboundId(inboundId); + row.setHoldAction(payload.detained() ? "HOLD" : "RELEASE"); row.setReason(payload.reason()); + row.setOperatorId(parseOperatorId(payload.operatorId())); row.setOperatorName(payload.operatorName()); + row.setOperatedAt(LocalDateTime.ofInstant(payload.changedAt(), ZoneOffset.UTC)); + holdRecords.insert(row); + } + + private ContainerEventInboxEntity inbox(EventEnvelope event) { + ContainerEventInboxEntity row = new ContainerEventInboxEntity(); + row.setEventId(event.eventId()); row.setEventType(event.eventType()); row.setEventVersion(event.eventVersion()); + row.setAggregateId(event.aggregateId()); row.setAggregateVersion(event.aggregateVersion()); + row.setPayloadHash(event.payloadHash().toLowerCase(Locale.ROOT)); row.setPayloadJson(event.payload().toString()); + row.setStatus("RECEIVED"); row.setReceivedAt(LocalDateTime.now(ZoneOffset.UTC)); + return row; + } + + private long parsePositive(String value, String message) { + try { long parsed = Long.parseLong(value); if (parsed > 0) return parsed; } + catch (RuntimeException ignored) { } + throw new IllegalArgumentException(message); + } + + private long parseOperatorId(String value) { + try { return value == null ? 0L : Long.parseLong(value); } + catch (NumberFormatException ignored) { return 0L; } + } +} diff --git a/firstleg/src/main/resources/db/phase2-carton-label-versioning-migration.sql b/firstleg/src/main/resources/db/phase2-carton-label-versioning-migration.sql index b15f57089..26df34533 100644 --- a/firstleg/src/main/resources/db/phase2-carton-label-versioning-migration.sql +++ b/firstleg/src/main/resources/db/phase2-carton-label-versioning-migration.sql @@ -15,6 +15,7 @@ DELIMITER ; CALL phase2_carton_label_add_column('label_version', 'INT UNSIGNED NOT NULL DEFAULT 1 COMMENT ''内部兼容批次字段,固定为1且不进入箱码'' AFTER carton_index'); CALL phase2_carton_label_add_column('batch_status', 'VARCHAR(16) NOT NULL DEFAULT ''ACTIVE'' COMMENT ''批次状态:ACTIVE有效、VOID已作废'' AFTER label_version'); CALL phase2_carton_label_add_column('cargo_fingerprint', 'CHAR(64) NULL COMMENT ''影响标签字段的SHA-256指纹'' AFTER batch_status'); +CALL phase2_carton_label_add_column('sku_code', 'VARCHAR(128) NULL COMMENT ''箱唛绑定的SKU快照;二维码内容不变'' AFTER cargo_name'); CALL phase2_carton_label_add_column('voided_at', 'DATETIME(3) NULL COMMENT ''批次作废时间'' AFTER cargo_fingerprint'); DROP PROCEDURE phase2_carton_label_add_column; diff --git a/firstleg/src/main/resources/db/phase2-inbound-carton-label.sql b/firstleg/src/main/resources/db/phase2-inbound-carton-label.sql index e67fc4327..1d93f2ec6 100644 --- a/firstleg/src/main/resources/db/phase2-inbound-carton-label.sql +++ b/firstleg/src/main/resources/db/phase2-inbound-carton-label.sql @@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS fl_inbound_carton_label ( inbound_detail_id BIGINT UNSIGNED NOT NULL COMMENT '订单货物明细ID,不建立跨库外键', cargo_item_no INT UNSIGNED NOT NULL COMMENT '订单货物明细序号快照', cargo_name VARCHAR(255) NULL COMMENT '货物名称快照', + sku_code VARCHAR(128) NULL COMMENT '箱唛绑定的SKU快照;二维码内容不变', detail_package_count INT UNSIGNED NOT NULL COMMENT '该订单货物明细箱数快照', inbound_code VARCHAR(40) NOT NULL COMMENT '入仓码快照', box_code VARCHAR(80) NOT NULL COMMENT '固定二维码内容:入仓码#总箱数-箱下标', diff --git a/firstleg/src/main/resources/db/phase2-inbound-schema.sql b/firstleg/src/main/resources/db/phase2-inbound-schema.sql index 902aede82..c248098c9 100644 --- a/firstleg/src/main/resources/db/phase2-inbound-schema.sql +++ b/firstleg/src/main/resources/db/phase2-inbound-schema.sql @@ -42,6 +42,7 @@ CREATE TABLE IF NOT EXISTS fl_inbound_order ( destination_name VARCHAR(128) NULL COMMENT '目的仓名称快照', hold_flag TINYINT(1) NOT NULL DEFAULT 0 COMMENT '当前是否扣货', current_hold_reason VARCHAR(500) NULL COMMENT '当前扣货理由快照', + last_overseas_hold_version BIGINT UNSIGNED NULL COMMENT '海外暂扣事件最新聚合版本,用于阻止乱序覆盖', total_package_count INT UNSIGNED NULL COMMENT '确认后的总箱数', actual_gross_weight_kg DECIMAL(18,3) NULL COMMENT '已确认称重图片含托盘毛重汇总kg', chargeable_weight_kg DECIMAL(18,3) NULL COMMENT '当前费用快照的计费重量kg', @@ -196,6 +197,7 @@ CREATE TABLE IF NOT EXISTS fl_inbound_carton_label ( inbound_detail_id BIGINT UNSIGNED NOT NULL COMMENT '订单货物明细ID,不建立跨库外键', cargo_item_no INT UNSIGNED NOT NULL COMMENT '订单货物明细序号快照', cargo_name VARCHAR(255) NULL COMMENT '货物名称快照', + sku_code VARCHAR(128) NULL COMMENT '箱唛绑定的SKU快照;二维码内容不变', detail_package_count INT UNSIGNED NOT NULL COMMENT '该订单货物明细箱数快照', inbound_code VARCHAR(40) NOT NULL COMMENT '入仓码快照', box_code VARCHAR(80) NOT NULL COMMENT '固定二维码内容:入仓码#总箱数-箱下标', diff --git a/firstleg/src/main/resources/db/phase3-smart-loading-schema.sql b/firstleg/src/main/resources/db/phase3-smart-loading-schema.sql index 432df921d..221594aca 100644 --- a/firstleg/src/main/resources/db/phase3-smart-loading-schema.sql +++ b/firstleg/src/main/resources/db/phase3-smart-loading-schema.sql @@ -104,8 +104,8 @@ CREATE TABLE IF NOT EXISTS fl_container_event_inbox ( event_id VARCHAR(64) NOT NULL COMMENT '跨区事件全局唯一ID', event_type VARCHAR(64) NOT NULL COMMENT '事件类型', event_version INT UNSIGNED NOT NULL COMMENT '事件契约版本', - aggregate_id VARCHAR(64) NOT NULL COMMENT '货柜聚合ID', - aggregate_version BIGINT UNSIGNED NOT NULL COMMENT '货柜聚合版本', + aggregate_id VARCHAR(64) NOT NULL COMMENT '跨区业务聚合ID', + aggregate_version BIGINT UNSIGNED NOT NULL COMMENT '跨区业务聚合版本', payload_hash VARCHAR(80) NOT NULL COMMENT '规范化负载SHA-256摘要', payload_json JSON NOT NULL COMMENT '原始事件负载', status VARCHAR(16) NOT NULL COMMENT 'RECEIVED或PROCESSED', @@ -113,7 +113,7 @@ CREATE TABLE IF NOT EXISTS fl_container_event_inbox ( processed_at DATETIME(3) NULL COMMENT '处理完成时间(UTC)', PRIMARY KEY (event_id), KEY idx_container_event_aggregate (aggregate_id, aggregate_version) -) ENGINE=InnoDB COMMENT='海外货柜事件消费Inbox'; +) ENGINE=InnoDB COMMENT='海外到国内业务事件消费Inbox'; DROP PROCEDURE IF EXISTS phase3_container_real_unit_columns; DELIMITER $$ @@ -673,6 +673,66 @@ CREATE TABLE IF NOT EXISTS fl_loading_pick_history ( CONSTRAINT fk_loading_pick_history_item FOREIGN KEY (pick_item_id) REFERENCES fl_loading_pick_item (id) ) ENGINE=InnoDB COMMENT='PDA拣货幂等和操作审计历史'; +CREATE TABLE IF NOT EXISTS fl_loading_work_pallet ( + id BIGINT UNSIGNED NOT NULL COMMENT '一件代发现场作业托盘ID', + pick_task_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA装柜拣货任务ID', + loading_plan_id BIGINT UNSIGNED NOT NULL COMMENT '装柜计划ID快照', + warehouse_id BIGINT UNSIGNED NOT NULL COMMENT '作业仓库ID', + warehouse_pallet_id BIGINT UNSIGNED NULL COMMENT '仓库实物托盘ID;未绑定仓库资源时为空', + pallet_code VARCHAR(64) NOT NULL COMMENT '现场托盘号', + open_operation_id VARCHAR(128) NOT NULL COMMENT '开托幂等操作号', + seal_operation_id VARCHAR(128) NULL COMMENT '封托幂等操作号', + pallet_status VARCHAR(16) NOT NULL DEFAULT 'OPEN' COMMENT '托盘状态:OPEN打托中、SEALED已封托、CANCELLED已取消', + created_by BIGINT UNSIGNED NOT NULL COMMENT '创建操作人ID', + created_by_name VARCHAR(64) NULL COMMENT '创建操作人名称快照', + sealed_by BIGINT UNSIGNED NULL COMMENT '封托操作人ID', + sealed_by_name VARCHAR(64) NULL COMMENT '封托操作人名称快照', + sealed_at DATETIME(3) NULL COMMENT '封托时间', + version INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '乐观锁版本号', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY (id), + UNIQUE KEY uk_loading_work_pallet_code (pick_task_id, pallet_code), + UNIQUE KEY uk_loading_work_pallet_open_operation (open_operation_id), + UNIQUE KEY uk_loading_work_pallet_seal_operation (seal_operation_id), + KEY idx_loading_work_pallet_plan (loading_plan_id, pallet_status, id), + CONSTRAINT fk_loading_work_pallet_task FOREIGN KEY (pick_task_id) REFERENCES fl_loading_pick_task (id), + CONSTRAINT fk_loading_work_pallet_plan FOREIGN KEY (loading_plan_id) REFERENCES fl_loading_plan (id), + CONSTRAINT chk_loading_work_pallet_status CHECK (pallet_status IN ('OPEN','SEALED','CANCELLED')) +) ENGINE=InnoDB COMMENT='一件代发PDA现场实际托盘;允许同托盘承载多个客户订单'; + +CREATE TABLE IF NOT EXISTS fl_loading_work_pallet_carton ( + id BIGINT UNSIGNED NOT NULL COMMENT '一件代发现场托盘箱唛绑定ID', + pick_task_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA装柜拣货任务ID', + work_pallet_id BIGINT UNSIGNED NOT NULL COMMENT '现场作业托盘ID', + loading_plan_item_id BIGINT UNSIGNED NOT NULL COMMENT '装柜计划货物明细ID', + pick_item_id BIGINT UNSIGNED NOT NULL COMMENT 'PDA拣货明细ID', + tenant_id BIGINT UNSIGNED NOT NULL COMMENT '箱唛所属客户ID', + inbound_id BIGINT UNSIGNED NOT NULL COMMENT '来源入仓单ID', + inbound_detail_id BIGINT UNSIGNED NOT NULL COMMENT '来源入仓货物明细ID', + carton_label_id BIGINT UNSIGNED NOT NULL COMMENT '稳定箱唛记录ID', + box_code VARCHAR(80) NOT NULL COMMENT '稳定箱唛箱码快照', + sku_code VARCHAR(128) NOT NULL COMMENT 'SKU编码快照', + units_per_carton INT UNSIGNED NULL COMMENT '每箱产品数量快照', + bound_by BIGINT UNSIGNED NOT NULL COMMENT '最后绑定操作人ID', + bound_by_name VARCHAR(64) NULL COMMENT '最后绑定操作人名称快照', + operation_id VARCHAR(128) NOT NULL COMMENT '最近一次绑定或移托幂等操作号', + version INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '乐观锁版本号', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '首次上托时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '最后移托时间', + PRIMARY KEY (id), + UNIQUE KEY uk_loading_work_carton_task_label (pick_task_id, carton_label_id), + UNIQUE KEY uk_loading_work_carton_operation (operation_id), + KEY idx_loading_work_carton_pallet (work_pallet_id, sku_code, id), + KEY idx_loading_work_carton_plan_item (loading_plan_item_id, id), + CONSTRAINT fk_loading_work_carton_task FOREIGN KEY (pick_task_id) REFERENCES fl_loading_pick_task (id), + CONSTRAINT fk_loading_work_carton_pallet FOREIGN KEY (work_pallet_id) REFERENCES fl_loading_work_pallet (id), + CONSTRAINT fk_loading_work_carton_plan_item FOREIGN KEY (loading_plan_item_id) REFERENCES fl_loading_plan_item (id), + CONSTRAINT fk_loading_work_carton_pick_item FOREIGN KEY (pick_item_id) REFERENCES fl_loading_pick_item (id), + CONSTRAINT fk_loading_work_carton_label FOREIGN KEY (carton_label_id) REFERENCES fl_inbound_carton_label (id), + CONSTRAINT chk_loading_work_carton_units CHECK (units_per_carton IS NULL OR units_per_carton > 0) +) ENGINE=InnoDB COMMENT='一件代发PDA逐箱上托事实;客户订单不合并但实际托盘可混托'; + DROP PROCEDURE IF EXISTS repair_phase3_loading_pick_task_foreign_keys; DELIMITER $$ CREATE PROCEDURE repair_phase3_loading_pick_task_foreign_keys() diff --git a/firstleg/src/main/resources/openapi/phase3-pda-convergence-v1.yaml b/firstleg/src/main/resources/openapi/phase3-pda-convergence-v1.yaml index 65e63be75..88f8a2aed 100644 --- a/firstleg/src/main/resources/openapi/phase3-pda-convergence-v1.yaml +++ b/firstleg/src/main/resources/openapi/phase3-pda-convergence-v1.yaml @@ -170,11 +170,104 @@ paths: expectedVersion: {type: integer, minimum: 0} reason: {type: string, minLength: 1, maxLength: 500} responses: {'200': {description: 作废后柜级证据列表}} + /api/platform/v1/first-leg/pda-loading/tasks/{taskId}/pallets: + get: + summary: 查询一件代发现场实际托盘、SKU汇总和逐箱明细 + parameters: + - {$ref: '#/components/parameters/TaskId'} + - {name: warehouseId, in: query, required: true, schema: {type: integer, format: int64, minimum: 1}} + responses: + '200': + description: 当前任务未取消的现场托盘 + content: {application/json: {schema: {type: array, items: {$ref: '#/components/schemas/WorkPallet'}}}} + /api/platform/v1/first-leg/pda-loading/tasks/{taskId}/pallets/open: + post: + summary: 为已领取任务开启一个一件代发现场托盘 + parameters: [{$ref: '#/components/parameters/TaskId'}] + requestBody: + required: true + content: {application/json: {schema: {$ref: '#/components/schemas/OpenPalletRequest'}}} + responses: + '200': {description: 已开启托盘或相同operationId的幂等结果} + /api/platform/v1/first-leg/pda-loading/tasks/{taskId}/pallets/{palletId}/cartons/scan: + post: + summary: 扫描一件代发箱唛上托;箱唛已在其他未封托托盘时按箱移托 + description: 本操作只记录实际箱到托关系,不推进既有拣货数量、订单状态或仓库资源结算。 + parameters: [{$ref: '#/components/parameters/TaskId'}, {$ref: '#/components/parameters/PalletId'}] + requestBody: + required: true + content: {application/json: {schema: {$ref: '#/components/schemas/ScanCartonRequest'}}} + responses: + '200': {description: 包含最新SKU与箱唛汇总的目标托盘} + /api/platform/v1/first-leg/pda-loading/tasks/{taskId}/pallets/{palletId}/seal: + post: + summary: 封托并校验现场上托箱数未超过既有PDA已提交拣货箱数 + parameters: [{$ref: '#/components/parameters/TaskId'}, {$ref: '#/components/parameters/PalletId'}] + requestBody: + required: true + content: {application/json: {schema: {$ref: '#/components/schemas/SealPalletRequest'}}} + responses: + '200': {description: 已封托托盘或A0409数量/版本冲突} components: parameters: TaskId: {name: taskId, in: path, required: true, schema: {type: integer, format: int64, minimum: 1}} ItemId: {name: itemId, in: path, required: true, schema: {type: integer, format: int64, minimum: 1}} + PalletId: {name: palletId, in: path, required: true, schema: {type: integer, format: int64, minimum: 1}} schemas: + OpenPalletRequest: + type: object + required: [warehouseId, palletCode, operationId] + properties: + warehouseId: {type: integer, format: int64, minimum: 1} + warehousePalletId: {type: integer, format: int64, minimum: 1, nullable: true} + palletCode: {type: string, maxLength: 64} + operationId: {type: string, maxLength: 128} + ScanCartonRequest: + type: object + required: [warehouseId, boxCode, operationId] + properties: + warehouseId: {type: integer, format: int64, minimum: 1} + boxCode: {type: string, maxLength: 80} + operationId: {type: string, maxLength: 128} + SealPalletRequest: + type: object + required: [warehouseId, expectedVersion, operationId] + properties: + warehouseId: {type: integer, format: int64, minimum: 1} + expectedVersion: {type: integer, minimum: 0} + operationId: {type: string, maxLength: 128} + WorkPallet: + type: object + required: [id, palletCode, status, version, cartonCount, skuCount, customerCount, skus, cartons] + properties: + id: {type: integer, format: int64} + palletCode: {type: string} + warehousePalletId: {type: integer, format: int64, nullable: true} + status: {type: string, enum: [OPEN, SEALED]} + version: {type: integer, minimum: 0} + cartonCount: {type: integer, minimum: 0} + skuCount: {type: integer, minimum: 0} + customerCount: {type: integer, minimum: 0} + skus: {type: array, items: {$ref: '#/components/schemas/WorkPalletSku'}} + cartons: {type: array, items: {$ref: '#/components/schemas/WorkPalletCarton'}} + WorkPalletSku: + type: object + required: [skuCode, cartonCount, productQuantity] + properties: + skuCode: {type: string} + cartonCount: {type: integer, minimum: 1} + productQuantity: {type: integer, format: int64, minimum: 0} + WorkPalletCarton: + type: object + required: [id, boxCode, skuCode, tenantId, inboundId, inboundDetailId] + properties: + id: {type: integer, format: int64} + boxCode: {type: string} + skuCode: {type: string} + tenantId: {type: integer, format: int64} + inboundId: {type: integer, format: int64} + inboundDetailId: {type: integer, format: int64} + unitsPerCarton: {type: integer, minimum: 1, nullable: true} Task: type: object required: [id, loadingPlanId, containerBookingId, inboundId, inboundVersion, loadingVersion, cabinetNo, sourceType, recordStatus, taskStatus, workflowStatus, version, items] diff --git a/firstleg/src/test/java/com/baiyue/firstleg/controller/PlatformLoadingPdaControllerContractTest.java b/firstleg/src/test/java/com/baiyue/firstleg/controller/PlatformLoadingPdaControllerContractTest.java index 553b49f95..6d477e85e 100644 --- a/firstleg/src/test/java/com/baiyue/firstleg/controller/PlatformLoadingPdaControllerContractTest.java +++ b/firstleg/src/test/java/com/baiyue/firstleg/controller/PlatformLoadingPdaControllerContractTest.java @@ -24,6 +24,13 @@ class PlatformLoadingPdaControllerContractTest { authority("review", "platform:firstleg:pda:review", AuthenticatedUser.class, long.class, ReviewRequest.class); authority("complete", "platform:firstleg:pda:work", AuthenticatedUser.class, long.class, CompleteLoadingRequest.class); authority("operations", "platform:firstleg:pda:view", AuthenticatedUser.class, long.class); + authority("pallets", "platform:firstleg:pda:view", AuthenticatedUser.class, long.class, long.class); + authority("openPallet", "platform:firstleg:pda:work", AuthenticatedUser.class, long.class, + com.baiyue.firstleg.dto.LoadingPalletDtos.OpenRequest.class); + authority("scanCarton", "platform:firstleg:pda:work", AuthenticatedUser.class, long.class, long.class, + com.baiyue.firstleg.dto.LoadingPalletDtos.ScanRequest.class); + authority("sealPallet", "platform:firstleg:pda:work", AuthenticatedUser.class, long.class, long.class, + com.baiyue.firstleg.dto.LoadingPalletDtos.SealRequest.class); } private void authority(String name, String expected, Class... parameterTypes) throws Exception { diff --git a/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingCompletedOutboxServiceTest.java b/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingCompletedOutboxServiceTest.java index 034e20e3c..d6bc53f47 100644 --- a/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingCompletedOutboxServiceTest.java +++ b/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingCompletedOutboxServiceTest.java @@ -290,6 +290,30 @@ class LoadingCompletedOutboxServiceTest { assertThat(event.payload().at("/orders/0/palletCount").asInt()).isEqualTo(2); } + @Test void publishesExactPdaCartonToPalletBindingsWithoutSequentialRedistribution() throws Exception { + Row first = row(11, 100, 101); first.setFulfillmentType("ONE_CLICK_DROPSHIP"); + first.setPackageCount(2); first.setProductQuantity(20); first.setSkuCode("SKU-A"); + Row second = row(11, 100, 102); second.setFulfillmentType("ONE_CLICK_DROPSHIP"); + second.setPackageCount(2); second.setProductQuantity(10); second.setSkuCode("SKU-B"); + when(snapshots.rows(90L)).thenReturn(List.of(first, second)); + when(snapshots.workPalletCount(90L)).thenReturn(2); + when(snapshots.actualPalletRows(90L)).thenReturn(List.of( + actualPallet(11,100,"PLT-01",101,"SKU-A","BOX-A2"), + actualPallet(11,100,"PLT-01",102,"SKU-B","BOX-B1"), + actualPallet(11,100,"PLT-02",101,"SKU-A","BOX-A1"), + actualPallet(11,100,"PLT-02",102,"SKU-B","BOX-B2"))); + + service.publish(90L); + + ArgumentCaptor captor = ArgumentCaptor.forClass(InboundOutboxEntity.class); + verify(outbox).insert(captor.capture()); + EventEnvelope event = mapper.readValue(captor.getValue().getPayloadJson(), EventEnvelope.class); + assertThat(event.payload().at("/orders/0/pallets/0/skuPackages/0/cartonMarks/0").asText()) + .isEqualTo("BOX-A2"); + assertThat(event.payload().at("/orders/0/pallets/1/skuPackages/0/cartonMarks/0").asText()) + .isEqualTo("BOX-A1"); + } + @Test void pricingBusinessTypeCannotTurnAStandardOrderIntoDropship() throws Exception { Row value = row(11, 100, 101); @@ -347,6 +371,13 @@ class LoadingCompletedOutboxServiceTest { row.setPackageCount(packages); row.setProductQuantity(quantity); row.setBoxCode(boxCode); return row; } + private PalletRow actualPallet(long account,long inbound,String palletNo,long detail, + String sku,String boxCode) { + PalletRow row = pallet(account,inbound,0,palletNo,1,detail,sku,1,1,boxCode); + row.setActualBinding(true); + return row; + } + private LabelRow label(long detailId, String boxCode) { LabelRow row = new LabelRow(); row.setInboundDetailId(detailId); row.setBoxCode(boxCode); return row; } diff --git a/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingWorkPalletServiceTest.java b/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingWorkPalletServiceTest.java new file mode 100644 index 000000000..c48acc2e2 --- /dev/null +++ b/firstleg/src/test/java/com/baiyue/firstleg/service/LoadingWorkPalletServiceTest.java @@ -0,0 +1,86 @@ +package com.baiyue.firstleg.service; + +import com.baiyue.firstleg.dto.LoadingPalletDtos.ScanRequest; +import com.baiyue.firstleg.dto.LoadingPalletDtos.SealRequest; +import com.baiyue.firstleg.entity.LoadingWorkPalletCartonEntity; +import com.baiyue.firstleg.entity.LoadingWorkPalletEntity; +import com.baiyue.firstleg.mapper.LoadingWorkPalletCartonMapper; +import com.baiyue.firstleg.mapper.LoadingWorkPalletCartonMapper.CartonCandidate; +import com.baiyue.firstleg.mapper.LoadingWorkPalletMapper; +import com.baiyue.firstleg.mapper.LoadingWorkPalletMapper.TaskContext; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringJUnitConfig(LoadingWorkPalletService.class) +class LoadingWorkPalletServiceTest { + @MockBean private LoadingWorkPalletMapper pallets; + @MockBean private LoadingWorkPalletCartonMapper cartons; + @Resource private LoadingWorkPalletService service; + + @Test + void scansDropshipCartonOntoMixedCustomerWorkPalletWithoutChangingPickWorkflow() { + when(pallets.selectTaskContext(10L, 20L)).thenReturn(context()); + LoadingWorkPalletEntity pallet = pallet(30L, "OPEN"); + when(pallets.selectScoped(10L, 30L, 20L)).thenReturn(pallet); + CartonCandidate candidate = candidate(); + when(cartons.selectCandidates(10L, 20L, "BOX-A")).thenReturn(List.of(candidate)); + doAnswer(invocation -> { + LoadingWorkPalletCartonEntity value = invocation.getArgument(0); + value.setId(50L); + return 1; + }).when(cartons).insert(any(LoadingWorkPalletCartonEntity.class)); + when(cartons.selectPalletCartons(30L)).thenAnswer(invocation -> List.of()); + + service.scan(10L, 30L, new ScanRequest(20L, "BOX-A", "SCAN-1"), 99L, "作业员"); + + var captor = org.mockito.ArgumentCaptor.forClass(LoadingWorkPalletCartonEntity.class); + verify(cartons).insert(captor.capture()); + assertThat(captor.getValue().getTenantId()).isEqualTo(88L); + assertThat(captor.getValue().getLoadingPlanItemId()).isEqualTo(41L); + assertThat(captor.getValue().getSkuCode()).isEqualTo("SKU-A"); + } + + @Test + void refusesToSealUntilScannedCartonsAreReconciledByExistingPickSubmission() { + when(pallets.selectTaskContext(10L, 20L)).thenReturn(context()); + when(pallets.selectScoped(10L, 30L, 20L)).thenReturn(pallet(30L, "OPEN")); + when(cartons.countPalletCartons(30L)).thenReturn(2); + when(cartons.countUnreconciled(10L)).thenReturn(1); + + assertThatThrownBy(() -> service.seal(10L, 30L, + new SealRequest(20L, 0, "SEAL-1"), 99L, "作业员")) + .hasMessageContaining("原PDA拣货提交"); + } + + private static TaskContext context() { + return new TaskContext(10L, 11L, "LOADING", 20L, 99L, 2); + } + + private static LoadingWorkPalletEntity pallet(long id, String status) { + LoadingWorkPalletEntity value = new LoadingWorkPalletEntity(); + value.setId(id); value.setPickTaskId(10L); value.setLoadingPlanId(11L); + value.setWarehouseId(20L); value.setPalletCode("PLT-01"); + value.setPalletStatus(status); value.setVersion(0); + return value; + } + + private static CartonCandidate candidate() { + CartonCandidate value = new CartonCandidate(); + value.setCartonLabelId(40L); value.setTenantId(88L); value.setInboundId(81L); + value.setInboundDetailId(82L); value.setBoxCode("BOX-A"); + value.setLoadingPlanItemId(41L); value.setPickItemId(42L); + value.setSkuCode("SKU-A"); value.setUnitsPerCarton(20); + return value; + } +} diff --git a/firstleg/src/test/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumerTest.java b/firstleg/src/test/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumerTest.java new file mode 100644 index 000000000..f065a93ec --- /dev/null +++ b/firstleg/src/test/java/com/baiyue/firstleg/service/OverseasInboundOrderDetainedConsumerTest.java @@ -0,0 +1,88 @@ +package com.baiyue.firstleg.service; + +import com.baiyue.common.event.EventEnvelope; +import com.baiyue.common.event.EventPayloadCanonicalizer; +import com.baiyue.common.event.EventPayloadHash; +import com.baiyue.common.event.EventTypes; +import com.baiyue.common.event.OverseasInboundOrderDetainedChangedV1; +import com.baiyue.common.event.RegionCode; +import com.baiyue.firstleg.entity.InboundHoldRecordEntity; +import com.baiyue.firstleg.entity.InboundOrderEntity; +import com.baiyue.firstleg.entity.ContainerEventInboxEntity; +import com.baiyue.firstleg.mapper.ContainerEventInboxMapper; +import com.baiyue.firstleg.mapper.InboundHoldRecordMapper; +import com.baiyue.firstleg.mapper.InboundOrderMapper; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.Instant; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class OverseasInboundOrderDetainedConsumerTest { + @Mock InboundOrderMapper inboundOrders; + @Mock InboundHoldRecordMapper holdRecords; + @Mock ContainerEventInboxMapper inbox; + ObjectMapper objectMapper; + OverseasInboundOrderDetainedConsumer consumer; + + @BeforeEach + void setUp() { + objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + consumer = new OverseasInboundOrderDetainedConsumer(inboundOrders, holdRecords, inbox, objectMapper); + } + + @Test + void appliesHoldBySourceInboundIdAndWritesHistory() { + InboundOrderEntity inbound = inbound(null); + when(inboundOrders.selectActiveByIdForUpdate(1001L)).thenReturn(inbound); + when(inboundOrders.applyOverseasHold(88L, 1001L, true, "等待查验", 7L, 66L)).thenReturn(1); + + consumer.consume(event(7, true, "等待查验")); + + verify(inboundOrders).applyOverseasHold(88L, 1001L, true, "等待查验", 7L, 66L); + ArgumentCaptor history = ArgumentCaptor.forClass(InboundHoldRecordEntity.class); + verify(holdRecords).insert(history.capture()); + assertThat(history.getValue().getInboundId()).isEqualTo(1001L); + assertThat(history.getValue().getHoldAction()).isEqualTo("HOLD"); + verify(inbox).updateById(argThat((ContainerEventInboxEntity row) -> "PROCESSED".equals(row.getStatus()))); + } + + @Test + void staleReleaseCannotOverwriteNewerHoldState() { + when(inboundOrders.selectActiveByIdForUpdate(1001L)).thenReturn(inbound(9L)); + + consumer.consume(event(7, false, "解除")); + + verify(inboundOrders, never()).applyOverseasHold(anyLong(), anyLong(), anyBoolean(), any(), anyLong(), anyLong()); + verifyNoInteractions(holdRecords); + verify(inbox).updateById(argThat((ContainerEventInboxEntity row) -> "PROCESSED".equals(row.getStatus()))); + } + + private InboundOrderEntity inbound(Long sourceVersion) { + InboundOrderEntity value = new InboundOrderEntity(); + value.setId(1001L); value.setTenantId(88L); value.setInboundCode("IN-1001"); + value.setLastOverseasHoldVersion(sourceVersion); + return value; + } + + private EventEnvelope event(long version, boolean detained, String reason) { + var payload = new OverseasInboundOrderDetainedChangedV1("1001", "IN-1001", "9001", + "LM-9001", detained, reason, "66", "海外操作员", Instant.parse("2026-08-31T06:00:00Z")); + var node = objectMapper.valueToTree(payload); + return new EventEnvelope(String.format("00000000-0000-4000-8000-%012d", version), + EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED, 1, payload.changedAt(), + RegionCode.OVERSEAS, RegionCode.CN, "88", "INBOUND_ORDER", "1001", version, 1L, + "00000000-0000-4000-8000-000000000099", "hold-1001-" + version, null, node, + EventPayloadHash.sha256(EventPayloadCanonicalizer.canonicalize(node))); + } +} diff --git a/gateway-overseas/src/main/resources/application.yml b/gateway-overseas/src/main/resources/application.yml index d61d0ec19..2d28efe8d 100644 --- a/gateway-overseas/src/main/resources/application.yml +++ b/gateway-overseas/src/main/resources/application.yml @@ -39,9 +39,9 @@ spring: baiyue: security: - issuer: ${JWT_ISSUER:https://auth.example.com} + issuer: ${JWT_ISSUER:auth-service} audience: ${JWT_AUDIENCE:lastleg-api} - jwk-set-uri: ${JWT_JWK_SET_URI:https://auth.example.com/.well-known/jwks.json} + jwk-set-uri: ${JWT_JWK_SET_URI:http://127.0.0.1:9100/api/auth/.well-known/jwks.json} pda-session-validation: auth-url: ${AUTH_SESSION_VALIDATION_URL} internal-service-token: ${INTERNAL_SERVICE_TOKEN} diff --git a/gateway-overseas/src/test/java/com/baiyue/gateway/config/OverseasSecurityValidationDeploymentContractTest.java b/gateway-overseas/src/test/java/com/baiyue/gateway/config/OverseasSecurityValidationDeploymentContractTest.java index 45bd73e1d..e377d6422 100644 --- a/gateway-overseas/src/test/java/com/baiyue/gateway/config/OverseasSecurityValidationDeploymentContractTest.java +++ b/gateway-overseas/src/test/java/com/baiyue/gateway/config/OverseasSecurityValidationDeploymentContractTest.java @@ -9,6 +9,36 @@ import java.nio.file.Path; import static org.assertj.core.api.Assertions.assertThat; class OverseasSecurityValidationDeploymentContractTest { + @Test + void gatewayLocalJwtDefaultsMatchAuthAndLastleg() throws Exception { + String gatewayApplication = read("gateway-overseas/src/main/resources/application.yml"); + String lastlegApplication = read("lastleg-service/src/main/resources/application.yml"); + String localGateway = read("config/nacos/local/gateway-overseas.yml"); + String localLastleg = read("config/nacos/local/lastleg-service.yml"); + String localWarehouse = read("config/nacos/local/warehouse-overseas-service.yml"); + String remoteGateway = read("config/nacos/overseas/gateway-overseas.yml"); + String remoteLastleg = read("config/nacos/overseas/lastleg-service.yml"); + String remoteWarehouse = read("config/nacos/overseas/warehouse-overseas-service.yml"); + + for (String source : new String[]{gatewayApplication, lastlegApplication, localGateway, localLastleg, localWarehouse}) { + assertThat(source) + .contains("${JWT_ISSUER:auth-service}") + .contains("${JWT_AUDIENCE:lastleg-api}") + .contains("${JWT_JWK_SET_URI:http://127.0.0.1:9100/api/auth/.well-known/jwks.json}"); + } + assertThat(gatewayApplication).doesNotContain("auth.example.com"); + assertThat(localGateway) + .contains("${AUTH_SESSION_VALIDATION_URL:http://127.0.0.1:9100}") + .contains("${LASTLEG_SECURITY_SERVICE_URL:http://127.0.0.1:9303}"); + assertThat(localLastleg).contains("${WAREHOUSE_OVERSEAS_SERVICE_URL:http://127.0.0.1:9305}"); + assertThat(localWarehouse).contains("${DOMESTIC_WAREHOUSE_URL:http://127.0.0.1:9205}"); + assertThat(remoteGateway + remoteLastleg + remoteWarehouse) + .doesNotContain("127.0.0.1", "root", "123456"); + assertThat(remoteGateway).contains("${LASTLEG_SECURITY_SERVICE_URL:http://lastleg-service:9303}"); + assertThat(remoteLastleg).contains("${LASTLEG_DB_URL}", "${LASTLEG_DB_USERNAME}", "${LASTLEG_DB_PASSWORD}"); + assertThat(remoteWarehouse).contains("${WAREHOUSE_DB_URL}", "${WAREHOUSE_DB_USERNAME}", "${WAREHOUSE_DB_PASSWORD}"); + } + @Test void gatewayAndEventBridgeUseLastlegSecurityEndpointsWithoutTheRetiredService() throws Exception { String client = read("gateway-overseas/src/main/java/com/baiyue/gateway/filter/HttpOverseasSecurityValidationClient.java"); diff --git a/iam/src/main/resources/db/lastleg-permissions.sql b/iam/src/main/resources/db/lastleg-permissions.sql index 77c2b9c9c..e92663b70 100644 --- a/iam/src/main/resources/db/lastleg-permissions.sql +++ b/iam/src/main/resources/db/lastleg-permissions.sql @@ -39,6 +39,9 @@ VALUES (5107061,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:create','创建海外库位',NULL,'POST',761,'ACTIVE',1,0), (5107062,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:edit','编辑海外库位',NULL,'PUT',762,'ACTIVE',1,0), (5107063,0,'PLATFORM','BUTTON',5107060,'platform:overseas-warehouse:location:delete','删除海外库位',NULL,'DELETE',763,'ACTIVE',1,0), + (5107090,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:outbound:list','海外出库单','/overseas-warehouse/outbounds','GET',765,'ACTIVE',1,0), + (5107091,0,'PLATFORM','BUTTON',5107090,'platform:overseas-warehouse:outbound:create','创建海外出库单',NULL,'POST',766,'ACTIVE',1,0), + (5107092,0,'PLATFORM','MENU',0,'platform:overseas-warehouse:delivery:list','海外派送单','/overseas-warehouse/delivery-orders','GET',767,'ACTIVE',1,0), (5107100,0,'PLATFORM','MENU',0,'platform:overseas-business:purchase:list','海外采购订单','/overseas-business/purchases','GET',800,'ACTIVE',1,0), (5107101,0,'PLATFORM','BUTTON',5107100,'platform:overseas-business:purchase:edit','编辑海外采购订单',NULL,'PATCH',801,'ACTIVE',1,0), (5107102,0,'PLATFORM','BUTTON',5107100,'platform:overseas-business:purchase:review','审核海外采购订单',NULL,'POST',802,'ACTIVE',1,0), @@ -212,3 +215,16 @@ JOIN iam_permission permission ON permission.tenant_id=0 AND permission.app_code WHERE role.tenant_id=0 AND role.app_code='PLATFORM' AND UPPER(role.role_code) IN ('PLATFORM_ADMIN','PLATFORM_CUSTOM_ADMIN') AND role.status='ACTIVE' AND role.deleted=0 AND permission.status='ACTIVE' AND permission.deleted=0; + +-- PLATFORM_CUSTOM_ADMIN needs access to the overseas outbound and delivery lists shown in the admin navigation. +INSERT IGNORE INTO iam_role_permission (tenant_id, role_id, permission_id, created_at) +SELECT 0, role.id, permission.id, NOW(3) +FROM iam_role role +JOIN iam_permission permission ON permission.tenant_id=0 AND permission.app_code='PLATFORM' + AND permission.permission_code IN ( + 'platform:overseas-warehouse:outbound:list', + 'platform:overseas-warehouse:delivery:list' + ) +WHERE role.tenant_id=0 AND role.app_code='PLATFORM' + AND UPPER(role.role_code)='PLATFORM_CUSTOM_ADMIN' + AND role.status='ACTIVE' AND role.deleted=0 AND permission.status='ACTIVE' AND permission.deleted=0; diff --git a/iam/src/test/java/com/baiyue/iam/repository/OverseasPhase4PermissionSqlContractTest.java b/iam/src/test/java/com/baiyue/iam/repository/OverseasPhase4PermissionSqlContractTest.java index e10060315..a691937d0 100644 --- a/iam/src/test/java/com/baiyue/iam/repository/OverseasPhase4PermissionSqlContractTest.java +++ b/iam/src/test/java/com/baiyue/iam/repository/OverseasPhase4PermissionSqlContractTest.java @@ -41,10 +41,12 @@ class OverseasPhase4PermissionSqlContractTest { assertThat(sql).contains(REQUIRED.toArray(String[]::new)); assertThat(sql).contains("'lastleg:pda:operate'"); assertPdaPickerGrant(sql); - assertThat(catalog).hasSize(53).noneMatch(code -> code.endsWith(":view")); + assertThat(catalog).hasSize(56).noneMatch(code -> code.endsWith(":view")); assertThat(sql).contains("permission_code LIKE 'platform:lastleg:%'", "status='INACTIVE'", "deleted=1"); - assertThat(sql).doesNotContain(":carton:", ":product:", ":outbound:", ":delivery:"); + assertThat(sql).doesNotContain(":carton:", ":product:"); + assertThat(catalog).contains("platform:overseas-warehouse:delivery:list"); assertCustomAdminGrant(sql); + assertCustomAdminNavigationGrant(sql); } @Test @@ -58,6 +60,7 @@ class OverseasPhase4PermissionSqlContractTest { assertThat(sql).contains("baiyue_iam.iam_permission", "baiyue_iam.iam_role_permission", "'lastleg:pda:operate'"); assertPdaPickerGrant(sql); assertCustomAdminGrant(sql); + assertCustomAdminNavigationGrant(sql); } @Test @@ -138,6 +141,17 @@ class OverseasPhase4PermissionSqlContractTest { assertThat(sql.substring(grantStart, grantEnd)).contains("lastleg:pda:operate"); } + private static void assertCustomAdminNavigationGrant(String sql) { + int permissionIndex = sql.indexOf("'platform:overseas-warehouse:delivery:list'", sql.indexOf("PLATFORM_CUSTOM_ADMIN needs access")); + assertThat(permissionIndex).isGreaterThanOrEqualTo(0); + int grantStart = sql.lastIndexOf("INSERT IGNORE INTO", permissionIndex); + int grantEnd = sql.indexOf(';', permissionIndex); + assertThat(sql.substring(grantStart, grantEnd)).contains( + "UPPER(role.role_code)='PLATFORM_CUSTOM_ADMIN'", + "platform:overseas-warehouse:outbound:list", + "platform:overseas-warehouse:delivery:list"); + } + private static String matrixBlock(String sql) { int start = sql.indexOf("INSERT INTO tmp_phase4_warehouse_role_permission"); int end = sql.indexOf("DELETE grant_row FROM", start); diff --git a/lastleg-service/AGENTS.md b/lastleg-service/AGENTS.md index fd9e111ed..1a8050e9b 100644 --- a/lastleg-service/AGENTS.md +++ b/lastleg-service/AGENTS.md @@ -28,14 +28,19 @@ | PDA 入仓、实收、上架与完成 | `inboundplan/pda/controller/ReceivingContainerController.java`、`inboundplan/pda/service/ReceivingContainerService.java`、内部编排 `InboundPdaWorkflowService.java`;公网入口固定为 `/api/pda/v1/last-leg/receiving-containers/**`,`/warehouse-profile` 只按 Token 绑定仓解析共享仓库中文名称与编码,旧 planNo/allocationId Controller 已移除 | | PDA 卸柜照片上传、归属校验与预览 | `ReceivingContainerController.java` 的 `/current/unload-photos/files`、`/current/unload-photos/files/{fileId}/download`,网关 `inboundplan/pda/gateway/UnloadPhotoFileGateway.java`、`HttpUnloadPhotoFileGateway.java`;File 类型固定 `OVERSEAS_UNLOAD_PHOTO`,下载只允许当前会话已签收的 fileId 并返回 `no-store` | | 卸柜照片与海外卸柜完成 Outbox | `ReceivingContainerController.java`、`ReceivingContainerService.java`、`inboundplan/pda/service/InboundUnloadCompletedOutboxService.java`、`entity/LastlegEventOutbox.java`、`mapper/LastlegEventOutboxMapper.java`;多计划同柜只能在全柜完成后调用 `publishContainerCompleted`,按来源柜预订 ID 幂等发布一次,完成时刻与拍照时刻分离;事件模型见 `../common-event-contract/AGENTS.md` | +| 尾程暂扣状态回传 | `service/InboundOrderDetainedOutboxService.java`;暂扣与解除暂扣在订单更新事务内按 `sourceInboundId` 写入海外到国内 Outbox,国内按来源入仓单绑定更新 | | 入仓后 SKU 库存余额、库位和流水 | `inventory/`;平台只读查询与非出库库存命令分别从 `inventory/query/controller/InventoryQueryController.java`、`inventory/command/controller/InventoryCommandController.java` 进入 | | 海外PDA盘点、同仓整托移库、托盘组拆换绑与换标现场作业 | `pda/stocktake/`、`pda/move/`、`palletops/`、`relabel/pda/`;版本化契约 `src/main/resources/openapi/overseas-pda-operations-v1.yaml`;全部范围取自PDA Token,不含出库和派送 | +| PDA 仓内通用扫码与操作历史 | `pda/warehouse/controller/PdaWarehouseController.java`、`pda/warehouse/service/PdaWarehouseService.java`;解析库存、库位和托盘码并查询操作历史,固定要求 `lastleg:pda:operate` 与 `ROLE_WAREHOUSE_PICKER`,仓库范围只能取自 PDA Token | | 船节点接收与海外 PDA 可收货门禁 | `shipping/service/ContainerShippingNodeConsumer.java`、`shipping/entity/ContainerShippingGate.java`、`shipping/mapper/ContainerShippingGateMapper.java`;只单调接受实际标准节点,`PICKUP_READY` 及以后才允许对应柜进入 PDA 收货候选 | | 平台排车列表/详情与智能预排 | `dispatch/controller/DispatchQueryController.java`、`SmartDispatchController.java`、`dispatch/mapper/DispatchCandidateMapper.java`、`dispatch/service/DispatchService.java`;草稿柜 `entity/DispatchDraftContainer.java`、`mapper/DispatchDraftContainerMapper.java`,托盘锁 `DispatchPalletLock.java`;入口 `/api/platform/v1/last-leg/dispatches`,范围开关与禁用写操作见“四期开发范围路由” | +| 排车编辑、PDA 出库、派送与交接 | `dispatch/controller/DispatchController.java`、`dispatch/pda/controller/DispatchPdaController.java`、`delivery/controller/DeliveryTransferController.java`、`MerchantDeliveryController.java`、`OutboundPdaController.java` | | 仓储资源调用 | `inboundplan/pda/gateway/WarehouseResourceGateway.java`、`HttpWarehouseResourceGateway.java` | | 仓储转运、历史订单/入仓绑定、可选换标联动 | `transfer/controller/WarehouseTransferController.java`、`transfer/service/WarehouseTransferService.java` | -| 一件代发接收、SKU/箱唛库存 | `dropship/service/DropshipOrderService.java`、`dropship/entity/`、`dropship/mapper/` | +| 一件代发接收、SKU/箱唛库存 | 内部接收入口 `dropship/controller/DropshipOrderController.java` 的 `/internal/v1/lastleg/dropship/receive`,必须经 `InternalServiceTokenGuard`;处理 `dropship/service/DropshipOrderService.java`、`dropship/entity/`、`dropship/mapper/` | | 货物废弃、待审锁箱、审批扣减 | `discard/controller/CargoDiscardController.java`、`discard/service/CargoDiscardService.java` | +| 客户拜访、派送状态与签收附件 | `customervisit/controller/CustomerVisitController.java`、`customervisit/service/CustomerVisitService.java`;平台维护拜访、附件上传、派送完成/状态 | +| 异常订单与派送退回入仓 | `exceptionorder/controller/ExceptionOrderController.java`、`exceptionorder/service/ExceptionOrderService.java`;异常订单建档、关联解析、派送退回候选与标记 | | 海外采购、客户下单、采购审核、客户/采购仓候选与操作流水 | `procurement/controller/`、`procurement/service/OverseasProcurementService.java`;OpenAPI `src/main/resources/openapi/overseas-business-v1.yaml` | | 美国/本土卡车订单 | `trucking/controller/PlatformUsTruckOrderController.java`、`MerchantUsTruckOrderController.java` 及同子域 Service/DTO | | 本土散板订单 | `looseboard/controller/PlatformLocalLooseboardController.java` 及同子域 Service/DTO/Entity/Mapper;OpenAPI `src/main/resources/openapi/overseas-business-v1.yaml` | @@ -47,9 +52,8 @@ ## 四期开发范围路由 -- 四期在本服务内只进入尾程订单、入仓计划/PDA 入仓、换标、产品与箱唛快照、库存、海外采购和本土卡车;本土散板新增实现也归本服务的独立稳定子域。 -- `dispatch/`、`delivery/`、出库单、PDA 出库拣货、派送和其他库存离仓能力虽然已存在于服务代码或表结构中,但不属于当前四期开发与验收范围,不得因代码已存在而自动纳入。 -- 历史排车编辑/取消/出库、PDA 出库和 `delivery/` Controller 默认不注册;只有后续阶段明确启用 `lastleg.future-outbound-api.enabled=true` 才可恢复。平台排车列表与详情由 `lastleg.dispatch-query-api.enabled=true` 独立启用;入库单智能排车候选、草稿锁定/解锁/释放和预排创建由 `lastleg.smart-dispatch-api.enabled=true` 独立启用。这些排车平台入口均不接收 `customerId`:排车列表跨客户分页,详情按全局排车单号定位后使用记录自身客户范围加载明细;智能排车候选未传计划 ID 时跨客户查询,传入时根据计划 ID 解析范围;托盘移入草稿时立即以操作人和随机草稿标识锁定,移除或关闭时释放,草稿锁超时失效;预排创建根据所选托盘 ID 在服务端解析并校验唯一客户范围,并将当前操作人的草稿锁原子转换为正式排车锁。预排只能创建待出库排车并锁定托盘,不得借此开放编辑、取消、depart、PDA 出库或派送。四期平台库存 Controller 不映射 allocate、unallocate、pick、ship。 +- 尾程服务覆盖尾程订单、入仓计划/PDA 入仓、换标、产品与箱唛快照、库存、海外采购、本土卡车、本土散板、排车、出库、派送、客户拜访及异常退回等完整业务域。 +- 排车编辑/取消/出库、PDA 出库和 `delivery/` Controller 与其他尾程业务接口一同注册,不受功能开关控制。平台排车列表与详情仍由 `lastleg.dispatch-query-api.enabled=true` 独立启用;入库单智能排车候选、草稿锁定/解锁/释放和预排创建仍由 `lastleg.smart-dispatch-api.enabled=true` 独立启用。这些排车平台入口均不接收 `customerId`:排车列表跨客户分页,详情按全局排车单号定位后使用记录自身客户范围加载明细;智能排车候选未传计划 ID 时跨客户查询,传入时根据计划 ID 解析范围;托盘移入草稿时立即以操作人和随机草稿标识锁定,移除或关闭时释放,草稿锁超时失效;预排创建根据所选托盘 ID 在服务端解析并校验唯一客户范围,并将当前操作人的草稿锁原子转换为正式排车锁。 - 仓库 ID、类型、状态、地址和能力唯一权威归国内 `warehouse-service`;海外库位、实物托盘和资源锁归 `warehouse-overseas-service/` 且只引用同一国内 `warehouseId`。Lastleg PDA 通过 Warehouse Overseas 内部资源入口校验该国内共享收货仓,不得要求海外数据库重复建仓;浏览器 API 统一经 `gateway-overseas/`。 ## 数据库表索引与注释约束 @@ -75,7 +79,7 @@ - 只接受冻结契约支持的事件类型和版本;校验事件 ID、租户、区域、Payload 哈希及必要字段。 - 同一实际集装箱 `containerNo` 生成一个共享业务尾程单号;`payload.orders` 每项按 `sourceInboundId` 独立落一条雪花 ID `ll_order`,并保存对应明细和初始状态历史。整柜操作不使用 `cabinetNo`。 - `ONE_CLICK_DROPSHIP` 每个来源入仓单必须携带一个或多个不可变来源托盘快照;每托按 SKU 携带箱数、数量和箱唛。校验订单汇总一致、托盘号/SKU/箱唛唯一后,按托建立档案及 SKU 库存,并建立箱唛和流水。仓库实物托盘由 Warehouse API 分配,不得覆盖来源托盘快照。 -- 其他订单类型只建立入仓单维度的尾程订单与冻结明细,来源 `palletCount` 强制为 0;后续 PDA 托盘属于海外仓作业资源。 +- 其他订单类型建立入仓单维度的尾程订单与冻结明细,并保留头程推荐托数;尾程入仓计划至少按推荐托数生成计划托盘,PDA 再把箱级事实绑定到海外仓实物托盘。 - 整个事件消费在一个事务内完成;任意订单失败时不得留下部分订单。 - 相同事件重复投递返回既有结果,不重新生成单号或插入明细。来源入仓单唯一约束是最终防线。 - 装柜完成事件只把尾程订单建立为 `PENDING_ASSIGNMENT`。`FIRST_LEG` 在途库存使用不可变来源入仓码作为计划生成前的 `inbound_order_no` 展示快照;平台人工转换成功后,同一事务将订单推进为 `INBOUND_PLANNED`,并把对应库存余额的该快照收口为新 `planNo`。 @@ -94,7 +98,7 @@ - 列表、详情、导出和更新必须使用同一租户边界,不能仅在列表过滤而让详情或更新越权。 - 目的仓变更必须写状态/操作历史或审计证据,并使用版本控制防止并发覆盖。 - 货物废弃只使用可核对 SKU 和箱唛的一件代发库存。创建申请只能将箱从 `AVAILABLE` 锁定为 `DISCARD_PENDING`,不得扣减 SKU 箱数;实际扣库存节点固定为具备 `lastleg:cargo-discard:approve` 权限的仓库/PDA 审核人确认废弃时,并且必须在审核事务内调用专用 discard adjustment。该 adjustment 必须将箱改为 `DISCARDED`、写逐箱业务流水及唯一幂等键,并校验 `扣减前箱数 - 废弃箱数 = 扣减后箱数` 且逐箱数等于申请总箱数;任一环节失败须整体回滚。拒绝申请只释放锁箱,不得扣库存。 -- 海外采购客户下单固定进入 `PENDING_REVIEW`;四期采购只维护采购订单、审核、入库数量、候选快照和操作流水,不提供采购出库端点、出库记录或出库数量。采购单据不得写入或冒充 `available_packages`;只有海外仓 PDA 完成实收后,`InboundStockReceiptService` 才能以 `PDA_STOCK_IN:{taskId}:{allocationId}` 幂等键调用库存 `stockIn` 转入可用库存。禁止通过通用平台接口绕过 PDA 入可用库存。 +- 海外采购客户下单固定进入 `PENDING_REVIEW`;采购编辑可维护入库数量和采购出库明细,出库量为明细数量汇总,审核通过且有入库量后按 `PENDING_OUTBOUND`、`PARTIALLY_OUTBOUND`、`OUTBOUNDED` 派生展示状态。采购出库只记录采购单据流转,不得写入或冒充 `available_packages`;只有海外仓 PDA 完成实收后,`InboundStockReceiptService` 才能以 `PDA_STOCK_IN:{taskId}:{allocationId}` 幂等键调用库存 `stockIn` 转入可用库存。禁止通过通用平台接口绕过 PDA 入可用库存。 - 派车状态只允许 PDA 拣货、完成和出库动作推进,普通派车编辑接口不得修改状态。PDA 写操作要求 `lastleg:pda:operate`;后台库存表单操作要求 `lastleg:inventory:manage`,盘点调整另要求 `lastleg:inventory:adjust`,转仓库存操作要求 `lastleg:warehouse-transfer:manage`。 - `ll_order.current_status` 只保存尾程主流程状态;换标、转仓、出库和派送等子单精确状态保存到各自单据并汇总到 `sub_order_status`,不得再用子单状态覆盖主流程状态。PDA 入仓及库存落账完成后,主流程状态固定推进为 `INBOUND_COMPLETED`。 diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/config/LastlegRestClientProperties.java b/lastleg-service/src/main/java/com/baiyue/lastleg/config/LastlegRestClientProperties.java index be6fd6900..0cec9a7e7 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/config/LastlegRestClientProperties.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/config/LastlegRestClientProperties.java @@ -10,6 +10,7 @@ import java.time.Duration; public class LastlegRestClientProperties { private ClientTimeouts unloadPhotoFile = new ClientTimeouts(Duration.ofSeconds(3), Duration.ofSeconds(30)); private ClientTimeouts relabelPhotoFile = new ClientTimeouts(Duration.ofSeconds(3), Duration.ofSeconds(30)); + private ClientTimeouts customerVisitFile = new ClientTimeouts(Duration.ofSeconds(3), Duration.ofSeconds(30)); private ClientTimeouts warehouseResource = new ClientTimeouts(Duration.ofSeconds(3), Duration.ofSeconds(5)); private ClientTimeouts palletPhysical = new ClientTimeouts(Duration.ofSeconds(3), Duration.ofSeconds(5)); @@ -17,6 +18,8 @@ public class LastlegRestClientProperties { public void setUnloadPhotoFile(ClientTimeouts value) { unloadPhotoFile = required(value, "unload-photo-file"); } public ClientTimeouts getRelabelPhotoFile() { return relabelPhotoFile; } public void setRelabelPhotoFile(ClientTimeouts value) { relabelPhotoFile = required(value, "relabel-photo-file"); } + public ClientTimeouts getCustomerVisitFile() { return customerVisitFile; } + public void setCustomerVisitFile(ClientTimeouts value) { customerVisitFile = required(value, "customer-visit-file"); } public ClientTimeouts getWarehouseResource() { return warehouseResource; } public void setWarehouseResource(ClientTimeouts value) { warehouseResource = required(value, "warehouse-resource"); } public ClientTimeouts getPalletPhysical() { return palletPhysical; } @@ -51,4 +54,3 @@ public class LastlegRestClientProperties { } } } - diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/controller/LastlegOrderController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/controller/LastlegOrderController.java index a47943906..17ad9e114 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/controller/LastlegOrderController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/controller/LastlegOrderController.java @@ -9,6 +9,7 @@ import com.baiyue.lastleg.dto.LastlegOrderRequests; import com.baiyue.lastleg.dto.LastlegOrderSummary; import com.baiyue.lastleg.dto.ContainerPlanProgressDetail; import com.baiyue.lastleg.dto.ContainerPlanProgressSummary; +import com.baiyue.lastleg.entity.LastlegOrderFee; import com.baiyue.lastleg.security.InternalServiceTokenGuard; import com.baiyue.lastleg.security.OverseasPermissions; import com.baiyue.lastleg.service.LastlegOrderService; @@ -107,6 +108,54 @@ public class LastlegOrderController { return ApiResponse.success(orderService.detail(platformScope(user, customerId), id)); } + @PostMapping("/api/platform/v1/last-leg/orders/{id}/fees") + @PreAuthorize("hasAuthority('" + OverseasPermissions.LAST_MILE_ORDER_EDIT + "')") + @Operation(summary = "Add a fee to one tail-leg order", description = "Required authority: " + OverseasPermissions.LAST_MILE_ORDER_EDIT) + public ApiResponse createFee( + @AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long id, + @Valid @RequestBody LastlegOrderRequests.CreateFee request) { + return ApiResponse.success(orderService.createFee(platformScope(user, customerId), id, request, operatorName(user))); + } + + @PutMapping("/api/platform/v1/last-leg/orders/{id}/detained") + @PreAuthorize("hasAuthority('" + OverseasPermissions.LAST_MILE_ORDER_EDIT + "')") + @Operation(summary = "Set tail-leg order hold status", description = "Required authority: " + OverseasPermissions.LAST_MILE_ORDER_EDIT) + public ApiResponse setDetained( + @AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long id, + @Valid @RequestBody LastlegOrderRequests.SetDetained request) { + orderService.setDetained(platformScope(user, customerId), id, request, + String.valueOf(user.userId()), operatorName(user)); + return ApiResponse.success(null); + } + + @PutMapping("/api/platform/v1/last-leg/orders/{id}/pallet-merge-required") + @PreAuthorize("hasAuthority('" + OverseasPermissions.LAST_MILE_ORDER_EDIT + "')") + @Operation(summary = "Set the PDA pallet-merge reminder for one tail-leg order", + description = "Required authority: " + OverseasPermissions.LAST_MILE_ORDER_EDIT) + public ApiResponse setPalletMergeRequired( + @AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long id, + @Valid @RequestBody LastlegOrderRequests.SetPalletMergeRequired request) { + orderService.setPalletMergeRequired(platformScope(user, customerId), id, request, operatorName(user)); + return ApiResponse.success(null); + } + + @PutMapping("/api/platform/v1/last-leg/orders/pallet-merge-required/batch") + @PreAuthorize("hasAuthority('" + OverseasPermissions.LAST_MILE_ORDER_EDIT + "')") + @Operation(summary = "Mark a selected tail-leg order group for PDA pallet merge") + public ApiResponse setPalletMergeBatch( + @AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @Valid @RequestBody LastlegOrderRequests.SetPalletMergeBatch request) { + orderService.setPalletMergeBatch(platformScope(user, customerId), request, operatorName(user)); + return ApiResponse.success(null); + } + @GetMapping(value = "/api/platform/v1/last-leg/orders/export", produces = "text/csv") @PreAuthorize("hasAuthority('" + OverseasPermissions.LAST_MILE_ORDER_EXPORT + "')") @Operation(summary = "Export filtered tail-leg orders as UTF-8 CSV", description = "Required authority: " + OverseasPermissions.LAST_MILE_ORDER_EXPORT) @@ -156,4 +205,8 @@ public class LastlegOrderController { return requestedCustomer == null || requestedCustomer.isBlank() || "0".equals(requestedCustomer) ? null : requestedCustomer.trim(); } + + private String operatorName(AuthenticatedUser user) { + return user.username() == null || user.username().isBlank() ? String.valueOf(user.userId()) : user.username(); + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/controller/CustomerVisitController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/controller/CustomerVisitController.java new file mode 100644 index 000000000..1c87236ee --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/controller/CustomerVisitController.java @@ -0,0 +1,92 @@ +package com.baiyue.lastleg.customervisit.controller; + +import com.baiyue.common.core.api.ApiResponse; +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.lastleg.customervisit.dto.CustomerVisitRequests; +import com.baiyue.lastleg.customervisit.dto.CustomerVisitResponses; +import com.baiyue.lastleg.customervisit.service.CustomerVisitService; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.server.ResponseStatusException; + +@RestController +@RequestMapping("/api/platform/v1/last-leg") +public class CustomerVisitController { + @Resource private CustomerVisitService service; + + @GetMapping("/customer-visits") + public ApiResponse page(@AuthenticationPrincipal AuthenticatedUser user, + @Valid CustomerVisitRequests.Query query) { + return ApiResponse.success(service.page(scope(user), query)); + } + + @GetMapping("/customer-visits/{visitId}") + public ApiResponse detail(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long visitId) { + return ApiResponse.success(service.detail(requiredScope(user, customerId), visitId)); + } + + @PutMapping("/customer-visits/{visitId}") + public ApiResponse save(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long visitId, + @Valid @RequestBody CustomerVisitRequests.Save request) { + return ApiResponse.success(service.save(requiredScope(user, customerId), operator(user), visitId, request)); + } + + @PostMapping(value = "/customer-visits/{visitId}/attachments", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ApiResponse upload(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable long visitId, + @RequestPart("file") MultipartFile file) { + if (user.userId() == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "当前用户缺少有效ID"); + return ApiResponse.success(service.upload(requiredScope(user, customerId), user.userId(), visitId, file)); + } + + @PostMapping("/delivery-orders/{deliveryNo}/complete") + public ApiResponse completeDelivery( + @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, + @PathVariable String deliveryNo) { + return ApiResponse.success(service.completeDelivery(requiredScope(user, customerId), operator(user), deliveryNo)); + } + + @PutMapping("/delivery-orders/{deliveryNo}/status") + public ApiResponse updateDeliveryStatus( + @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, + @PathVariable String deliveryNo, @Valid @RequestBody CustomerVisitRequests.DeliveryStatus request) { + return ApiResponse.success(service.updateDeliveryStatus( + requiredScope(user, customerId), operator(user), deliveryNo, request)); + } + + private String scope(AuthenticatedUser user) { + if (user == null || user.tenantId() == null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); + return "PLATFORM".equals(user.appCode()) && user.tenantId() == 0L ? null : String.valueOf(user.tenantId()); + } + + private String requiredScope(AuthenticatedUser user, String requestedCustomer) { + String own = scope(user); + if (own != null) return own; + if (!StringUtils.hasText(requestedCustomer)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "请选择客户"); + return requestedCustomer.trim(); + } + + private String operator(AuthenticatedUser user) { + return user.userId() == null ? user.username() : String.valueOf(user.userId()); + } + +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitRequests.java new file mode 100644 index 000000000..5c7ffcb36 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitRequests.java @@ -0,0 +1,35 @@ +package com.baiyue.lastleg.customervisit.dto; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import java.time.LocalDateTime; +import java.util.List; + +public final class CustomerVisitRequests { + private CustomerVisitRequests() { } + + public record Query(@Min(1) long page, @Min(1) @Max(200) long size, + String keyword, String status, String customerId, + LocalDateTime completedFrom, LocalDateTime completedTo) { + public Query { if (page == 0) page = 1; if (size == 0) size = 20; } + } + + public record Save(@NotNull Long selectedInboundOrderId, + @NotBlank @Size(max = 32) String visitResult, + @Size(max = 2000) String customerFeedback, + @Size(max = 1000) String remark, + @Valid @Size(max = 20) List attachments) { } + + public record Attachment(@NotNull Long fileId, + @NotBlank @Size(max = 255) String fileName, + @Size(max = 128) String contentType, + @NotNull @Min(0) Long sizeBytes) { } + + public record DeliveryStatus(@NotBlank @Size(max = 32) String status, + @Size(max = 500) String remark) { } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitResponses.java new file mode 100644 index 000000000..943fb2f28 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/dto/CustomerVisitResponses.java @@ -0,0 +1,31 @@ +package com.baiyue.lastleg.customervisit.dto; + +import java.time.LocalDateTime; +import java.util.List; + +public final class CustomerVisitResponses { + private CustomerVisitResponses() { } + + public record Row(Long id, String customerId, String deliveryNo, String customerName, + String contactName, String contactPhone, String destination, + LocalDateTime deliveryCompletedAt, String status, + String selectedInboundCode, String visitResult, LocalDateTime visitedAt, + int visitCount, List attachments) { } + + public record Page(List records, long current, long size, long total, long pages, + long visitable, long pending, long completed) { } + + public record InboundOption(Long inboundOrderId, String inboundCode, Long lastMileOrderId, + String lastMileOrderNo, String destinationWarehouseCode, + String destinationWarehouseName, int packageCount) { } + + public record Attachment(Long fileId, String fileName, String contentType, long sizeBytes) { } + + public record Detail(Row visit, List inboundOrders, + String customerFeedback, String remark, String visitedBy) { } + + public record UploadedFile(Long fileId, String fileName, String contentType, long sizeBytes) { } + + public record Completed(Long visitId, String deliveryNo, String deliveryStatus, + String visitStatus, boolean replayed) { } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/entity/CustomerVisit.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/entity/CustomerVisit.java new file mode 100644 index 000000000..d750d39c1 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/entity/CustomerVisit.java @@ -0,0 +1,37 @@ +package com.baiyue.lastleg.customervisit.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("ll_customer_visit") +public class CustomerVisit { + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private String customerId; + private Long deliveryOrderId; + private String deliveryNo; + private String customerName; + private String contactName; + private String contactPhone; + private String destination; + private LocalDateTime deliveryCompletedAt; + private String status; + private Long selectedLastMileOrderId; + private String selectedLastMileOrderNo; + private Long selectedInboundOrderId; + private String selectedInboundCode; + private String visitResult; + private String customerFeedback; + private String remark; + private String attachmentJson; + private String visitedBy; + private LocalDateTime visitedAt; + private Integer visitCount; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/CustomerVisitFileGateway.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/CustomerVisitFileGateway.java new file mode 100644 index 000000000..b3d81e99d --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/CustomerVisitFileGateway.java @@ -0,0 +1,10 @@ +package com.baiyue.lastleg.customervisit.gateway; + +import org.springframework.web.multipart.MultipartFile; + +public interface CustomerVisitFileGateway { + UploadedFile upload(String customerId, long visitId, long operatorId, MultipartFile file); + void validate(String customerId, long visitId, long fileId); + + record UploadedFile(long fileId, String fileName, String contentType, long sizeBytes) { } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/HttpCustomerVisitFileGateway.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/HttpCustomerVisitFileGateway.java new file mode 100644 index 000000000..a301603e2 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/gateway/HttpCustomerVisitFileGateway.java @@ -0,0 +1,101 @@ +package com.baiyue.lastleg.customervisit.gateway; + +import com.baiyue.lastleg.config.LastlegRestClientFactory; +import com.baiyue.lastleg.config.LastlegRestClientProperties; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.MediaType; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestClient; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.server.ResponseStatusException; + +import java.io.IOException; + +@Component +public class HttpCustomerVisitFileGateway implements CustomerVisitFileGateway { + private static final String BUSINESS_TYPE = "OVERSEAS_CUSTOMER_VISIT"; + @Resource private RestClient.Builder restClientBuilder; + @Resource private LastlegRestClientFactory restClientFactory; + @Resource private LastlegRestClientProperties restClientProperties; + @Value("${baiyue.clients.file.url:http://127.0.0.1:9205}") private String baseUrl; + @Value("${baiyue.security.internal-service-token:}") private String internalToken; + private RestClient client; + + @PostConstruct + void initializeClient() { + client = restClientFactory.build(restClientBuilder, baseUrl, restClientProperties.getCustomerVisitFile()); + } + + @Override + public UploadedFile upload(String customerId, long visitId, long operatorId, MultipartFile file) { + MultipartBodyBuilder body = new MultipartBodyBuilder(); + try { + body.part("file", new ByteArrayResource(file.getBytes()) { + @Override public String getFilename() { return file.getOriginalFilename(); } + }).contentType(file.getContentType() == null ? MediaType.APPLICATION_OCTET_STREAM + : MediaType.parseMediaType(file.getContentType())); + } catch (IOException exception) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "无法读取回访资料", exception); + } + MetadataEnvelope response = client.post() + .uri("/internal/file/v1/business-files/{tenantId}/{businessType}/{businessId}", + tenantId(customerId), BUSINESS_TYPE, visitId) + .header("X-Internal-Service-Token", internalToken) + .header("X-Operator-Id", String.valueOf(operatorId)) + .contentType(MediaType.MULTIPART_FORM_DATA).body(body.build()).retrieve() + .onStatus(HttpStatusCode::isError, (request, result) -> { + throw new ResponseStatusException(result.getStatusCode().is4xxClientError() + ? HttpStatus.BAD_REQUEST : HttpStatus.SERVICE_UNAVAILABLE, "回访资料上传失败"); + }).body(MetadataEnvelope.class); + requireSuccess(response, "回访资料上传失败"); + Metadata data = response == null ? null : response.data(); + if (data == null || data.fileId() == null) throw new ResponseStatusException( + HttpStatus.SERVICE_UNAVAILABLE, "文件服务未返回回访资料元数据"); + return new UploadedFile(data.fileId(), data.fileName(), data.contentType(), + data.sizeBytes() == null ? 0L : data.sizeBytes()); + } + + @Override + public void validate(String customerId, long visitId, long fileId) { + long tenantId = tenantId(customerId); + MetadataEnvelope response = client.get() + .uri("/internal/file/v1/business-files/{tenantId}/{businessType}/{businessId}/{fileId}", + tenantId, BUSINESS_TYPE, visitId, fileId) + .header("X-Internal-Service-Token", internalToken).retrieve() + .onStatus(HttpStatusCode::isError, (request, result) -> { + throw new ResponseStatusException(result.getStatusCode().is4xxClientError() + ? HttpStatus.BAD_REQUEST : HttpStatus.SERVICE_UNAVAILABLE, "回访资料不存在或归属不正确"); + }).body(MetadataEnvelope.class); + requireSuccess(response, "回访资料不存在或归属不正确"); + Metadata data = response == null ? null : response.data(); + if (data == null || data.fileId() == null || data.fileId() != fileId + || data.tenantId() == null || data.tenantId() != tenantId + || !BUSINESS_TYPE.equals(data.businessType()) || !String.valueOf(visitId).equals(data.businessId())) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "回访资料与当前记录不匹配"); + } + } + + private static long tenantId(String customerId) { + try { return Long.parseLong(customerId); } + catch (NumberFormatException exception) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "客户ID格式无效", exception); + } + } + + private static void requireSuccess(MetadataEnvelope response, String fallback) { + if (response == null) throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, fallback); + if ("200".equals(response.code())) return; + throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, + response.message() == null || response.message().isBlank() ? fallback : response.message()); + } + + private record MetadataEnvelope(String code, String message, Metadata data) { } + private record Metadata(Long fileId, Long tenantId, String businessType, String businessId, + String fileName, String contentType, Long sizeBytes) { } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/mapper/CustomerVisitMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/mapper/CustomerVisitMapper.java new file mode 100644 index 000000000..f52254c71 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/mapper/CustomerVisitMapper.java @@ -0,0 +1,17 @@ +package com.baiyue.lastleg.customervisit.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.customervisit.entity.CustomerVisit; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +@Mapper +public interface CustomerVisitMapper extends BaseMapper { + @Select("SELECT * FROM ll_customer_visit WHERE customer_id=#{customerId} AND delivery_order_id=#{deliveryOrderId} LIMIT 1") + CustomerVisit findByDelivery(@Param("customerId") String customerId, + @Param("deliveryOrderId") Long deliveryOrderId); + + @Select("SELECT * FROM ll_customer_visit WHERE customer_id=#{customerId} AND id=#{id} LIMIT 1 FOR UPDATE") + CustomerVisit lockById(@Param("customerId") String customerId, @Param("id") Long id); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/service/CustomerVisitService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/service/CustomerVisitService.java new file mode 100644 index 000000000..416d19088 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/customervisit/service/CustomerVisitService.java @@ -0,0 +1,315 @@ +package com.baiyue.lastleg.customervisit.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baiyue.lastleg.customervisit.dto.CustomerVisitRequests; +import com.baiyue.lastleg.customervisit.dto.CustomerVisitResponses; +import com.baiyue.lastleg.customervisit.entity.CustomerVisit; +import com.baiyue.lastleg.customervisit.gateway.CustomerVisitFileGateway; +import com.baiyue.lastleg.customervisit.mapper.CustomerVisitMapper; +import com.baiyue.lastleg.delivery.entity.DeliveryOperation; +import com.baiyue.lastleg.delivery.entity.DeliveryOrder; +import com.baiyue.lastleg.delivery.mapper.DeliveryOperationMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryOrderMapper; +import com.baiyue.lastleg.entity.LastlegInboundOrder; +import com.baiyue.lastleg.entity.LastlegOrder; +import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; +import com.baiyue.lastleg.mapper.LastlegInboundOrderMapper; +import com.baiyue.lastleg.mapper.LastlegOrderMapper; +import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.annotation.Resource; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.server.ResponseStatusException; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +@Service +public class CustomerVisitService { + private static final Set RESULTS = Set.of("SATISFIED", "NORMAL", "FOLLOW_UP", "NO_CONTACT"); + private static final long MAX_FILE_BYTES = 20L * 1024L * 1024L; + private static final Set FILE_EXTENSIONS = Set.of( + "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "jpg", "jpeg", "png", "webp"); + + @Resource private CustomerVisitMapper visits; + @Resource private DeliveryOrderMapper deliveryOrders; + @Resource private DeliveryOperationMapper deliveryOperations; + @Resource private LastlegOrderMapper lastMileOrders; + @Resource private LastlegInboundOrderMapper inboundOrders; + @Resource private LastlegOrderStatusHistoryMapper histories; + @Resource private CustomerVisitFileGateway files; + @Resource private ObjectMapper objectMapper; + + public CustomerVisitResponses.Page page(String scopedCustomerId, CustomerVisitRequests.Query query) { + String customerId = customerScope(scopedCustomerId, query.customerId()); + LambdaQueryWrapper filter = filter(customerId, query); + Page result = visits.selectPage(new Page<>(query.page(), query.size()), + filter.orderByDesc(CustomerVisit::getDeliveryCompletedAt).orderByDesc(CustomerVisit::getId)); + LambdaQueryWrapper countScope = new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), CustomerVisit::getCustomerId, customerId); + long visitable = visits.selectCount(countScope); + long pending = visits.selectCount(new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), CustomerVisit::getCustomerId, customerId) + .eq(CustomerVisit::getStatus, "PENDING")); + long completed = visits.selectCount(new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), CustomerVisit::getCustomerId, customerId) + .eq(CustomerVisit::getStatus, "COMPLETED")); + return new CustomerVisitResponses.Page(result.getRecords().stream().map(this::row).toList(), + result.getCurrent(), result.getSize(), result.getTotal(), result.getPages(), visitable, pending, completed); + } + + public CustomerVisitResponses.Detail detail(String customerId, long visitId) { + CustomerVisit visit = required(customerId, visitId); + return detail(visit); + } + + @Transactional + public CustomerVisitResponses.Completed completeDelivery(String customerId, String operatorId, String deliveryNo) { + DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo); + if (delivery == null) throw notFound("派送单不存在"); + if (Set.of("RETURN_PENDING", "REJECTED", "CANCELLED").contains(delivery.getStatus())) { + throw conflict("当前派送状态不能标记为派送完成"); + } + boolean replayed = "DELIVERED".equals(delivery.getStatus()); + LocalDateTime now = replayed && delivery.getSignedAt() != null ? delivery.getSignedAt() : LocalDateTime.now(); + if (!replayed) { + delivery.setStatus("DELIVERED"); + delivery.setSignedAt(now); + delivery.setUpdatedAt(now); + deliveryOrders.updateById(delivery); + for (LastlegOrder order : lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegOrder::getCustomerId, customerId) + .eq(LastlegOrder::getDeliveryOrderNo, deliveryNo))) { + String from = order.getCurrentStatus(); + order.setCurrentStatus("DELIVERED"); + order.setSubOrderStatus("DELIVERY:DELIVERED"); + order.setUpdatedAt(now); + lastMileOrders.updateById(order); + LastlegOrderStatusHistory history = new LastlegOrderStatusHistory(); + history.setCustomerId(customerId); history.setOrderId(order.getId()); history.setFromStatus(from); + history.setToStatus("DELIVERED"); history.setEventId("DELIVERY_COMPLETED:" + deliveryNo); + history.setRemark("派送完成"); history.setCreatedAt(now); histories.insert(history); + } + recordOperation(delivery, operatorId, "COMPLETE_DELIVERY", "派送完成并生成客户回访记录", now); + } + CustomerVisit visit = ensureVisit(delivery, now); + return new CustomerVisitResponses.Completed(visit.getId(), deliveryNo, delivery.getStatus(), visit.getStatus(), replayed); + } + + @Transactional + public CustomerVisitResponses.Completed updateDeliveryStatus(String customerId, String operatorId, + String deliveryNo, + CustomerVisitRequests.DeliveryStatus request) { + String target = trim(request.status()).toUpperCase(Locale.ROOT); + if (!Set.of("WAIT_DELIVERY", "DELIVERING", "DELIVERED", "REJECTED", "CANCELLED").contains(target)) { + throw badRequest("不支持的派送状态"); + } + if ("DELIVERED".equals(target)) return completeDelivery(customerId, operatorId, deliveryNo); + DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo); + if (delivery == null) throw notFound("派送单不存在"); + if ("RETURN_PENDING".equals(delivery.getStatus())) throw conflict("退货处理中的派送单不能直接修改状态"); + if (target.equals(delivery.getStatus())) { + CustomerVisit existing = visits.findByDelivery(customerId, delivery.getId()); + return new CustomerVisitResponses.Completed(existing == null ? null : existing.getId(), deliveryNo, + delivery.getStatus(), existing == null ? null : existing.getStatus(), true); + } + String from = delivery.getStatus(); + LocalDateTime now = LocalDateTime.now(); + delivery.setStatus(target); + if ("DELIVERING".equals(target) && delivery.getDispatchedAt() == null) delivery.setDispatchedAt(now); + delivery.setUpdatedAt(now); + deliveryOrders.updateById(delivery); + String result = "派送状态由 " + from + " 修改为 " + target; + if (StringUtils.hasText(request.remark())) result += ",备注:" + trim(request.remark()); + recordOperation(delivery, operatorId, "UPDATE_DELIVERY_STATUS", result, now); + CustomerVisit existing = visits.findByDelivery(customerId, delivery.getId()); + return new CustomerVisitResponses.Completed(existing == null ? null : existing.getId(), deliveryNo, + delivery.getStatus(), existing == null ? null : existing.getStatus(), false); + } + + @Transactional + public CustomerVisitResponses.Detail save(String customerId, String operatorId, long visitId, + CustomerVisitRequests.Save request) { + CustomerVisit visit = visits.lockById(customerId, visitId); + if (visit == null) throw notFound("回访记录不存在"); + CustomerVisitResponses.InboundOption selected = inboundOptions(visit).stream() + .filter(option -> Objects.equals(option.inboundOrderId(), request.selectedInboundOrderId())) + .findFirst().orElseThrow(() -> conflict("所选入仓单不属于该派送单")); + String result = trim(request.visitResult()).toUpperCase(Locale.ROOT); + if (!RESULTS.contains(result)) throw badRequest("不支持的回访结果"); + List attachments = new ArrayList<>(); + Map unique = new LinkedHashMap<>(); + if (request.attachments() != null) { + for (CustomerVisitRequests.Attachment attachment : request.attachments()) { + files.validate(customerId, visitId, attachment.fileId()); + unique.put(attachment.fileId(), new CustomerVisitResponses.Attachment(attachment.fileId(), + trim(attachment.fileName()), trim(attachment.contentType()), attachment.sizeBytes())); + } + } + attachments.addAll(unique.values()); + LocalDateTime now = LocalDateTime.now(); + visit.setSelectedInboundOrderId(selected.inboundOrderId()); + visit.setSelectedInboundCode(selected.inboundCode()); + visit.setSelectedLastMileOrderId(selected.lastMileOrderId()); + visit.setSelectedLastMileOrderNo(selected.lastMileOrderNo()); + visit.setVisitResult(result); + visit.setCustomerFeedback(trim(request.customerFeedback())); + visit.setRemark(trim(request.remark())); + visit.setAttachmentJson(writeAttachments(attachments)); + visit.setStatus("COMPLETED"); + visit.setVisitedBy(operatorId); + visit.setVisitedAt(now); + visit.setVisitCount(Objects.requireNonNullElse(visit.getVisitCount(), 0) + 1); + visit.setUpdatedAt(now); + visits.updateById(visit); + DeliveryOrder delivery = deliveryOrders.findByNo(customerId, visit.getDeliveryNo()); + if (delivery != null) recordOperation(delivery, operatorId, "CUSTOMER_VISIT", + "已完成第" + visit.getVisitCount() + "次客户回访,入仓单 " + selected.inboundCode(), now); + return detail(visit); + } + + public CustomerVisitResponses.UploadedFile upload(String customerId, long operatorId, long visitId, + MultipartFile file) { + required(customerId, visitId); + validateFile(file); + CustomerVisitFileGateway.UploadedFile uploaded = files.upload(customerId, visitId, operatorId, file); + return new CustomerVisitResponses.UploadedFile(uploaded.fileId(), uploaded.fileName(), + uploaded.contentType(), uploaded.sizeBytes()); + } + + private CustomerVisit ensureVisit(DeliveryOrder delivery, LocalDateTime completedAt) { + CustomerVisit existing = visits.findByDelivery(delivery.getCustomerId(), delivery.getId()); + if (existing != null) return existing; + List orders = orders(delivery); + CustomerVisit value = new CustomerVisit(); + value.setId(IdWorker.getId()); value.setCustomerId(delivery.getCustomerId()); + value.setDeliveryOrderId(delivery.getId()); value.setDeliveryNo(delivery.getDeliveryNo()); + value.setCustomerName(delivery.getCustomerName()); + value.setDestination(orders.stream().map(LastlegOrder::getFbaWarehouseCode) + .filter(StringUtils::hasText).distinct().reduce((left, right) -> left + ", " + right).orElse(null)); + value.setDeliveryCompletedAt(completedAt); value.setStatus("PENDING"); value.setVisitCount(0); + value.setAttachmentJson("[]"); value.setCreatedAt(LocalDateTime.now()); value.setUpdatedAt(value.getCreatedAt()); + try { visits.insert(value); } + catch (DuplicateKeyException exception) { + CustomerVisit concurrent = visits.findByDelivery(delivery.getCustomerId(), delivery.getId()); + if (concurrent != null) return concurrent; + throw exception; + } + return value; + } + + private CustomerVisitResponses.Detail detail(CustomerVisit visit) { + return new CustomerVisitResponses.Detail(row(visit), inboundOptions(visit), visit.getCustomerFeedback(), + visit.getRemark(), visit.getVisitedBy()); + } + + private List inboundOptions(CustomerVisit visit) { + List orders = lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegOrder::getCustomerId, visit.getCustomerId()) + .eq(LastlegOrder::getDeliveryOrderNo, visit.getDeliveryNo()) + .orderByAsc(LastlegOrder::getInboundCode)); + if (orders.isEmpty()) return List.of(); + List codes = orders.stream().map(LastlegOrder::getInboundCode).filter(StringUtils::hasText).distinct().toList(); + Map byCode = new LinkedHashMap<>(); + if (!codes.isEmpty()) inboundOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegInboundOrder::getCustomerId, visit.getCustomerId()).in(LastlegInboundOrder::getInboundCode, codes)) + .forEach(value -> byCode.putIfAbsent(value.getInboundCode(), value)); + return orders.stream().map(order -> { + LastlegInboundOrder inbound = byCode.get(order.getInboundCode()); + return new CustomerVisitResponses.InboundOption(inbound == null ? null : inbound.getId(), + order.getInboundCode(), order.getId(), order.getOrderNo(), order.getFbaWarehouseCode(), + inbound == null ? null : inbound.getDestinationWarehouseName(), + Objects.requireNonNullElse(order.getPackageCount(), 0)); + }).filter(option -> option.inboundOrderId() != null).toList(); + } + + private List orders(DeliveryOrder delivery) { + return lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegOrder::getCustomerId, delivery.getCustomerId()) + .eq(LastlegOrder::getDeliveryOrderNo, delivery.getDeliveryNo())); + } + + private CustomerVisitResponses.Row row(CustomerVisit value) { + return new CustomerVisitResponses.Row(value.getId(), value.getCustomerId(), value.getDeliveryNo(), + value.getCustomerName(), value.getContactName(), value.getContactPhone(), value.getDestination(), + value.getDeliveryCompletedAt(), value.getStatus(), value.getSelectedInboundCode(), value.getVisitResult(), + value.getVisitedAt(), Objects.requireNonNullElse(value.getVisitCount(), 0), readAttachments(value.getAttachmentJson())); + } + + private LambdaQueryWrapper filter(String customerId, CustomerVisitRequests.Query query) { + return new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), CustomerVisit::getCustomerId, customerId) + .and(StringUtils.hasText(query.keyword()), wrapper -> wrapper + .like(CustomerVisit::getDeliveryNo, trim(query.keyword())) + .or().like(CustomerVisit::getCustomerName, trim(query.keyword())) + .or().like(CustomerVisit::getContactName, trim(query.keyword())) + .or().like(CustomerVisit::getContactPhone, trim(query.keyword()))) + .eq(StringUtils.hasText(query.status()), CustomerVisit::getStatus, trim(query.status())) + .ge(query.completedFrom() != null, CustomerVisit::getDeliveryCompletedAt, query.completedFrom()) + .le(query.completedTo() != null, CustomerVisit::getDeliveryCompletedAt, query.completedTo()); + } + + private CustomerVisit required(String customerId, long visitId) { + CustomerVisit value = visits.selectOne(new LambdaQueryWrapper() + .eq(CustomerVisit::getCustomerId, customerId).eq(CustomerVisit::getId, visitId)); + if (value == null) throw notFound("回访记录不存在"); + return value; + } + + private void recordOperation(DeliveryOrder delivery, String operatorId, String node, String result, LocalDateTime now) { + DeliveryOperation operation = new DeliveryOperation(); + operation.setCustomerId(delivery.getCustomerId()); operation.setDeliveryOrderId(delivery.getId()); + operation.setOperationNode(node); operation.setOperatorId(operatorId); operation.setOperatorName(operatorId); + operation.setResult(result); operation.setCreatedAt(now); deliveryOperations.insert(operation); + } + + private String writeAttachments(List values) { + try { return objectMapper.writeValueAsString(values); } + catch (JsonProcessingException exception) { throw new IllegalStateException("无法保存回访资料", exception); } + } + + private List readAttachments(String value) { + if (!StringUtils.hasText(value)) return List.of(); + try { return objectMapper.readValue(value, new TypeReference<>() { }); } + catch (JsonProcessingException exception) { throw new IllegalStateException("回访资料数据损坏", exception); } + } + + private static void validateFile(MultipartFile file) { + if (file == null || file.isEmpty()) throw badRequest("请选择回访资料文件"); + if (file.getSize() > MAX_FILE_BYTES) throw badRequest("单个回访资料不能超过20MB"); + String name = Objects.requireNonNullElse(file.getOriginalFilename(), ""); + int dot = name.lastIndexOf('.'); + String extension = dot < 0 ? "" : name.substring(dot + 1).toLowerCase(Locale.ROOT); + if (!FILE_EXTENSIONS.contains(extension)) throw badRequest("仅支持PDF、Office、JPG、PNG和WEBP文件"); + } + + private static String customerScope(String scopedCustomerId, String requestedCustomerId) { + if (StringUtils.hasText(scopedCustomerId)) { + if (StringUtils.hasText(requestedCustomerId) && !scopedCustomerId.equals(trim(requestedCustomerId))) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "不能访问其他客户数据"); + } + return scopedCustomerId; + } + return trim(requestedCustomerId); + } + + private static String trim(String value) { return StringUtils.hasText(value) ? value.trim() : null; } + private static ResponseStatusException badRequest(String message) { return new ResponseStatusException(HttpStatus.BAD_REQUEST, message); } + private static ResponseStatusException notFound(String message) { return new ResponseStatusException(HttpStatus.NOT_FOUND, message); } + private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/DeliveryTransferController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/DeliveryTransferController.java index b56b7d5e3..4ce4ba9ec 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/DeliveryTransferController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/DeliveryTransferController.java @@ -6,18 +6,18 @@ import com.baiyue.common.security.AuthenticatedUser; import com.baiyue.lastleg.delivery.dto.DeliveryRequests; import com.baiyue.lastleg.delivery.dto.DeliveryResponses; import com.baiyue.lastleg.delivery.service.DeliveryTransferService; +import com.baiyue.lastleg.security.OverseasPermissions; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.validation.Valid; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpStatus; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; @RestController -@ConditionalOnProperty(name = "lastleg.future-outbound-api.enabled", havingValue = "true") @RequestMapping("/api/platform/v1/last-leg") @Tag(name = "Outbound and delivery") public class DeliveryTransferController { @@ -32,11 +32,19 @@ public class DeliveryTransferController { return ApiResponse.success(service.transfer(scope(user, customerId), dispatchNo, operator(user), request)); } + @PostMapping("/outbound-orders/one-click-dropship") + @Operation(summary = "Create one-click dropship outbound and push a PDA picking task") + public ApiResponse createOneClick( + @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, + @Valid @RequestBody DeliveryRequests.OneClickOutbound request) { + return ApiResponse.success(service.createOneClick(scope(user, customerId), operator(user), request)); + } + @GetMapping("/outbound-orders") public ApiResponse> outboundPage( @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, @Valid DeliveryRequests.Query query) { - return ApiResponse.success(service.outboundPage(scope(user, customerId), query)); + return ApiResponse.success(service.outboundPage(listScope(user, customerId), query)); } @GetMapping("/outbound-orders/{outboundNo}") @@ -46,18 +54,30 @@ public class DeliveryTransferController { return ApiResponse.success(service.outboundDetail(scope(user, customerId), outboundNo, query)); } + @PostMapping("/outbound-orders/{outboundNo}/cancel") + @Operation(summary = "Cancel outbound and linked delivery, restoring inventory") + public ApiResponse cancelOutbound(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable String outboundNo, + @Valid @RequestBody DeliveryRequests.Cancel request) { + service.cancelOutbound(scope(user, customerId), operator(user), outboundNo, request); + return ApiResponse.success(null); + } + @GetMapping("/delivery-orders") + @PreAuthorize("hasAuthority('" + OverseasPermissions.DELIVERY_LIST + "')") public ApiResponse> deliveryPage( @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, @Valid DeliveryRequests.Query query) { - return ApiResponse.success(service.deliveryPage(scope(user, customerId), query)); + return ApiResponse.success(service.deliveryPage(listScope(user, customerId), query)); } @GetMapping("/delivery-orders/{deliveryNo}") - public ApiResponse deliveryDetail( + @PreAuthorize("hasAuthority('" + OverseasPermissions.DELIVERY_LIST + "')") + public ApiResponse deliveryDetail( @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, - @PathVariable String deliveryNo, @Valid DeliveryRequests.PalletQuery query) { - return ApiResponse.success(service.deliveryDetail(scope(user, customerId), deliveryNo, query)); + @PathVariable String deliveryNo) { + return ApiResponse.success(service.deliveryDetail(scope(user, customerId), deliveryNo)); } private String scope(AuthenticatedUser user, String requestedCustomer) { @@ -73,4 +93,9 @@ public class DeliveryTransferController { private String operator(AuthenticatedUser user) { return user.userId() == null ? user.username() : String.valueOf(user.userId()); } + private String listScope(AuthenticatedUser user, String requestedCustomer) { + if (user == null || user.tenantId() == null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); + if ("PLATFORM".equals(user.appCode()) && user.tenantId() == 0L) return requestedCustomer == null || requestedCustomer.isBlank() ? null : requestedCustomer.trim(); + return String.valueOf(user.tenantId()); + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/MerchantDeliveryController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/MerchantDeliveryController.java new file mode 100644 index 000000000..a2eea0102 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/MerchantDeliveryController.java @@ -0,0 +1,46 @@ +package com.baiyue.lastleg.delivery.controller; + +import com.baiyue.common.core.api.ApiResponse; +import com.baiyue.common.core.api.PageResult; +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.lastleg.delivery.dto.DeliveryRequests; +import com.baiyue.lastleg.delivery.dto.DeliveryResponses; +import com.baiyue.lastleg.delivery.service.DeliveryTransferService; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +@RestController +@RequestMapping("/api/merchant/v1/last-leg/delivery-orders") +public class MerchantDeliveryController { + @Resource private DeliveryTransferService service; + + @GetMapping + public ApiResponse> page(@AuthenticationPrincipal AuthenticatedUser user, + @Valid DeliveryRequests.Query query) { + return ApiResponse.success(service.deliveryPage(customer(user), query)); + } + + @PostMapping + public ApiResponse create(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam String dispatchNo, + @Valid @RequestBody DeliveryRequests.Transfer request) { + return ApiResponse.success(service.transfer(customer(user), dispatchNo, operator(user), request)); + } + + @GetMapping("/{deliveryNo}") + public ApiResponse detail(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable String deliveryNo) { + return ApiResponse.success(service.deliveryDetail(customer(user), deliveryNo)); + } + + private String customer(AuthenticatedUser user) { + if (user == null || user.tenantId() == null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); + if ("PLATFORM".equals(user.appCode()) || user.tenantId() == 0L) throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Merchant identity is required"); + return String.valueOf(user.tenantId()); + } + private String operator(AuthenticatedUser user) { return user.userId() == null ? user.username() : String.valueOf(user.userId()); } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/OutboundPdaController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/OutboundPdaController.java new file mode 100644 index 000000000..4c88c2ff4 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/controller/OutboundPdaController.java @@ -0,0 +1,41 @@ +package com.baiyue.lastleg.delivery.controller; + +import com.baiyue.common.core.api.ApiResponse; +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.lastleg.delivery.dto.DeliveryRequests; +import com.baiyue.lastleg.delivery.dto.DeliveryResponses; +import com.baiyue.lastleg.delivery.service.DeliveryTransferService; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +@RestController +@RequestMapping("/api/pda/v1/last-leg/outbound-tasks") +public class OutboundPdaController { + @Resource private DeliveryTransferService service; + @PostMapping("/{taskNo}/pick") + public ApiResponse pick(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable String taskNo, @RequestParam(required=false) String customerId, + @Valid @RequestBody DeliveryRequests.PdaPick request) { + return ApiResponse.success(service.pdaPick(scope(user, customerId), operator(user), taskNo, request)); + } + @GetMapping + public ApiResponse> list(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required=false) String customerId, @RequestParam(required=false) String status) { + return ApiResponse.success(service.pdaTasks(scope(user, customerId), status)); + } + @PostMapping("/{taskNo}/complete") + public ApiResponse complete(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable String taskNo, @RequestParam(required=false) String customerId) { + return ApiResponse.success(service.completePda(scope(user, customerId), taskNo)); + } + private String scope(AuthenticatedUser user, String requested) { + if (user == null || user.tenantId() == null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); + if (requested != null && !requested.isBlank()) return requested.trim(); + return String.valueOf(user.tenantId()); + } + private String operator(AuthenticatedUser user) { return user.userId() == null ? user.username() : String.valueOf(user.userId()); } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryRequests.java index ac8083c6f..cc75ed53e 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryRequests.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryRequests.java @@ -3,16 +3,43 @@ package com.baiyue.lastleg.delivery.dto; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import jakarta.validation.Valid; +import java.util.List; +import java.math.BigDecimal; +import java.time.LocalDateTime; public final class DeliveryRequests { private DeliveryRequests() {} public record Transfer(@NotBlank @Size(max = 255) String idempotencyKey, - @Size(max = 1000) String remark) {} + @NotNull LocalDateTime plannedDeliveryAt, + @NotBlank @Size(max = 32) String deliveryMethod, + @Size(max = 128) String waybillNo, + @Size(max = 255) String carrierName, + @NotBlank @Size(max = 64) String pickupMethod, + @NotBlank @Size(max = 64) String loadingType, + @Size(max = 64) String vehicleType, + @Size(max = 64) String vehicleSize, + @Size(max = 128) String driverName, + @Size(max = 64) String driverPhone, + @Size(max = 64) String vehiclePlate, + @Size(max = 128) String appointmentAccount, + @Size(max = 128) String sellerName, + @Size(max = 1000) String remark, + @NotNull @Valid @Size(min = 1, max = 20) List fees) {} + + public record FeeItem(@NotBlank @Size(max = 64) String feeType, + @NotBlank @Size(max = 8) String currency, + @NotNull @Min(0) BigDecimal amount, + @Size(max = 500) String remark) {} public record Query(@Min(1) long page, @Min(1) @Max(200) long size, - String documentNo, String warehouseId, String status) { + String documentNo, String warehouseId, String status, + String dispatchNo, String waybillNo, String vehiclePlate, + String driverName, String deliveryMethod, + LocalDateTime plannedFrom, LocalDateTime plannedTo) { public Query { if (page == 0) page = 1; if (size == 0) size = 20; } } @@ -20,4 +47,14 @@ public final class DeliveryRequests { String destinationWarehouseCode) { public PalletQuery { if (page == 0) page = 1; if (size == 0) size = 20; } } + + public record OneClickOutbound(@NotBlank @Size(max=1000) String recipient, + @NotNull @Size(min=1, max=200) List<@Valid OneClickItem> items, + @Size(max=1000) String remark, + @NotBlank @Size(max=255) String idempotencyKey) {} + public record OneClickItem(@NotNull Long inventoryPositionId, @NotNull @Min(1) Long packages, + @NotBlank @Size(max=128) String sku) {} + public record PdaPick(@NotNull Long outboundPalletItemId, @NotNull Long inventoryPositionId, + @NotNull @Min(1) Long packages, @NotBlank @Size(max=255) String idempotencyKey) {} + public record Cancel(@NotBlank @Size(max=1000) String reason) {} } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryResponses.java index b763efe88..116cbd2ad 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryResponses.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/dto/DeliveryResponses.java @@ -1,6 +1,8 @@ package com.baiyue.lastleg.delivery.dto; import java.time.LocalDateTime; +import java.math.BigDecimal; +import java.util.List; public final class DeliveryResponses { private DeliveryResponses() {} @@ -12,9 +14,32 @@ public final class DeliveryResponses { public record OutboundRow(Long id, String outboundNo, String dispatchNo, String overseasWarehouseId, String status, int palletCount, int packageCount, LocalDateTime createdAt) {} - public record DeliveryRow(Long id, String deliveryNo, String outboundNo, String dispatchNo, - String overseasWarehouseId, String status, int palletCount, - int packageCount, LocalDateTime createdAt) {} + public record DeliveryRow(Long id, String customerId, String customerName, String deliveryNo, + String outboundNo, String dispatchNo, String overseasWarehouseId, + String overseasWarehouseName, String status, String waybillNo, + String deliveryMethod, String carrierName, String pickupMethod, + String loadingType, String vehicleType, String vehicleSize, + String vehiclePlate, String driverName, String driverPhone, + String appointmentAccount, String sellerName, int palletCount, + int packageCount, BigDecimal totalWeightKg, BigDecimal totalVolumeCbm, + LocalDateTime plannedDeliveryAt, LocalDateTime dispatchedAt, + LocalDateTime expectedArrivalAt, LocalDateTime signedAt, + String remark, LocalDateTime createdAt) {} + + public record DeliverySku(String inboundCode, String customerName, String destinationWarehouseCode, + String receivingAddress, String shipmentNo, String fbaTrackingNo, + String skuCode, String productName, int packageCount, + BigDecimal weightKg, BigDecimal volumeCbm) {} + + public record DeliveryFeeRow(Long id, String feeDirection, String feeType, String currency, + BigDecimal amount, String remark) {} + + public record DeliveryOperationRow(Long id, LocalDateTime operatedAt, String operationNode, + String operatorName, String result) {} + + public record DeliveryDetail(DeliveryRow order, List skus, + List fees, List operations) {} public record OutboundDetail(OutboundRow outbound, DeliveryRow delivery) {} + public record PdaTask(Long id, String taskNo, String status, String outboundNo, LocalDateTime pushedAt, LocalDateTime completedAt) {} } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryFee.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryFee.java new file mode 100644 index 000000000..adcf4cc51 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryFee.java @@ -0,0 +1,24 @@ +package com.baiyue.lastleg.delivery.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +@Data +@TableName("ll_delivery_fee") +public class DeliveryFee { + @TableId(type = IdType.ASSIGN_ID) private Long id; + private String customerId; + private Long deliveryOrderId; + private String feeDirection; + private String feeType; + private String currency; + private BigDecimal amount; + private String remark; + private String createdBy; + private LocalDateTime createdAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOperation.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOperation.java new file mode 100644 index 000000000..8cd075215 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOperation.java @@ -0,0 +1,21 @@ +package com.baiyue.lastleg.delivery.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("ll_delivery_operation") +public class DeliveryOperation { + @TableId(type = IdType.ASSIGN_ID) private Long id; + private String customerId; + private Long deliveryOrderId; + private String operationNode; + private String operatorId; + private String operatorName; + private String result; + private LocalDateTime createdAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOrder.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOrder.java index ee1b4f809..93dbf8173 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOrder.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/DeliveryOrder.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; +import java.math.BigDecimal; import java.time.LocalDateTime; /** Delivery document linked one-to-one to its outbound document and dispatch. */ @@ -20,9 +21,30 @@ public class DeliveryOrder { private Long dispatchOrderId; private String dispatchNo; private String overseasWarehouseId; + private String overseasWarehouseName; + private String customerName; private String status; + private String waybillNo; + private String deliveryMethod; + private String carrierName; + private String pickupMethod; + private String loadingType; + private String vehicleType; + private String vehicleSize; + private String vehiclePlate; + private String driverName; + private String driverPhone; + private String appointmentAccount; + private String sellerName; private Integer packageCount; private Integer palletCount; + private BigDecimal totalWeightKg; + private BigDecimal totalVolumeCbm; + private LocalDateTime plannedDeliveryAt; + private LocalDateTime dispatchedAt; + private LocalDateTime expectedArrivalAt; + private LocalDateTime signedAt; + private String remark; private String idempotencyKey; private String createdBy; private LocalDateTime createdAt; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundOrder.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundOrder.java index 0f438791f..1df985dac 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundOrder.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundOrder.java @@ -18,6 +18,8 @@ public class OutboundOrder { private Long dispatchOrderId; private String dispatchNo; private String overseasWarehouseId; + private String outboundType; + private String recipientInfo; private String status; private Integer packageCount; private Integer palletCount; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPalletItem.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPalletItem.java index 8145f8f82..0e1d60d84 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPalletItem.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPalletItem.java @@ -21,6 +21,9 @@ public class OutboundPalletItem { private String destinationWarehouseCode; private String destinationWarehouseName; private Integer packageCount; + private Integer pickedPackageCount; + private String status; + private String skuCode; private String orderRefs; private String inboundCode; private LocalDateTime createdAt; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaPick.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaPick.java new file mode 100644 index 000000000..64d4b091b --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaPick.java @@ -0,0 +1,20 @@ +package com.baiyue.lastleg.delivery.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import java.time.LocalDateTime; + +@Data +@TableName("ll_outbound_pda_pick") +public class OutboundPdaPick { + @TableId(type = IdType.ASSIGN_ID) private Long id; + private String customerId; + private Long taskId; + private Long outboundPalletItemId; + private Long inventoryPositionId; + private Long packages; + private String idempotencyKey; + private LocalDateTime pickedAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaTask.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaTask.java new file mode 100644 index 000000000..bb1640a6e --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/entity/OutboundPdaTask.java @@ -0,0 +1,21 @@ +package com.baiyue.lastleg.delivery.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import java.time.LocalDateTime; + +@Data +@TableName("ll_outbound_pda_task") +public class OutboundPdaTask { + @TableId(type = IdType.ASSIGN_ID) private Long id; + private String customerId; + private Long outboundOrderId; + private String taskNo; + private String status; + private LocalDateTime pushedAt; + private LocalDateTime completedAt; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryFeeMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryFeeMapper.java new file mode 100644 index 000000000..372d62e69 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryFeeMapper.java @@ -0,0 +1,16 @@ +package com.baiyue.lastleg.delivery.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.delivery.entity.DeliveryFee; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +@Mapper +public interface DeliveryFeeMapper extends BaseMapper { + @Select("SELECT * FROM ll_delivery_fee WHERE customer_id=#{customerId} AND delivery_order_id=#{deliveryOrderId} ORDER BY id") + List findByDelivery(@Param("customerId") String customerId, + @Param("deliveryOrderId") Long deliveryOrderId); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryOperationMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryOperationMapper.java new file mode 100644 index 000000000..1b34e5973 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/DeliveryOperationMapper.java @@ -0,0 +1,16 @@ +package com.baiyue.lastleg.delivery.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.delivery.entity.DeliveryOperation; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +@Mapper +public interface DeliveryOperationMapper extends BaseMapper { + @Select("SELECT * FROM ll_delivery_operation WHERE customer_id=#{customerId} AND delivery_order_id=#{deliveryOrderId} ORDER BY created_at,id") + List findByDelivery(@Param("customerId") String customerId, + @Param("deliveryOrderId") Long deliveryOrderId); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundOrderMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundOrderMapper.java index e16968f70..2059ee200 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundOrderMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundOrderMapper.java @@ -14,4 +14,6 @@ public interface OutboundOrderMapper extends BaseMapper { OutboundOrder findByIdempotency(@Param("customerId") String customerId, @Param("key") String key); @Select("SELECT * FROM ll_outbound_order WHERE customer_id=#{customerId} AND dispatch_order_id=#{dispatchOrderId} LIMIT 1 FOR UPDATE") OutboundOrder lockByDispatch(@Param("customerId") String customerId, @Param("dispatchOrderId") Long dispatchOrderId); + @Select("SELECT * FROM ll_outbound_order WHERE customer_id=#{customerId} AND outbound_no=#{outboundNo} LIMIT 1 FOR UPDATE") + OutboundOrder lockByNo(@Param("customerId") String customerId, @Param("outboundNo") String outboundNo); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaPickMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaPickMapper.java new file mode 100644 index 000000000..c4d30223b --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaPickMapper.java @@ -0,0 +1,14 @@ +package com.baiyue.lastleg.delivery.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.delivery.entity.OutboundPdaPick; +import org.apache.ibatis.annotations.*; +import java.util.List; + +@Mapper +public interface OutboundPdaPickMapper extends BaseMapper { + @Select("SELECT * FROM ll_outbound_pda_pick WHERE customer_id=#{customerId} AND idempotency_key=#{key} LIMIT 1") + OutboundPdaPick findByKey(@Param("customerId") String customerId, @Param("key") String key); + @Select("SELECT * FROM ll_outbound_pda_pick WHERE customer_id=#{customerId} AND task_id IN (SELECT id FROM ll_outbound_pda_task WHERE customer_id=#{customerId} AND outbound_order_id=#{outboundId})") + List findByOutbound(@Param("customerId") String customerId, @Param("outboundId") Long outboundId); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaTaskMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaTaskMapper.java new file mode 100644 index 000000000..88307fa3a --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/mapper/OutboundPdaTaskMapper.java @@ -0,0 +1,16 @@ +package com.baiyue.lastleg.delivery.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.delivery.entity.OutboundPdaTask; +import org.apache.ibatis.annotations.*; +import java.util.List; + +@Mapper +public interface OutboundPdaTaskMapper extends BaseMapper { + @Select("SELECT * FROM ll_outbound_pda_task WHERE customer_id=#{customerId} AND outbound_order_id=#{outboundOrderId} LIMIT 1 FOR UPDATE") + OutboundPdaTask lockByOutbound(@Param("customerId") String customerId, @Param("outboundOrderId") Long outboundOrderId); + @Select("SELECT * FROM ll_outbound_pda_task WHERE customer_id=#{customerId} AND task_no=#{taskNo} LIMIT 1 FOR UPDATE") + OutboundPdaTask lockByNo(@Param("customerId") String customerId, @Param("taskNo") String taskNo); + @Select("SELECT * FROM ll_outbound_pda_task WHERE customer_id=#{customerId} AND (#{status} IS NULL OR status=#{status}) ORDER BY pushed_at DESC LIMIT 200") + List list(@Param("customerId") String customerId, @Param("status") String status); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/service/DeliveryTransferService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/service/DeliveryTransferService.java index 57ec1814b..35e419205 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/service/DeliveryTransferService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/delivery/service/DeliveryTransferService.java @@ -7,9 +7,13 @@ import com.baiyue.common.core.api.PageResult; import com.baiyue.lastleg.delivery.dto.DeliveryRequests; import com.baiyue.lastleg.delivery.dto.DeliveryResponses; import com.baiyue.lastleg.delivery.entity.DeliveryOrder; +import com.baiyue.lastleg.delivery.entity.DeliveryFee; +import com.baiyue.lastleg.delivery.entity.DeliveryOperation; import com.baiyue.lastleg.delivery.entity.OutboundOrder; import com.baiyue.lastleg.delivery.entity.OutboundPalletItem; import com.baiyue.lastleg.delivery.mapper.DeliveryOrderMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryFeeMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryOperationMapper; import com.baiyue.lastleg.delivery.mapper.OutboundOrderMapper; import com.baiyue.lastleg.delivery.mapper.OutboundPalletItemMapper; import com.baiyue.lastleg.dispatch.entity.DispatchItem; @@ -18,12 +22,27 @@ import com.baiyue.lastleg.dispatch.mapper.DispatchItemMapper; import com.baiyue.lastleg.dispatch.mapper.DispatchOrderMapper; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; +import com.baiyue.lastleg.entity.LastlegOrderDetail; import com.baiyue.lastleg.inboundplan.entity.InboundPlanLine; import com.baiyue.lastleg.inboundplan.entity.InboundPlanPalletAllocation; import com.baiyue.lastleg.inboundplan.mapper.InboundPlanLineMapper; import com.baiyue.lastleg.inboundplan.mapper.InboundPlanPalletAllocationMapper; +import com.baiyue.lastleg.delivery.entity.OutboundPdaTask; +import com.baiyue.lastleg.delivery.entity.OutboundPdaPick; +import com.baiyue.lastleg.delivery.mapper.OutboundPdaTaskMapper; +import com.baiyue.lastleg.delivery.mapper.OutboundPdaPickMapper; +import com.baiyue.lastleg.inventory.command.dto.InventoryCommandRequests; +import com.baiyue.lastleg.inventory.command.service.InventoryCommandService; +import com.baiyue.lastleg.inventory.entity.InventoryPosition; +import com.baiyue.lastleg.inventory.mapper.InventoryPositionMapper; +import com.baiyue.lastleg.dropship.entity.DropshipOrderProfile; +import com.baiyue.lastleg.dropship.entity.DropshipCarton; +import com.baiyue.lastleg.dropship.mapper.DropshipOrderProfileMapper; +import com.baiyue.lastleg.dropship.mapper.DropshipCartonMapper; +import com.baiyue.lastleg.dropship.mapper.DropshipSkuInventoryMapper; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; +import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; import jakarta.annotation.Resource; import org.springframework.dao.DuplicateKeyException; import org.springframework.http.HttpStatus; @@ -34,6 +53,8 @@ import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import com.baiyue.lastleg.service.LastlegOrderNoGenerator; +import java.math.BigDecimal; import java.util.*; @Service @@ -43,10 +64,21 @@ public class DeliveryTransferService { @Resource private OutboundOrderMapper outboundOrders; @Resource private OutboundPalletItemMapper outboundPallets; @Resource private DeliveryOrderMapper deliveryOrders; + @Resource private DeliveryFeeMapper deliveryFees; + @Resource private DeliveryOperationMapper deliveryOperations; + @Resource private OutboundPdaTaskMapper pdaTasks; + @Resource private OutboundPdaPickMapper pdaPicks; + @Resource private InventoryPositionMapper inventoryPositions; + @Resource private InventoryCommandService inventoryCommands; + @Resource private DropshipOrderProfileMapper dropshipProfiles; + @Resource private DropshipCartonMapper dropshipCartons; + @Resource private DropshipSkuInventoryMapper dropshipSkuInventory; @Resource private InboundPlanPalletAllocationMapper planAllocations; @Resource private InboundPlanLineMapper planLines; @Resource private LastlegOrderMapper tailOrders; @Resource private LastlegOrderStatusHistoryMapper histories; + @Resource private LastlegOrderDetailMapper tailOrderDetails; + @Resource private LastlegOrderNoGenerator orderNoGenerator; @Transactional public DeliveryResponses.TransferResult transfer(String customerId, String dispatchNo, String operatorId, @@ -70,13 +102,14 @@ public class DeliveryTransferService { throw conflict("All dispatch pallets must be received before transfer to delivery"); } if (!StringUtils.hasText(dispatch.getOverseasWarehouseId())) throw conflict("Dispatch warehouse is required"); + validateTransferFees(request); LocalDateTime now = LocalDateTime.now(); OutboundOrder outbound = new OutboundOrder(); outbound.setId(IdWorker.getId()); outbound.setCustomerId(customerId); - outbound.setOutboundNo("OUT" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + outbound.getId()); + outbound.setOutboundNo(orderNoGenerator.next("OUTBOUND", dispatch.getOverseasWarehouseId(), outbound.getId())); outbound.setDispatchOrderId(dispatch.getId()); outbound.setDispatchNo(dispatch.getDispatchNo()); - outbound.setOverseasWarehouseId(dispatch.getOverseasWarehouseId()); outbound.setStatus("WAIT_OUTBOUND"); + outbound.setOverseasWarehouseId(dispatch.getOverseasWarehouseId()); outbound.setStatus("WAIT_OUTBOUND"); outbound.setOutboundType("DISPATCH_DELIVERY"); outbound.setPackageCount(items.stream().map(DispatchItem::getPackageCount).filter(Objects::nonNull).mapToInt(Integer::intValue).sum()); outbound.setPalletCount(items.size()); outbound.setIdempotencyKey(request.idempotencyKey().trim()); outbound.setCreatedBy(operatorId); outbound.setCreatedAt(now); outbound.setUpdatedAt(now); @@ -103,27 +136,179 @@ public class DeliveryTransferService { item.setDestinationWarehouseId(source.getDestinationWarehouseId()); item.setDestinationWarehouseCode(source.getDestinationWarehouseCode()); item.setDestinationWarehouseName(source.getDestinationWarehouseName()); - item.setPackageCount(Objects.requireNonNullElse(source.getPackageCount(), 0)); + item.setPackageCount(Objects.requireNonNullElse(source.getPackageCount(), 0)); item.setPickedPackageCount(0); item.setStatus("WAIT_PICK"); item.setOrderRefs(source.getOrderRefs()); item.setInboundCode(source.getInboundCode()); item.setCreatedAt(now); outboundPallets.insert(item); } DeliveryOrder delivery = new DeliveryOrder(); delivery.setId(IdWorker.getId()); delivery.setCustomerId(customerId); - delivery.setDeliveryNo("DEL" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + delivery.getId()); + delivery.setDeliveryNo(orderNoGenerator.next("DELIVERY", dispatch.getOverseasWarehouseId(), delivery.getId())); delivery.setOutboundOrderId(outbound.getId()); delivery.setOutboundNo(outbound.getOutboundNo()); delivery.setDispatchOrderId(dispatch.getId()); delivery.setDispatchNo(dispatch.getDispatchNo()); - delivery.setOverseasWarehouseId(dispatch.getOverseasWarehouseId()); delivery.setStatus("WAIT_DELIVERY"); + delivery.setOverseasWarehouseId(dispatch.getOverseasWarehouseId()); delivery.setOverseasWarehouseName(dispatch.getOverseasWarehouseName()); + LastlegOrder firstOrder = firstTailOrder(customerId, items); + delivery.setCustomerName(firstOrder == null ? null : firstOrder.getCustomerName()); delivery.setStatus("WAIT_DELIVERY"); + delivery.setWaybillNo(trim(request.waybillNo())); delivery.setDeliveryMethod(request.deliveryMethod().trim()); + delivery.setCarrierName(firstText(request.carrierName(), dispatch.getCarrierName())); delivery.setPickupMethod(trim(request.pickupMethod())); + delivery.setLoadingType(trim(request.loadingType())); delivery.setVehicleType(trim(request.vehicleType())); delivery.setVehicleSize(trim(request.vehicleSize())); + delivery.setVehiclePlate(firstText(request.vehiclePlate(), dispatch.getVehiclePlate())); delivery.setDriverName(firstText(request.driverName(), dispatch.getDriverName())); + delivery.setDriverPhone(firstText(request.driverPhone(), dispatch.getDriverPhone())); delivery.setAppointmentAccount(trim(request.appointmentAccount())); + delivery.setSellerName(trim(request.sellerName())); delivery.setPlannedDeliveryAt(request.plannedDeliveryAt()); delivery.setPackageCount(outbound.getPackageCount()); delivery.setPalletCount(outbound.getPalletCount()); + delivery.setTotalVolumeCbm(items.stream().map(DispatchItem::getVolumeCbm).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)); + delivery.setTotalWeightKg(totalWeight(customerId, items)); delivery.setExpectedArrivalAt(dispatch.getEstimatedArrivalAt()); delivery.setRemark(trim(request.remark())); delivery.setIdempotencyKey(request.idempotencyKey().trim()); delivery.setCreatedBy(operatorId); delivery.setCreatedAt(now); delivery.setUpdatedAt(now); deliveryOrders.insert(delivery); + List feeInputs = request.fees() == null ? List.of() : request.fees(); + for (DeliveryRequests.FeeItem input : feeInputs) { + DeliveryFee fee = new DeliveryFee(); fee.setCustomerId(customerId); fee.setDeliveryOrderId(delivery.getId()); + fee.setFeeDirection("PAYABLE"); fee.setFeeType(input.feeType().trim()); fee.setCurrency(input.currency().trim().toUpperCase(Locale.ROOT)); + fee.setAmount(input.amount()); fee.setRemark(trim(input.remark())); fee.setCreatedBy(operatorId); fee.setCreatedAt(now); deliveryFees.insert(fee); + } + recordOperation(customerId, delivery.getId(), operatorId, "CREATE_DELIVERY", "CREATED", now); bindTailOrders(customerId, items, outbound.getOutboundNo(), delivery.getDeliveryNo(), request.remark(), now); return result(outbound, delivery, false); } + private void validateTransferFees(DeliveryRequests.Transfer request) { + List inputs = request.fees() == null ? List.of() : request.fees(); + if (inputs.isEmpty()) throw conflict("Delivery fee is required"); + Set allowed = Set.of("DELIVERY_FEE", "LOADING_FEE", "APPOINTMENT_FEE"); + Set seen = new HashSet<>(); + for (DeliveryRequests.FeeItem input : inputs) { + String feeType = input.feeType().trim().toUpperCase(Locale.ROOT); + if (!allowed.contains(feeType)) throw conflict("Unsupported delivery fee type: " + feeType); + if (!seen.add(feeType)) throw conflict("Duplicate delivery fee type: " + feeType); + if (!"USD".equalsIgnoreCase(input.currency().trim())) throw conflict("Delivery fees must use USD"); + if (input.amount().signum() < 0) throw conflict("Delivery fee amount cannot be negative"); + } + if (!seen.contains("DELIVERY_FEE")) throw conflict("Delivery fee is required"); + if (Set.of("FLOOR", "MIXED").contains(request.loadingType().trim().toUpperCase(Locale.ROOT)) + && !seen.contains("LOADING_FEE")) { + throw conflict("Loading fee is required for floor or mixed loading"); + } + if ("BUY_APPOINTMENT".equalsIgnoreCase(trim(request.appointmentAccount()))) { + if (!StringUtils.hasText(request.sellerName())) throw conflict("Seller name is required when buying an appointment"); + if (!seen.contains("APPOINTMENT_FEE")) throw conflict("Appointment fee is required when buying an appointment"); + } + } + + @Transactional + public DeliveryResponses.PdaTask createOneClick(String customerId, String operatorId, DeliveryRequests.OneClickOutbound request) { + OutboundOrder existing = outboundOrders.findByIdempotency(customerId, request.idempotencyKey().trim()); + if (existing != null) return taskResult(existing, pdaTasks.lockByOutbound(customerId, existing.getId())); + LocalDateTime now = LocalDateTime.now(); + InventoryPosition firstPosition = inventoryPositions.selectById(request.items().get(0).inventoryPositionId()); + if (firstPosition == null || !customerId.equals(firstPosition.getCustomerId())) throw notFound("Inventory position not found"); + String selectedWarehouseId = firstPosition.getOverseasWarehouseId(); + OutboundOrder outbound = new OutboundOrder(); + outbound.setId(IdWorker.getId()); outbound.setCustomerId(customerId); + outbound.setOutboundNo(orderNoGenerator.next("ONE_CLICK_DROPSHIP", selectedWarehouseId, outbound.getId())); + outbound.setOutboundType("ONE_CLICK_DROPSHIP"); outbound.setStatus("WAIT_PICK"); outbound.setRecipientInfo(request.recipient().trim()); + outbound.setPackageCount(request.items().stream().mapToInt(i -> i.packages().intValue()).sum()); outbound.setPalletCount(request.items().size()); + outbound.setIdempotencyKey(request.idempotencyKey().trim()); outbound.setCreatedBy(operatorId); outbound.setCreatedAt(now); outbound.setUpdatedAt(now); + outboundOrders.insert(outbound); + for (DeliveryRequests.OneClickItem input : request.items()) { + InventoryPosition position = inventoryPositions.selectById(input.inventoryPositionId()); + if (position == null || !customerId.equals(position.getCustomerId())) throw notFound("Inventory position not found"); + DropshipOrderProfile profile = dropshipProfiles.findPosition(customerId, position.getId()); + if (profile == null) throw conflict("Selected pallet is not one-click dropship inventory"); + if (selectedWarehouseId == null) selectedWarehouseId = position.getOverseasWarehouseId(); + else if (!Objects.equals(selectedWarehouseId, position.getOverseasWarehouseId())) throw conflict("One outbound order must use inventory from the same overseas warehouse"); + if (value(position.getAvailablePackages()) < input.packages()) throw conflict("Insufficient available inventory for pallet " + position.getPalletNo()); + OutboundPalletItem item = new OutboundPalletItem(); item.setId(IdWorker.getId()); item.setCustomerId(customerId); item.setOutboundOrderId(outbound.getId()); + item.setWarehousePalletId(position.getPalletId()); item.setPalletNo(position.getPalletNo()); item.setPackageCount(input.packages().intValue()); item.setPickedPackageCount(0); item.setStatus("WAIT_PICK"); item.setSkuCode(input.sku().trim()); item.setCreatedAt(now); + outboundPallets.insert(item); + } + outbound.setOverseasWarehouseId(selectedWarehouseId); outboundOrders.updateById(outbound); + OutboundPdaTask task = new OutboundPdaTask(); task.setId(IdWorker.getId()); task.setCustomerId(customerId); task.setOutboundOrderId(outbound.getId()); + task.setTaskNo("PDA" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + task.getId()); task.setStatus("PENDING"); task.setPushedAt(now); task.setCreatedAt(now); task.setUpdatedAt(now); pdaTasks.insert(task); + return taskResult(outbound, task); + } + + @Transactional + public DeliveryResponses.PdaTask pdaPick(String customerId, String operatorId, String taskNo, DeliveryRequests.PdaPick request) { + OutboundPdaTask task = pdaTasks.lockByNo(customerId, taskNo); if (task == null) throw notFound("PDA task not found"); + OutboundPdaPick replay = pdaPicks.findByKey(customerId, request.idempotencyKey().trim()); if (replay != null) return taskResult(outboundOrders.selectById(task.getOutboundOrderId()), task); + OutboundOrder outbound = outboundOrders.lockByNo(customerId, outboundNo(task.getOutboundOrderId())); + OutboundPalletItem item = outboundPallets.selectById(request.outboundPalletItemId()); + if (item == null || !task.getOutboundOrderId().equals(item.getOutboundOrderId())) throw notFound("Outbound pallet item not found"); + if (request.packages() > item.getPackageCount() - value(item.getPickedPackageCount())) throw conflict("Picked quantity exceeds outbound pallet quantity"); + InventoryPosition selectedPosition = inventoryPositions.selectById(request.inventoryPositionId()); + if (selectedPosition == null || !customerId.equals(selectedPosition.getCustomerId()) || !Objects.equals(selectedPosition.getPalletId(), item.getWarehousePalletId())) throw conflict("Scanned inventory position does not belong to the outbound pallet"); + DropshipOrderProfile profile = dropshipProfiles.findPosition(customerId, request.inventoryPositionId()); + if (profile == null) throw conflict("Scanned pallet is not one-click dropship inventory"); + List cartons = dropshipCartons.lockAvailable(customerId, profile.getId(), item.getSkuCode(), request.packages()); + if (cartons.size() != request.packages()) throw conflict("Insufficient available one-click dropship cartons"); + InventoryCommandRequests.Move move = new InventoryCommandRequests.Move(request.inventoryPositionId(), request.packages(), outbound.getOutboundNo(), outbound.getOutboundNo(), null, request.idempotencyKey() + ":ALLOCATE", null); + inventoryCommands.allocate(customerId, operatorId, move); + inventoryCommands.pick(customerId, operatorId, new InventoryCommandRequests.Move(request.inventoryPositionId(), request.packages(), outbound.getOutboundNo(), outbound.getOutboundNo(), null, request.idempotencyKey() + ":PICK", null)); + inventoryCommands.ship(customerId, operatorId, new InventoryCommandRequests.Move(request.inventoryPositionId(), request.packages(), outbound.getOutboundNo(), outbound.getOutboundNo(), null, request.idempotencyKey() + ":SHIP", null)); + if (dropshipSkuInventory.deduct(customerId, profile.getId(), item.getSkuCode(), request.packages()) != 1) throw conflict("Insufficient one-click dropship SKU inventory"); + for (DropshipCarton carton : cartons) { carton.setStatus("SHIPPED"); carton.setOutboundOrderNo(outbound.getOutboundNo()); carton.setVersion((carton.getVersion() == null ? 0 : carton.getVersion()) + 1); carton.setUpdatedAt(LocalDateTime.now()); dropshipCartons.updateById(carton); } + OutboundPdaPick pick = new OutboundPdaPick(); pick.setId(IdWorker.getId()); pick.setCustomerId(customerId); pick.setTaskId(task.getId()); pick.setOutboundPalletItemId(item.getId()); pick.setInventoryPositionId(request.inventoryPositionId()); pick.setPackages(request.packages()); pick.setIdempotencyKey(request.idempotencyKey().trim()); pick.setPickedAt(LocalDateTime.now()); pdaPicks.insert(pick); + item.setPickedPackageCount(value(item.getPickedPackageCount()) + request.packages().intValue()); item.setStatus(item.getPickedPackageCount() >= item.getPackageCount() ? "PICKED" : "PARTIAL_PICKED"); outboundPallets.updateById(item); + List pickedItems = outboundPallets.findByOutbound(customerId, outbound.getId()); + if (!pickedItems.isEmpty() && pickedItems.stream().allMatch(i -> "PICKED".equals(i.getStatus()))) { + outbound.setStatus("PICKED"); outbound.setUpdatedAt(LocalDateTime.now()); outboundOrders.updateById(outbound); + } + task.setStatus("IN_PROGRESS"); task.setUpdatedAt(LocalDateTime.now()); pdaTasks.updateById(task); + return taskResult(outbound, task); + } + + @Transactional + public DeliveryResponses.PdaTask completePda(String customerId, String taskNo) { + OutboundPdaTask task = pdaTasks.lockByNo(customerId, taskNo); if (task == null) throw notFound("PDA task not found"); + OutboundOrder outbound = outboundOrders.selectById(task.getOutboundOrderId()); + List items = outboundPallets.findByOutbound(customerId, outbound.getId()); + if (items.stream().anyMatch(i -> value(i.getPickedPackageCount()) < value(i.getPackageCount()))) throw conflict("All outbound pallets must be picked before completion"); + task.setStatus("COMPLETED"); task.setCompletedAt(LocalDateTime.now()); task.setUpdatedAt(LocalDateTime.now()); pdaTasks.updateById(task); outbound.setStatus("OUTBOUNDED"); outbound.setUpdatedAt(LocalDateTime.now()); outboundOrders.updateById(outbound); return taskResult(outbound, task); + } + + @Transactional + public void cancelOutbound(String customerId, String operatorId, String outboundNo, DeliveryRequests.Cancel request) { + OutboundOrder outbound = outboundOrders.lockByNo(customerId, outboundNo); + if (outbound == null) throw notFound("Outbound order not found"); + if ("CANCELLED".equals(outbound.getStatus())) return; + List outboundItems = outboundPallets.findByOutbound(customerId, outbound.getId()); + if (outboundItems.isEmpty() || outboundItems.stream().anyMatch(i -> !"PICKED".equals(i.getStatus()))) + throw conflict("Outbound order can only be cancelled after picking is completed"); + DeliveryOrder existingDelivery = deliveryOrders.lockByOutbound(customerId, outbound.getId()); + if (existingDelivery != null && !"WAIT_DELIVERY".equals(existingDelivery.getStatus())) + throw conflict("Outbound order cannot be cancelled while delivery is in progress"); + LocalDateTime now = LocalDateTime.now(); + for (OutboundPdaPick pick : pdaPicks.findByOutbound(customerId, outbound.getId())) { + inventoryCommands.adjust(customerId, operatorId, + new InventoryCommandRequests.Adjustment(pick.getInventoryPositionId(), pick.getPackages(), "OUTBOUND_CANCEL", + outbound.getOutboundNo(), outbound.getOutboundNo() + ":CANCEL:" + pick.getId(), request.reason())); + OutboundPalletItem pickedItem = outboundItems.stream().filter(i -> Objects.equals(i.getId(), pick.getOutboundPalletItemId())).findFirst().orElse(null); + DropshipOrderProfile profile = dropshipProfiles.findPosition(customerId, pick.getInventoryPositionId()); + if (profile != null && pickedItem != null) dropshipSkuInventory.restore(customerId, profile.getId(), pickedItem.getSkuCode(), pick.getPackages()); + } + dropshipCartons.restoreOutbound(customerId, outbound.getOutboundNo()); + DeliveryOrder delivery = existingDelivery; + if (delivery != null) { delivery.setStatus("CANCELLED"); delivery.setUpdatedAt(now); deliveryOrders.updateById(delivery); } + outbound.setStatus("CANCELLED"); outbound.setUpdatedAt(now); outboundOrders.updateById(outbound); + for (OutboundPalletItem item : outboundItems) { item.setStatus("CANCELLED"); outboundPallets.updateById(item); } + if (delivery != null) { + for (LastlegOrder order : tailOrders.selectList(new LambdaQueryWrapper().eq(LastlegOrder::getCustomerId, customerId).eq(LastlegOrder::getOutboundOrderNo, outboundNo))) { + String from = order.getCurrentStatus(); order.setCurrentStatus("RECEIVED"); order.setSubOrderStatus(null); order.setOutboundOrderNo(null); order.setDeliveryOrderNo(null); order.setUpdatedAt(now); tailOrders.updateById(order); + LastlegOrderStatusHistory history = new LastlegOrderStatusHistory(); history.setCustomerId(customerId); history.setOrderId(order.getId()); history.setFromStatus(from); history.setToStatus("RECEIVED"); history.setEventId("CANCEL_OUTBOUND:" + outboundNo); history.setRemark(request.reason().trim()); history.setCreatedAt(now); histories.insert(history); + } + } + } + + public List pdaTasks(String customerId, String status) { return pdaTasks.list(customerId, StringUtils.hasText(status) ? status : null).stream().map(task -> taskResult(outboundOrders.selectById(task.getOutboundOrderId()), task)).toList(); } + + private DeliveryResponses.PdaTask taskResult(OutboundOrder outbound, OutboundPdaTask task) { return new DeliveryResponses.PdaTask(task.getId(), task.getTaskNo(), task.getStatus(), outbound.getOutboundNo(), task.getPushedAt(), task.getCompletedAt()); } + private String outboundNo(Long id) { OutboundOrder value = outboundOrders.selectById(id); return value == null ? "" : value.getOutboundNo(); } + private int value(Integer value) { return value == null ? 0 : value; } + private long value(Long value) { return value == null ? 0L : value; } + public PageResult outboundPage(String customerId, DeliveryRequests.Query query) { Page page = outboundOrders.selectPage(new Page<>(query.page(), query.size()), - new LambdaQueryWrapper().eq(OutboundOrder::getCustomerId, customerId) + new LambdaQueryWrapper().eq(StringUtils.hasText(customerId), OutboundOrder::getCustomerId, customerId) .like(StringUtils.hasText(query.documentNo()), OutboundOrder::getOutboundNo, query.documentNo()) .eq(StringUtils.hasText(query.warehouseId()), OutboundOrder::getOverseasWarehouseId, query.warehouseId()) .eq(StringUtils.hasText(query.status()), OutboundOrder::getStatus, query.status()) @@ -141,21 +326,34 @@ public class DeliveryTransferService { public PageResult deliveryPage(String customerId, DeliveryRequests.Query query) { Page page = deliveryOrders.selectPage(new Page<>(query.page(), query.size()), - new LambdaQueryWrapper().eq(DeliveryOrder::getCustomerId, customerId) + new LambdaQueryWrapper().eq(StringUtils.hasText(customerId), DeliveryOrder::getCustomerId, customerId) .like(StringUtils.hasText(query.documentNo()), DeliveryOrder::getDeliveryNo, query.documentNo()) .eq(StringUtils.hasText(query.warehouseId()), DeliveryOrder::getOverseasWarehouseId, query.warehouseId()) .eq(StringUtils.hasText(query.status()), DeliveryOrder::getStatus, query.status()) + .like(StringUtils.hasText(query.dispatchNo()), DeliveryOrder::getDispatchNo, query.dispatchNo()) + .like(StringUtils.hasText(query.waybillNo()), DeliveryOrder::getWaybillNo, query.waybillNo()) + .like(StringUtils.hasText(query.vehiclePlate()), DeliveryOrder::getVehiclePlate, query.vehiclePlate()) + .like(StringUtils.hasText(query.driverName()), DeliveryOrder::getDriverName, query.driverName()) + .eq(StringUtils.hasText(query.deliveryMethod()), DeliveryOrder::getDeliveryMethod, query.deliveryMethod()) + .ge(query.plannedFrom() != null, DeliveryOrder::getPlannedDeliveryAt, query.plannedFrom()) + .le(query.plannedTo() != null, DeliveryOrder::getPlannedDeliveryAt, query.plannedTo()) .orderByDesc(DeliveryOrder::getCreatedAt)); return page(page, page.getRecords().stream().map(this::deliveryRow).toList()); } - public DeliveryResponses.OutboundDetail deliveryDetail(String customerId, String deliveryNo, - DeliveryRequests.PalletQuery query) { + public DeliveryResponses.DeliveryDetail deliveryDetail(String customerId, String deliveryNo) { DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo); if (delivery == null) throw notFound("Delivery order not found"); - OutboundOrder outbound = outboundOrders.selectById(delivery.getOutboundOrderId()); - if (outbound == null || !customerId.equals(outbound.getCustomerId())) throw notFound("Outbound order not found"); - return outboundDetail(customerId, outbound.getOutboundNo(), query); + List items = dispatchItems.findByDispatchOrder(customerId, delivery.getDispatchOrderId()); + List orderIds = items.stream().map(DispatchItem::getOrderId).filter(Objects::nonNull).distinct().toList(); + Map ordersById = orderIds.isEmpty() ? Map.of() : tailOrders.selectBatchIds(orderIds).stream() + .filter(order -> customerId.equals(order.getCustomerId())).collect(java.util.stream.Collectors.toMap(LastlegOrder::getId, order -> order)); + List details = orderIds.isEmpty() ? List.of() : tailOrderDetails.selectList( + new LambdaQueryWrapper().eq(LastlegOrderDetail::getCustomerId, customerId).in(LastlegOrderDetail::getOrderId, orderIds)); + List skus = details.stream().map(detail -> sku(detail, ordersById.get(detail.getOrderId()))).toList(); + return new DeliveryResponses.DeliveryDetail(deliveryRow(delivery), skus, + deliveryFees.findByDelivery(customerId, delivery.getId()).stream().map(this::feeRow).toList(), + deliveryOperations.findByDelivery(customerId, delivery.getId()).stream().map(this::operationRow).toList()); } private void bindTailOrders(String customerId, List items, String outboundNo, String deliveryNo, @@ -180,7 +378,7 @@ public class DeliveryTransferService { order.setDeliveryOrderNo(deliveryNo); order.setCurrentStatus("WAIT_OUTBOUND"); order.setUpdatedAt(now); order.setSubOrderStatus("OUTBOUND:WAIT_OUTBOUND;DELIVERY:WAIT_DELIVERY"); tailOrders.updateById(order); - LastlegOrderStatusHistory history = new LastlegOrderStatusHistory(); history.setOrderId(order.getId()); + LastlegOrderStatusHistory history = new LastlegOrderStatusHistory(); history.setCustomerId(customerId); history.setOrderId(order.getId()); history.setFromStatus(from); history.setToStatus("WAIT_OUTBOUND"); history.setEventId("TRANSFER_DELIVERY:" + deliveryNo); history.setRemark(StringUtils.hasText(remark) ? remark.trim() : "Transferred to outbound and delivery documents"); @@ -198,7 +396,15 @@ public class DeliveryTransferService { d.getDeliveryNo(), d.getStatus(), o.getPalletCount(), o.getPackageCount(), replayed); } private DeliveryResponses.OutboundRow outboundRow(OutboundOrder o) { return new DeliveryResponses.OutboundRow(o.getId(), o.getOutboundNo(), o.getDispatchNo(), o.getOverseasWarehouseId(), o.getStatus(), o.getPalletCount(), o.getPackageCount(), o.getCreatedAt()); } - private DeliveryResponses.DeliveryRow deliveryRow(DeliveryOrder d) { return new DeliveryResponses.DeliveryRow(d.getId(), d.getDeliveryNo(), d.getOutboundNo(), d.getDispatchNo(), d.getOverseasWarehouseId(), d.getStatus(), d.getPalletCount(), d.getPackageCount(), d.getCreatedAt()); } + private DeliveryResponses.DeliveryRow deliveryRow(DeliveryOrder d) { return new DeliveryResponses.DeliveryRow(d.getId(), d.getCustomerId(), d.getCustomerName(), d.getDeliveryNo(), d.getOutboundNo(), d.getDispatchNo(), d.getOverseasWarehouseId(), d.getOverseasWarehouseName(), d.getStatus(), d.getWaybillNo(), d.getDeliveryMethod(), d.getCarrierName(), d.getPickupMethod(), d.getLoadingType(), d.getVehicleType(), d.getVehicleSize(), d.getVehiclePlate(), d.getDriverName(), d.getDriverPhone(), d.getAppointmentAccount(), d.getSellerName(), value(d.getPalletCount()), value(d.getPackageCount()), d.getTotalWeightKg(), d.getTotalVolumeCbm(), d.getPlannedDeliveryAt(), d.getDispatchedAt(), d.getExpectedArrivalAt(), d.getSignedAt(), d.getRemark(), d.getCreatedAt()); } + private DeliveryResponses.DeliverySku sku(LastlegOrderDetail d, LastlegOrder order) { return new DeliveryResponses.DeliverySku(order == null ? null : order.getInboundCode(), order == null ? null : order.getCustomerName(), d.getDestinationWarehouseCode(), null, d.getShipmentNo(), d.getFbaTrackingNo(), d.getSkuCode(), firstText(d.getProductNameZh(), d.getProductNameEn()), value(d.getPackageCount()), d.getGrossWeightKg(), d.getVolumeCbm()); } + private DeliveryResponses.DeliveryFeeRow feeRow(DeliveryFee f) { return new DeliveryResponses.DeliveryFeeRow(f.getId(), f.getFeeDirection(), f.getFeeType(), f.getCurrency(), f.getAmount(), f.getRemark()); } + private DeliveryResponses.DeliveryOperationRow operationRow(DeliveryOperation o) { return new DeliveryResponses.DeliveryOperationRow(o.getId(), o.getCreatedAt(), o.getOperationNode(), firstText(o.getOperatorName(), o.getOperatorId()), o.getResult()); } + private void recordOperation(String customerId, Long deliveryId, String operatorId, String node, String result, LocalDateTime now) { DeliveryOperation operation = new DeliveryOperation(); operation.setCustomerId(customerId); operation.setDeliveryOrderId(deliveryId); operation.setOperationNode(node); operation.setOperatorId(operatorId); operation.setOperatorName(operatorId); operation.setResult(result); operation.setCreatedAt(now); deliveryOperations.insert(operation); } + private LastlegOrder firstTailOrder(String customerId, List items) { return items.stream().map(DispatchItem::getOrderId).filter(Objects::nonNull).map(tailOrders::selectById).filter(Objects::nonNull).filter(order -> customerId.equals(order.getCustomerId())).findFirst().orElse(null); } + private BigDecimal totalWeight(String customerId, List items) { List ids = items.stream().map(DispatchItem::getOrderId).filter(Objects::nonNull).distinct().toList(); if (ids.isEmpty()) return BigDecimal.ZERO; return tailOrderDetails.selectList(new LambdaQueryWrapper().eq(LastlegOrderDetail::getCustomerId, customerId).in(LastlegOrderDetail::getOrderId, ids)).stream().map(LastlegOrderDetail::getGrossWeightKg).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add); } + private static String trim(String value) { return StringUtils.hasText(value) ? value.trim() : null; } + private static String firstText(String primary, String fallback) { return StringUtils.hasText(primary) ? primary.trim() : trim(fallback); } private static PageResult page(Page page, List records) { return new PageResult<>(records, page.getCurrent(), page.getSize(), page.getTotal(), page.getPages()); } private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); } private static ResponseStatusException notFound(String message) { return new ResponseStatusException(HttpStatus.NOT_FOUND, message); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/discard/service/CargoDiscardService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/discard/service/CargoDiscardService.java index 2f366c605..60d0f580a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/discard/service/CargoDiscardService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/discard/service/CargoDiscardService.java @@ -44,6 +44,7 @@ import java.util.Set; @Service public class CargoDiscardService { + @Resource private com.baiyue.lastleg.service.LastlegOrderNoGenerator orderNoGenerator; public static final String APPROVAL_PERMISSION = "lastleg:cargo-discard:approve"; public static final String PENDING_REVIEW = "PENDING_REVIEW"; public static final String APPROVED = "APPROVED"; @@ -105,7 +106,7 @@ public class CargoDiscardService { LocalDateTime now = LocalDateTime.now(); CargoDiscardApplication application = new CargoDiscardApplication(); application.setId(IdWorker.getId()); application.setCustomerId(customerId); - application.setDiscardNo("FH" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + application.getId()); + application.setDiscardNo(orderNoGenerator.next("DISCARD", warehouseId, application.getId())); application.setOverseasWarehouseId(warehouseId); application.setOverseasWarehouseName(warehouseName); application.setInboundCodes(orderCache.values().stream().map(LastlegOrder::getInboundCode) .distinct().sorted().collect(java.util.stream.Collectors.joining(","))); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/DispatchController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/DispatchController.java index d6de4b587..dfad563da 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/DispatchController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/controller/DispatchController.java @@ -13,7 +13,6 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.validation.Valid; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpStatus; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; @@ -27,7 +26,6 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; @RestController -@ConditionalOnProperty(name = "lastleg.future-outbound-api.enabled", havingValue = "true") @RequestMapping("/api/platform/v1/last-leg/dispatches") @Tag(name = "Tail-leg vehicle dispatch") public class DispatchController { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/controller/DispatchPdaController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/controller/DispatchPdaController.java index 77005a1e8..56bf9a70e 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/controller/DispatchPdaController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/controller/DispatchPdaController.java @@ -7,14 +7,12 @@ import com.baiyue.lastleg.dispatch.pda.dto.DispatchPdaResponses; import com.baiyue.lastleg.dispatch.pda.service.DispatchPdaWorkflowService; import jakarta.annotation.Resource; import jakarta.validation.Valid; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpStatus; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; @RestController -@ConditionalOnProperty(name = "lastleg.future-outbound-api.enabled", havingValue = "true") @RequestMapping("/api/pda/v1/last-leg/dispatches/{dispatchNo}") public class DispatchPdaController { public static final String OPERATE_PERMISSION = "lastleg:pda:operate"; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/dto/DispatchPdaResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/dto/DispatchPdaResponses.java index 638d51be2..e259a89b8 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/dto/DispatchPdaResponses.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/dto/DispatchPdaResponses.java @@ -7,7 +7,8 @@ public final class DispatchPdaResponses { public record Task(Long id, String taskNo, String dispatchNo, String vehiclePlate, String warehouseId, String status, List items) {} public record Item(Long dispatchItemId, String palletNo, String destinationWarehouseCode, - String destinationWarehouseName, String fbaWarehouseCode, int scheduledPackages, + String destinationWarehouseName, String fbaWarehouseCode, String warehouseRouteLabel, + int scheduledPackages, long pickedPackages, List positions) {} public record Position(Long id, Long balanceId, long availablePackages, long allocatedPackages, long pickedPackages) {} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowService.java index 96da4a278..e988a9ba5 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowService.java @@ -119,6 +119,11 @@ public class DispatchPdaWorkflowService { inventory.pick(customerId, operatorId, new InventoryCommandRequests.Move(request.positionId(), request.packages(), order.getDispatchNo(), order.getDispatchNo(), null, keyBase + ":PICK", request.remark())); + // A successful PDA scan is the inventory deduction point. Departure only + // changes outbound/dispatch facts and must not deduct the same quantity again. + inventory.ship(customerId, operatorId, new InventoryCommandRequests.Move(request.positionId(), + request.packages(), order.getDispatchNo(), order.getDispatchNo(), null, + keyBase + ":SHIP", request.remark())); DispatchPdaPick pick = new DispatchPdaPick(); pick.setId(IdWorker.getId()); pick.setCustomerId(customerId); pick.setTaskId(task.getId()); pick.setDispatchItemId(itemId); pick.setInventoryPositionId(request.positionId()); pick.setPackages(request.packages()); @@ -149,6 +154,14 @@ public class DispatchPdaWorkflowService { if (replay != null) return read(replay, DispatchPdaResponses.PickingCompleted.class).withReplay(); requireVehicle(order); DispatchPdaTask task = tasks.lockByDispatch(customerId, order.getId()); + if (task != null && "COMPLETED".equals(task.getStatus())) { + List completedItems = items.findByDispatchOrder(customerId, order.getId()); + long planned = completedItems.stream().map(DispatchItem::getPackageCount).filter(Objects::nonNull) + .mapToLong(Integer::longValue).sum(); + long picked = picks.listTask(customerId, task.getId()).stream().mapToLong(p -> n(p.getPackages())).sum(); + return new DispatchPdaResponses.PickingCompleted(order.getDispatchNo(), order.getStatus(), planned, + picked, Math.max(0, planned - picked), picked < planned, true); + } if (task == null || !("PDA_PENDING".equals(task.getStatus()) || "PICKING".equals(task.getStatus()))) { throw conflict("Dispatch PDA task is not available for completion"); } @@ -197,14 +210,7 @@ public class DispatchPdaWorkflowService { } List dispatchPicks = picks.listTask(customerId, task.getId()); if (dispatchPicks.isEmpty()) throw conflict("Dispatch has no PDA picks to ship"); - long shipped = 0; - for (DispatchPdaPick pick : dispatchPicks) { - String key = "DISPATCH:" + order.getDispatchNo() + ":PICK:" + pick.getId() + ":SHIP"; - inventory.ship(customerId, operatorId, new InventoryCommandRequests.Move( - pick.getInventoryPositionId(), n(pick.getPackages()), - outbound.getOutboundNo(), outbound.getOutboundNo(), null, key, request.remark())); - shipped += n(pick.getPackages()); - } + long shipped = dispatchPicks.stream().mapToLong(p -> n(p.getPackages())).sum(); LocalDateTime now = LocalDateTime.now(); pickAttribution.depart(customerId, outbound.getOutboundNo(), order.getDispatchNo(), dispatchPicks, now); outbound.setStatus("OUTBOUNDED"); @@ -227,7 +233,8 @@ public class DispatchPdaWorkflowService { List palletPositions = item.getWarehousePalletId() == null ? List.of() : positions.listPallet(customerId, item.getWarehousePalletId()); values.add(new DispatchPdaResponses.Item(item.getId(), item.getPalletNo(), item.getDestinationWarehouseCode(), - item.getDestinationWarehouseName(), item.getFbaWarehouseCode(), item.getPackageCount(), + item.getDestinationWarehouseName(), item.getFbaWarehouseCode(), + warehouseRouteLabel(item.getFbaWarehouseCode(), item.getDestinationWarehouseCode()), item.getPackageCount(), picks.totalItem(customerId, task.getId(), item.getId()), palletPositions.stream().map(p -> new DispatchPdaResponses.Position(p.getId(), p.getBalanceId(), n(p.getAvailablePackages()), n(p.getAllocatedPackages()), n(p.getPickedPackages()))).toList())); @@ -270,6 +277,12 @@ public class DispatchPdaWorkflowService { private String json(Object value) { try { return objectMapper.writeValueAsString(value); } catch (Exception e) { throw new IllegalStateException(e); } } private static long n(Long value) { return value == null ? 0 : value; } private static long n(Integer value) { return value == null ? 0 : value; } + static String warehouseRouteLabel(String originalFbaWarehouseCode, String destinationWarehouseCode) { + String original = trim(originalFbaWarehouseCode); + String destination = trim(destinationWarehouseCode); + return original != null && destination != null && !original.equalsIgnoreCase(destination) + ? original + " -> " + destination : null; + } private static String trim(String value) { return value == null || value.trim().isEmpty() ? null : value.trim(); } private static String required(String value, String message) { String result = trim(value); if (result == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, message); return result; } private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionService.java index 441d30d56..c435f098e 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionService.java @@ -84,6 +84,7 @@ public class DispatchPickOrderAttributionService { long shipped = value(before.getShippedPackageCount()) + entry.getValue(); String to = shipped == value(before.getPackageCount()) ? "COMPLETED" : "PARTIALLY_SHIPPED"; LastlegOrderStatusHistory row = new LastlegOrderStatusHistory(); + row.setCustomerId(customerId); row.setOrderId(entry.getKey()); row.setFromStatus(before.getCurrentStatus()); row.setToStatus(to); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/service/DispatchService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/service/DispatchService.java index 54a7aa03e..9fcd08079 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/service/DispatchService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dispatch/service/DispatchService.java @@ -31,6 +31,7 @@ import org.springframework.web.server.ResponseStatusException; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import com.baiyue.lastleg.service.LastlegOrderNoGenerator; import java.util.HashSet; import java.util.List; import java.util.Objects; @@ -48,6 +49,7 @@ public class DispatchService { @Resource private DispatchDraftMapper drafts; @Resource private DispatchDraftContainerMapper draftContainers; @Resource private DispatchCandidateMapper candidates; + @Resource private LastlegOrderNoGenerator orderNoGenerator; public String resolveCustomerScopeForPlans(List planIds) { return requireSingleCustomerScope(candidates.findCustomerScopesByPlanIds(normalizeIds(planIds, "Plan IDs")), @@ -239,7 +241,7 @@ public class DispatchService { DispatchOrder dispatch = new DispatchOrder(); dispatch.setId(IdWorker.getId()); dispatch.setCustomerId(customerId); - dispatch.setDispatchNo("PC" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + dispatch.getId()); + dispatch.setDispatchNo(orderNoGenerator.next("DISPATCH", selected.get(0).overseasWarehouseId(), dispatch.getId())); dispatch.setOverseasWarehouseId(selected.get(0).overseasWarehouseId()); dispatch.setOverseasWarehouseName(selected.get(0).overseasWarehouseName()); dispatch.setRoute(StringUtils.hasText(request.route()) ? request.route().trim() : route(selected)); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipCartonMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipCartonMapper.java index 141959c3f..d38840c65 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipCartonMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipCartonMapper.java @@ -15,4 +15,8 @@ public interface DropshipCartonMapper extends BaseMapper { @Select("SELECT * FROM ll_dropship_carton WHERE customer_id=#{customerId} AND order_id=#{orderId} ORDER BY profile_id,carton_sequence") List listOrder(@Param("customerId") String customerId, @Param("orderId") Long orderId); + @Select("SELECT * FROM ll_dropship_carton WHERE customer_id=#{customerId} AND profile_id=#{profileId} AND sku_code=#{sku} AND status='AVAILABLE' ORDER BY carton_sequence LIMIT #{packages} FOR UPDATE") + List lockAvailable(@Param("customerId") String customerId, @Param("profileId") Long profileId, @Param("sku") String sku, @Param("packages") long packages); + @Update("UPDATE ll_dropship_carton SET status='AVAILABLE', outbound_order_no=NULL, updated_at=NOW(3) WHERE customer_id=#{customerId} AND outbound_order_no=#{outboundNo} AND status='SHIPPED'") + int restoreOutbound(@Param("customerId") String customerId, @Param("outboundNo") String outboundNo); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipOrderProfileMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipOrderProfileMapper.java index fbd4730b0..cc99b10ff 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipOrderProfileMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipOrderProfileMapper.java @@ -9,4 +9,6 @@ import java.util.List; public interface DropshipOrderProfileMapper extends BaseMapper { @Select("SELECT * FROM ll_dropship_order_profile WHERE customer_id=#{customerId} AND order_id=#{orderId} ORDER BY source_pallet_no") List listOrder(@Param("customerId") String customerId, @Param("orderId") Long orderId); + @Select("SELECT * FROM ll_dropship_order_profile WHERE customer_id=#{customerId} AND inventory_position_id=#{positionId} LIMIT 1") + DropshipOrderProfile findPosition(@Param("customerId") String customerId, @Param("positionId") Long positionId); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipSkuInventoryMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipSkuInventoryMapper.java index 9810273c2..9c21b3a0f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipSkuInventoryMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/mapper/DropshipSkuInventoryMapper.java @@ -6,6 +6,7 @@ import com.baiyue.lastleg.dropship.entity.DropshipSkuInventory; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; import java.util.List; @@ -13,4 +14,8 @@ import java.util.List; public interface DropshipSkuInventoryMapper extends BaseMapper { @Select("SELECT * FROM ll_dropship_sku_inventory WHERE customer_id=#{customerId} AND order_id=#{orderId} ORDER BY profile_id,sku_code") List listOrder(@Param("customerId") String customerId, @Param("orderId") Long orderId); + @Update("UPDATE ll_dropship_sku_inventory SET package_count=package_count-#{packages},updated_at=NOW(3) WHERE customer_id=#{customerId} AND profile_id=#{profileId} AND sku_code=#{sku} AND package_count>=#{packages}") + int deduct(@Param("customerId") String customerId, @Param("profileId") Long profileId, @Param("sku") String sku, @Param("packages") long packages); + @Update("UPDATE ll_dropship_sku_inventory SET package_count=package_count+#{packages},updated_at=NOW(3) WHERE customer_id=#{customerId} AND profile_id=#{profileId} AND sku_code=#{sku}") + int restore(@Param("customerId") String customerId, @Param("profileId") Long profileId, @Param("sku") String sku, @Param("packages") long packages); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/model/DropshipPalletSnapshot.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/model/DropshipPalletSnapshot.java new file mode 100644 index 000000000..105ae33f1 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/model/DropshipPalletSnapshot.java @@ -0,0 +1,8 @@ +package com.baiyue.lastleg.dropship.model; + +import java.util.List; + +public record DropshipPalletSnapshot(String sourcePalletNo, long packageCount, + List skus) { + public record Sku(String skuCode, String productName, long packageCount, long productQuantity) {} +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/service/DropshipOrderService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/service/DropshipOrderService.java index 6d37fc43f..c8e1e029f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/service/DropshipOrderService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dropship/service/DropshipOrderService.java @@ -7,6 +7,7 @@ import com.baiyue.lastleg.dropship.mapper.*; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.inboundplan.pda.entity.InboundStockReceipt; import com.baiyue.lastleg.mapper.LastlegOrderMapper; +import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot; import jakarta.annotation.Resource; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -109,6 +110,21 @@ public class DropshipOrderService { return profile; } + @Transactional(readOnly = true) + public List palletSnapshots(String customerId, Long orderId) { + List orderProfiles = profiles.listOrder(customerId, orderId); + Map> skusByProfile = skuInventory.listOrder(customerId, orderId).stream() + .collect(Collectors.groupingBy(DropshipSkuInventory::getProfileId)); + return orderProfiles.stream().map(profile -> { + List skus = skusByProfile.getOrDefault(profile.getId(), List.of()).stream() + .map(sku -> new DropshipPalletSnapshot.Sku(sku.getSkuCode(), sku.getProductName(), + value(sku.getPackageCount()), value(sku.getProductQuantity()))) + .toList(); + return new DropshipPalletSnapshot(profile.getSourcePalletNo(), + skus.stream().mapToLong(DropshipPalletSnapshot.Sku::packageCount).sum(), skus); + }).toList(); + } + @Transactional public void activateAfterStockIn(String customerId, Long orderId, Long taskId, List receipts) { @@ -118,20 +134,18 @@ public class DropshipOrderService { if (profilesForOrder.stream().anyMatch(profile -> "ACTIVE".equals(profile.getStatus()))) { throw conflict("Dropship pallet activation is incomplete; repair profiles before retrying"); } - Map> receiptsByPallet = receipts.stream() - .filter(receipt -> receipt.getWarehousePalletId() != null) - .collect(Collectors.groupingBy(InboundStockReceipt::getWarehousePalletId)); + Map> receiptsByPallet = receipts.stream() + .filter(receipt -> receipt.getPalletNo() != null) + .collect(Collectors.groupingBy(InboundStockReceipt::getPalletNo)); if (receiptsByPallet.size() != profilesForOrder.size()) { throw conflict("PDA pallet count does not match dropship source pallet count"); } - List orderedProfiles = profilesForOrder.stream() - .sorted(java.util.Comparator.comparing(DropshipOrderProfile::getSourcePalletNo)).toList(); - List> orderedReceipts = receiptsByPallet.values().stream() - .sorted(java.util.Comparator.comparing(value -> value.get(0).getWarehousePalletId())).toList(); LocalDateTime now = LocalDateTime.now(); - for (int index = 0; index < orderedProfiles.size(); index++) { - DropshipOrderProfile profile = orderedProfiles.get(index); - List actual = orderedReceipts.get(index); + for (DropshipOrderProfile profile : profilesForOrder) { + List actual = receiptsByPallet.get(profile.getSourcePalletNo()); + if (actual == null || actual.isEmpty()) { + throw conflict("PDA receipt is missing for dropship source pallet " + profile.getSourcePalletNo()); + } List profileCartons = cartons.listProfile(customerId, profile.getId()); long expected = profileCartons.size(); long received = actual.stream().mapToLong(value -> value.getReceivedPackages() == null ? 0 : value.getReceivedPackages()).sum(); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderDetailResponse.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderDetailResponse.java index 6c1285038..27650b7b0 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderDetailResponse.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderDetailResponse.java @@ -1,6 +1,7 @@ package com.baiyue.lastleg.dto; import com.baiyue.lastleg.entity.LastlegOrderDetail; +import com.baiyue.lastleg.entity.LastlegOrderFee; import java.time.LocalDateTime; @@ -9,7 +10,10 @@ import java.util.List; public record LastlegOrderDetailResponse( LastlegOrderSummary order, List cargo, - List statusHistory + List statusHistory, + List fees, + List operationHistory ) { public record History(Long id, String fromStatus, String toStatus, String remark, LocalDateTime createdAt) {} + public record Operation(Long id, String operationNode, String operatorName, String result, LocalDateTime createdAt) {} } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderRequests.java index c5472f7e7..9292e841f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderRequests.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderRequests.java @@ -5,7 +5,11 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Size; +import jakarta.validation.constraints.DecimalMin; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.NotNull; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; @@ -14,8 +18,8 @@ public final class LastlegOrderRequests { } public record Query( - @Min(1) long page, - @Min(1) @Max(200) long size, + @Min(1) Long page, + @Min(1) @Max(200) Long size, String orderNo, String orderType, String inboundCode, @@ -31,8 +35,8 @@ public final class LastlegOrderRequests { LocalDateTime createdTo ) { public Query { - if (page == 0) page = 1; - if (size == 0) size = 20; + if (page == null || page == 0) page = 1L; + if (size == null || size == 0) size = 20L; } } @@ -68,4 +72,30 @@ public final class LastlegOrderRequests { return new TransferDestination(List.of(orderId), warehouseId, warehouseCode, warehouseName, remark); } } + + public record CreateFee( + @NotBlank @Size(max = 128) String feeItem, + @NotBlank @Pattern(regexp = "[A-Z]{3}") String currency, + @NotNull @DecimalMin(value = "0.01") BigDecimal amount, + @NotBlank @Pattern(regexp = "PENDING_REVIEW|APPROVED|REJECTED") String status, + @Size(max = 500) String supplierDescription + ) { + } + + public record SetDetained( + @NotNull Boolean detained, + @NotBlank @Size(max = 500) String remark + ) { + } + + public record SetPalletMergeRequired( + @NotNull Boolean palletMergeRequired + ) { + } + + public record SetPalletMergeBatch( + @NotEmpty @Size(max = 200) List orderIds, + @NotNull Boolean palletMergeRequired + ) { + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderSummary.java b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderSummary.java index fb5b95346..6384a8b69 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderSummary.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/dto/LastlegOrderSummary.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import java.math.BigDecimal; import java.time.LocalDateTime; +import java.util.List; @Schema(description = "按入仓单编码粒度展示的尾程订单列表行") public record LastlegOrderSummary( @@ -40,6 +41,9 @@ public record LastlegOrderSummary( String relabelOrderNo, String relabelStatus, String palletMergeFlag, + String palletMergeBatchId, + @Schema(description = "同一合托组内其他来源入仓单的入仓码") + List palletMergeInboundNos, String currentStatus, String subOrderStatus, String privateRemark, diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrder.java b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrder.java index 89234d307..4f48bb76c 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrder.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrder.java @@ -52,6 +52,7 @@ public class LastlegOrder { private String relabelOrderNo; private String relabelStatus; private String palletMergeFlag; + private String palletMergeBatchId; private String currentStatus; private String subOrderStatus; private String privateRemark; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderFee.java b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderFee.java new file mode 100644 index 000000000..7e1b2d9e9 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderFee.java @@ -0,0 +1,25 @@ +package com.baiyue.lastleg.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +@Data +@TableName("ll_order_fee") +public class LastlegOrderFee { + @TableId(type = IdType.INPUT) private Long id; + private String customerId; + private Long orderId; + private String feeItem; + private String currency; + private BigDecimal amount; + private String status; + private String supplierDescription; + private String createdBy; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderOperationLog.java b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderOperationLog.java new file mode 100644 index 000000000..db8a7b860 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/entity/LastlegOrderOperationLog.java @@ -0,0 +1,20 @@ +package com.baiyue.lastleg.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("ll_order_operation_log") +public class LastlegOrderOperationLog { + @TableId(type = IdType.INPUT) private Long id; + private String customerId; + private Long orderId; + private String operationNode; + private String operatorName; + private String result; + private LocalDateTime createdAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/controller/ExceptionOrderController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/controller/ExceptionOrderController.java new file mode 100644 index 000000000..2804eec2f --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/controller/ExceptionOrderController.java @@ -0,0 +1,73 @@ +package com.baiyue.lastleg.exceptionorder.controller; + +import com.baiyue.common.core.api.ApiResponse; +import com.baiyue.common.core.api.PageResult; +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.lastleg.exceptionorder.dto.ExceptionOrderRequests; +import com.baiyue.lastleg.exceptionorder.dto.ExceptionOrderResponses; +import com.baiyue.lastleg.exceptionorder.service.ExceptionOrderService; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +@RestController +@RequestMapping("/api/platform/v1/last-leg") +public class ExceptionOrderController { + @Resource private ExceptionOrderService service; + + @GetMapping("/exception-orders") + public ApiResponse> page(@AuthenticationPrincipal AuthenticatedUser user, + @Valid ExceptionOrderRequests.Query query) { + return ApiResponse.success(service.page(scope(user), query)); + } + + @GetMapping("/exception-orders/resolve") + public ApiResponse resolve(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @RequestParam(required = false) String inboundCode, + @RequestParam(required = false) String lastMileOrderNo) { + return ApiResponse.success(service.resolve(scope(user), customerId, inboundCode, lastMileOrderNo)); + } + + @PostMapping("/exception-orders") + public ApiResponse create(@AuthenticationPrincipal AuthenticatedUser user, + @Valid @RequestBody ExceptionOrderRequests.Create request) { + return ApiResponse.success(service.create(scope(user), operator(user), request)); + } + + @GetMapping("/delivery-orders/{deliveryNo}/return-inbound-options") + public ApiResponse> returnOptions(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable String deliveryNo) { + return ApiResponse.success(service.returnCandidates(requiredScope(user, customerId), deliveryNo)); + } + + @PostMapping("/delivery-orders/{deliveryNo}/mark-return") + public ApiResponse markReturn(@AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(required = false) String customerId, + @PathVariable String deliveryNo, + @Valid @RequestBody ExceptionOrderRequests.MarkReturn request) { + return ApiResponse.success(service.markReturn(requiredScope(user, customerId), operator(user), deliveryNo, request)); + } + + private String scope(AuthenticatedUser user) { + if (user == null || user.tenantId() == null) throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); + return "PLATFORM".equals(user.appCode()) && user.tenantId() == 0L ? null : String.valueOf(user.tenantId()); + } + + private String requiredScope(AuthenticatedUser user, String requestedCustomer) { + String own = scope(user); + if (own != null) return own; + if (requestedCustomer == null || requestedCustomer.isBlank()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "请选择客户"); + return requestedCustomer.trim(); + } + + private String operator(AuthenticatedUser user) { + return user.userId() == null ? user.username() : String.valueOf(user.userId()); + } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderRequests.java new file mode 100644 index 000000000..8af47a000 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderRequests.java @@ -0,0 +1,34 @@ +package com.baiyue.lastleg.exceptionorder.dto; + +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +import java.time.LocalDateTime; + +public final class ExceptionOrderRequests { + private ExceptionOrderRequests() {} + + public record Query(@Min(1) long page, @Min(1) @Max(200) long size, + String keyword, String exceptionType, String status, + String customerId, String warehouseId, + LocalDateTime createdFrom, LocalDateTime createdTo) { + public Query { if (page == 0) page = 1; if (size == 0) size = 20; } + } + + public record Create(String customerId, @Size(max = 128) String inboundCode, + @Size(max = 64) String lastMileOrderNo, + @NotBlank @Size(max = 32) String exceptionType, + @NotBlank @Size(max = 1000) String reason, + @Min(1) Integer packageCount, + @Size(max = 128) String skuCode) {} + + public record MarkReturn(@NotNull Long inboundOrderId, + @NotBlank @Size(max = 1000) String reason, + @NotBlank @Size(max = 32) String returnDirection, + @Size(max = 128) String returnLogisticsNo, + @Min(1) Integer packageCount, + @Size(max = 128) String skuCode) {} +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderResponses.java new file mode 100644 index 000000000..5847820c8 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/dto/ExceptionOrderResponses.java @@ -0,0 +1,25 @@ +package com.baiyue.lastleg.exceptionorder.dto; + +import java.time.LocalDateTime; + +public final class ExceptionOrderResponses { + private ExceptionOrderResponses() {} + + public record Row(Long id, String exceptionNo, String exceptionType, String status, + String reason, String customerId, String customerName, + String overseasWarehouseId, String overseasWarehouseName, + Long lastMileOrderId, String lastMileOrderNo, + Long inboundOrderId, String inboundCode, + Long deliveryOrderId, String deliveryNo, String outboundNo, + String skuCode, Integer packageCount, String returnDirection, + String returnLogisticsNo, String handlingAction, + String handlerName, LocalDateTime exceptionAt, LocalDateTime updatedAt) {} + + public record Relation(Long lastMileOrderId, String lastMileOrderNo, + Long inboundOrderId, String inboundCode, + String customerId, String customerName, + String overseasWarehouseId, String overseasWarehouseName, + String deliveryNo, String outboundNo, Integer availablePackages) {} + + public record Created(Long id, String exceptionNo, String status) {} +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/entity/ExceptionOrder.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/entity/ExceptionOrder.java new file mode 100644 index 000000000..b60908bb5 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/entity/ExceptionOrder.java @@ -0,0 +1,42 @@ +package com.baiyue.lastleg.exceptionorder.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +@TableName("ll_exception_order") +public class ExceptionOrder { + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private String customerId; + private String exceptionNo; + private String exceptionType; + private String status; + private String reason; + private Long lastMileOrderId; + private String lastMileOrderNo; + private Long inboundOrderId; + private String inboundCode; + private Long deliveryOrderId; + private String deliveryNo; + private String outboundNo; + private String customerName; + private String overseasWarehouseId; + private String overseasWarehouseName; + private String skuCode; + private Integer packageCount; + private String returnDirection; + private String returnLogisticsNo; + private String handlingAction; + private String handlerId; + private String handlerName; + private String sourceType; + private String createdBy; + private LocalDateTime exceptionAt; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/mapper/ExceptionOrderMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/mapper/ExceptionOrderMapper.java new file mode 100644 index 000000000..39c25ca45 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/mapper/ExceptionOrderMapper.java @@ -0,0 +1,15 @@ +package com.baiyue.lastleg.exceptionorder.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.exceptionorder.entity.ExceptionOrder; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +@Mapper +public interface ExceptionOrderMapper extends BaseMapper { + @Select("SELECT * FROM ll_exception_order WHERE customer_id=#{customerId} AND delivery_order_id=#{deliveryOrderId} " + + "AND exception_type='RETURN' LIMIT 1") + ExceptionOrder findDeliveryReturn(@Param("customerId") String customerId, + @Param("deliveryOrderId") Long deliveryOrderId); +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderService.java new file mode 100644 index 000000000..61122525e --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderService.java @@ -0,0 +1,247 @@ +package com.baiyue.lastleg.exceptionorder.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baiyue.common.core.api.PageResult; +import com.baiyue.lastleg.delivery.entity.DeliveryOperation; +import com.baiyue.lastleg.delivery.entity.DeliveryOrder; +import com.baiyue.lastleg.delivery.mapper.DeliveryOperationMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryOrderMapper; +import com.baiyue.lastleg.entity.LastlegInboundOrder; +import com.baiyue.lastleg.entity.LastlegOrder; +import com.baiyue.lastleg.exceptionorder.dto.ExceptionOrderRequests; +import com.baiyue.lastleg.exceptionorder.dto.ExceptionOrderResponses; +import com.baiyue.lastleg.exceptionorder.entity.ExceptionOrder; +import com.baiyue.lastleg.exceptionorder.mapper.ExceptionOrderMapper; +import com.baiyue.lastleg.mapper.LastlegInboundOrderMapper; +import com.baiyue.lastleg.mapper.LastlegOrderMapper; +import jakarta.annotation.Resource; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import org.springframework.web.server.ResponseStatusException; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Objects; + +@Service +public class ExceptionOrderService { + @Resource private com.baiyue.lastleg.service.LastlegOrderNoGenerator orderNoGenerator; + @Resource private ExceptionOrderMapper exceptionOrders; + @Resource private LastlegOrderMapper lastMileOrders; + @Resource private LastlegInboundOrderMapper inboundOrders; + @Resource private DeliveryOrderMapper deliveryOrders; + @Resource private DeliveryOperationMapper deliveryOperations; + + public PageResult page(String scopedCustomerId, ExceptionOrderRequests.Query query) { + String customerId = customerScope(scopedCustomerId, query.customerId()); + Page result = exceptionOrders.selectPage(new Page<>(query.page(), query.size()), + new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), ExceptionOrder::getCustomerId, customerId) + .and(StringUtils.hasText(query.keyword()), wrapper -> wrapper + .like(ExceptionOrder::getExceptionNo, trim(query.keyword())) + .or().like(ExceptionOrder::getLastMileOrderNo, trim(query.keyword())) + .or().like(ExceptionOrder::getInboundCode, trim(query.keyword())) + .or().like(ExceptionOrder::getDeliveryNo, trim(query.keyword())) + .or().like(ExceptionOrder::getCustomerName, trim(query.keyword())) + .or().like(ExceptionOrder::getReason, trim(query.keyword()))) + .eq(StringUtils.hasText(query.exceptionType()), ExceptionOrder::getExceptionType, trim(query.exceptionType())) + .eq(StringUtils.hasText(query.status()), ExceptionOrder::getStatus, trim(query.status())) + .eq(StringUtils.hasText(query.warehouseId()), ExceptionOrder::getOverseasWarehouseId, trim(query.warehouseId())) + .ge(query.createdFrom() != null, ExceptionOrder::getExceptionAt, query.createdFrom()) + .le(query.createdTo() != null, ExceptionOrder::getExceptionAt, query.createdTo()) + .orderByDesc(ExceptionOrder::getExceptionAt).orderByDesc(ExceptionOrder::getId)); + return new PageResult<>(result.getRecords().stream().map(this::row).toList(), result.getCurrent(), + result.getSize(), result.getTotal(), result.getPages()); + } + + public ExceptionOrderResponses.Relation resolve(String scopedCustomerId, String requestedCustomerId, + String inboundCode, String lastMileOrderNo) { + return relation(scopedCustomerId, requestedCustomerId, inboundCode, lastMileOrderNo); + } + + public List returnCandidates(String customerId, String deliveryNo) { + DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo); + if (delivery == null) throw notFound("派送单不存在"); + return lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegOrder::getCustomerId, customerId) + .eq(LastlegOrder::getDeliveryOrderNo, deliveryNo) + .orderByAsc(LastlegOrder::getOrderNo)).stream() + .map(order -> relation(customerId, customerId, order.getInboundCode(), order.getOrderNo())) + .distinct().toList(); + } + + @Transactional + public ExceptionOrderResponses.Created create(String scopedCustomerId, String operatorId, + ExceptionOrderRequests.Create request) { + ExceptionOrderResponses.Relation relation = relation(scopedCustomerId, request.customerId(), + request.inboundCode(), request.lastMileOrderNo()); + return insert(relation, null, normalizeType(request.exceptionType()), request.reason(), request.packageCount(), + request.skuCode(), null, null, "MANUAL", operatorId); + } + + @Transactional + public ExceptionOrderResponses.Created markReturn(String customerId, String operatorId, String deliveryNo, + ExceptionOrderRequests.MarkReturn request) { + DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo); + if (delivery == null) throw notFound("派送单不存在"); + ExceptionOrder existing = exceptionOrders.findDeliveryReturn(customerId, delivery.getId()); + if (existing != null) return new ExceptionOrderResponses.Created(existing.getId(), existing.getExceptionNo(), existing.getStatus()); + + List candidates = returnCandidates(customerId, deliveryNo); + ExceptionOrderResponses.Relation selected = candidates.stream() + .filter(candidate -> Objects.equals(candidate.inboundOrderId(), request.inboundOrderId())) + .findFirst().orElseThrow(() -> conflict("所选入仓单不属于该派送单")); + ExceptionOrderResponses.Created created = insert(selected, delivery, "RETURN", request.reason(), + request.packageCount(), request.skuCode(), request.returnDirection(), request.returnLogisticsNo(), + "DELIVERY_RETURN", operatorId); + + LocalDateTime now = LocalDateTime.now(); + delivery.setStatus("RETURN_PENDING"); + delivery.setUpdatedAt(now); + deliveryOrders.updateById(delivery); + LastlegOrder tail = lastMileOrders.selectById(selected.lastMileOrderId()); + if (tail != null) { + tail.setAbnormal(true); + tail.setSubOrderStatus("RETURN_PENDING"); + tail.setUpdatedAt(now); + lastMileOrders.updateById(tail); + } + DeliveryOperation operation = new DeliveryOperation(); + operation.setCustomerId(customerId); operation.setDeliveryOrderId(delivery.getId()); + operation.setOperationNode("MARK_RETURN"); operation.setOperatorId(operatorId); operation.setOperatorName(operatorId); + operation.setResult("已生成异常退货单 " + created.exceptionNo()); operation.setCreatedAt(now); + deliveryOperations.insert(operation); + return created; + } + + private ExceptionOrderResponses.Relation relation(String scopedCustomerId, String requestedCustomerId, + String inboundCode, String lastMileOrderNo) { + String customerId = customerScope(scopedCustomerId, requestedCustomerId); + String inbound = trim(inboundCode); + String tailNo = trim(lastMileOrderNo); + if (!StringUtils.hasText(inbound) && !StringUtils.hasText(tailNo)) { + throw badRequest("入仓码和尾程订单号至少填写一个"); + } + LastlegOrder tail = StringUtils.hasText(tailNo) ? uniqueTail(customerId, tailNo) : null; + LastlegInboundOrder inboundOrder = StringUtils.hasText(inbound) ? uniqueInbound(customerId, inbound) : null; + if (tail != null && inboundOrder != null && !Objects.equals(tail.getInboundCode(), inboundOrder.getInboundCode())) { + throw conflict("尾程订单与入仓单不是同一关联关系"); + } + if (tail == null && inboundOrder != null) tail = optionalTail(inboundOrder.getCustomerId(), inboundOrder.getInboundCode()); + if (inboundOrder == null && tail != null) inboundOrder = optionalInbound(tail.getCustomerId(), tail.getInboundCode()); + if (tail == null && inboundOrder == null) throw notFound("未找到对应单据"); + String resolvedCustomer = tail != null ? tail.getCustomerId() : inboundOrder.getCustomerId(); + if (tail != null && inboundOrder != null && !Objects.equals(tail.getCustomerId(), inboundOrder.getCustomerId())) { + throw conflict("尾程订单与入仓单不属于同一客户"); + } + return new ExceptionOrderResponses.Relation( + tail == null ? null : tail.getId(), tail == null ? null : tail.getOrderNo(), + inboundOrder == null ? null : inboundOrder.getId(), inboundOrder == null ? null : inboundOrder.getInboundCode(), + resolvedCustomer, tail == null ? null : tail.getCustomerName(), + tail == null ? null : tail.getOverseasWarehouseId(), tail == null ? null : tail.getOverseasWarehouseName(), + tail == null ? null : tail.getDeliveryOrderNo(), tail == null ? null : tail.getOutboundOrderNo(), + tail == null ? inboundOrder.getPackageCount() : tail.getPackageCount()); + } + + private ExceptionOrderResponses.Created insert(ExceptionOrderResponses.Relation relation, DeliveryOrder delivery, + String type, String reason, Integer packages, String sku, + String direction, String logisticsNo, String sourceType, + String operatorId) { + if (packages != null && relation.availablePackages() != null && packages > relation.availablePackages()) { + throw conflict("异常箱数不能大于订单箱数 " + relation.availablePackages()); + } + LocalDateTime now = LocalDateTime.now(); + ExceptionOrder value = new ExceptionOrder(); + value.setId(IdWorker.getId()); value.setCustomerId(relation.customerId()); + value.setExceptionNo(orderNoGenerator.next("EXCEPTION", relation.overseasWarehouseId(), value.getId())); + value.setExceptionType(type); value.setStatus("PENDING"); value.setReason(trim(reason)); + value.setLastMileOrderId(relation.lastMileOrderId()); value.setLastMileOrderNo(relation.lastMileOrderNo()); + value.setInboundOrderId(relation.inboundOrderId()); value.setInboundCode(relation.inboundCode()); + value.setDeliveryOrderId(delivery == null ? null : delivery.getId()); + value.setDeliveryNo(delivery == null ? relation.deliveryNo() : delivery.getDeliveryNo()); + value.setOutboundNo(delivery == null ? relation.outboundNo() : delivery.getOutboundNo()); + value.setCustomerName(relation.customerName()); + value.setOverseasWarehouseId(delivery == null ? relation.overseasWarehouseId() : delivery.getOverseasWarehouseId()); + value.setOverseasWarehouseName(delivery == null ? relation.overseasWarehouseName() : delivery.getOverseasWarehouseName()); + value.setSkuCode(trim(sku)); value.setPackageCount(packages); value.setReturnDirection(trim(direction)); + value.setReturnLogisticsNo(trim(logisticsNo)); + if (StringUtils.hasText(direction)) { + value.setHandlingAction("OVERSEAS_WAREHOUSE".equals(direction) ? "退回海外仓" : "CUSTOMER".equals(direction) ? "退回客户" : "其他退货处理"); + value.setHandlerId(operatorId); value.setHandlerName(operatorId); + } + value.setSourceType(sourceType); value.setCreatedBy(operatorId); + value.setExceptionAt(now); value.setCreatedAt(now); value.setUpdatedAt(now); + try { exceptionOrders.insert(value); } + catch (DuplicateKeyException exception) { throw conflict("该关联单据已存在异常退货单"); } + return new ExceptionOrderResponses.Created(value.getId(), value.getExceptionNo(), value.getStatus()); + } + + private LastlegOrder uniqueTail(String customerId, String orderNo) { + List values = lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), LastlegOrder::getCustomerId, customerId) + .eq(LastlegOrder::getOrderNo, orderNo).last("LIMIT 2")); + if (values.isEmpty()) throw notFound("尾程订单不存在"); + if (values.size() > 1) throw conflict("尾程订单号不唯一,请先选择客户"); + return values.get(0); + } + + private LastlegInboundOrder uniqueInbound(String customerId, String inboundCode) { + List values = inboundOrders.selectList(new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), LastlegInboundOrder::getCustomerId, customerId) + .eq(LastlegInboundOrder::getInboundCode, inboundCode).last("LIMIT 2")); + if (values.isEmpty()) throw notFound("入仓单不存在"); + if (values.size() > 1) throw conflict("入仓码不唯一,请先选择客户"); + return values.get(0); + } + + private LastlegOrder optionalTail(String customerId, String inboundCode) { + List values = lastMileOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegOrder::getCustomerId, customerId).eq(LastlegOrder::getInboundCode, inboundCode).last("LIMIT 2")); + if (values.size() > 1) throw conflict("该入仓码关联了多个尾程订单,请修复关联关系后重试"); + return values.size() == 1 ? values.get(0) : null; + } + + private LastlegInboundOrder optionalInbound(String customerId, String inboundCode) { + if (!StringUtils.hasText(inboundCode)) return null; + List values = inboundOrders.selectList(new LambdaQueryWrapper() + .eq(LastlegInboundOrder::getCustomerId, customerId).eq(LastlegInboundOrder::getInboundCode, inboundCode).last("LIMIT 2")); + if (values.size() > 1) throw conflict("该尾程订单关联了多个入仓单,请修复关联关系后重试"); + return values.size() == 1 ? values.get(0) : null; + } + + private ExceptionOrderResponses.Row row(ExceptionOrder value) { + return new ExceptionOrderResponses.Row(value.getId(), value.getExceptionNo(), value.getExceptionType(), value.getStatus(), + value.getReason(), value.getCustomerId(), value.getCustomerName(), value.getOverseasWarehouseId(), + value.getOverseasWarehouseName(), value.getLastMileOrderId(), value.getLastMileOrderNo(), + value.getInboundOrderId(), value.getInboundCode(), value.getDeliveryOrderId(), value.getDeliveryNo(), + value.getOutboundNo(), value.getSkuCode(), value.getPackageCount(), value.getReturnDirection(), + value.getReturnLogisticsNo(), value.getHandlingAction(), value.getHandlerName(), + value.getExceptionAt(), value.getUpdatedAt()); + } + + private String customerScope(String scopedCustomerId, String requestedCustomerId) { + if (StringUtils.hasText(scopedCustomerId)) { + if (StringUtils.hasText(requestedCustomerId) && !scopedCustomerId.equals(trim(requestedCustomerId))) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "不能访问其他客户数据"); + } + return scopedCustomerId; + } + return trim(requestedCustomerId); + } + + private static String normalizeType(String value) { + String normalized = trim(value).toUpperCase(); + if (!java.util.Set.of("RETURN", "REJECT", "DAMAGED", "OTHER").contains(normalized)) throw badRequest("不支持的异常类型"); + return normalized; + } + private static String trim(String value) { return StringUtils.hasText(value) ? value.trim() : null; } + private static ResponseStatusException badRequest(String message) { return new ResponseStatusException(HttpStatus.BAD_REQUEST, message); } + private static ResponseStatusException notFound(String message) { return new ResponseStatusException(HttpStatus.NOT_FOUND, message); } + private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/controller/InboundPlanController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/controller/InboundPlanController.java index 42772e141..849239c28 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/controller/InboundPlanController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/controller/InboundPlanController.java @@ -12,6 +12,7 @@ import com.baiyue.lastleg.inboundplan.dto.InboundPlanListResponse; import com.baiyue.lastleg.inboundplan.dto.InboundPlanRequests; import com.baiyue.lastleg.inboundplan.dto.SupplementPalletRequest; import com.baiyue.lastleg.inboundplan.dto.UpdateFbaAppointmentRequest; +import com.baiyue.lastleg.inboundplan.dto.UpdateInboundPlanRequest; import com.baiyue.lastleg.inboundplan.service.InboundFbaAppointmentService; import com.baiyue.lastleg.inboundplan.service.InboundPlanService; import com.baiyue.lastleg.security.OverseasPermissions; @@ -75,6 +76,15 @@ public class InboundPlanController { return ApiResponse.success(service.detailByPlanNo(scope(user, customerId), planNo)); } + @PutMapping("/{planNo}") + @PreAuthorize("hasAuthority('" + OverseasPermissions.INBOUND_ORDER_EDIT + "')") + @Operation(summary = "编辑入仓计划") + public ApiResponse update(@AuthenticationPrincipal AuthenticatedUser user, + @PathVariable String planNo, + @Valid @RequestBody UpdateInboundPlanRequest request) { + return ApiResponse.success(service.update(scope(user, request.customerId()), planNo, request)); + } + @PostMapping("/{planNo}/pallets/supplement") @PreAuthorize("hasAuthority('" + OverseasPermissions.INBOUND_ORDER_EDIT + "')") /** 为计划目标仓新增一个可打印的补充托盘。 */ diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanListResponse.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanListResponse.java index 5a4e1c6a5..d59689a19 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanListResponse.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanListResponse.java @@ -17,6 +17,7 @@ public record InboundPlanListResponse( String fbaWarehouseCode, String customerName, Integer packageCount, + Integer pickedPackageCount, BigDecimal volumeCbm, Integer palletCount, String status, @@ -25,6 +26,20 @@ public record InboundPlanListResponse( LocalDateTime appointmentAt, LocalDateTime expectedLoadingAt, String dispatchStatus, - LocalDateTime createdAt + LocalDateTime createdAt, + Integer version ) { + public InboundPlanListResponse(Long lineId, Long planId, String planNo, String customerId, + Long orderId, String inboundCode, String orderType, + String warehouseId, String warehouseCode, String warehouseName, + String fbaWarehouseCode, String customerName, Integer packageCount, + BigDecimal volumeCbm, Integer palletCount, String status, + String appointmentNo, String appointmentStatus, + LocalDateTime appointmentAt, LocalDateTime expectedLoadingAt, + String dispatchStatus, LocalDateTime createdAt) { + this(lineId, planId, planNo, customerId, orderId, inboundCode, orderType, warehouseId, + warehouseCode, warehouseName, fbaWarehouseCode, customerName, packageCount, 0, + volumeCbm, palletCount, status, appointmentNo, appointmentStatus, appointmentAt, + expectedLoadingAt, dispatchStatus, createdAt, 0); + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanResponse.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanResponse.java index e4e1c5e70..0fd8cd3e6 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanResponse.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/InboundPlanResponse.java @@ -14,10 +14,29 @@ public record InboundPlanResponse( int lineCount, int palletCount, BigDecimal totalVolumeCbm, + int expectedPackageCount, + int pickedPackageCount, List lines, List pallets, - List allocations + List allocations, + int version ) { + public InboundPlanResponse(Long id, String planNo, String customerId, Long sourceOrderId, + String containerNo, String status, int lineCount, int palletCount, + BigDecimal totalVolumeCbm, List lines, List pallets, + List allocations) { + this(id, planNo, customerId, sourceOrderId, containerNo, status, lineCount, palletCount, + totalVolumeCbm, lines, pallets, allocations, 0); + } + + public InboundPlanResponse(Long id, String planNo, String customerId, Long sourceOrderId, + String containerNo, String status, int lineCount, int palletCount, + BigDecimal totalVolumeCbm, List lines, List pallets, + List allocations, int version) { + this(id, planNo, customerId, sourceOrderId, containerNo, status, lineCount, palletCount, + totalVolumeCbm, lines.stream().mapToInt(Line::requiredPackageCount).sum(), + allocations.stream().mapToInt(Allocation::pickedPackageCount).sum(), lines, pallets, allocations, version); + } public record Line(Long id, Long orderId, String inboundCode, String fbaWarehouseCode, String destinationWarehouseId, String destinationWarehouseCode, String destinationWarehouseName, BigDecimal volumeCbm, int requiredPackageCount, int plannedPalletCount) { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/UpdateInboundPlanRequest.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/UpdateInboundPlanRequest.java new file mode 100644 index 000000000..77bf05034 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/dto/UpdateInboundPlanRequest.java @@ -0,0 +1,12 @@ +package com.baiyue.lastleg.inboundplan.dto; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; + +public record UpdateInboundPlanRequest( + @NotBlank String customerId, + @NotNull Long overseasWarehouseId, + String warehouseCode, + String warehouseName, + @NotNull Integer version +) {} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/entity/InboundPlan.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/entity/InboundPlan.java index 79ffac343..51c30b470 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/entity/InboundPlan.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/entity/InboundPlan.java @@ -3,6 +3,7 @@ package com.baiyue.lastleg.inboundplan.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.annotation.Version; import lombok.Data; import java.math.BigDecimal; @@ -24,4 +25,6 @@ public class InboundPlan { private BigDecimal totalVolumeCbm; private LocalDateTime createdAt; private LocalDateTime updatedAt; + @Version + private Integer version; } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapper.java index 793fba684..5c4c39f3a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapper.java @@ -24,19 +24,17 @@ public interface InboundPlanOrderMapper { @Select("SELECT o.id AS orderId,o.customer_id AS customerId,o.container_no AS containerNo," + "o.inbound_code AS inboundCode,o.current_status AS currentStatus,o.inbound_order_no AS inboundOrderNo," + "COALESCE(o.volume_cbm,0) AS volumeCbm,COALESCE(o.package_count,0) AS packageCount," + + "COALESCE(o.pallet_count,0) AS recommendedPalletCount," + "NULLIF(o.fba_warehouse_code,'') AS fbaWarehouseCode," + "COALESCE(NULLIF(o.transfer_warehouse_id,''),MIN(NULLIF(d.destination_warehouse_id,''))) AS destinationWarehouseId," + "COALESCE(NULLIF(o.transfer_warehouse_code,''),MIN(NULLIF(d.destination_warehouse_code,''))) AS destinationWarehouseCode," + "COALESCE(NULLIF(o.transfer_warehouse_name,''),MIN(NULLIF(d.destination_warehouse_name,''))) AS destinationWarehouseName," + - "COUNT(DISTINCT COALESCE(NULLIF(d.destination_warehouse_id,''),NULLIF(d.destination_warehouse_code,''),'#EMPTY#')) AS destinationCount," + - "SUM(CASE WHEN d.id IS NOT NULL " + - "AND COALESCE(NULLIF(o.transfer_warehouse_id,''),NULLIF(o.transfer_warehouse_code,'')) IS NOT NULL " + - "AND COALESCE(NULLIF(d.destination_warehouse_id,''),NULLIF(d.destination_warehouse_code,''),'#EMPTY#') " + - "<> COALESCE(NULLIF(o.transfer_warehouse_id,''),NULLIF(o.transfer_warehouse_code,'')) THEN 1 ELSE 0 END) " + - "AS destinationMismatchCount " + + "CASE WHEN COALESCE(NULLIF(o.transfer_warehouse_id,''),NULLIF(o.transfer_warehouse_code,'')) IS NOT NULL THEN 1 " + + "ELSE COUNT(DISTINCT COALESCE(NULLIF(d.destination_warehouse_id,''),NULLIF(d.destination_warehouse_code,''),'#EMPTY#')) END AS destinationCount," + + "0 AS destinationMismatchCount " + "FROM ll_order o LEFT JOIN ll_order_detail d ON d.order_id=o.id " + "WHERE o.customer_id=#{customerId} AND o.id=#{orderId} " + - "GROUP BY o.id,o.customer_id,o.container_no,o.inbound_code,o.current_status,o.inbound_order_no,o.volume_cbm,o.package_count,o.fba_warehouse_code," + + "GROUP BY o.id,o.customer_id,o.container_no,o.inbound_code,o.current_status,o.inbound_order_no,o.volume_cbm,o.package_count,o.pallet_count,o.fba_warehouse_code," + "o.transfer_warehouse_id,o.transfer_warehouse_code,o.transfer_warehouse_name " + "ORDER BY o.id FOR UPDATE") List lockOrder(@Param("customerId") String customerId, diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapper.java index ce4f67d05..effaff3e2 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapper.java @@ -30,8 +30,8 @@ public interface InboundPlanQueryMapper { " AND l.fba_warehouse_code LIKE CONCAT('%',#{q.fbaWarehouseCode},'%')" + " AND ai.id IS NULL" + " AND a.appointment_status=#{q.appointmentStatus}" + - " AND EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE di.order_id=l.order_id AND di.customer_id=#{customerId})" + - " AND NOT EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE di.order_id=l.order_id AND di.customer_id=#{customerId})" + + " AND EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE di.customer_id=#{customerId} AND di.order_id=l.order_id)" + + " AND NOT EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE di.customer_id=#{customerId} AND di.order_id=l.order_id)" + " AND p.created_at >=#{q.createdFrom}" + " AND p.created_at <=#{q.createdTo}"; @@ -39,12 +39,15 @@ public interface InboundPlanQueryMapper { "l.order_id orderId,l.inbound_code inboundCode,o.order_type orderType," + "l.destination_warehouse_id warehouseId,l.destination_warehouse_code warehouseCode," + "l.destination_warehouse_name warehouseName,l.fba_warehouse_code fbaWarehouseCode," + - "o.customer_name customerName,l.required_package_count packageCount,l.volume_cbm volumeCbm," + + "o.customer_name customerName,l.required_package_count packageCount," + + "COALESCE((SELECT SUM(pa.picked_package_count) FROM ll_inbound_plan_pallet_allocation pa WHERE pa.line_id=l.id),0) pickedPackageCount," + + "l.volume_cbm volumeCbm," + "l.planned_pallet_count palletCount,p.status status,a.appointment_no appointmentNo," + "a.appointment_status appointmentStatus,a.appointment_at appointmentAt," + "a.expected_loading_at expectedLoadingAt," + - "CASE WHEN EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE di.customer_id=#{customerId} AND di.order_id=l.order_id)" + - " THEN 'SCHEDULED' ELSE 'UNSCHEDULED' END dispatchStatus,p.created_at createdAt" + + "CASE WHEN EXISTS (SELECT 1 FROM ll_dispatch_item di WHERE " + + "di.customer_id=#{customerId} AND di.order_id=l.order_id)" + + " THEN 'SCHEDULED' ELSE 'UNSCHEDULED' END dispatchStatus,p.created_at createdAt,p.version version" + FROM_AND_FILTER + " ORDER BY p.created_at DESC,l.id DESC LIMIT #{limit} OFFSET #{offset}") List rows(@Param("customerId") String customerId, @Param("q") InboundPlanRequests.Query query, diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/model/SourceOrderSnapshot.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/model/SourceOrderSnapshot.java index b4d7d5886..fed4c1eab 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/model/SourceOrderSnapshot.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/model/SourceOrderSnapshot.java @@ -11,6 +11,7 @@ public record SourceOrderSnapshot( String inboundOrderNo, BigDecimal volumeCbm, int packageCount, + int recommendedPalletCount, String fbaWarehouseCode, String destinationWarehouseId, String destinationWarehouseCode, @@ -18,13 +19,23 @@ public record SourceOrderSnapshot( int destinationCount, int destinationMismatchCount ) { + public SourceOrderSnapshot(Long orderId, String customerId, String containerNo, String inboundCode, + String currentStatus, String inboundOrderNo, BigDecimal volumeCbm, + int packageCount, String fbaWarehouseCode, String destinationWarehouseId, + String destinationWarehouseCode, String destinationWarehouseName, + int destinationCount, int destinationMismatchCount) { + this(orderId, customerId, containerNo, inboundCode, currentStatus, inboundOrderNo, volumeCbm, + packageCount, 0, fbaWarehouseCode, destinationWarehouseId, destinationWarehouseCode, + destinationWarehouseName, destinationCount, destinationMismatchCount); + } + public SourceOrderSnapshot(Long orderId, String customerId, String containerNo, String inboundCode, String currentStatus, String inboundOrderNo, BigDecimal volumeCbm, int packageCount, String destinationWarehouseId, String destinationWarehouseCode, String destinationWarehouseName, int destinationCount, int destinationMismatchCount) { this(orderId, customerId, containerNo, inboundCode, currentStatus, inboundOrderNo, volumeCbm, - packageCount, null, destinationWarehouseId, destinationWarehouseCode, + packageCount, 0, null, destinationWarehouseId, destinationWarehouseCode, destinationWarehouseName, destinationCount, destinationMismatchCount); } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaRequests.java index 018e04692..d05ea8f4a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaRequests.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaRequests.java @@ -17,11 +17,22 @@ public final class PdaRequests { public record PackageOperation(@Positive int packageCount, @NotBlank String idempotencyKey, String operatorId, String operatorName) {} + public record WholePalletOperation(@NotBlank String idempotencyKey, + String operatorId, String operatorName) {} + public record ConfirmMerge(@Positive long sourceAllocationId, @Positive int packageCount, @NotBlank String idempotencyKey, String operatorId, String operatorName) {} - public record ScanCarton(@NotBlank String cartonMark, @Positive long planPalletId, - @NotBlank String idempotencyKey, String operatorId, String operatorName) {} + public record ScanCarton(@NotBlank String cartonMark, Long planPalletId, Integer packageCount, + @NotBlank String idempotencyKey, String operatorId, String operatorName) { + /** Backward-compatible single-carton request with an explicitly selected pallet. */ + public ScanCarton(String cartonMark, long planPalletId, String idempotencyKey, + String operatorId, String operatorName) { + this(cartonMark, planPalletId, 1, idempotencyKey, operatorId, operatorName); + } + } + + public record ForceComplete(@NotBlank String idempotencyKey, String operatorId, String operatorName) {} public record RemoveCarton(@NotBlank String cartonMark, @NotBlank String idempotencyKey, String operatorId, String operatorName) {} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaResponses.java index ca79c2b25..a1fce95bd 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaResponses.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/PdaResponses.java @@ -10,9 +10,34 @@ public final class PdaResponses { public record Task(long taskId, String taskNo, String planNo, String status, LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt, List unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt, - LocalDateTime estimatedEmptyReturnAt, + LocalDateTime estimatedEmptyReturnAt, int expectedPackageCount, + int pickedPackageCount, String orderType, boolean wholePalletInbound, + List dropshipPallets, String inboundMode, List pallets, List allocations, - List expectedCartons) {} + List expectedCartons) { + public Task(long taskId, String taskNo, String planNo, String status, + LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt, + List unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt, + LocalDateTime estimatedEmptyReturnAt, int expectedPackageCount, + int pickedPackageCount) { + this(taskId, taskNo, planNo, status, pushedAt, startedAt, completedAt, + unloadPhotoFileIds, unloadPhotoUploadedAt, estimatedEmptyReturnAt, + expectedPackageCount, pickedPackageCount, null, false, List.of(), null, + List.of(), List.of(), List.of()); + } + + public Task(long taskId, String taskNo, String planNo, String status, + LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt, + List unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt, + LocalDateTime estimatedEmptyReturnAt) { + this(taskId, taskNo, planNo, status, pushedAt, startedAt, completedAt, + unloadPhotoFileIds, unloadPhotoUploadedAt, estimatedEmptyReturnAt, 0, 0); + } + } + + public record DropshipPallet(String sourcePalletNo, long planPalletId, long allocationId, + long packageCount, List skus) {} + public record DropshipSku(String skuCode, String productName, long packageCount, long productQuantity) {} public record Pallet(long planPalletId, String palletNo, String destinationWarehouseId, String destinationWarehouseCode, String destinationWarehouseName, @@ -46,5 +71,10 @@ public final class PdaResponses { boolean sourcePalletScrapped) {} public record Carton(String result, String cartonMark, long planPalletId, long allocationId, - int palletActualPackageCount) {} + int palletActualPackageCount, String palletNo, int scannedPackageCount) { + public Carton(String result, String cartonMark, long planPalletId, long allocationId, + int palletActualPackageCount) { + this(result, cartonMark, planPalletId, allocationId, palletActualPackageCount, null, 1); + } + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/ReceivingContainerResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/ReceivingContainerResponses.java index ea5d5ccda..5666c19ea 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/ReceivingContainerResponses.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/dto/ReceivingContainerResponses.java @@ -30,14 +30,28 @@ public final class ReceivingContainerResponses { String destinationWarehouseId, String destinationWarehouseCode, String destinationWarehouseName, String fbaWarehouseCode, int plannedPalletCount, int totalPackages, - int receivedPackages, int pendingPackages) { } + int receivedPackages, int pendingPackages, + boolean palletMergeRequired, String palletMergeHint) { } public record CartonResolved(String cartonMark, String inboundCode, String destinationType, String overseasWarehouseId, String overseasWarehouseName, String destinationWarehouseId, String destinationWarehouseCode, String destinationWarehouseName, String fbaWarehouseCode, int plannedPalletCount, int totalPackages, - int receivedPackages, int pendingPackages) { } + int receivedPackages, int pendingPackages, + boolean palletMergeRequired, String palletMergeHint) { + public CartonResolved(String cartonMark, String inboundCode, String destinationType, + String overseasWarehouseId, String overseasWarehouseName, + String destinationWarehouseId, String destinationWarehouseCode, + String destinationWarehouseName, String fbaWarehouseCode, + int plannedPalletCount, int totalPackages, int receivedPackages, + int pendingPackages) { + this(cartonMark, inboundCode, destinationType, overseasWarehouseId, overseasWarehouseName, + destinationWarehouseId, destinationWarehouseCode, destinationWarehouseName, + fbaWarehouseCode, plannedPalletCount, totalPackages, receivedPackages, + pendingPackages, false, null); + } + } public record PalletResolved(String palletCode, long warehouseId, String status, String fbaWarehouseCode, String inboundCode, @@ -45,7 +59,15 @@ public final class ReceivingContainerResponses { public record Receipt(String result, String containerNo, String inboundCode, String destinationType, String palletCode, int acceptedPackages, - int receivedPackages, int pendingPackages, String sessionStatus) { } + int receivedPackages, int pendingPackages, String sessionStatus, + boolean palletMergeRequired, String palletMergeHint) { + public Receipt(String result, String containerNo, String inboundCode, + String destinationType, String palletCode, int acceptedPackages, + int receivedPackages, int pendingPackages, String sessionStatus) { + this(result, containerNo, inboundCode, destinationType, palletCode, acceptedPackages, + receivedPackages, pendingPackages, sessionStatus, false, null); + } + } public record Photo(String fileId, String fileName, String contentType, long sizeBytes) { } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/repository/PdaWorkflowRepository.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/repository/PdaWorkflowRepository.java index 5f8f1f8b3..3968df33d 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/repository/PdaWorkflowRepository.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/repository/PdaWorkflowRepository.java @@ -17,6 +17,8 @@ import com.baiyue.lastleg.inboundplan.pda.entity.InboundPdaTask; import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPalletMergeMapper; import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPdaOperationMapper; import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPdaTaskMapper; +import com.baiyue.lastleg.entity.LastlegOrder; +import com.baiyue.lastleg.mapper.LastlegOrderMapper; import org.springframework.stereotype.Repository; import jakarta.annotation.Resource; @@ -32,6 +34,7 @@ public class PdaWorkflowRepository { @Resource private InboundPdaOperationMapper operations; @Resource private InboundPalletMergeMapper merges; @Resource private InboundPlanCartonMapper cartons; + @Resource private LastlegOrderMapper orders; public InboundPlan lockPlan(String customerId, String planNo) { return plans.selectOne(new LambdaQueryWrapper() @@ -93,6 +96,8 @@ public class PdaWorkflowRepository { .eq(InboundPlanPalletAllocation::getPlanId, planId)); } + public LastlegOrder order(Long orderId) { return orderId == null ? null : orders.selectById(orderId); } + public void insert(InboundPdaTask value) { tasks.insert(value); } public void insert(InboundPdaOperation value) { operations.insert(value); } public void insert(InboundPalletMerge value) { merges.insert(value); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowService.java index dde1adef1..ae5d85b9a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowService.java @@ -14,6 +14,8 @@ import com.baiyue.lastleg.inboundplan.pda.entity.InboundPdaTask; import com.baiyue.lastleg.inboundplan.pda.gateway.WarehouseResourceGateway; import com.baiyue.lastleg.inboundplan.pda.gateway.UnloadPhotoFileGateway; import com.baiyue.lastleg.inboundplan.pda.repository.PdaWorkflowRepository; +import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot; +import com.baiyue.lastleg.dropship.service.DropshipOrderService; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.fasterxml.jackson.core.JsonProcessingException; @@ -54,6 +56,7 @@ public class InboundPdaWorkflowService { @Resource private InboundUnloadCompletedOutboxService unloadCompletedOutbox; @Resource private LastlegOrderMapper orders; @Resource private UnloadPhotoFileGateway unloadPhotoFiles; + @Resource private DropshipOrderService dropshipOrders; @Resource private ObjectMapper sourceObjectMapper; private ObjectMapper objectMapper; @@ -77,6 +80,9 @@ public class InboundPdaWorkflowService { } InboundPdaTask task = repository.lockTask(customerId, plan.getId()); LocalDateTime now = LocalDateTime.now(); + if (task != null && terminal(task.getStatus())) { + throw conflict("PDA inbound task is already completed"); + } if (task == null) { task = new InboundPdaTask(); task.setId(IdWorker.getId()); @@ -95,7 +101,7 @@ public class InboundPdaWorkflowService { plan.setStatus("PDA_PENDING"); plan.setUpdatedAt(now); repository.update(plan); - PdaResponses.Task result = task(task, planNo); + PdaResponses.Task result = taskResponse(task, plan, planNo); saveOperation(customerId, task.getId(), null, "PUSH", request.idempotencyKey(), request, result); return result; } @@ -136,7 +142,7 @@ public class InboundPdaWorkflowService { InboundPlan plan = requirePlan(customerId, planNo); InboundPdaTask task = repository.task(customerId, plan.getId()); if (task == null) throw notFound("PDA task not found"); - return task(task, planNo); + return taskResponse(task, plan, planNo); } @Transactional @@ -255,7 +261,7 @@ public class InboundPdaWorkflowService { public PdaResponses.Operation count(String customerId, String planNo, long allocationId, PdaRequests.PackageOperation request) { InboundPlan plan = requirePlan(customerId, planNo); - requireLegacyCounting(plan); + requireCountMode(plan); InboundPdaOperation replay = replay(customerId, "COUNT", request.idempotencyKey(), request); if (replay != null) return read(replay, PdaResponses.Operation.class); InboundPdaTask task = requireActiveTask(customerId, plan); @@ -283,7 +289,7 @@ public class InboundPdaWorkflowService { public PdaResponses.Operation pick(String customerId, String planNo, long allocationId, PdaRequests.PackageOperation request) { InboundPlan plan = requirePlan(customerId, planNo); - requireLegacyCounting(plan); + requireCountMode(plan); InboundPdaOperation replay = replay(customerId, "PICK", request.idempotencyKey(), request); if (replay != null) return read(replay, PdaResponses.Operation.class); InboundPdaTask task = requireActiveTask(customerId, plan); @@ -322,10 +328,44 @@ public class InboundPdaWorkflowService { return result; } + @Transactional + public PdaResponses.Operation pickWholePallet(String customerId, String planNo, long allocationId, + PdaRequests.WholePalletOperation request) { + InboundPlan plan = requirePlan(customerId, planNo); + requireDropship(plan); + InboundPdaOperation replay = replay(customerId, "PICK_WHOLE_PALLET", request.idempotencyKey(), request); + if (replay != null) return read(replay, PdaResponses.Operation.class); + InboundPdaTask task = requireActiveTask(customerId, plan); + InboundPlanPalletAllocation allocation = requireAllocation(plan.getId(), allocationId); + InboundPlanLine line = requireLine(plan.getId(), allocation.getLineId()); + int planned = value(allocation.getPlannedPackageCount()); + if (planned <= 0) throw conflict("One-click dropship pallet has no planned packages"); + if (value(allocation.getPickedPackageCount()) != 0) { + throw conflict("One-click dropship pallet has already been partially operated"); + } + allocation.setCountedPackageCount(planned); + allocation.setPickedPackageCount(planned); + allocation.setActualVolumeCbm(volume(allocation.getAllocatedVolumeCbm())); + allocation.setStatus("PICKED"); + bump(allocation); + repository.update(allocation); + InboundPlanPallet pallet = requirePallet(plan.getId(), allocation.getPalletId()); + pallet.setActualPackageCount(planned); + pallet.setActualVolumeCbm(volume(allocation.getAllocatedVolumeCbm())); + repository.update(pallet); + startPicking(task, plan, request.operatorId(), request.operatorName()); + PdaResponses.Operation result = operation("WHOLE_PALLET_PICKED", allocation, line, + List.of(), 0, planned, 0); + saveOperation(customerId, task.getId(), allocationId, "PICK_WHOLE_PALLET", + request.idempotencyKey(), request, result); + return result; + } + /** 海外首次扫描箱唛:仅接受本柜国内冻结且已归属本计划的箱。 */ @Transactional public PdaResponses.Carton scanCarton(String customerId, String planNo, PdaRequests.ScanCarton request) { InboundPlan plan = requirePlan(customerId, planNo); + requireCartonScanMode(plan); InboundPdaOperation replay = replay(customerId, "SCAN_CARTON", request.idempotencyKey(), request); if (replay != null) return read(replay, PdaResponses.Carton.class); InboundPdaTask task = requireActiveTask(customerId, plan); @@ -336,16 +376,23 @@ public class InboundPdaWorkflowService { if (!("UNASSIGNED".equals(carton.getStatus()) || "EXPECTED".equals(carton.getStatus()))) { throw conflict("Carton mark has already been scanned; remove it before scanning again"); } - InboundPlanPallet pallet = requirePallet(plan.getId(), request.planPalletId()); + long requestedPalletId = request.planPalletId() == null ? 0L : request.planPalletId(); + InboundPlanPallet pallet = requestedPalletId > 0 + ? requirePallet(plan.getId(), requestedPalletId) + : autoPallet(plan, carton); if (isNotAssigned(pallet)) throw conflict("Planned pallet must be assigned and locked before carton scanning"); if (!sameWarehouse(carton, pallet)) throw conflict("Carton and pallet destination warehouses must match"); InboundPlanLine line = carton.getLineId() == null ? repository.lineForCarton(plan.getId(), carton) : requireLine(plan.getId(), carton.getLineId()); if (line == null) throw notFound("Carton mark does not belong to cargo in this inbound plan"); - if (repository.scannedCartons(plan.getId(), line.getId()) >= value(line.getRequiredPackageCount())) { + int requestedCount = request.packageCount() == null ? 1 : request.packageCount(); + if (requestedCount != 1) throw conflict("Overseas warehouse carton scanning accepts exactly one carton per scan"); + int alreadyScanned = (int) repository.scannedCartons(plan.getId(), line.getId()); + if (alreadyScanned + requestedCount > value(line.getRequiredPackageCount())) { throw conflict("This inbound plan line has reached its required carton count"); } + List cartonsToScan = List.of(carton); InboundPlanPalletAllocation allocation = repository.lockAllocation(plan.getId(), line.getId(), pallet.getId()); if (allocation == null) { allocation = new InboundPlanPalletAllocation(); allocation.setId(IdWorker.getId()); @@ -353,23 +400,29 @@ public class InboundPdaWorkflowService { allocation.setLineId(line.getId()); allocation.setPalletId(pallet.getId()); allocation.setAllocatedVolumeCbm(ZERO_VOLUME); allocation.setPlannedPackageCount(0); allocation.setCountedPackageCount(0); allocation.setPickedPackageCount(0); allocation.setActualVolumeCbm(ZERO_VOLUME); allocation.setStatus("PICKING"); allocation.setVersion(0); repository.insert(allocation); + } else if (value(allocation.getPlannedPackageCount()) > 0 + && value(allocation.getPickedPackageCount()) + requestedCount > value(allocation.getPlannedPackageCount())) { + throw conflict("Requested carton quantity exceeds the recommended pallet package count"); } BigDecimal perPackage = perPackageVolume(line); - carton.setOrderId(line.getOrderId()); carton.setPlanId(plan.getId()); carton.setLineId(line.getId()); - carton.setPlanPalletId(pallet.getId()); carton.setWarehousePalletId(pallet.getWarehousePalletId()); - carton.setWarehouseLocationId(pallet.getWarehouseLocationId()); carton.setPdaTaskId(task.getId()); - carton.setStatus("ON_PALLET"); carton.setBoundAt(LocalDateTime.now()); carton.setUpdatedAt(LocalDateTime.now()); - carton.setVersion(value(carton.getVersion()) + 1); repository.update(carton); - allocation.setCountedPackageCount(value(allocation.getCountedPackageCount()) + 1); - allocation.setPickedPackageCount(value(allocation.getPickedPackageCount()) + 1); - allocation.setActualVolumeCbm(volume(allocation.getActualVolumeCbm()).add(perPackage).setScale(4, RoundingMode.HALF_UP)); + LocalDateTime now = LocalDateTime.now(); + for (InboundPlanCarton item : cartonsToScan) { + item.setOrderId(line.getOrderId()); item.setPlanId(plan.getId()); item.setLineId(line.getId()); + item.setPlanPalletId(pallet.getId()); item.setWarehousePalletId(pallet.getWarehousePalletId()); + item.setWarehouseLocationId(pallet.getWarehouseLocationId()); item.setPdaTaskId(task.getId()); + item.setStatus("ON_PALLET"); item.setBoundAt(now); item.setUpdatedAt(now); + item.setVersion(value(item.getVersion()) + 1); repository.update(item); + } + allocation.setCountedPackageCount(value(allocation.getCountedPackageCount()) + requestedCount); + allocation.setPickedPackageCount(value(allocation.getPickedPackageCount()) + requestedCount); + allocation.setActualVolumeCbm(volume(allocation.getActualVolumeCbm()).add(perPackage.multiply(BigDecimal.valueOf(requestedCount))).setScale(4, RoundingMode.HALF_UP)); allocation.setStatus(value(allocation.getPickedPackageCount()) >= value(allocation.getPlannedPackageCount()) ? "PICKED" : "PICKING"); bump(allocation); repository.update(allocation); - pallet.setActualPackageCount(value(pallet.getActualPackageCount()) + 1); - pallet.setActualVolumeCbm(volume(pallet.getActualVolumeCbm()).add(perPackage).setScale(4, RoundingMode.HALF_UP)); + pallet.setActualPackageCount(value(pallet.getActualPackageCount()) + requestedCount); + pallet.setActualVolumeCbm(volume(pallet.getActualVolumeCbm()).add(perPackage.multiply(BigDecimal.valueOf(requestedCount))).setScale(4, RoundingMode.HALF_UP)); repository.update(pallet); startPicking(task, plan, request.operatorId(), request.operatorName()); PdaResponses.Carton result = new PdaResponses.Carton("SCANNED", carton.getCartonMark(), pallet.getId(), - allocation.getId(), pallet.getActualPackageCount()); + allocation.getId(), pallet.getActualPackageCount(), pallet.getPalletNo(), requestedCount); saveOperation(customerId, task.getId(), allocation.getId(), "SCAN_CARTON", request.idempotencyKey(), request, result); return result; } @@ -377,6 +430,7 @@ public class InboundPdaWorkflowService { @Transactional public PdaResponses.Carton removeCarton(String customerId, String planNo, PdaRequests.RemoveCarton request) { InboundPlan plan = requirePlan(customerId, planNo); + requireCartonScanMode(plan); InboundPdaOperation replay = replay(customerId, "REMOVE_CARTON", request.idempotencyKey(), request); if (replay != null) return read(replay, PdaResponses.Carton.class); InboundPdaTask task = requireActiveTask(customerId, plan); @@ -542,6 +596,7 @@ public class InboundPdaWorkflowService { if ("COMPLETED".equals(task.getStatus())) { return new CompletionContext(plan, task, List.of(), List.of(), List.of(), List.of()); } + if ("PARTIAL_INBOUND".equals(task.getStatus())) throw conflict("PDA inbound task was partially completed"); List unloadPhotoFileIds = unloadPhotoFileIds(task); if (unloadPhotoFileIds.isEmpty() || task.getUnloadPhotoUploadedAt() == null || task.getEstimatedEmptyReturnAt() == null) { @@ -589,6 +644,41 @@ public class InboundPdaWorkflowService { repository.update(plan); } + /** Explicit operator decision to receive only the cartons actually picked. */ + @Transactional + public PdaResponses.Task partialComplete(String customerId, String planNo, PdaRequests.ForceComplete request) { + InboundPlan plan = requirePlan(customerId, planNo); + InboundPdaOperation replay = replay(customerId, "PARTIAL_COMPLETE", request.idempotencyKey(), request); + if (replay != null) return read(replay, PdaResponses.Task.class); + InboundPdaTask task = requireActiveTask(customerId, plan); + List unloadPhotoFileIds = unloadPhotoFileIds(task); + if (unloadPhotoFileIds.isEmpty() || task.getUnloadPhotoUploadedAt() == null + || task.getEstimatedEmptyReturnAt() == null) { + throw conflict("Upload unload photos before partially completing the PDA inbound task"); + } + List lines = repository.lines(plan.getId()); + List allocations = repository.allocations(plan.getId()); + int expected = lines.stream().mapToInt(line -> value(line.getRequiredPackageCount())).sum(); + int picked = allocations.stream().mapToInt(allocation -> value(allocation.getPickedPackageCount())).sum(); + if (picked <= 0) throw conflict("At least one carton must be picked before partial inbound completion"); + if (picked >= expected) throw conflict("All cartons are picked; use normal inbound completion"); + LocalDateTime now = LocalDateTime.now(); + stockReceipts.stockIn(customerId, plan, task, lines, allocations, repository.pallets(plan.getId()), now, true); + for (InboundPlanCarton carton : repository.cartons(plan.getId())) { + if ("ON_PALLET".equals(carton.getStatus())) { + carton.setStatus("AVAILABLE"); carton.setReceivedAt(now); carton.setUpdatedAt(now); + carton.setVersion(value(carton.getVersion()) + 1); repository.update(carton); + } + } + task.setStatus("PARTIAL_INBOUND"); task.setCompletedAt(now); task.setUpdatedAt(now); + task.setVersion(value(task.getVersion()) + 1); repository.update(task); + plan.setStatus("PARTIAL_INBOUND"); plan.setUpdatedAt(now); repository.update(plan); + unloadCompletedOutbox.publish(plan, task, unloadPhotoFileIds); + PdaResponses.Task result = taskResponse(task, plan, planNo); + saveOperation(customerId, task.getId(), null, "PARTIAL_COMPLETE", request.idempotencyKey(), request, result); + return result; + } + @Transactional public PdaResponses.Task uploadUnloadPhotos(String customerId, String planNo, PdaRequests.UnloadPhotos request) { InboundPlan plan = requirePlan(customerId, planNo); @@ -605,7 +695,7 @@ public class InboundPdaWorkflowService { task.setUpdatedAt(uploadedAt); task.setVersion(value(task.getVersion()) + 1); repository.update(task); - PdaResponses.Task result = task(task, planNo); + PdaResponses.Task result = taskResponse(task, plan, planNo); saveOperation(customerId, task.getId(), null, "UPLOAD_UNLOAD_PHOTOS", request.idempotencyKey(), request, result); return result; } @@ -749,12 +839,34 @@ public class InboundPdaWorkflowService { repository.update(plan); } + private void requireCountMode(InboundPlan plan) { + InboundMode mode = mode(plan); + if (mode == InboundMode.DROPSHIP) { + throw conflict("One-click dropship inbound must be picked by complete source pallet"); + } + if (mode == InboundMode.OWN_OVERSEAS) { + throw conflict("Overseas warehouse inbound must be operated by one-carton scanning"); + } + } + + /** Backward-compatible guard for pallet merge, which remains a legacy quantity workflow. */ private void requireLegacyCounting(InboundPlan plan) { + if (isDropship(plan)) { + throw conflict("One-click dropship inbound must be picked by complete source pallet"); + } if (repository.hasCartonLabelPool(plan.getId())) { throw conflict("This inbound plan has a carton manifest and must be operated by carton scanning"); } } + private void requireCartonScanMode(InboundPlan plan) { + if (mode(plan) != InboundMode.OWN_OVERSEAS) { + throw conflict(mode(plan) == InboundMode.FBA + ? "FBA inbound must be operated by package quantity, not carton scanning" + : "One-click dropship inbound must be operated by complete source pallet"); + } + } + private boolean isNotAssigned(InboundPlanPallet pallet) { return pallet.getWarehousePalletId() == null || pallet.getWarehouseLocationId() == null || pallet.getWarehouseResourceLockId() == null @@ -770,7 +882,7 @@ public class InboundPdaWorkflowService { private InboundPdaTask requireActiveTask(String customerId, InboundPlan plan) { InboundPdaTask task = repository.lockTask(customerId, plan.getId()); if (task == null) throw conflict("Push the inbound plan to PDA before operating"); - if ("COMPLETED".equals(task.getStatus())) throw conflict("PDA task is already completed"); + if (terminal(task.getStatus())) throw conflict("PDA task is already completed"); return task; } @@ -786,6 +898,24 @@ public class InboundPdaWorkflowService { return value; } + private InboundPlanPallet autoPallet(InboundPlan plan, InboundPlanCarton carton) { + List candidates = repository.pallets(plan.getId()).stream() + .filter(pallet -> sameWarehouse(carton, pallet)) + .filter(pallet -> !isNotAssigned(pallet)) + .sorted(Comparator.comparingInt(pallet -> value(pallet.getSequenceNo()))) + .toList(); + if (candidates.isEmpty()) throw conflict("No assigned pallet exists for the carton destination warehouse"); + Map plannedByPallet = new HashMap<>(); + for (InboundPlanPalletAllocation allocation : repository.allocations(plan.getId())) { + plannedByPallet.merge(allocation.getPalletId(), value(allocation.getPlannedPackageCount()), Integer::sum); + } + InboundPlanPallet selected = candidates.stream() + .filter(pallet -> value(pallet.getActualPackageCount()) < plannedByPallet.getOrDefault(pallet.getId(), Integer.MAX_VALUE)) + .findFirst().orElse(null); + if (selected == null) throw conflict("All assigned pallets for the carton destination warehouse are full"); + return repository.lockPallet(plan.getId(), selected.getId()); + } + private InboundPlanLine requireLine(Long planId, Long id) { InboundPlanLine value = repository.line(planId, id); if (value == null) throw notFound("Inbound plan line not found"); @@ -801,6 +931,30 @@ public class InboundPdaWorkflowService { volume(allocation.getActualVolumeCbm()), remaining, requested, merge, candidates); } + private PdaResponses.Task taskResponse(InboundPdaTask task, InboundPlan plan, String planNo) { + int expected = repository.lines(plan.getId()).stream().mapToInt(line -> value(line.getRequiredPackageCount())).sum(); + List allocations = repository.allocations(plan.getId()); + int picked = allocations.stream().mapToInt(allocation -> value(allocation.getPickedPackageCount())).sum(); + LastlegOrder order = repository.order(plan.getSourceOrderId()); + String orderType = order == null ? null : order.getOrderType(); + boolean wholePalletInbound = "ONE_CLICK_DROPSHIP".equals(orderType); + Map palletsByNo = wholePalletInbound + ? repository.pallets(plan.getId()).stream().collect(java.util.stream.Collectors.toMap( + InboundPlanPallet::getPalletNo, value -> value)) : Map.of(); + Map allocationsByPallet = wholePalletInbound + ? allocations.stream().collect(java.util.stream.Collectors.toMap( + InboundPlanPalletAllocation::getPalletId, value -> value)) : Map.of(); + List dropshipPallets = wholePalletInbound + ? dropshipOrders.palletSnapshots(task.getCustomerId(), order.getId()).stream() + .map(pallet -> dropshipPallet(pallet, palletsByNo, allocationsByPallet)).toList() : List.of(); + PdaResponses.Task workInstructions = task(task, planNo); + return new PdaResponses.Task(task.getId(), task.getTaskNo(), planNo, task.getStatus(), + task.getPushedAt(), task.getStartedAt(), task.getCompletedAt(), unloadPhotoFileIds(task), + task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt(), expected, picked, + orderType, wholePalletInbound, dropshipPallets, mode(plan).name(), + workInstructions.pallets(), workInstructions.allocations(), workInstructions.expectedCartons()); + } + private PdaResponses.Task task(InboundPdaTask task, String planNo) { List planPallets = repository.pallets(task.getPlanId()); List planAllocations = repository.allocations(task.getPlanId()); @@ -830,8 +984,10 @@ public class InboundPdaWorkflowService { .toList(); return new PdaResponses.Task(task.getId(), task.getTaskNo(), planNo, task.getStatus(), task.getPushedAt(), task.getStartedAt(), task.getCompletedAt(), unloadPhotoFileIds(task), - task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt(), pallets, allocations, - expectedCartons); + task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt(), + lines.values().stream().mapToInt(line -> value(line.getRequiredPackageCount())).sum(), + planAllocations.stream().mapToInt(value -> InboundPdaWorkflowService.value(value.getPickedPackageCount())).sum(), + null, false, List.of(), null, pallets, allocations, expectedCartons); } private long recommendedPallet(InboundPlanCarton carton, List pallets) { @@ -842,6 +998,39 @@ public class InboundPdaWorkflowService { .map(InboundPlanPallet::getId).findFirst().orElse(0L); } + private PdaResponses.DropshipPallet dropshipPallet(DropshipPalletSnapshot pallet, + Map palletsByNo, + Map allocationsByPallet) { + InboundPlanPallet planPallet = palletsByNo.get(pallet.sourcePalletNo()); + if (planPallet == null) throw conflict("Dropship source pallet is missing from the inbound plan"); + InboundPlanPalletAllocation allocation = allocationsByPallet.get(planPallet.getId()); + if (allocation == null) throw conflict("Dropship source pallet has no inbound allocation"); + return new PdaResponses.DropshipPallet(pallet.sourcePalletNo(), planPallet.getId(), allocation.getId(), + pallet.packageCount(), pallet.skus().stream() + .map(sku -> new PdaResponses.DropshipSku(sku.skuCode(), sku.productName(), + sku.packageCount(), sku.productQuantity())).toList()); + } + + private void requireDropship(InboundPlan plan) { + if (!isDropship(plan)) { + throw conflict("Whole-pallet picking is only available for one-click dropship inbound plans"); + } + } + + private boolean isDropship(InboundPlan plan) { + LastlegOrder order = repository.order(plan.getSourceOrderId()); + return order != null && "ONE_CLICK_DROPSHIP".equals(order.getOrderType()); + } + + private InboundMode mode(InboundPlan plan) { + if (isDropship(plan)) return InboundMode.DROPSHIP; + List lines = repository.lines(plan.getId()); + return lines.stream().anyMatch(line -> line.getFbaWarehouseCode() != null + && !line.getFbaWarehouseCode().isBlank()) ? InboundMode.FBA : InboundMode.OWN_OVERSEAS; + } + + private enum InboundMode { DROPSHIP, OWN_OVERSEAS, FBA } + private List unloadPhotoFileIds(InboundPdaTask task) { if (task.getUnloadPhotoFileIds() == null || task.getUnloadPhotoFileIds().isBlank()) return List.of(); try { @@ -945,6 +1134,7 @@ public class InboundPdaWorkflowService { List cartons) { } private static int value(Integer value) { return value == null ? 0 : value; } + private static boolean terminal(String status) { return "COMPLETED".equals(status) || "PARTIAL_INBOUND".equals(status); } private static BigDecimal volume(BigDecimal value) { return value == null ? ZERO_VOLUME : value; } private static BigDecimal nonNegative(BigDecimal value) { return value.signum() < 0 ? ZERO_VOLUME : value.setScale(4, RoundingMode.HALF_UP); } private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptService.java index 58ee791f0..0a7b27372 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptService.java @@ -20,6 +20,7 @@ import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; import com.baiyue.lastleg.dropship.service.DropshipOrderService; +import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot; import jakarta.annotation.Resource; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -49,6 +50,13 @@ public class InboundStockReceiptService { public void stockIn(String customerId, InboundPlan plan, InboundPdaTask task, List lines, List allocations, List pallets, LocalDateTime receivedAt) { + stockIn(customerId, plan, task, lines, allocations, pallets, receivedAt, false); + } + + @Transactional + public void stockIn(String customerId, InboundPlan plan, InboundPdaTask task, + List lines, List allocations, + List pallets, LocalDateTime receivedAt, boolean allowPartial) { Map orderById = orders.selectBatchIds(lines.stream() .map(InboundPlanLine::getOrderId).collect(Collectors.toSet())).stream() .filter(order -> customerId.equals(order.getCustomerId())) @@ -70,8 +78,8 @@ public class InboundStockReceiptService { Set receivedAllocationIds = existing.stream().map(InboundStockReceipt::getAllocationId) .filter(Objects::nonNull).collect(Collectors.toSet()); if (receivedAllocationIds.equals(expectedAllocationIds)) { - activateDropship(customerId, task.getId(), orderById, existing); - completeOrders(customerId, plan, orderById, receivedAt); + if (!allowPartial) activateDropship(customerId, task.getId(), orderById, existing); + completeOrders(customerId, plan, orderById, receivedAt, allowPartial); return; } throw conflict("PDA stock-in receipts are incomplete; repair the task receipts before retrying"); @@ -85,7 +93,9 @@ public class InboundStockReceiptService { .getOrDefault(line.getId(), List.of()); long pickedPackages = lineAllocations.stream() .mapToLong(allocation -> value(allocation.getPickedPackageCount())).sum(); - if (pickedPackages != value(line.getRequiredPackageCount())) { + if (pickedPackages == 0 && allowPartial) continue; + if ((!allowPartial && pickedPackages != value(line.getRequiredPackageCount())) + || (allowPartial && pickedPackages > value(line.getRequiredPackageCount()))) { throw conflict("PDA picked packages must equal the inbound order package total"); } List exactSkuReceipts = skuReceiptsByLine.getOrDefault(line.getId(), List.of()); @@ -95,12 +105,25 @@ public class InboundStockReceiptService { continue; } List skuRemainders = "ONE_CLICK_DROPSHIP".equals(order.getOrderType()) - ? List.of() : skuRemainders(order.getId(), pickedPackages); + ? List.of() : skuRemainders(order.getId(), value(line.getRequiredPackageCount())); + Map dropshipPallets = "ONE_CLICK_DROPSHIP".equals(order.getOrderType()) + ? dropshipOrders.palletSnapshots(customerId, order.getId()).stream() + .collect(Collectors.toMap(DropshipPalletSnapshot::sourcePalletNo, value -> value)) + : Map.of(); for (InboundPlanPalletAllocation allocation : lineAllocations) { InboundPlanPallet pallet = requirePallet(palletById, allocation); if ("ONE_CLICK_DROPSHIP".equals(order.getOrderType())) { - created.add(persist(customerId, plan, task, line, allocation, pallet, order, - SkuSnapshot.unspecified(value(allocation.getPickedPackageCount())), receivedAt)); + if (value(allocation.getPickedPackageCount()) != value(allocation.getPlannedPackageCount())) { + throw conflict("One-click dropship inbound must receive a complete source pallet"); + } + DropshipPalletSnapshot sourcePallet = dropshipPallets.get(pallet.getPalletNo()); + if (sourcePallet == null || sourcePallet.packageCount() != value(allocation.getPickedPackageCount())) { + throw conflict("One-click dropship source pallet SKU package total does not match PDA receipt"); + } + for (DropshipPalletSnapshot.Sku sku : sourcePallet.skus()) { + created.add(persist(customerId, plan, task, line, allocation, pallet, order, + new SkuSnapshot(sku.skuCode(), sku.productName(), null, sku.packageCount()), receivedAt)); + } } else { long allocationPackages = value(allocation.getPickedPackageCount()); for (SkuSnapshot sku : consume(skuRemainders, allocationPackages)) { @@ -108,12 +131,12 @@ public class InboundStockReceiptService { } } } - if (skuRemainders.stream().anyMatch(value -> value.remaining > 0)) { + if (!allowPartial && skuRemainders.stream().anyMatch(value -> value.remaining > 0)) { throw conflict("Inbound SKU package snapshots were not fully received"); } } - activateDropship(customerId, task.getId(), orderById, created); - completeOrders(customerId, plan, orderById, receivedAt); + if (!allowPartial) activateDropship(customerId, task.getId(), orderById, created); + completeOrders(customerId, plan, orderById, receivedAt, allowPartial); } private List persistExactSkuReceipts(String customerId, InboundPlan plan, @@ -159,14 +182,16 @@ public class InboundStockReceiptService { } private void completeOrders(String customerId, InboundPlan plan, Map orderById, - LocalDateTime completedAt) { + LocalDateTime completedAt, boolean partial) { + String subOrderStatus = partial ? "INBOUND_PLAN:PARTIAL_INBOUND" : "INBOUND_PLAN:COMPLETED"; for (LastlegOrder snapshot : orderById.values()) { LastlegOrder order = orders.lockById(customerId, snapshot.getId()); if (order == null) throw conflict("Inbound tail-leg order disappeared while completing stock-in"); - if ("INBOUND_COMPLETED".equals(order.getCurrentStatus())) continue; + if ("INBOUND_COMPLETED".equals(order.getCurrentStatus()) + && subOrderStatus.equals(order.getSubOrderStatus())) continue; String before = order.getCurrentStatus(); order.setCurrentStatus("INBOUND_COMPLETED"); - order.setSubOrderStatus("INBOUND_PLAN:COMPLETED"); + order.setSubOrderStatus(subOrderStatus); order.setUpdatedAt(completedAt); orders.updateById(order); LastlegOrderStatusHistory history = new LastlegOrderStatusHistory(); @@ -174,8 +199,11 @@ public class InboundStockReceiptService { history.setOrderId(order.getId()); history.setFromStatus(before); history.setToStatus("INBOUND_COMPLETED"); - history.setEventId("PDA_INBOUND_COMPLETE:" + plan.getPlanNo()); - history.setRemark("PDA inbound completed and inventory stocked in"); + history.setEventId((partial ? "PDA_PARTIAL_INBOUND_COMPLETE:" : "PDA_INBOUND_COMPLETE:") + + plan.getPlanNo()); + history.setRemark(partial + ? "PDA inbound partially completed and actual inventory stocked in" + : "PDA inbound completed and inventory stocked in"); history.setCreatedAt(completedAt); orderHistories.insert(history); } @@ -207,15 +235,17 @@ public class InboundStockReceiptService { if (overseasWarehouseId.equals(trim(order.getSourceWarehouseId()))) { throw conflict("Actual overseas warehouse must not use the domestic source warehouse"); } - InventoryCommandResponse result = inventory.stockIn(customerId, task.getOperatorId(), - new InventoryCommandRequests.StockIn(null, customerName, overseasWarehouseId, - null, overseasWarehouseName, line.getDestinationWarehouseId(), destinationCode, + InventoryCommandRequests.StockIn stockIn = new InventoryCommandRequests.StockIn(null, customerName, + overseasWarehouseId, null, overseasWarehouseName, line.getDestinationWarehouseId(), destinationCode, line.getDestinationWarehouseName(), order.getId(), plan.getPlanNo(), line.getInboundCode(), sku.skuCode(), sku.productNameZh(), sku.productNameEn(), pallet.getWarehousePalletId(), pallet.getPalletNo(), plan.getPlanNo(), pallet.getWarehouseLocationId(), null, packages, order.getOrderNo(), order.getSourceEventId(), operationKey, - "PDA inbound completion for " + line.getInboundCode())); + "PDA inbound completion for " + line.getInboundCode()); + InventoryCommandResponse result = Boolean.TRUE.equals(order.getDetained()) + ? inventory.stockInHeld(customerId, task.getOperatorId(), stockIn) + : inventory.stockIn(customerId, task.getOperatorId(), stockIn); InboundStockReceipt receipt = new InboundStockReceipt(); receipt.setId(IdWorker.getId()); @@ -334,9 +364,5 @@ public class InboundStockReceiptService { } } - private record SkuSnapshot(String skuCode, String productNameZh, String productNameEn, long packages) { - private static SkuSnapshot unspecified(long packages) { - return new SkuSnapshot("UNSPECIFIED", null, null, packages); - } - } + private record SkuSnapshot(String skuCode, String productNameZh, String productNameEn, long packages) {} } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundUnloadCompletedOutboxRelay.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundUnloadCompletedOutboxRelay.java index e9a5e50c0..b7988f67d 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundUnloadCompletedOutboxRelay.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/InboundUnloadCompletedOutboxRelay.java @@ -15,7 +15,7 @@ import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.UUID; -/** 至少一次投递海外卸柜完成事件;国内事件桥按 eventId 幂等接收。 */ +/** 至少一次投递海外到国内业务事件;国内事件桥按 eventId 幂等接收。 */ @Component @Slf4j public class InboundUnloadCompletedOutboxRelay { @@ -44,7 +44,7 @@ public class InboundUnloadCompletedOutboxRelay { } catch (Exception exception) { repository.retry(row.getId(), workerId, LocalDateTime.now().plusSeconds(Math.max(retrySeconds, 1)), exception.getMessage()); - log.warn("海外卸柜完成事件投递失败: eventKey={}, reason={}", row.getEventKey(), exception.getMessage()); + log.warn("海外到国内事件投递失败: eventKey={}, reason={}", row.getEventKey(), exception.getMessage()); } } } @@ -53,7 +53,7 @@ public class InboundUnloadCompletedOutboxRelay { com.baiyue.common.core.api.ApiResponse response) { if (response == null) throw new IllegalStateException("国内事件桥返回空响应"); if (!response.successful()) { - throw new IllegalStateException("国内事件桥拒绝海外卸柜完成事件: code=" + response.code() + throw new IllegalStateException("国内事件桥拒绝海外事件: code=" + response.code() + ", message=" + response.message() + ", requestId=" + response.requestId()); } if (response.data() == null) throw new IllegalStateException("国内事件桥成功响应缺少确认数据"); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerService.java index 154db1285..79069434c 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerService.java @@ -61,6 +61,7 @@ import java.util.stream.Collectors; public class ReceivingContainerService { private static final String PHOTO_DOWNLOAD_PATH = "/api/pda/v1/last-leg/receiving-containers/current/unload-photos/files/{fileId}/download"; + private static final String PALLET_MERGE_HINT = "该入仓单已标记合托,请按合托要求拣货"; @Resource private InboundPlanMapper plans; @Resource private InboundPlanLineMapper lines; @@ -258,7 +259,8 @@ public class ReceivingContainerService { inbound.inboundCode(), inbound.destinationType(), inbound.overseasWarehouseId(), inbound.overseasWarehouseName(), inbound.destinationWarehouseId(), inbound.destinationWarehouseCode(), inbound.destinationWarehouseName(), inbound.fbaWarehouseCode(), - inbound.plannedPalletCount(), inbound.totalPackages(), inbound.receivedPackages(), inbound.pendingPackages()); + inbound.plannedPalletCount(), inbound.totalPackages(), inbound.receivedPackages(), inbound.pendingPackages(), + inbound.palletMergeRequired(), inbound.palletMergeHint()); } public ReceivingContainerResponses.PalletResolved resolvePallet(String operatorKey, @@ -309,7 +311,8 @@ public class ReceivingContainerService { int total = fbaPlanRefs.stream().mapToInt(refValue -> value(refValue.line().getRequiredPackageCount())).sum(); ReceivingContainerResponses.Receipt result = new ReceivingContainerResponses.Receipt("RECEIVED", session.getContainerNo(), ref.line().getInboundCode(), "FBA", resource.palletNo(), accepted, - received, Math.max(0, total - received), session.getStatus()); + received, Math.max(0, total - received), session.getStatus(), palletMergeRequired(ref.order()), + palletMergeHint(ref.order())); save(session, "BULK_RECEIVE", request.idempotencyKey(), request, result); return result; } catch (RuntimeException failure) { @@ -347,7 +350,8 @@ public class ReceivingContainerService { ReceivingContainerResponses.Inbound inbound = inbound(ref); ReceivingContainerResponses.Receipt result = new ReceivingContainerResponses.Receipt("RECEIVED", session.getContainerNo(), inbound.inboundCode(), inbound.destinationType(), resource.palletNo(), 1, - inbound.receivedPackages(), inbound.pendingPackages(), session.getStatus()); + inbound.receivedPackages(), inbound.pendingPackages(), session.getStatus(), + inbound.palletMergeRequired(), inbound.palletMergeHint()); save(session, "CARTON_RECEIVE", request.idempotencyKey(), request, result); return result; } catch (RuntimeException failure) { @@ -666,7 +670,17 @@ public class ReceivingContainerService { : String.valueOf(ref.plan().getOverseasWarehouseId()), ref.order().getOverseasWarehouseName(), ref.line().getDestinationWarehouseId(), ref.line().getDestinationWarehouseCode(), ref.line().getDestinationWarehouseName(), ref.line().getFbaWarehouseCode(), - value(ref.line().getPlannedPalletCount()), total, received, Math.max(0, total - received)); + value(ref.line().getPlannedPalletCount()), total, received, Math.max(0, total - received), + palletMergeRequired(ref.order()), palletMergeHint(ref.order())); + } + + private boolean palletMergeRequired(LastlegOrder order) { + return order != null && ("REQUIRED".equals(order.getPalletMergeFlag()) + || "MERGED".equals(order.getPalletMergeFlag())); + } + + private String palletMergeHint(LastlegOrder order) { + return palletMergeRequired(order) ? PALLET_MERGE_HINT : null; } private Totals totals(List containerPlans) { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundPlanService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundPlanService.java index 64dfab200..513c59353 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundPlanService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundPlanService.java @@ -7,6 +7,7 @@ import com.baiyue.lastleg.inboundplan.dto.InboundPlanListResponse; import com.baiyue.lastleg.inboundplan.dto.InboundPlanRequests; import com.baiyue.lastleg.inboundplan.dto.InboundPlanResponse; import com.baiyue.lastleg.inboundplan.dto.SupplementPalletRequest; +import com.baiyue.lastleg.inboundplan.dto.UpdateInboundPlanRequest; import com.baiyue.lastleg.inboundplan.entity.InboundPlan; import com.baiyue.lastleg.inboundplan.entity.InboundPlanLine; import com.baiyue.lastleg.inboundplan.entity.InboundPlanPallet; @@ -33,10 +34,12 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.ArrayList; +import java.util.HashSet; import java.util.stream.Collectors; @Service public class InboundPlanService { + @Resource private com.baiyue.lastleg.service.LastlegOrderNoGenerator orderNoGenerator; @Resource private InboundPlanMapper planMapper; @Resource private InboundPlanLineMapper lineMapper; @Resource private InboundPlanPalletMapper palletMapper; @@ -61,6 +64,37 @@ public class InboundPlanService { @Transactional public InboundPlanResponse convert(String customerId, Long selectedOrderId) { + return convert(customerId, selectedOrderId, List.of()); + } + + @Transactional + public InboundPlanResponse convert(String customerId, Long selectedOrderId, List sourcePalletNos) { + return convert(customerId, selectedOrderId, sourcePalletNos, null); + } + + @Transactional + public InboundPlanResponse convertDropship(String customerId, Long selectedOrderId, + List sourcePallets) { + if (sourcePallets == null || sourcePallets.isEmpty()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "One-click dropship inbound requires source pallets"); + } + HashSet palletNos = new HashSet<>(); + for (WholePalletInput pallet : sourcePallets) { + if (pallet == null || trimToNull(pallet.palletNo()) == null || pallet.packageCount() <= 0) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "One-click dropship source pallet number and package count are required"); + } + if (!palletNos.add(pallet.palletNo().trim())) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "Duplicate one-click dropship source pallet number: " + pallet.palletNo()); + } + } + return convert(customerId, selectedOrderId, List.of(), List.copyOf(sourcePallets)); + } + + private InboundPlanResponse convert(String customerId, Long selectedOrderId, List sourcePalletNos, + List wholePallets) { if (customerId == null || customerId.isBlank() || "0".equals(customerId.trim())) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "平台生成入库计划必须指定客户"); } @@ -75,16 +109,17 @@ public class InboundPlanService { return detail(customerId, existing); } validateOrders(orders); - return createPlan(customerId, selectedOrderId, orders); + return createPlan(customerId, selectedOrderId, orders, sourcePalletNos, wholePallets); } private InboundPlanResponse createPlan(String customerId, Long selectedOrderId, - List orders) { + List orders, List sourcePalletNos, + List wholePallets) { String containerNo = requiredContainerNo(orders.get(0).containerNo()); LocalDateTime now = LocalDateTime.now(); InboundPlan plan = new InboundPlan(); plan.setId(IdWorker.getId()); - plan.setPlanNo("LIP" + plan.getId()); + plan.setPlanNo(orderNoGenerator.next("INBOUND_PLAN", orders.get(0).destinationWarehouseCode(), plan.getId())); plan.setCustomerId(customerId); plan.setSourceOrderId(selectedOrderId); plan.setContainerNo(containerNo); @@ -98,20 +133,27 @@ public class InboundPlanService { Map orderById = orders.stream() .collect(Collectors.toMap(SourceOrderSnapshot::orderId, value -> value)); Map lineByOrder = createLines(plan, orders); - PalletAllocator.Result allocation = PalletAllocator.allocate(orders.stream() + PalletAllocator.Result allocation = wholePallets == null ? PalletAllocator.allocate(orders.stream() .map(order -> new PalletAllocator.LineInput(order.orderId(), warehouseKey(order), volume(order), - order.packageCount())) - .toList()); - plan.setPalletCount(allocation.pallets().size()); + order.packageCount(), order.recommendedPalletCount())).toList()) : null; + if (wholePallets != null) validateWholePallets(orders, wholePallets); + plan.setPalletCount(wholePallets == null ? allocation.pallets().size() : wholePallets.size()); planMapper.insert(plan); lineByOrder.values().forEach(lineMapper::insert); - Map persistedPallets = createPallets(plan, allocation, orderById); - createAllocations(plan, allocation, lineByOrder, persistedPallets); - - Map palletCounts = allocation.allocations().stream().collect(Collectors.groupingBy( - PalletAllocator.AllocationDraft::orderId, - Collectors.collectingAndThen(Collectors.mapping(item -> item.pallet().sequenceNo(), Collectors.toSet()), java.util.Set::size))); + Map palletCounts; + if (wholePallets == null) { + Map persistedPallets = createPallets(plan, allocation, + orderById, sourcePalletNos == null ? List.of() : sourcePalletNos); + createAllocations(plan, allocation, lineByOrder, persistedPallets); + palletCounts = allocation.allocations().stream().collect(Collectors.groupingBy( + PalletAllocator.AllocationDraft::orderId, Collectors.collectingAndThen(Collectors.mapping( + item -> item.pallet().sequenceNo(), Collectors.toSet()), java.util.Set::size))); + } else { + SourceOrderSnapshot order = orders.get(0); + createWholePallets(plan, order, lineByOrder.get(order.orderId()), wholePallets); + palletCounts = Map.of(order.orderId(), wholePallets.size()); + } for (SourceOrderSnapshot order : orders) { int count = palletCounts.getOrDefault(order.orderId(), 0); InboundPlanLine line = lineByOrder.get(order.orderId()); @@ -180,7 +222,7 @@ public class InboundPlanService { List created = new ArrayList<>(refs.size()); for (int index = 0; index < refs.size(); index++) { InboundPlanOrderMapper.ContainerOrderRef ref = refs.get(index); - created.add(createPlan(ref.customerId(), ref.orderId(), snapshots.get(index))); + created.add(createPlan(ref.customerId(), ref.orderId(), snapshots.get(index), List.of(), null)); } return List.copyOf(created); } @@ -201,6 +243,53 @@ public class InboundPlanService { } } + private void validateWholePallets(List orders, List pallets) { + if (orders.size() != 1) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "One-click dropship inbound plan must belong to one inbound order"); + } + int packages = pallets.stream().mapToInt(WholePalletInput::packageCount).sum(); + if (packages != orders.get(0).packageCount()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + "One-click dropship pallet package total must equal inbound order package count"); + } + } + + private void createWholePallets(InboundPlan plan, SourceOrderSnapshot order, InboundPlanLine line, + List sourcePallets) { + BigDecimal remainingVolume = volume(order); + int remainingPackages = order.packageCount(); + for (int index = 0; index < sourcePallets.size(); index++) { + WholePalletInput source = sourcePallets.get(index); + BigDecimal palletVolume = index == sourcePallets.size() - 1 ? remainingVolume + : volume(order).multiply(BigDecimal.valueOf(source.packageCount())) + .divide(BigDecimal.valueOf(order.packageCount()), 4, java.math.RoundingMode.HALF_UP); + if (palletVolume.compareTo(remainingVolume) > 0) palletVolume = remainingVolume; + InboundPlanPallet pallet = new InboundPlanPallet(); + pallet.setId(IdWorker.getId()); pallet.setCustomerId(plan.getCustomerId()); pallet.setPlanId(plan.getId()); + pallet.setPalletNo(source.palletNo().trim()); pallet.setDestinationWarehouseId(order.destinationWarehouseId()); + pallet.setDestinationWarehouseCode(order.destinationWarehouseCode()); + pallet.setDestinationWarehouseName(order.destinationWarehouseName()); + pallet.setCapacityCbm(PalletAllocator.PALLET_CAPACITY_CBM); pallet.setAllocatedVolumeCbm(palletVolume); + pallet.setSequenceNo(index + 1); pallet.setAssignmentStatus("UNASSIGNED"); + pallet.setActualVolumeCbm(BigDecimal.ZERO); pallet.setActualPackageCount(0); palletMapper.insert(pallet); + + InboundPlanPalletAllocation allocation = new InboundPlanPalletAllocation(); + allocation.setId(IdWorker.getId()); allocation.setCustomerId(plan.getCustomerId()); allocation.setPlanId(plan.getId()); + allocation.setLineId(line.getId()); allocation.setPalletId(pallet.getId()); + allocation.setAllocatedVolumeCbm(palletVolume); allocation.setPlannedPackageCount(source.packageCount()); + allocation.setCountedPackageCount(0); allocation.setPickedPackageCount(0); + allocation.setActualVolumeCbm(BigDecimal.ZERO); allocation.setStatus("PENDING_COUNT"); allocation.setVersion(0); + allocationMapper.insert(allocation); + remainingVolume = remainingVolume.subtract(palletVolume); + remainingPackages -= source.packageCount(); + } + if (remainingPackages != 0 || remainingVolume.signum() != 0) { + throw new IllegalStateException("Failed to conserve one-click dropship pallet totals"); + } + } + + public record WholePalletInput(String palletNo, int packageCount) {} @Transactional(readOnly = true) public InboundPlanResponse detailByPlanNo(String customerId, String planNo) { @@ -214,6 +303,44 @@ public class InboundPlanService { return detail(customerId, plan); } + @Transactional + public InboundPlanResponse update(String customerId, String planNo, UpdateInboundPlanRequest request) { + InboundPlan plan = planMapper.selectOne(new LambdaQueryWrapper() + .eq(InboundPlan::getPlanNo, planNo).eq(customerId != null, InboundPlan::getCustomerId, customerId).last("FOR UPDATE")); + if (plan == null) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Inbound plan not found"); + if (!java.util.Set.of("PENDING_ASSIGNMENT", "ASSIGNED").contains(plan.getStatus())) + throw new ResponseStatusException(HttpStatus.CONFLICT, "Inbound plan has started and cannot be edited"); + int currentVersion = plan.getVersion() == null ? 0 : plan.getVersion(); + if (request.version() != currentVersion) + throw new ResponseStatusException(HttpStatus.CONFLICT, "Inbound plan version is stale; reload before editing"); + plan.setOverseasWarehouseId(request.overseasWarehouseId()); + plan.setUpdatedAt(LocalDateTime.now()); + planMapper.updateById(plan); + // Keep the denormalized destination snapshot used by list queries in sync with the plan. + List lines = lineMapper.selectList(new LambdaQueryWrapper() + .eq(InboundPlanLine::getPlanId, plan.getId()) + .eq(InboundPlanLine::getCustomerId, plan.getCustomerId())); + for (InboundPlanLine line : lines) { + line.setDestinationWarehouseId(String.valueOf(request.overseasWarehouseId())); + if (trimToNull(request.warehouseCode()) != null) line.setDestinationWarehouseCode(request.warehouseCode().trim()); + if (trimToNull(request.warehouseName()) != null) line.setDestinationWarehouseName(request.warehouseName().trim()); + lineMapper.updateById(line); + } + // Pallets carry the same destination snapshot and are consumed by PDA/resource assignment. + // Update them in this transaction so a plan edit cannot leave lines and pallets pointing at + // different warehouses. + List pallets = palletMapper.selectList(new LambdaQueryWrapper() + .eq(InboundPlanPallet::getPlanId, plan.getId()) + .eq(InboundPlanPallet::getCustomerId, plan.getCustomerId())); + for (InboundPlanPallet pallet : pallets) { + pallet.setDestinationWarehouseId(String.valueOf(request.overseasWarehouseId())); + if (trimToNull(request.warehouseCode()) != null) pallet.setDestinationWarehouseCode(request.warehouseCode().trim()); + if (trimToNull(request.warehouseName()) != null) pallet.setDestinationWarehouseName(request.warehouseName().trim()); + palletMapper.updateById(pallet); + } + return detail(customerId, plan); + } + @Transactional public InboundPlanResponse.Pallet supplementPallet(String customerId, String planNo, SupplementPalletRequest request) { InboundPlan plan = planMapper.selectOne(new LambdaQueryWrapper() @@ -265,7 +392,8 @@ public class InboundPlanService { } private Map createPallets( - InboundPlan plan, PalletAllocator.Result allocation, Map orderById) { + InboundPlan plan, PalletAllocator.Result allocation, Map orderById, + List sourcePalletNos) { Map warehouseSamples = orderById.values().stream() .collect(Collectors.toMap(this::warehouseKey, value -> value, (left, right) -> left)); Map result = new HashMap<>(); @@ -275,7 +403,10 @@ public class InboundPlanService { pallet.setId(IdWorker.getId()); pallet.setCustomerId(plan.getCustomerId()); pallet.setPlanId(plan.getId()); - pallet.setPalletNo(plan.getPlanNo() + "-P" + String.format("%03d", result.size() + 1)); + int palletIndex = result.size(); + String sourcePalletNo = palletIndex < sourcePalletNos.size() ? sourcePalletNos.get(palletIndex) : null; + pallet.setPalletNo(sourcePalletNo == null || sourcePalletNo.isBlank() + ? plan.getPlanNo() + "-P" + String.format("%03d", palletIndex + 1) : sourcePalletNo.trim()); pallet.setDestinationWarehouseId(sample.destinationWarehouseId()); pallet.setDestinationWarehouseCode(sample.destinationWarehouseCode()); pallet.setDestinationWarehouseName(sample.destinationWarehouseName()); @@ -319,7 +450,7 @@ public class InboundPlanService { throw new ResponseStatusException(HttpStatus.CONFLICT, "Inbound code " + order.inboundCode() + " is not ready for an inbound plan"); } - if (order.destinationCount() != 1 || order.destinationMismatchCount() != 0 || warehouseKey(order) == null) { + if (order.destinationCount() != 1 || warehouseKey(order) == null) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Inbound code " + order.inboundCode() + " must have one destination warehouse consistent with all cargo details"); @@ -365,7 +496,8 @@ public class InboundPlanService { pallet.getActualPackageCount())).toList(), allocations.stream().map(item -> new InboundPlanResponse.Allocation(item.getLineId(), item.getPalletId(), item.getAllocatedVolumeCbm(), item.getPlannedPackageCount(), item.getCountedPackageCount(), - item.getPickedPackageCount(), item.getActualVolumeCbm(), item.getStatus(), item.getVersion())).toList()); + item.getPickedPackageCount(), item.getActualVolumeCbm(), item.getStatus(), item.getVersion())).toList(), + plan.getVersion() == null ? 0 : plan.getVersion()); } private BigDecimal volume(SourceOrderSnapshot order) { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/PalletAllocator.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/PalletAllocator.java index 74f6f0af9..0726c1d08 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/PalletAllocator.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/PalletAllocator.java @@ -34,6 +34,23 @@ public final class PalletAllocator { PalletDraft current = null; int sequence = 0; for (LineInput line : group) { + if (line.recommendedPalletCount() > 0) { + int minimumByVolume = line.volumeCbm().divide(PALLET_CAPACITY_CBM, 0, RoundingMode.CEILING).intValue(); + int count = Math.max(line.recommendedPalletCount(), minimumByVolume); + BigDecimal remaining = line.volumeCbm(); + for (int index = 0; index < count; index++) { + PalletDraft recommended = new PalletDraft(line.warehouseKey(), ++sequence, BigDecimal.ZERO); + pallets.add(recommended); + int remainingPallets = count - index; + BigDecimal allocated = remainingPallets == 1 ? remaining + : remaining.divide(BigDecimal.valueOf(remainingPallets), 4, RoundingMode.HALF_UP); + recommended.add(allocated); + allocations.add(new AllocationDraft(line.orderId(), recommended, allocated, 0)); + remaining = remaining.subtract(allocated); + } + current = null; + continue; + } BigDecimal remaining = line.volumeCbm(); while (remaining.signum() > 0) { if (current == null || current.remainingCapacity().signum() == 0) { @@ -85,7 +102,11 @@ public final class PalletAllocator { return List.copyOf(result); } - public record LineInput(Long orderId, String warehouseKey, BigDecimal volumeCbm, int packageCount) { + public record LineInput(Long orderId, String warehouseKey, BigDecimal volumeCbm, int packageCount, + int recommendedPalletCount) { + public LineInput(Long orderId, String warehouseKey, BigDecimal volumeCbm, int packageCount) { + this(orderId, warehouseKey, volumeCbm, packageCount, 0); + } } public static final class PalletDraft { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/mapper/InventoryCommandMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/mapper/InventoryCommandMapper.java index 0fd568123..343491524 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/mapper/InventoryCommandMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/mapper/InventoryCommandMapper.java @@ -6,6 +6,8 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.List; + @Mapper public interface InventoryCommandMapper { @Select("SELECT * FROM ll_inventory_position WHERE id=#{positionId} LIMIT 1") @@ -22,4 +24,17 @@ public interface InventoryCommandMapper { @Select("SELECT COALESCE(SUM(available_packages+hold_packages+allocated_packages+picked_packages+receiving_packages),0) " + "FROM ll_inventory_position WHERE customer_id=#{customerId} AND pallet_id=#{palletId}") long palletPackages(@Param("customerId") String customerId, @Param("palletId") Long palletId); + + @Select("SELECT * FROM ll_inventory_balance WHERE customer_id=#{customerId} " + + "AND inbound_order_id=#{inboundOrderId} AND inbound_code=#{inboundCode} " + + "AND in_transit_packages>0 ORDER BY id FOR UPDATE") + List lockInboundTransitBalances(@Param("customerId") String customerId, + @Param("inboundOrderId") Long inboundOrderId, + @Param("inboundCode") String inboundCode); + + @Select("SELECT p.* FROM ll_inventory_position p JOIN ll_inventory_balance b ON b.id=p.balance_id " + + "WHERE p.customer_id=#{customerId} AND b.inbound_order_id=#{inboundOrderId} " + + "AND p.hold_packages>0 ORDER BY p.id FOR UPDATE") + List lockInboundHeldPositions(@Param("customerId") String customerId, + @Param("inboundOrderId") Long inboundOrderId); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/service/InventoryCommandService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/service/InventoryCommandService.java index 21b72b25a..860677428 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/service/InventoryCommandService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/command/service/InventoryCommandService.java @@ -27,6 +27,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.util.HexFormat; +import java.util.List; @Service public class InventoryCommandService { @@ -59,23 +60,38 @@ public class InventoryCommandService { @Transactional public InventoryCommandResponse stockIn(String customerId, String operatorId, InventoryCommandRequests.StockIn request) { + return stockIn(customerId, operatorId, request, false); + } + + @Transactional + public InventoryCommandResponse stockInHeld(String customerId, String operatorId, + InventoryCommandRequests.StockIn request) { + return stockIn(customerId, operatorId, request, true); + } + + private InventoryCommandResponse stockIn(String customerId, String operatorId, + InventoryCommandRequests.StockIn request, boolean held) { return execute(customerId, operatorId, "STOCK_IN", request.idempotencyKey(), request, operation -> { + List transitBalances = commands.lockInboundTransitBalances(customerId, + request.inboundOrderId(), request.inboundCode()); InventoryBalance balance = balance(customerId, request); InventoryPosition position = position(customerId, balance, request); - long before = value(position.getAvailablePackages()); - position.setAvailablePackages(before + request.packages()); + long before = held ? value(position.getHoldPackages()) : value(position.getAvailablePackages()); + if (held) position.setHoldPackages(before + request.packages()); + else position.setAvailablePackages(before + request.packages()); position.setStatus("OCCUPIED"); position.setVersion(value(position.getVersion()) + 1); position.setUpdatedAt(LocalDateTime.now()); positions.updateById(position); - long transferred = Math.min(value(balance.getInTransitPackages()), request.packages()); - balance.setInTransitPackages(value(balance.getInTransitPackages()) - transferred); - balance.setAvailablePackages(value(balance.getAvailablePackages()) + request.packages()); + long transferred = transferInTransit(customerId, operatorId, operation.getId(), balance, + transitBalances, request); + if (held) balance.setHoldPackages(value(balance.getHoldPackages()) + request.packages()); + else balance.setAvailablePackages(value(balance.getAvailablePackages()) + request.packages()); touch(balance); balances.updateById(balance); ledger(customerId, operatorId, operation.getId(), balance, position, sku(request.skuCode()), "STOCK_IN", - transferred == request.packages() ? "IN_TRANSIT" : "OUTSIDE", "AVAILABLE", + transferred == request.packages() ? "IN_TRANSIT" : "OUTSIDE", held ? "HOLD" : "AVAILABLE", before, request.packages(), before + request.packages(), request.businessOrderNo(), request.inboundOrderNo(), null, request.sourceEventId(), request.idempotencyKey(), request.remark()); @@ -83,6 +99,44 @@ public class InventoryCommandService { }); } + @Transactional + public void releaseInboundOrderHold(String customerId, String operatorId, long inboundOrderId, + String businessOrderNo, String remark) { + for (InventoryPosition position : commands.lockInboundHeldPositions(customerId, inboundOrderId)) { + long packages = value(position.getHoldPackages()); + if (packages == 0) continue; + release(customerId, operatorId, new InventoryCommandRequests.Move(position.getId(), packages, + businessOrderNo, null, null, "ORDER_HOLD_RELEASE:" + inboundOrderId + ":" + position.getId(), remark)); + } + } + + private long transferInTransit(String customerId, String operatorId, Long operationId, + InventoryBalance target, List sources, + InventoryCommandRequests.StockIn request) { + long pending = request.packages(); + long transferred = 0; + for (InventoryBalance source : sources) { + if (pending == 0) break; + boolean targetDimension = source.getId().equals(target.getId()); + long sourceBefore = value(targetDimension ? target.getInTransitPackages() : source.getInTransitPackages()); + long moved = Math.min(sourceBefore, pending); + if (moved == 0) continue; + if (targetDimension) target.setInTransitPackages(sourceBefore - moved); + else source.setInTransitPackages(sourceBefore - moved); + transferred += moved; + pending -= moved; + if (targetDimension) continue; + touch(source); + balances.updateById(source); + ledger(customerId, operatorId, operationId, source, null, sku(request.skuCode()), + "STOCK_IN_TRANSIT_RELEASE", "IN_TRANSIT", "STOCK_IN", sourceBefore, -moved, + sourceBefore - moved, request.businessOrderNo(), request.inboundOrderNo(), null, + request.sourceEventId(), request.idempotencyKey() + ":TRANSIT:" + source.getId(), + "In-transit inventory transferred to overseas warehouse " + target.getOverseasWarehouseId()); + } + return transferred; + } + @Transactional public InventoryCommandResponse hold(String customerId, String operatorId, InventoryCommandRequests.Move request) { return move(customerId, operatorId, "HOLD", Bucket.AVAILABLE, Bucket.HOLD, request); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/controller/InventoryQueryController.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/controller/InventoryQueryController.java index 87226951e..efb7abbbe 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/controller/InventoryQueryController.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/controller/InventoryQueryController.java @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; import org.springframework.web.server.ResponseStatusException; import java.nio.charset.StandardCharsets; @@ -80,28 +81,30 @@ public class InventoryQueryController { @GetMapping(value = BASE_PATH + "/balances/export", produces = "text/csv") @PreAuthorize("hasAuthority('" + OverseasPermissions.INVENTORY_EXPORT + "')") @Operation(summary = "Export filtered inbound-order inventory as UTF-8 CSV", description = "Required authority: " + OverseasPermissions.INVENTORY_EXPORT) - public ResponseEntity exportBalances( + public ResponseEntity exportBalances( @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, @Valid InventoryQueryRequests.BalanceQuery query) { - return csv(service.exportBalances(platformScope(user, customerId), query), "inventory-balances.csv"); + String scope = platformScope(user, customerId); + return csv(output -> service.writeBalancesCsv(scope, query, output), "inventory-balances.csv"); } @GetMapping(value = BASE_PATH + "/ledger/export", produces = "text/csv") @PreAuthorize("hasAuthority('" + OverseasPermissions.INVENTORY_EXPORT + "')") @Operation(summary = "Export filtered inventory movements as UTF-8 CSV", description = "Required authority: " + OverseasPermissions.INVENTORY_EXPORT) - public ResponseEntity exportLedgers( + public ResponseEntity exportLedgers( @AuthenticationPrincipal AuthenticatedUser user, @RequestParam(required = false) String customerId, @Valid InventoryQueryRequests.LedgerQuery query) { - return csv(service.exportLedgers(platformScope(user, customerId), query), "inventory-ledger.csv"); + String scope = platformScope(user, customerId); + return csv(output -> service.writeLedgersCsv(scope, query, output), "inventory-ledger.csv"); } - private static ResponseEntity csv(byte[] body, String filename) { + private static ResponseEntity csv(StreamingResponseBody stream, String filename) { return ResponseEntity.ok() .contentType(new MediaType("text", "csv", StandardCharsets.UTF_8)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename) - .body(body); + .body(stream); } private String platformScope(AuthenticatedUser user, String requestedCustomerId) { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/service/InventoryQueryService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/service/InventoryQueryService.java index b61fb98db..e32de7bd4 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/service/InventoryQueryService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/inventory/query/service/InventoryQueryService.java @@ -17,13 +17,14 @@ import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.List; import java.util.Set; @Service public class InventoryQueryService { - private static final int EXPORT_LIMIT = 10_000; private static final Set BALANCE_STATUSES = Set.of( "AVAILABLE", "HOLD", "ALLOCATED", "PICKED", "IN_TRANSIT", "RECEIVING", "PROCUREMENT"); private static final Set POSITION_STATUSES = Set.of( @@ -79,34 +80,74 @@ public class InventoryQueryService { } public byte[] exportBalances(String customerId, InventoryQueryRequests.BalanceQuery query) { + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + validateStatus(query.inventoryStatus(), BALANCE_STATUSES); + output.write(("\uFEFFcustomerId,customerName,overseasWarehouseCode,overseasWarehouseName,destinationWarehouseCode,destinationWarehouseName,inboundOrderId,inboundOrderNo,inboundCode,skuCode,productNameZh,productNameEn,totalPackages,availablePackages,holdPackages,allocatedPackages,pickedPackages,inTransitPackages,receivingPackages,procurementPackages\r\n").getBytes(StandardCharsets.UTF_8)); + for (InventoryBalance value : mapper.balances(customerId, query, 0, 10_000)) { + InventoryBalanceView v = balanceView(value); + output.write((String.join(",", csv(v.customerId()), csv(v.customerName()), csv(v.overseasWarehouseCode()), csv(v.overseasWarehouseName()), csv(v.destinationWarehouseCode()), csv(v.destinationWarehouseName()), csv(v.inboundOrderId()), csv(v.inboundOrderNo()), csv(v.inboundCode()), csv(v.skuCode()), csv(v.productNameZh()), csv(v.productNameEn()), csv(v.totalPackages()), csv(v.availablePackages()), csv(v.holdPackages()), csv(v.allocatedPackages()), csv(v.pickedPackages()), csv(v.inTransitPackages()), csv(v.receivingPackages()), csv(v.procurementPackages())) + "\r\n").getBytes(StandardCharsets.UTF_8)); + } + return output.toByteArray(); + } catch (IOException e) { throw new IllegalStateException("Unable to export inventory balances", e); } + } + + public void writeBalancesCsv(String customerId, InventoryQueryRequests.BalanceQuery query, OutputStream output) + throws IOException { validateStatus(query.inventoryStatus(), BALANCE_STATUSES); - List rows = new ArrayList<>(); - rows.add("customerId,customerName,overseasWarehouseCode,overseasWarehouseName,destinationWarehouseCode," + + output.write(("\uFEFFcustomerId,customerName,overseasWarehouseCode,overseasWarehouseName,destinationWarehouseCode," + "destinationWarehouseName,inboundOrderId,inboundOrderNo,inboundCode,skuCode,productNameZh,productNameEn,totalPackages,availablePackages," + - "holdPackages,allocatedPackages,pickedPackages,inTransitPackages,receivingPackages,procurementPackages"); - mapper.balances(customerId, query, 0, EXPORT_LIMIT).stream().map(this::balanceView).forEach(v -> rows.add( + "holdPackages,allocatedPackages,pickedPackages,inTransitPackages,receivingPackages,procurementPackages\r\n").getBytes(StandardCharsets.UTF_8)); + final int batchSize = 1000; + long offset = 0; + while (true) { + List batch = mapper.balances(customerId, query, offset, batchSize).stream().map(this::balanceView).toList(); + if (batch.isEmpty()) break; + for (InventoryBalanceView v : batch) output.write(( String.join(",", csv(v.customerId()), csv(v.customerName()), csv(v.overseasWarehouseCode()), csv(v.overseasWarehouseName()), csv(v.destinationWarehouseCode()), csv(v.destinationWarehouseName()), csv(v.inboundOrderId()), csv(v.inboundOrderNo()), csv(v.inboundCode()), csv(v.skuCode()), csv(v.productNameZh()), csv(v.productNameEn()), csv(v.totalPackages()), csv(v.availablePackages()), csv(v.holdPackages()), csv(v.allocatedPackages()), csv(v.pickedPackages()), csv(v.inTransitPackages()), csv(v.receivingPackages()), - csv(v.procurementPackages())))); - return ("\uFEFF" + String.join("\r\n", rows)).getBytes(StandardCharsets.UTF_8); + csv(v.procurementPackages())) + "\r\n").getBytes(StandardCharsets.UTF_8)); + output.flush(); + if (batch.size() < batchSize) break; + offset += batchSize; + } } public byte[] exportLedgers(String customerId, InventoryQueryRequests.LedgerQuery query) { + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + validateDateRange(query); + output.write(("\uFEFFoccurredAt,customerId,overseasWarehouseId,destinationWarehouseCode,inboundOrderId,inboundOrderNo,inboundCode,businessType,businessOrderNo,outboundOrderNo,palletNo,batchNo,statusFrom,statusTo,beforePackages,deltaPackages,afterPackages,operatorId,remark\r\n").getBytes(StandardCharsets.UTF_8)); + for (InventoryLedger value : mapper.ledgers(customerId, query, 0, 10_000)) { + InventoryLedgerView v = ledgerView(value); + output.write((String.join(",", csv(v.occurredAt()), csv(v.customerId()), csv(v.overseasWarehouseId()), csv(v.destinationWarehouseCode()), csv(v.inboundOrderId()), csv(v.inboundOrderNo()), csv(v.inboundCode()), csv(v.businessType()), csv(v.businessOrderNo()), csv(v.outboundOrderNo()), csv(v.palletNo()), csv(v.batchNo()), csv(v.statusFrom()), csv(v.statusTo()), csv(v.beforePackages()), csv(v.deltaPackages()), csv(v.afterPackages()), csv(v.operatorId()), csv(v.remark())) + "\r\n").getBytes(StandardCharsets.UTF_8)); + } + return output.toByteArray(); + } catch (IOException e) { throw new IllegalStateException("Unable to export inventory ledger", e); } + } + + public void writeLedgersCsv(String customerId, InventoryQueryRequests.LedgerQuery query, OutputStream output) + throws IOException { validateDateRange(query); - List rows = new ArrayList<>(); - rows.add("occurredAt,customerId,customerName,overseasWarehouseId,destinationWarehouseCode,inboundOrderId,inboundOrderNo," + + output.write(("\uFEFFoccurredAt,customerId,customerName,overseasWarehouseId,destinationWarehouseCode,inboundOrderId,inboundOrderNo," + "inboundCode,businessType,businessOrderNo,outboundOrderNo,palletNo,batchNo,statusFrom,statusTo," + - "beforePackages,deltaPackages,afterPackages,operatorId,remark"); - mapper.ledgers(customerId, query, 0, EXPORT_LIMIT).stream().map(this::ledgerView).forEach(v -> rows.add( + "beforePackages,deltaPackages,afterPackages,operatorId,remark\r\n").getBytes(StandardCharsets.UTF_8)); + final int batchSize = 1000; + long offset = 0; + while (true) { + List batch = mapper.ledgers(customerId, query, offset, batchSize).stream().map(this::ledgerView).toList(); + if (batch.isEmpty()) break; + for (InventoryLedgerView v : batch) output.write(( String.join(",", csv(v.occurredAt()), csv(v.customerId()), csv(v.customerName()), csv(v.overseasWarehouseId()), csv(v.destinationWarehouseCode()), csv(v.inboundOrderId()), csv(v.inboundOrderNo()), csv(v.inboundCode()), csv(v.businessType()), csv(v.businessOrderNo()), csv(v.outboundOrderNo()), csv(v.palletNo()), csv(v.batchNo()), csv(v.statusFrom()), csv(v.statusTo()), csv(v.beforePackages()), csv(v.deltaPackages()), - csv(v.afterPackages()), csv(v.operatorId()), csv(v.remark())))); - return ("\uFEFF" + String.join("\r\n", rows)).getBytes(StandardCharsets.UTF_8); + csv(v.afterPackages()), csv(v.operatorId()), csv(v.remark())) + "\r\n").getBytes(StandardCharsets.UTF_8)); + output.flush(); + if (batch.size() < batchSize) break; + offset += batchSize; + } } private InventoryBalanceView balanceView(InventoryBalance value) { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardService.java index b50c883ba..9dfa094fd 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardService.java @@ -70,7 +70,7 @@ public class LocalLooseboardService { LocalLooseboardOrder order = new LocalLooseboardOrder(); order.setId(IdWorker.getId()); order.setCustomerId(customerId); - order.setOrderNo("LB" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + order.getId()); + order.setOrderNo(lastlegOrderNoGenerator.next("LOOSEBOARD", request.destination(), order.getId())); apply(order, request.transportMode(), request.palletRequired(), request.transportType(), request.origin(), request.originAddress(), request.destination(), request.destinationAddress(), request.containerType(), request.packageCount(), request.totalWeightKg(), request.totalVolumeCbm(), request.budgetAmount(), @@ -257,7 +257,7 @@ public class LocalLooseboardService { private LastlegOrder createLastlegOrder(LocalLooseboardOrder source, String requestHash, LocalDateTime now) { LastlegOrder order = new LastlegOrder(); order.setId(IdWorker.getId()); - order.setOrderNo(lastlegOrderNoGenerator.next()); + order.setOrderNo(lastlegOrderNoGenerator.next("LOOSEBOARD", source.getDestination(), order.getId())); order.setCustomerId(source.getCustomerId()); order.setOrderType("LOCAL_LOOSEBOARD"); order.setInboundCode(source.getOrderNo()); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderFeeMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderFeeMapper.java new file mode 100644 index 000000000..761e7620d --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderFeeMapper.java @@ -0,0 +1,9 @@ +package com.baiyue.lastleg.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.entity.LastlegOrderFee; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface LastlegOrderFeeMapper extends BaseMapper { +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderMapper.java index 28f6e1884..aeac45f2f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderMapper.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderMapper.java @@ -78,6 +78,28 @@ public interface LastlegOrderMapper extends BaseMapper { List findContainerProgressItems( @Param("sourceContainerBookingId") String sourceContainerBookingId); + record PalletMergeRelationRow(Long orderId, String relatedInboundNo, String mergeGroupNo) {} + + @Select("") + List findPalletMergeRelations(@Param("orderIds") List orderIds); + @Select("SELECT * FROM ll_order WHERE customer_id=#{customerId} AND source_region=#{sourceRegion} " + "AND source_inbound_id=#{sourceInboundId} LIMIT 1") LastlegOrder findSource(@Param("customerId") String customerId, @@ -116,4 +138,18 @@ public interface LastlegOrderMapper extends BaseMapper { int claimReceivingWarehouse(@Param("customerId") String customerId, @Param("orderId") long orderId, @Param("warehouseId") String warehouseId, @Param("warehouseName") String warehouseName); + + @Update("UPDATE ll_order SET pallet_merge_flag=#{flag}," + + "pallet_merge_batch_id=CASE WHEN #{flag} IS NULL THEN NULL ELSE pallet_merge_batch_id END," + + "updated_at=#{updatedAt} " + + "WHERE customer_id=#{customerId} AND id=#{id}") + int updatePalletMergeFlag(@Param("customerId") String customerId, @Param("id") long id, + @Param("flag") String flag, + @Param("updatedAt") java.time.LocalDateTime updatedAt); + + @Update("UPDATE ll_order SET pallet_merge_flag=#{flag},pallet_merge_batch_id=#{batchId},updated_at=#{updatedAt} " + + "WHERE customer_id=#{customerId} AND id=#{id}") + int updatePalletMergeBatch(@Param("customerId") String customerId, @Param("id") long id, + @Param("flag") String flag, @Param("batchId") String batchId, + @Param("updatedAt") java.time.LocalDateTime updatedAt); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderOperationLogMapper.java b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderOperationLogMapper.java new file mode 100644 index 000000000..c5b9fb76d --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/mapper/LastlegOrderOperationLogMapper.java @@ -0,0 +1,9 @@ +package com.baiyue.lastleg.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baiyue.lastleg.entity.LastlegOrderOperationLog; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface LastlegOrderOperationLogMapper extends BaseMapper { +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementRequests.java b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementRequests.java index d31cd0672..59581f373 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementRequests.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementRequests.java @@ -56,5 +56,15 @@ public final class OverseasProcurementRequests { JsonNode overseasCarrier, JsonNode overseasTrackingNo, JsonNode estimatedArrivalAt, - JsonNode remark) {} + JsonNode remark, + JsonNode outboundDetails) { + public Update(Long version, String purchaseCategory, String productName, JsonNode productSpecification, + JsonNode productUrl, Long inboundQuantity, JsonNode purchaseWarehouseId, JsonNode purchaseWarehouseName, + JsonNode domesticCarrier, JsonNode domesticTrackingNo, JsonNode overseasCarrier, JsonNode overseasTrackingNo, + JsonNode estimatedArrivalAt, JsonNode remark) { + this(version, purchaseCategory, productName, productSpecification, productUrl, inboundQuantity, + purchaseWarehouseId, purchaseWarehouseName, domesticCarrier, domesticTrackingNo, overseasCarrier, + overseasTrackingNo, estimatedArrivalAt, remark, null); + } + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementResponses.java b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementResponses.java index d0f94de6b..06bd0242f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementResponses.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementResponses.java @@ -7,12 +7,14 @@ import java.util.List; public final class OverseasProcurementResponses { private OverseasProcurementResponses() {} + public record OutboundDetail(String id, long quantity, String carrier, String trackingNo, LocalDateTime shippedAt, String remark) {} + public record Summary( Long id, String orderNo, String customerId, String customerName, String purchaseCategory, String productName, String productSpecification, long purchaseQuantity, BigDecimal budgetUnitPrice, String currency, String purchaseWarehouseId, String purchaseWarehouseName, - long inboundQuantity, long inventoryQuantity, + long inboundQuantity, long outboundQuantity, long inventoryQuantity, String status, long version, LocalDateTime createdAt, LocalDateTime updatedAt) {} public record CustomerOption(String customerId, String customerName) {} @@ -32,5 +34,5 @@ public final class OverseasProcurementResponses { String overseasCarrier, String overseasTrackingNo, LocalDateTime estimatedArrivalAt, String remark, String createdBy, String reviewedBy, String reviewRemark, LocalDateTime reviewedAt, - List histories) {} + List histories, List outboundDetails) {} } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/entity/OverseasProcurementOrder.java b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/entity/OverseasProcurementOrder.java index 0a0c52dd9..df14cd381 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/entity/OverseasProcurementOrder.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/entity/OverseasProcurementOrder.java @@ -30,6 +30,7 @@ public class OverseasProcurementOrder { @TableField(updateStrategy = FieldStrategy.ALWAYS) private String purchaseWarehouseId; @TableField(updateStrategy = FieldStrategy.ALWAYS) private String purchaseWarehouseName; private Long inboundQuantity; + @TableField(updateStrategy = FieldStrategy.ALWAYS) private String outboundDetails; @TableField(updateStrategy = FieldStrategy.ALWAYS) private String domesticCarrier; @TableField(updateStrategy = FieldStrategy.ALWAYS) private String domesticTrackingNo; @TableField(updateStrategy = FieldStrategy.ALWAYS) private String overseasCarrier; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/service/OverseasProcurementService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/service/OverseasProcurementService.java index 6a08ae5aa..9d3becfb7 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/service/OverseasProcurementService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/procurement/service/OverseasProcurementService.java @@ -37,6 +37,7 @@ import java.util.Set; @Service public class OverseasProcurementService { + @Resource private com.baiyue.lastleg.service.LastlegOrderNoGenerator orderNoGenerator; public static final String REVIEW_PERMISSION = OverseasPermissions.PURCHASE_REVIEW; public static final String MANAGE_PERMISSION = OverseasPermissions.PURCHASE_EDIT; public static final String PENDING_REVIEW = "PENDING_REVIEW"; @@ -62,7 +63,7 @@ public class OverseasProcurementService { order.setId(IdWorker.getId()); order.setCustomerId(customerId); order.setCustomerName(trim(request.customerName())); - order.setOrderNo("HC" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + order.getId()); + order.setOrderNo(orderNoGenerator.next("PROCUREMENT", request.receivingAddress(), order.getId())); order.setPurchaseCategory(request.purchaseCategory().trim()); order.setProductName(request.productName().trim()); order.setProductSpecification(trim(request.productSpecification())); @@ -147,7 +148,7 @@ public class OverseasProcurementService { new OverseasProcurementResponses.History(value.getOperationType(), value.getFromStatus(), value.getToStatus(), value.getOperatorId(), value.getIdempotencyKey(), value.getChangeDetail(), value.getRemark(), - value.getCreatedAt())).toList()); + value.getCreatedAt())).toList(), outboundDetails(order)); } @Transactional @@ -204,6 +205,7 @@ public class OverseasProcurementService { patchText(request.overseasTrackingNo(), 128, "overseasTrackingNo", order::setOverseasTrackingNo); patchDateTime(request.estimatedArrivalAt(), order::setEstimatedArrivalAt); patchText(request.remark(), 1000, "remark", order::setRemark); + if (request.outboundDetails() != null) order.setOutboundDetails(normalizeOutbound(request.outboundDetails(), order.getInboundQuantity())); Map after = editableSnapshot(order); if (before.equals(after)) return detail(customerId, orderNo); LocalDateTime now = LocalDateTime.now(); @@ -229,11 +231,16 @@ public class OverseasProcurementService { private OverseasProcurementResponses.Summary summary(OverseasProcurementOrder order) { long inbound = value(order.getInboundQuantity()); + long outbound = outboundDetails(order).stream().mapToLong(OverseasProcurementResponses.OutboundDetail::quantity).sum(); + String displayStatus = order.getStatus(); + if (APPROVED.equals(order.getStatus()) && inbound > 0) { + displayStatus = outbound == 0 ? "PENDING_OUTBOUND" : outbound < inbound ? "PARTIALLY_OUTBOUND" : "OUTBOUNDED"; + } return new OverseasProcurementResponses.Summary(order.getId(), order.getOrderNo(), order.getCustomerId(), order.getCustomerName(), order.getPurchaseCategory(), order.getProductName(), order.getProductSpecification(), value(order.getPurchaseQuantity()), order.getBudgetUnitPrice(), - order.getCurrency(), order.getPurchaseWarehouseId(), order.getPurchaseWarehouseName(), inbound, inbound, - order.getStatus(), value(order.getVersion()), order.getCreatedAt(), order.getUpdatedAt()); + order.getCurrency(), order.getPurchaseWarehouseId(), order.getPurchaseWarehouseName(), inbound, outbound, Math.max(0, inbound - outbound), + displayStatus, value(order.getVersion()), order.getCreatedAt(), order.getUpdatedAt()); } private void history(OverseasProcurementOrder order, String operationType, String from, String to, @@ -335,9 +342,26 @@ public class OverseasProcurementService { result.put("overseasTrackingNo", order.getOverseasTrackingNo()); result.put("estimatedArrivalAt", order.getEstimatedArrivalAt()); result.put("remark", order.getRemark()); + result.put("outboundDetails", order.getOutboundDetails()); return result; } + private List outboundDetails(OverseasProcurementOrder order) { + if (!StringUtils.hasText(order.getOutboundDetails())) return List.of(); + try { return objectMapper.readValue(order.getOutboundDetails(), objectMapper.getTypeFactory().constructCollectionType(List.class, OverseasProcurementResponses.OutboundDetail.class)); } + catch (JsonProcessingException e) { return List.of(); } + } + private String normalizeOutbound(JsonNode node, Long inbound) { + if (!node.isArray()) throw badRequest("outboundDetails must be an array"); + long total = 0; + for (JsonNode item : node) { + if (!item.isObject() || item.path("quantity").asLong(-1) < 0) throw badRequest("outboundDetails quantity must be non-negative"); + total += item.path("quantity").asLong(); + } + if (total > value(inbound)) throw badRequest("outbound quantity cannot exceed inbound quantity"); + try { return objectMapper.writeValueAsString(node); } catch (JsonProcessingException e) { throw badRequest("Invalid outboundDetails"); } + } + private String changeDetail(Map before, Map after) { Map changes = new LinkedHashMap<>(); before.forEach((field, oldValue) -> { diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/relabel/service/RelabelApplicationService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/relabel/service/RelabelApplicationService.java index c9bce8008..99cc8d7cc 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/relabel/service/RelabelApplicationService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/relabel/service/RelabelApplicationService.java @@ -67,10 +67,10 @@ public class RelabelApplicationService { } LocalDateTime now = LocalDateTime.now(); long applicationId = IdWorker.getId(); - String relabelNo = "FBR" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + applicationId; + String relabelNo = orderNoGenerator.next("RELABEL", request.overseasWarehouseId(), applicationId); LastlegOrder order = new LastlegOrder(); order.setId(IdWorker.getId()); - order.setOrderNo(orderNoGenerator.next()); + order.setOrderNo(orderNoGenerator.next("RELABEL", request.overseasWarehouseId(), order.getId())); order.setCustomerId(customerId); order.setOrderType("RELABEL_APPLICATION"); order.setInboundCode(relabelNo); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/repository/UnloadCompletedOutboxRepository.java b/lastleg-service/src/main/java/com/baiyue/lastleg/repository/UnloadCompletedOutboxRepository.java index 53e110c8a..92863e47a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/repository/UnloadCompletedOutboxRepository.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/repository/UnloadCompletedOutboxRepository.java @@ -13,11 +13,14 @@ import java.util.List; @Repository public class UnloadCompletedOutboxRepository { + private static final List OVERSEAS_TO_CN_TYPES = List.of( + EventTypes.OVERSEAS_CONTAINER_UNLOADED, + EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED); @Resource private LastlegEventOutboxMapper mapper; public List pending(int limit, LocalDateTime now, LocalDateTime expiredLeaseBefore) { return mapper.selectList(new LambdaQueryWrapper() - .eq(LastlegEventOutbox::getEventType, EventTypes.OVERSEAS_CONTAINER_UNLOADED) + .in(LastlegEventOutbox::getEventType, OVERSEAS_TO_CN_TYPES) .and(status -> status.eq(LastlegEventOutbox::getStatus, "PENDING") .and(retry -> retry.isNull(LastlegEventOutbox::getNextRetryAt) .or().le(LastlegEventOutbox::getNextRetryAt, now)) @@ -30,7 +33,7 @@ public class UnloadCompletedOutboxRepository { public boolean claim(long id, String worker, LocalDateTime now, LocalDateTime expiredLeaseBefore) { return mapper.update(null, new LambdaUpdateWrapper() .eq(LastlegEventOutbox::getId, id) - .eq(LastlegEventOutbox::getEventType, EventTypes.OVERSEAS_CONTAINER_UNLOADED) + .in(LastlegEventOutbox::getEventType, OVERSEAS_TO_CN_TYPES) .and(status -> status.eq(LastlegEventOutbox::getStatus, "PENDING") .and(retry -> retry.isNull(LastlegEventOutbox::getNextRetryAt) .or().le(LastlegEventOutbox::getNextRetryAt, now)) @@ -44,7 +47,7 @@ public class UnloadCompletedOutboxRepository { public LastlegEventOutbox claimed(long id, String worker) { return mapper.selectOne(new LambdaQueryWrapper() .eq(LastlegEventOutbox::getId, id) - .eq(LastlegEventOutbox::getEventType, EventTypes.OVERSEAS_CONTAINER_UNLOADED) + .in(LastlegEventOutbox::getEventType, OVERSEAS_TO_CN_TYPES) .eq(LastlegEventOutbox::getStatus, "PROCESSING") .eq(LastlegEventOutbox::getLockedBy, worker)); } @@ -72,7 +75,7 @@ public class UnloadCompletedOutboxRepository { private LambdaUpdateWrapper claimedBase(long id, String worker) { return new LambdaUpdateWrapper() .eq(LastlegEventOutbox::getId, id) - .eq(LastlegEventOutbox::getEventType, EventTypes.OVERSEAS_CONTAINER_UNLOADED) + .in(LastlegEventOutbox::getEventType, OVERSEAS_TO_CN_TYPES) .eq(LastlegEventOutbox::getStatus, "PROCESSING") .eq(LastlegEventOutbox::getLockedBy, worker); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/security/OverseasPermissions.java b/lastleg-service/src/main/java/com/baiyue/lastleg/security/OverseasPermissions.java index 5ac51600c..4bd4f198f 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/security/OverseasPermissions.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/security/OverseasPermissions.java @@ -19,6 +19,7 @@ public final class OverseasPermissions { public static final String INVENTORY_EXPORT = "platform:overseas-warehouse:inventory:export"; public static final String INVENTORY_EDIT = "platform:overseas-warehouse:inventory:edit"; public static final String INVENTORY_ADJUST = "platform:overseas-warehouse:inventory:adjust"; + public static final String DELIVERY_LIST = "platform:overseas-warehouse:delivery:list"; public static final String PURCHASE_LIST = "platform:overseas-business:purchase:list"; public static final String PURCHASE_EDIT = "platform:overseas-business:purchase:edit"; public static final String PURCHASE_REVIEW = "platform:overseas-business:purchase:review"; diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/service/InboundOrderDetainedOutboxService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/service/InboundOrderDetainedOutboxService.java new file mode 100644 index 000000000..f3bcdc4f0 --- /dev/null +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/service/InboundOrderDetainedOutboxService.java @@ -0,0 +1,73 @@ +package com.baiyue.lastleg.service; + +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baiyue.common.event.EventEnvelope; +import com.baiyue.common.event.EventIdentifiers; +import com.baiyue.common.event.EventPayloadCanonicalizer; +import com.baiyue.common.event.EventPayloadHash; +import com.baiyue.common.event.EventTypes; +import com.baiyue.common.event.OverseasInboundOrderDetainedChangedV1; +import com.baiyue.common.event.RegionCode; +import com.baiyue.lastleg.entity.LastlegEventOutbox; +import com.baiyue.lastleg.entity.LastlegOrder; +import com.baiyue.lastleg.mapper.LastlegEventOutboxMapper; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; + +/** 在尾程业务事务内冻结按来源入仓单回传国内的暂扣状态事件。 */ +@Service +public class InboundOrderDetainedOutboxService { + @Resource private LastlegEventOutboxMapper mapper; + @Resource private ObjectMapper objectMapper; + + public void publish(LastlegOrder order, boolean detained, String reason, String operatorId, + String operatorName, Instant changedAt, long changeVersion) { + requireText(order.getSourceInboundId(), "sourceInboundId"); + requireText(order.getInboundCode(), "inboundCode"); + requireText(order.getCustomerId(), "customerId"); + var payload = new OverseasInboundOrderDetainedChangedV1(order.getSourceInboundId(), + order.getInboundCode(), String.valueOf(order.getId()), order.getOrderNo(), detained, + reason, operatorId, operatorName, changedAt); + JsonNode payloadNode = objectMapper.valueToTree(payload); + String eventKey = EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED + ':' + + order.getSourceInboundId() + ':' + changeVersion; + EventEnvelope envelope = new EventEnvelope(EventIdentifiers.newUuid(), + EventTypes.OVERSEAS_INBOUND_ORDER_DETAINED_CHANGED, 1, changedAt, + RegionCode.OVERSEAS, RegionCode.CN, order.getCustomerId(), "INBOUND_ORDER", + order.getSourceInboundId(), changeVersion, 1L, EventIdentifiers.newUuid(), + eventKey, order.getSourceEventId(), payloadNode, + EventPayloadHash.sha256(EventPayloadCanonicalizer.canonicalize(payloadNode))).requireValid(); + + LastlegEventOutbox event = new LastlegEventOutbox(); + event.setId(IdWorker.getId()); + event.setCustomerId(order.getCustomerId()); + event.setAggregateType(envelope.aggregateType()); + event.setAggregateId(envelope.aggregateId()); + event.setEventType(envelope.eventType()); + event.setEventVersion(envelope.eventVersion()); + event.setEventKey(eventKey); + event.setPayloadJson(json(envelope)); + event.setStatus("PENDING"); + event.setRetryCount(0); + event.setCreatedAt(LocalDateTime.ofInstant(changedAt, ZoneOffset.UTC)); + mapper.insert(event); + } + + private static void requireText(String value, String field) { + if (value == null || value.isBlank()) throw new IllegalStateException(field + " is required"); + } + + private String json(Object value) { + try { return objectMapper.writeValueAsString(value); } + catch (JsonProcessingException exception) { + throw new IllegalStateException("Cannot serialize inbound detained event", exception); + } + } +} diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderNoGenerator.java b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderNoGenerator.java index 766152347..137d5df9a 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderNoGenerator.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderNoGenerator.java @@ -4,9 +4,13 @@ import com.baiyue.lastleg.mapper.LastlegOrderNoDailySequenceMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Component; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.Locale; @Component public class LastlegOrderNoGenerator { @@ -26,4 +30,61 @@ public class LastlegOrderNoGenerator { } return "LMO-" + BUSINESS_DATE.format(businessDate) + "-" + String.format("%04d", sequence); } + + public String next(String businessType, String warehouse, long uniqueId) { + String type = normalizeType(businessType); + return type + "-" + warehouseCode(warehouse) + "-" + + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + "-" + + String.format(Locale.ROOT, "%06d", Math.floorMod(uniqueId, 1_000_000)); + } + + private String normalizeType(String value) { + String code = value == null ? "LMO" : value.trim().toUpperCase(Locale.ROOT); + return switch (code) { + case "FIRST_LEG" -> "FL"; + case "ONE_CLICK_DROPSHIP" -> "OD"; + case "RELABEL_APPLICATION", "RELABEL" -> "RL"; + case "WAREHOUSE_TRANSFER", "TRANSFER" -> "WT"; + case "LOCAL_LOOSEBOARD", "LOOSEBOARD" -> "LB"; + case "DISPATCH", "PICKUP_CAR" -> "DS"; + case "OUTBOUND" -> "OB"; + case "DELIVERY" -> "DL"; + case "TRUCK" -> "TR"; + case "EXCEPTION" -> "EX"; + case "DISCARD" -> "CD"; + case "PROCUREMENT" -> "PR"; + case "INBOUND_PLAN" -> "IP"; + default -> { + String normalized = code.replaceAll("[^A-Z0-9]", ""); + yield normalized.isEmpty() ? "LMO" : normalized.substring(0, Math.min(4, normalized.length())); + } + }; + } + + public String warehouseCode(String value) { + String code = value == null ? "" : value.trim().toUpperCase(Locale.ROOT); + if (code.isEmpty()) return "WH"; + if (code.matches(".*(LAX|ONT|NYC|JFK|LGB|EWR|SFO|SEA|DFW|ORD|ATL|MIA|PHX|LAS|YYZ|YVR|AMS|LHR|FRA|CDG|HKG|NRT|SYD).*")) { + var match = java.util.regex.Pattern.compile("(LAX|ONT|NYC|JFK|LGB|EWR|SFO|SEA|DFW|ORD|ATL|MIA|PHX|LAS|YYZ|YVR|AMS|LHR|FRA|CDG|HKG|NRT|SYD)").matcher(code); + if (match.find()) return match.group(1); + } + if (code.matches("[A-Z0-9]{2,8}")) return code; + return "WH"; + } + + /** The physical container number is the tail-leg business order grouping boundary. */ + public String forContainerNo(String containerNo) { + if (containerNo == null || containerNo.isBlank()) { + throw new IllegalArgumentException("containerNo is required"); + } + String normalized = containerNo.trim(); + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest(normalized.getBytes(StandardCharsets.UTF_8)); + long suffix = java.nio.ByteBuffer.wrap(digest, 0, 8).getLong(); + return next("FIRST_LEG", null, suffix); + } catch (NoSuchAlgorithmException exception) { + throw new IllegalStateException("SHA-256 is unavailable", exception); + } + } } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderService.java index 71686ae8e..2ce6af4f2 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LastlegOrderService.java @@ -1,6 +1,7 @@ package com.baiyue.lastleg.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baiyue.common.core.api.PageResult; import com.baiyue.common.event.ContainerNumbers; @@ -12,11 +13,16 @@ import com.baiyue.lastleg.dto.ContainerPlanProgressSummary; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.entity.LastlegOrderDetail; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; +import com.baiyue.lastleg.entity.LastlegOrderFee; +import com.baiyue.lastleg.entity.LastlegOrderOperationLog; import com.baiyue.lastleg.inboundplan.entity.InboundPlanCarton; import com.baiyue.lastleg.inboundplan.mapper.InboundPlanCartonMapper; +import com.baiyue.lastleg.inventory.command.service.InventoryCommandService; import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; +import com.baiyue.lastleg.mapper.LastlegOrderFeeMapper; +import com.baiyue.lastleg.mapper.LastlegOrderOperationLogMapper; import com.baiyue.lastleg.model.LastlegOrderType; import jakarta.annotation.Resource; import org.springframework.http.HttpStatus; @@ -26,9 +32,15 @@ import org.springframework.util.StringUtils; import org.springframework.web.server.ResponseStatusException; import java.nio.charset.StandardCharsets; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Objects; @Service @@ -36,17 +48,24 @@ public class LastlegOrderService { public static final String PENDING_ASSIGNMENT = "PENDING_ASSIGNMENT"; public static final String DESTINATION_ASSIGNED = "DESTINATION_ASSIGNED"; public static final String INBOUND_PLANNED = "INBOUND_PLANNED"; + public static final String PALLET_MERGE_REQUIRED = "REQUIRED"; + public static final String PALLET_MERGED = "MERGED"; @Resource private LastlegOrderMapper orderMapper; @Resource private LastlegOrderDetailMapper detailMapper; @Resource private LastlegOrderStatusHistoryMapper historyMapper; + @Resource private LastlegOrderFeeMapper feeMapper; + @Resource private LastlegOrderOperationLogMapper operationLogMapper; @Resource private InboundPlanCartonMapper cartonMapper; @Resource private FbaWarehouseValidator fbaWarehouseValidator; + @Resource private InventoryCommandService inventory; + @Resource private InboundOrderDetainedOutboxService detainedOutbox; public PageResult page(String customerId, LastlegOrderRequests.Query query) { Page page = orderMapper.selectPage( new Page<>(query.page(), query.size()), filter(customerId, query).orderByDesc(LastlegOrder::getCreatedAt)); - return new PageResult<>(page.getRecords().stream().map(this::summary).toList(), + Map> mergeRelations = palletMergeRelations(page.getRecords()); + return new PageResult<>(page.getRecords().stream().map(order -> summary(order, mergeRelations.get(order.getId()))).toList(), page.getCurrent(), page.getSize(), page.getTotal(), page.getPages()); } @@ -100,9 +119,165 @@ public class LastlegOrderService { .eq(LastlegOrderStatusHistory::getCustomerId, order.getCustomerId()) .eq(LastlegOrderStatusHistory::getOrderId, orderId) .orderByAsc(LastlegOrderStatusHistory::getCreatedAt)); - return new LastlegOrderDetailResponse(summary(order), cargo, history.stream() + List fees = feeMapper.selectList(new LambdaQueryWrapper() + .eq(LastlegOrderFee::getCustomerId, order.getCustomerId()) + .eq(LastlegOrderFee::getOrderId, orderId).orderByAsc(LastlegOrderFee::getCreatedAt)); + List operations = operationLogMapper.selectList( + new LambdaQueryWrapper() + .eq(LastlegOrderOperationLog::getCustomerId, order.getCustomerId()) + .eq(LastlegOrderOperationLog::getOrderId, orderId) + .orderByAsc(LastlegOrderOperationLog::getCreatedAt)); + return new LastlegOrderDetailResponse(summary(order, palletMergeRelations(List.of(order)).get(order.getId())), cargo, history.stream() .map(value -> new LastlegOrderDetailResponse.History(value.getId(), value.getFromStatus(), - value.getToStatus(), value.getRemark(), value.getCreatedAt())).toList()); + value.getToStatus(), value.getRemark(), value.getCreatedAt())).toList(), fees, operations.stream() + .map(value -> new LastlegOrderDetailResponse.Operation(value.getId(), value.getOperationNode(), + value.getOperatorName(), value.getResult(), value.getCreatedAt())).toList()); + } + + @Transactional + public LastlegOrderFee createFee(String customerId, long orderId, LastlegOrderRequests.CreateFee request, + String operatorName) { + LastlegOrder order = findOwned(customerId, orderId); + LocalDateTime now = LocalDateTime.now(); + LastlegOrderFee fee = new LastlegOrderFee(); + fee.setId(IdWorker.getId()); + fee.setCustomerId(order.getCustomerId()); + fee.setOrderId(orderId); + fee.setFeeItem(request.feeItem().trim()); + fee.setCurrency(request.currency()); + fee.setAmount(request.amount()); + fee.setStatus(request.status()); + fee.setSupplierDescription(StringUtils.hasText(request.supplierDescription()) + ? request.supplierDescription().trim() : null); + fee.setCreatedBy(operatorName); + fee.setCreatedAt(now); + fee.setUpdatedAt(now); + feeMapper.insert(fee); + + LastlegOrderOperationLog operation = new LastlegOrderOperationLog(); + operation.setId(IdWorker.getId()); + operation.setCustomerId(order.getCustomerId()); + operation.setOrderId(orderId); + operation.setOperationNode("新增费用"); + operation.setOperatorName(operatorName); + operation.setResult("新增" + fee.getFeeItem() + ":" + fee.getCurrency() + " " + fee.getAmount().toPlainString()); + operation.setCreatedAt(now); + operationLogMapper.insert(operation); + return fee; + } + + @Transactional + public void setDetained(String customerId, long orderId, LastlegOrderRequests.SetDetained request, + String operatorName) { + setDetained(customerId, orderId, request, null, operatorName); + } + + @Transactional + public void setDetained(String customerId, long orderId, LastlegOrderRequests.SetDetained request, + String operatorId, String operatorName) { + LastlegOrder current = findOwned(customerId, orderId); + LastlegOrder order = orderMapper.lockById(current.getCustomerId(), orderId); + if (order == null) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Tail-leg order not found"); + boolean detained = request.detained(); + if (Boolean.TRUE.equals(order.getDetained()) == detained) { + throw new ResponseStatusException(HttpStatus.CONFLICT, detained ? "Order is already held" : "Order is not held"); + } + if (detained && "INBOUND_COMPLETED".equals(order.getCurrentStatus())) { + throw new ResponseStatusException(HttpStatus.CONFLICT, "Completed inbound orders cannot be newly held"); + } + LocalDateTime now = LocalDateTime.now(); + order.setDetained(detained); + order.setUpdatedAt(now); + orderMapper.updateById(order); + if (!detained && "INBOUND_COMPLETED".equals(order.getCurrentStatus())) { + inventory.releaseInboundOrderHold(order.getCustomerId(), operatorName, orderId, order.getOrderNo(), request.remark().trim()); + } + LastlegOrderOperationLog operation = new LastlegOrderOperationLog(); + operation.setId(IdWorker.getId()); + operation.setCustomerId(order.getCustomerId()); + operation.setOrderId(orderId); + operation.setOperationNode(detained ? "暂扣" : "解除暂扣"); + operation.setOperatorName(operatorName); + operation.setResult(request.remark().trim()); + operation.setCreatedAt(now); + operationLogMapper.insert(operation); + if (StringUtils.hasText(order.getSourceInboundId())) { + detainedOutbox.publish(order, detained, request.remark().trim(), operatorId, operatorName, + now.toInstant(ZoneOffset.UTC), operation.getId()); + } + } + + @Transactional + public void setPalletMergeRequired(String customerId, long orderId, + LastlegOrderRequests.SetPalletMergeRequired request, + String operatorName) { + LastlegOrder current = findOwned(customerId, orderId); + LastlegOrder order = orderMapper.lockById(current.getCustomerId(), orderId); + if (order == null) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Tail-leg order not found"); + if ("INBOUND_COMPLETED".equals(order.getCurrentStatus())) { + throw new ResponseStatusException(HttpStatus.CONFLICT, + "Completed inbound orders cannot change the pallet merge mark"); + } + if (PALLET_MERGED.equals(order.getPalletMergeFlag())) { + throw new ResponseStatusException(HttpStatus.CONFLICT, + "A completed pallet merge cannot be unmarked"); + } + String target = request.palletMergeRequired() ? PALLET_MERGE_REQUIRED : null; + if (Objects.equals(target, order.getPalletMergeFlag())) return; + LocalDateTime now = LocalDateTime.now(); + if (orderMapper.updatePalletMergeFlag(order.getCustomerId(), orderId, target, now) != 1) { + throw new ResponseStatusException(HttpStatus.CONFLICT, "Pallet merge mark was not updated"); + } + LastlegOrderOperationLog operation = new LastlegOrderOperationLog(); + operation.setId(IdWorker.getId()); + operation.setCustomerId(order.getCustomerId()); + operation.setOrderId(orderId); + operation.setOperationNode(request.palletMergeRequired() ? "标记合托" : "取消合托标记"); + operation.setOperatorName(operatorName); + operation.setResult(request.palletMergeRequired() + ? "PDA扫描本单箱唛时提示需要合托" : "已取消PDA合托提示"); + operation.setCreatedAt(now); + operationLogMapper.insert(operation); + } + + @Transactional + public void setPalletMergeBatch(String customerId, LastlegOrderRequests.SetPalletMergeBatch request, + String operatorName) { + List ids = request.orderIds().stream().filter(Objects::nonNull).distinct().toList(); + if (ids.isEmpty()) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "At least one order is required"); + List orders = orderMapper.selectList(new LambdaQueryWrapper() + .eq(StringUtils.hasText(customerId), LastlegOrder::getCustomerId, customerId) + .in(LastlegOrder::getId, ids).last("FOR UPDATE")); + if (orders.size() != ids.size()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "One or more tail-leg orders were not found"); + } + if (orders.stream().map(LastlegOrder::getCustomerId).distinct().count() != 1) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Pallet merge orders must belong to one customer"); + } + if (orders.stream().map(order -> StringUtils.hasText(order.getTransferWarehouseCode()) + ? order.getTransferWarehouseCode() : order.getFbaWarehouseCode()).filter(Objects::nonNull).distinct().count() > 1) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Pallet merge orders must have one destination warehouse"); + } + if (orders.stream().anyMatch(order -> "INBOUND_COMPLETED".equals(order.getCurrentStatus()) + || PALLET_MERGED.equals(order.getPalletMergeFlag()))) { + throw new ResponseStatusException(HttpStatus.CONFLICT, "Completed pallet merge orders cannot be changed"); + } + String batchId = request.palletMergeRequired() + ? "PMG-" + java.util.UUID.randomUUID().toString().replace("-", "") : null; + String flag = request.palletMergeRequired() ? PALLET_MERGE_REQUIRED : null; + LocalDateTime now = LocalDateTime.now(); + for (LastlegOrder order : orders) { + if (orderMapper.updatePalletMergeBatch(order.getCustomerId(), order.getId(), flag, batchId, now) != 1) { + throw new ResponseStatusException(HttpStatus.CONFLICT, "Pallet merge group was not updated"); + } + LastlegOrderOperationLog operation = new LastlegOrderOperationLog(); + operation.setId(IdWorker.getId()); operation.setCustomerId(order.getCustomerId()); + operation.setOrderId(order.getId()); + operation.setOperationNode(request.palletMergeRequired() ? "批量标记合托" : "取消合托标记"); + operation.setOperatorName(operatorName); + operation.setResult(request.palletMergeRequired() ? "PDA合托分组 " + batchId : "已取消PDA合托分组"); + operation.setCreatedAt(now); operationLogMapper.insert(operation); + } } @Transactional @@ -163,12 +338,25 @@ public class LastlegOrderService { } public byte[] exportCsv(String customerId, LastlegOrderRequests.Query query) { - List orders = orderMapper.selectList(filter(customerId, query) - .orderByDesc(LastlegOrder::getCreatedAt).last("LIMIT 10000")); - List lines = new ArrayList<>(); - lines.add("ID,尾程单号,订单类型,入仓码,头程单号,货主,海外仓,转仓,转仓编码,FBA仓库,集装箱号,货柜状态,是否异常,是否暂扣,托数,箱数,体积(m3),数量,体积重量,预计到港时间,实际到港时间,预计到仓,入库单号,换标单号,换标状态,合托标记,主流程状态,子单状态,备注(仅自己),备注(客户),备注(仓库)"); - for (LastlegOrder order : orders) { - lines.add(String.join(",", List.of( + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + writeExportCsv(customerId, query, output); + return output.toByteArray(); + } catch (IOException e) { + throw new IllegalStateException("Unable to export tail-leg orders", e); + } + } + + public void writeExportCsv(String customerId, LastlegOrderRequests.Query query, OutputStream output) + throws IOException { + output.write("\uFEFFID,尾程单号,订单类型,入仓码,头程单号,货主,海外仓,转仓,转仓编码,FBA仓库,集装箱号,货柜状态,是否异常,是否暂扣,托数,箱数,体积(m3),数量,体积重量,预计到港时间,实际到港时间,预计到仓,入库单号,换标单号,换标状态,合托标记,主流程状态,子单状态,备注(仅自己),备注(客户),备注(仓库)\r\n".getBytes(StandardCharsets.UTF_8)); + final int batchSize = 1000; + long page = 1; + while (true) { + List orders = orderMapper.selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, batchSize), + filter(customerId, query).orderByDesc(LastlegOrder::getCreatedAt).orderByDesc(LastlegOrder::getId)).getRecords(); + if (orders.isEmpty()) break; + for (LastlegOrder order : orders) { + String line = String.join(",", List.of( csv(order.getId()), csv(order.getOrderNo()), csv(LastlegOrderType.labelOf(order.getOrderType())), csv(order.getInboundCode()), csv(order.getSourceOrderNo()), csv(order.getCustomerName()), csv(order.getOverseasWarehouseName()), @@ -178,9 +366,13 @@ public class LastlegOrderService { csv(order.getVolumeWeightKg()), csv(order.getEstimatedArrivalAt()), csv(order.getActualArrivalAt()), csv(order.getEstimatedWarehouseAt()), csv(order.getInboundOrderNo()), csv(order.getRelabelOrderNo()), csv(order.getRelabelStatus()), csv(order.getPalletMergeFlag()), csv(order.getCurrentStatus()), csv(order.getSubOrderStatus()), csv(order.getPrivateRemark()), - csv(order.getCustomerRemark()), csv(order.getWarehouseRemark())))); + csv(order.getCustomerRemark()), csv(order.getWarehouseRemark()))) + "\r\n"; + output.write(line.getBytes(StandardCharsets.UTF_8)); + } + output.flush(); + if (orders.size() < batchSize) break; + page++; } - return ("\uFEFF" + String.join("\r\n", lines)).getBytes(StandardCharsets.UTF_8); } private LambdaQueryWrapper filter(String customerId, LastlegOrderRequests.Query query) { @@ -224,7 +416,20 @@ public class LastlegOrderService { } } - private LastlegOrderSummary summary(LastlegOrder o) { + private Map> palletMergeRelations(List orders) { + if (orders.isEmpty()) return Map.of(); + Map> result = new LinkedHashMap<>(); + List rows = orderMapper.findPalletMergeRelations( + orders.stream().map(LastlegOrder::getId).toList()); + if (rows == null) return Map.of(); + for (LastlegOrderMapper.PalletMergeRelationRow row : rows) { + result.computeIfAbsent(row.orderId(), ignored -> new ArrayList<>()).add(row); + } + return result; + } + + private LastlegOrderSummary summary(LastlegOrder o, List relations) { + List related = relations == null ? List.of() : relations; return new LastlegOrderSummary(o.getId(), o.getOrderNo(), o.getOrderType(), LastlegOrderType.labelOf(o.getOrderType()), o.getDestinationType(), o.getSourceInboundId(), o.getInboundCode(), o.getSourceOrderNo(), o.getCustomerId(), o.getCustomerName(), o.getOverseasWarehouseId(), @@ -233,7 +438,9 @@ public class LastlegOrderService { o.getAbnormal(), o.getDetained(), o.getPalletCount(), o.getPackageCount(), o.getVolumeCbm(), o.getProductQuantity(), o.getVolumeWeightKg(), o.getEstimatedArrivalAt(), o.getActualArrivalAt(), o.getEstimatedWarehouseAt(), o.getInboundOrderNo(), o.getRelabelOrderNo(), o.getRelabelStatus(), - o.getPalletMergeFlag(), o.getCurrentStatus(), o.getSubOrderStatus(), o.getPrivateRemark(), + o.getPalletMergeFlag(), related.stream().map(LastlegOrderMapper.PalletMergeRelationRow::mergeGroupNo).distinct().findFirst().orElse(null), + related.stream().map(LastlegOrderMapper.PalletMergeRelationRow::relatedInboundNo).filter(Objects::nonNull).distinct().toList(), + o.getCurrentStatus(), o.getSubOrderStatus(), o.getPrivateRemark(), o.getCustomerRemark(), o.getWarehouseRemark(), o.getCreatedAt(), o.getUpdatedAt()); } diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java index b38ab8e20..5cd1c5046 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/service/LoadingCompletedConsumer.java @@ -82,8 +82,8 @@ public class LoadingCompletedConsumer { // The loading-plan and container-shipping events are independent aggregate streams. // Seeding the shipping version from the loading-plan version makes a valid first node // (aggregateVersion=0) look stale whenever the loading plan has a higher version. - String orderNo = orderNoGenerator.next(); JsonNode rawOrders = envelope.payload().path("orders"); + String orderNo = orderNoGenerator.forContainerNo(payload.containerNo()); for (int index = 0; index < payload.orders().size(); index++) { var source = payload.orders().get(index); JsonNode rawSource = rawOrders.isArray() && index < rawOrders.size() ? rawOrders.get(index) : objectMapper.createObjectNode(); @@ -216,7 +216,9 @@ public class LoadingCompletedConsumer { order.setOverseasWarehouseId(null); order.setOverseasWarehouseName(null); order.setFbaWarehouseCode(source.destinationWarehouseCode()); order.setContainerNo(containerNo); order.setContainerStatus("待转柜"); order.setAbnormal(false); order.setDetained(false); - order.setPackageCount(source.packageCount()); order.setPalletCount(dropshipPallets.size()); + order.setPackageCount(source.packageCount()); + order.setPalletCount("ONE_CLICK_DROPSHIP".equals(orderType) + ? dropshipPallets.size() : Objects.requireNonNullElse(source.palletCount(), 0)); order.setProductQuantity(source.productQuantity()); order.setVolumeCbm(source.volumeCbm()); order.setEstimatedWarehouseAt(parsePlannedArrival(payload.estimatedWarehouseArrivalAt())); order.setVolumeWeightKg(source.items().stream().map(FirstLegLoadingPlanCompletedV1.Item::dimensionalWeightKg) diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/transfer/service/WarehouseTransferService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/transfer/service/WarehouseTransferService.java index 8995b02bd..3abe2459e 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/transfer/service/WarehouseTransferService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/transfer/service/WarehouseTransferService.java @@ -117,7 +117,7 @@ public class WarehouseTransferService { LocalDateTime now = LocalDateTime.now(); long transferId = IdWorker.getId(); - String transferNo = "WTR" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + transferId; + String transferNo = orderNoGenerator.next("TRANSFER", request.operatingWarehouseId(), transferId); LastlegOrder order = createTailOrder(customerId, inboundOrderId, transferNo, request, requestHash, now); WarehouseTransferOrder transfer = new WarehouseTransferOrder(); @@ -464,7 +464,7 @@ public class WarehouseTransferService { private LastlegOrder createTailOrder(String customerId, Long inboundOrderId, String transferNo, WarehouseTransferRequests.Create request, String requestHash, LocalDateTime now) { LastlegOrder order = new LastlegOrder(); - order.setId(IdWorker.getId()); order.setOrderNo(orderNoGenerator.next()); order.setCustomerId(customerId); + order.setId(IdWorker.getId()); order.setOrderNo(orderNoGenerator.next("TRANSFER", request.operatingWarehouseId(), order.getId())); order.setCustomerId(customerId); order.setOrderType(request.needRelabel() ? RETURN_RELABEL_ORDER_TYPE : ORDER_TYPE); order.setInboundCode(request.inboundCode().trim()); order.setSourceRegion("CUSTOMER"); order.setSourceInboundId("WAREHOUSE_TRANSFER:" + transferNo); order.setRootInboundId(String.valueOf(inboundOrderId)); @@ -505,7 +505,7 @@ public class WarehouseTransferService { private void createRelabel(String customerId, String operatorId, WarehouseTransferOrder transfer, LastlegOrder order, WarehouseTransferRequests.Create request, LocalDateTime now) { long applicationId = IdWorker.getId(); - String relabelNo = "FBR" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + applicationId; + String relabelNo = orderNoGenerator.next("RELABEL", request.operatingWarehouseId(), applicationId); RelabelApplication application = new RelabelApplication(); application.setId(applicationId); application.setCustomerId(customerId); application.setRelabelNo(relabelNo); application.setLastlegOrderId(order.getId()); application.setCustomerOrderNo(transfer.getTransferNo()); diff --git a/lastleg-service/src/main/java/com/baiyue/lastleg/trucking/service/UsTruckOrderService.java b/lastleg-service/src/main/java/com/baiyue/lastleg/trucking/service/UsTruckOrderService.java index fe586c4da..f6aae16a0 100644 --- a/lastleg-service/src/main/java/com/baiyue/lastleg/trucking/service/UsTruckOrderService.java +++ b/lastleg-service/src/main/java/com/baiyue/lastleg/trucking/service/UsTruckOrderService.java @@ -39,6 +39,7 @@ import java.util.function.Consumer; @Service public class UsTruckOrderService { + @Resource private com.baiyue.lastleg.service.LastlegOrderNoGenerator orderNoGenerator; public static final String MANAGE_PERMISSION = OverseasPermissions.TRUCK_STATUS; public static final String PENDING_REVIEW = "PENDING_REVIEW"; /** Kept for source compatibility; new orders use PENDING_REVIEW. */ @@ -79,7 +80,7 @@ public class UsTruckOrderService { UsTruckOrder order = new UsTruckOrder(); order.setId(IdWorker.getId()); order.setCustomerId(customerId); - order.setOrderNo("TK" + now.format(DateTimeFormatter.BASIC_ISO_DATE) + order.getId()); + order.setOrderNo(orderNoGenerator.next("TRUCK", request.origin(), order.getId())); order.setTransportType(StringUtils.hasText(request.transportType()) ? norm(request.transportType()) : null); order.setOrigin(trim(request.origin())); order.setOriginAddress(trim(request.originAddress())); order.setDestination(trim(request.destination())); order.setDestinationAddress(trim(request.destinationAddress())); diff --git a/lastleg-service/src/main/resources/application.yml b/lastleg-service/src/main/resources/application.yml index b44c1dfed..79683848c 100644 --- a/lastleg-service/src/main/resources/application.yml +++ b/lastleg-service/src/main/resources/application.yml @@ -53,6 +53,9 @@ baiyue: relabel-photo-file: connect-timeout: ${LASTLEG_RELABEL_PHOTO_FILE_CONNECT_TIMEOUT:3s} read-timeout: ${LASTLEG_RELABEL_PHOTO_FILE_READ_TIMEOUT:30s} + customer-visit-file: + connect-timeout: ${LASTLEG_CUSTOMER_VISIT_FILE_CONNECT_TIMEOUT:3s} + read-timeout: ${LASTLEG_CUSTOMER_VISIT_FILE_READ_TIMEOUT:30s} warehouse-resource: connect-timeout: ${LASTLEG_WAREHOUSE_RESOURCE_CONNECT_TIMEOUT:3s} read-timeout: ${LASTLEG_WAREHOUSE_RESOURCE_READ_TIMEOUT:5s} diff --git a/lastleg-service/src/main/resources/db/lastleg.sql b/lastleg-service/src/main/resources/db/lastleg.sql index 2d1bf4d6b..e7081d1ea 100644 --- a/lastleg-service/src/main/resources/db/lastleg.sql +++ b/lastleg-service/src/main/resources/db/lastleg.sql @@ -83,6 +83,7 @@ CREATE TABLE IF NOT EXISTS ll_order ( relabel_order_no VARCHAR(128) COMMENT '换标单号', relabel_status VARCHAR(32) COMMENT '换标状态', pallet_merge_flag VARCHAR(32) COMMENT '合托标记', + pallet_merge_batch_id VARCHAR(64) NULL COMMENT '平台预标记合托分组号', current_status VARCHAR(32) NOT NULL DEFAULT 'PENDING_ASSIGNMENT' COMMENT '当前业务状态:PENDING_ASSIGNMENT待分配、PENDING_INBOUND待入库、INBOUND_COMPLETED入库完成、PENDING_DELIVERY待派送、DELIVERING派送中、DELIVERED派送完成', sub_order_status VARCHAR(255) NULL COMMENT '子单据状态汇总', private_remark VARCHAR(1000) COMMENT '备注(仅自己)', @@ -104,6 +105,9 @@ CREATE TABLE IF NOT EXISTS ll_order ( KEY idx_ll_order_container_progress(source_container_booking_id,estimated_warehouse_at,created_at,id) ) ENGINE=InnoDB COMMENT='尾程订单主表'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='pallet_merge_batch_id'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN pallet_merge_batch_id VARCHAR(64) NULL COMMENT ''平台预标记合托分组号'' AFTER pallet_merge_flag')); +PREPARE ll_order_merge_batch_ddl FROM @ddl; EXECUTE ll_order_merge_batch_ddl; DEALLOCATE PREPARE ll_order_merge_batch_ddl; + CREATE TABLE IF NOT EXISTS ll_order_detail ( id BIGINT NOT NULL AUTO_INCREMENT COMMENT '订单明细主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', @@ -144,6 +148,56 @@ CREATE TABLE IF NOT EXISTS ll_loading_order_status_history ( KEY idx_ll_loading_history_customer_order(customer_id,order_id,created_at) ) ENGINE=InnoDB COMMENT='尾程订单状态变更历史'; +CREATE TABLE IF NOT EXISTS ll_order_fee ( + id BIGINT NOT NULL COMMENT '费用明细主键', + customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', + fee_item VARCHAR(128) NOT NULL COMMENT '费用项目', + currency VARCHAR(3) NOT NULL COMMENT '币种代码', + amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING_REVIEW' COMMENT '费用状态:PENDING_REVIEW待审核、APPROVED审核通过、REJECTED已驳回', + supplier_description VARCHAR(500) NULL COMMENT '供应商或费用说明', + created_by VARCHAR(128) NOT NULL COMMENT '创建操作人', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), + KEY idx_ll_order_fee_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单费用明细'; + +CREATE TABLE IF NOT EXISTS ll_order_operation_log ( + id BIGINT NOT NULL COMMENT '操作记录主键', + customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', + operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', + operator_name VARCHAR(128) NOT NULL COMMENT '操作人名称', + result VARCHAR(500) NOT NULL COMMENT '处理结果', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', + PRIMARY KEY(id), + KEY idx_ll_order_operation_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单操作记录'; + +-- 派送单列表与详情快照、转派送费用及操作记录。 +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='overseas_warehouse_name'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN overseas_warehouse_name VARCHAR(255) NULL COMMENT ''海外仓名称快照'' AFTER overseas_warehouse_id')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='customer_name'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN customer_name VARCHAR(255) NULL COMMENT ''客户名称快照'' AFTER overseas_warehouse_name')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='waybill_no'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN waybill_no VARCHAR(128) NULL COMMENT ''承运运单号'', ADD COLUMN delivery_method VARCHAR(32) NULL COMMENT ''派送方式'', ADD COLUMN carrier_name VARCHAR(255) NULL COMMENT ''派送承运商'', ADD COLUMN pickup_method VARCHAR(64) NULL COMMENT ''提货方式'', ADD COLUMN loading_type VARCHAR(64) NULL COMMENT ''装车类型'', ADD COLUMN vehicle_type VARCHAR(64) NULL COMMENT ''车辆类型'', ADD COLUMN vehicle_size VARCHAR(64) NULL COMMENT ''车辆大小'', ADD COLUMN vehicle_plate VARCHAR(64) NULL COMMENT ''车牌号'', ADD COLUMN driver_name VARCHAR(128) NULL COMMENT ''司机或车队名称'', ADD COLUMN driver_phone VARCHAR(64) NULL COMMENT ''车队联系电话'', ADD COLUMN appointment_account VARCHAR(128) NULL COMMENT ''预约账号'', ADD COLUMN seller_name VARCHAR(128) NULL COMMENT ''卖家姓名'' AFTER status')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='total_weight_kg'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN total_weight_kg DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总重量(千克)'', ADD COLUMN total_volume_cbm DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总体积(立方米)'', ADD COLUMN planned_delivery_at DATETIME(3) NULL COMMENT ''预计派送时间'', ADD COLUMN dispatched_at DATETIME(3) NULL COMMENT ''实际派送时间'', ADD COLUMN expected_arrival_at DATETIME(3) NULL COMMENT ''预计到达时间'', ADD COLUMN signed_at DATETIME(3) NULL COMMENT ''签收时间'', ADD COLUMN remark VARCHAR(1000) NULL COMMENT ''派送备注'' AFTER pallet_count')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +CREATE TABLE IF NOT EXISTS ll_delivery_fee ( + id BIGINT NOT NULL COMMENT '派送费用主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', + fee_direction VARCHAR(16) NOT NULL DEFAULT 'PAYABLE' COMMENT '应收应付方向', fee_type VARCHAR(64) NOT NULL COMMENT '费用类型', currency VARCHAR(8) NOT NULL COMMENT '币种', + amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', remark VARCHAR(500) NULL COMMENT '费用备注', created_by VARCHAR(128) NULL COMMENT '创建人ID', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', PRIMARY KEY(id), KEY idx_ll_delivery_fee_order(customer_id,delivery_order_id,id) +) ENGINE=InnoDB COMMENT='派送单费用明细'; +CREATE TABLE IF NOT EXISTS ll_delivery_operation ( + id BIGINT NOT NULL COMMENT '派送操作主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', + operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', operator_id VARCHAR(128) NULL COMMENT '操作人ID', operator_name VARCHAR(128) NULL COMMENT '操作人名称', + result VARCHAR(500) NOT NULL COMMENT '处理结果', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', + PRIMARY KEY(id), KEY idx_ll_delivery_operation_order(customer_id,delivery_order_id,created_at,id) +) ENGINE=InnoDB COMMENT='派送单操作记录'; + CREATE TABLE IF NOT EXISTS ll_event_conflict ( id BIGINT NOT NULL COMMENT '冲突记录主键', event_id VARCHAR(128) NOT NULL COMMENT '冲突事件 ID', customer_id BIGINT NOT NULL COMMENT '租户 ID', conflict_type VARCHAR(64) NOT NULL COMMENT '冲突类型', @@ -205,6 +259,10 @@ SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE PREPARE lastleg_order_container_progress_idx_stmt FROM @ddl; EXECUTE lastleg_order_container_progress_idx_stmt; DEALLOCATE PREPARE lastleg_order_container_progress_idx_stmt; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='destination_type'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN destination_type VARCHAR(32) NULL COMMENT ''目的类型:FBA按箱数接收、OVERSEAS逐箱扫码'' AFTER order_type')); PREPARE lastleg_order_destination_type_stmt FROM @ddl; EXECUTE lastleg_order_destination_type_stmt; DEALLOCATE PREPARE lastleg_order_destination_type_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='source_warehouse_id'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN source_warehouse_id VARCHAR(128) NULL COMMENT ''来源国内装柜仓 ID 快照'' AFTER customer_name')); +PREPARE lastleg_order_source_warehouse_stmt FROM @ddl; EXECUTE lastleg_order_source_warehouse_stmt; DEALLOCATE PREPARE lastleg_order_source_warehouse_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='source_warehouse_name'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN source_warehouse_name VARCHAR(255) NULL COMMENT ''来源国内装柜仓名称快照'' AFTER source_warehouse_id')); +PREPARE lastleg_order_source_warehouse_stmt FROM @ddl; EXECUTE lastleg_order_source_warehouse_stmt; DEALLOCATE PREPARE lastleg_order_source_warehouse_stmt; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inbound_order' AND column_name='destination_type'), 'SELECT 1', 'ALTER TABLE ll_inbound_order ADD COLUMN destination_type VARCHAR(32) NULL COMMENT ''目的类型:FBA按箱数接收、OVERSEAS逐箱扫码'' AFTER destination_warehouse_name')); PREPARE lastleg_inbound_destination_type_stmt FROM @ddl; EXECUTE lastleg_inbound_destination_type_stmt; DEALLOCATE PREPARE lastleg_inbound_destination_type_stmt; ALTER TABLE ll_order_detail COMMENT='尾程订单实装商品冻结明细'; @@ -389,6 +447,7 @@ CREATE TABLE IF NOT EXISTS ll_inbound_plan ( total_volume_cbm DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT '集装箱总体积(立方米)', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + version INT NOT NULL DEFAULT 0 COMMENT '入库计划乐观锁版本', PRIMARY KEY (id), UNIQUE KEY uk_ll_inbound_plan_no (plan_no), UNIQUE KEY uk_ll_inbound_plan_source_order (customer_id, source_order_id), @@ -485,7 +544,6 @@ ALTER TABLE ll_inbound_plan_line MODIFY COLUMN plan_id BIGINT NOT NULL COMMENT '入库计划ID', MODIFY COLUMN order_id BIGINT NOT NULL COMMENT '尾程订单ID', MODIFY COLUMN inbound_code VARCHAR(128) NOT NULL COMMENT '头程入仓码快照', - MODIFY COLUMN destination_type VARCHAR(32) NOT NULL DEFAULT 'OVERSEAS' COMMENT '目的类型:FBA按箱数接收、OVERSEAS逐箱扫码', MODIFY COLUMN destination_warehouse_id VARCHAR(128) NULL COMMENT '目的仓ID快照', MODIFY COLUMN destination_warehouse_code VARCHAR(128) NULL COMMENT '目的仓编码快照', MODIFY COLUMN destination_warehouse_name VARCHAR(255) NULL COMMENT '目的仓名称快照', @@ -687,19 +745,17 @@ UPDATE ll_inbound_plan_pallet_allocation a JOIN ll_inbound_plan p ON p.id=a.plan ALTER TABLE ll_inbound_plan_pallet_allocation MODIFY COLUMN customer_id VARCHAR(128) NOT NULL COMMENT '客户账号标识'; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name='ll_inbound_plan_pallet_allocation' AND index_name='idx_ll_inbound_allocation_customer_plan'), 'SELECT 1', 'ALTER TABLE ll_inbound_plan_pallet_allocation ADD KEY idx_ll_inbound_allocation_customer_plan(customer_id,plan_id)')); PREPARE inbound_customer_scope_stmt FROM @ddl; EXECUTE inbound_customer_scope_stmt; DEALLOCATE PREPARE inbound_customer_scope_stmt; -DROP PROCEDURE IF EXISTS assert_inbound_plan_customer_scope_ready; -DELIMITER $$ -CREATE PROCEDURE assert_inbound_plan_customer_scope_ready() -BEGIN - IF EXISTS (SELECT 1 FROM ll_inbound_plan_line l LEFT JOIN ll_inbound_plan p ON p.id=l.plan_id WHERE l.customer_id IS NULL OR l.customer_id='' OR p.id IS NULL OR l.customer_id<>p.customer_id) - OR EXISTS (SELECT 1 FROM ll_inbound_plan_pallet v LEFT JOIN ll_inbound_plan p ON p.id=v.plan_id WHERE v.customer_id IS NULL OR v.customer_id='' OR p.id IS NULL OR v.customer_id<>p.customer_id) - OR EXISTS (SELECT 1 FROM ll_inbound_plan_pallet_allocation a LEFT JOIN ll_inbound_plan p ON p.id=a.plan_id WHERE a.customer_id IS NULL OR a.customer_id='' OR p.id IS NULL OR a.customer_id<>p.customer_id) THEN - SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Inbound plan child customer scope migration is incomplete'; - END IF; -END$$ -DELIMITER ; -CALL assert_inbound_plan_customer_scope_ready(); -DROP PROCEDURE assert_inbound_plan_customer_scope_ready; +DROP TEMPORARY TABLE IF EXISTS inbound_plan_customer_scope_assertion; +CREATE TEMPORARY TABLE inbound_plan_customer_scope_assertion ( + is_valid TINYINT NOT NULL, + CONSTRAINT chk_inbound_plan_customer_scope_ready CHECK (is_valid=1) +); +INSERT INTO inbound_plan_customer_scope_assertion(is_valid) +SELECT 0 +WHERE EXISTS (SELECT 1 FROM ll_inbound_plan_line l LEFT JOIN ll_inbound_plan p ON p.id=l.plan_id WHERE l.customer_id IS NULL OR l.customer_id='' OR p.id IS NULL OR l.customer_id<>p.customer_id) + OR EXISTS (SELECT 1 FROM ll_inbound_plan_pallet v LEFT JOIN ll_inbound_plan p ON p.id=v.plan_id WHERE v.customer_id IS NULL OR v.customer_id='' OR p.id IS NULL OR v.customer_id<>p.customer_id) + OR EXISTS (SELECT 1 FROM ll_inbound_plan_pallet_allocation a LEFT JOIN ll_inbound_plan p ON p.id=a.plan_id WHERE a.customer_id IS NULL OR a.customer_id='' OR p.id IS NULL OR a.customer_id<>p.customer_id); +DROP TEMPORARY TABLE inbound_plan_customer_scope_assertion; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inbound_plan_line' AND column_name='destination_type'), 'ALTER TABLE ll_inbound_plan_line MODIFY COLUMN destination_type VARCHAR(32) NOT NULL DEFAULT ''OVERSEAS'' COMMENT ''目的类型:FBA按箱数接收、OVERSEAS逐箱扫码''', 'ALTER TABLE ll_inbound_plan_line ADD COLUMN destination_type VARCHAR(32) NOT NULL DEFAULT ''OVERSEAS'' COMMENT ''目的类型:FBA按箱数接收、OVERSEAS逐箱扫码'' AFTER inbound_code')); PREPARE receiving_container_line_stmt FROM @ddl; EXECUTE receiving_container_line_stmt; DEALLOCATE PREPARE receiving_container_line_stmt; UPDATE ll_inbound_plan_line l JOIN ll_order o ON o.id=l.order_id @@ -1115,11 +1171,13 @@ CREATE TABLE IF NOT EXISTS ll_dispatch_pda_pick_order ( -- Transfer a dispatch to delivery: one outbound header, many pallet rows, one delivery header. CREATE TABLE IF NOT EXISTS ll_outbound_order ( id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户租户ID', - outbound_no VARCHAR(64) NOT NULL COMMENT '出库单号', dispatch_order_id BIGINT NOT NULL COMMENT '派车单ID', dispatch_no VARCHAR(64) NOT NULL COMMENT '派车单号', + outbound_no VARCHAR(64) NOT NULL COMMENT '出库单号', dispatch_order_id BIGINT NULL COMMENT '派车单ID,一件代发为空', dispatch_no VARCHAR(64) NULL COMMENT '派车单号,一件代发为空', overseas_warehouse_id VARCHAR(128) NULL COMMENT '海外仓ID', status VARCHAR(32) NOT NULL DEFAULT 'WAIT_OUTBOUND' COMMENT '业务状态', package_count INT NOT NULL DEFAULT 0 COMMENT '箱数', pallet_count INT NOT NULL DEFAULT 0 COMMENT '托盘数', idempotency_key VARCHAR(255) NOT NULL COMMENT '业务幂等键', created_by VARCHAR(128) NULL COMMENT '创建人ID', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + outbound_type VARCHAR(32) NOT NULL DEFAULT 'DISPATCH_DELIVERY' COMMENT '出库类型:派送转出库、一件代发', + recipient_info VARCHAR(1000) NULL COMMENT '一件代发收件信息', PRIMARY KEY (id), UNIQUE KEY uk_ll_outbound_no (customer_id,outbound_no), UNIQUE KEY uk_ll_outbound_idempotency (customer_id,idempotency_key), UNIQUE KEY uk_ll_outbound_dispatch (customer_id,dispatch_order_id), KEY idx_ll_outbound_list (customer_id,status,created_at) @@ -1129,12 +1187,39 @@ CREATE TABLE IF NOT EXISTS ll_outbound_pallet_item ( id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户租户ID', outbound_order_id BIGINT NOT NULL COMMENT '出库单ID', warehouse_pallet_id BIGINT NULL COMMENT '仓库实物托盘ID', pallet_no VARCHAR(128) NULL COMMENT '托盘号', destination_warehouse_id VARCHAR(128) NULL COMMENT '目的仓ID', destination_warehouse_code VARCHAR(128) NULL COMMENT '目的仓编码快照', destination_warehouse_name VARCHAR(255) NULL COMMENT '目的仓名称快照', - package_count INT NOT NULL DEFAULT 0 COMMENT '箱数', order_refs VARCHAR(2000) NULL COMMENT '关联订单信息', inbound_code VARCHAR(128) NULL COMMENT '入仓单号快照', + package_count INT NOT NULL DEFAULT 0 COMMENT '箱数', picked_package_count INT NOT NULL DEFAULT 0 COMMENT '已拣箱数', status VARCHAR(32) NOT NULL DEFAULT 'WAIT_PICK' COMMENT '拣货状态', sku_code VARCHAR(128) NULL COMMENT '一件代发SKU', order_refs VARCHAR(2000) NULL COMMENT '关联订单信息', inbound_code VARCHAR(128) NULL COMMENT '入仓单号快照', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', PRIMARY KEY (id), UNIQUE KEY uk_ll_outbound_pallet (customer_id,outbound_order_id,warehouse_pallet_id), KEY idx_ll_outbound_pallet_list (customer_id,outbound_order_id), KEY idx_ll_outbound_pallet_destination (customer_id,destination_warehouse_code) ) ENGINE=InnoDB COMMENT='出库托盘明细'; +CREATE TABLE IF NOT EXISTS ll_outbound_pda_task ( + id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户租户ID', outbound_order_id BIGINT NOT NULL COMMENT '出库单ID', + task_no VARCHAR(64) NOT NULL COMMENT 'PDA任务号', status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态:待拣货、拣货中、已完成', + pushed_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '推送时间', completed_at DATETIME(3) NULL COMMENT '完成时间', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_outbound_pda_task_order(customer_id,outbound_order_id), UNIQUE KEY uk_ll_outbound_pda_task_no(customer_id,task_no) +) ENGINE=InnoDB COMMENT='一件代发出库PDA拣货任务'; + +CREATE TABLE IF NOT EXISTS ll_outbound_pda_pick ( + id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', task_id BIGINT NOT NULL COMMENT 'PDA任务ID', outbound_pallet_item_id BIGINT NOT NULL COMMENT '出库托盘明细ID', + inventory_position_id BIGINT NOT NULL COMMENT '库存仓位ID', packages BIGINT NOT NULL COMMENT '拣货箱数', idempotency_key VARCHAR(255) NOT NULL COMMENT '拣货幂等键', + picked_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '拣货时间', PRIMARY KEY(id), UNIQUE KEY uk_ll_outbound_pda_pick_key(customer_id,idempotency_key) +) ENGINE=InnoDB COMMENT='一件代发出库PDA托盘拣货记录'; + +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_order' AND column_name='outbound_type'), 'SELECT 1', 'ALTER TABLE ll_outbound_order ADD COLUMN outbound_type VARCHAR(32) NOT NULL DEFAULT ''DISPATCH_DELIVERY'' COMMENT ''出库类型'' AFTER status')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='sku_code'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN sku_code VARCHAR(128) NULL COMMENT ''一件代发SKU''')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_order' AND column_name='recipient_info'), 'SELECT 1', 'ALTER TABLE ll_outbound_order ADD COLUMN recipient_info VARCHAR(1000) NULL COMMENT ''一件代发收件信息'' AFTER outbound_type')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='picked_package_count'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN picked_package_count INT NOT NULL DEFAULT 0 COMMENT ''已拣箱数'' AFTER package_count')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='status'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN status VARCHAR(32) NOT NULL DEFAULT ''WAIT_PICK'' COMMENT ''拣货状态'' AFTER picked_package_count')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_order' AND column_name='dispatch_order_id' AND is_nullable='YES'), 'SELECT 1', 'ALTER TABLE ll_outbound_order MODIFY COLUMN dispatch_order_id BIGINT NULL COMMENT ''派车单ID,一件代发为空'', MODIFY COLUMN dispatch_no VARCHAR(64) NULL COMMENT ''派车单号,一件代发为空''')); +PREPARE outbound_upgrade_stmt FROM @ddl; EXECUTE outbound_upgrade_stmt; DEALLOCATE PREPARE outbound_upgrade_stmt; + CREATE TABLE IF NOT EXISTS ll_delivery_order ( id BIGINT NOT NULL COMMENT '主键ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户租户ID', delivery_no VARCHAR(64) NOT NULL COMMENT '派送单号', outbound_order_id BIGINT NOT NULL COMMENT '出库单ID', outbound_no VARCHAR(64) NOT NULL COMMENT '出库单号', @@ -1488,7 +1573,7 @@ CREATE TABLE IF NOT EXISTS ll_overseas_procurement_order ( purchase_quantity BIGINT NOT NULL COMMENT '采购数量', budget_unit_price DECIMAL(18,2) NULL COMMENT '预算单价', currency CHAR(3) NOT NULL DEFAULT 'USD' COMMENT '币种', receiving_address VARCHAR(1000) NULL COMMENT '收货地址', contact_name VARCHAR(128) NOT NULL COMMENT '联系人姓名', contact_phone VARCHAR(64) NOT NULL COMMENT '联系人电话', - purchase_warehouse_id VARCHAR(128) NULL COMMENT '采购入仓仓库ID', purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', + purchase_warehouse_id VARCHAR(128) NULL COMMENT '采购入仓仓库ID', purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', outbound_details JSON NULL COMMENT '出库明细', inbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '采购单据累计入库数量', domestic_carrier VARCHAR(128) NULL COMMENT '境内承运商', domestic_tracking_no VARCHAR(128) NULL COMMENT '境内物流单号', overseas_carrier VARCHAR(128) NULL COMMENT '海外承运商', overseas_tracking_no VARCHAR(128) NULL COMMENT '海外物流单号', @@ -1637,6 +1722,52 @@ CREATE TABLE IF NOT EXISTS ll_local_looseboard_operation ( KEY idx_ll_local_looseboard_operation_order(customer_id,order_id,created_at) ) ENGINE=InnoDB COMMENT='本土散板订单幂等操作账'; +CREATE TABLE IF NOT EXISTS ll_exception_order ( + id BIGINT NOT NULL COMMENT '异常订单ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', exception_no VARCHAR(64) NOT NULL COMMENT '异常订单号', + exception_type VARCHAR(32) NOT NULL COMMENT '异常类型', status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '异常处理状态', reason VARCHAR(1000) NOT NULL COMMENT '异常原因', + last_mile_order_id BIGINT NULL COMMENT '关联尾程订单ID', last_mile_order_no VARCHAR(64) NULL COMMENT '关联尾程订单号', inbound_order_id BIGINT NULL COMMENT '关联入仓单ID', inbound_code VARCHAR(128) NULL COMMENT '关联入仓码', + delivery_order_id BIGINT NULL COMMENT '关联派送单ID', delivery_no VARCHAR(64) NULL COMMENT '关联派送单号', outbound_no VARCHAR(64) NULL COMMENT '关联出库单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', overseas_warehouse_id VARCHAR(128) NULL COMMENT '海外仓ID快照', overseas_warehouse_name VARCHAR(255) NULL COMMENT '海外仓名称快照', + sku_code VARCHAR(128) NULL COMMENT '异常货物SKU', package_count INT NULL COMMENT '异常箱数', return_direction VARCHAR(32) NULL COMMENT '退回方向', return_logistics_no VARCHAR(128) NULL COMMENT '退回物流单号', + handling_action VARCHAR(500) NULL COMMENT '异常处理措施', handler_id VARCHAR(128) NULL COMMENT '处理人ID', handler_name VARCHAR(128) NULL COMMENT '处理人名称', + source_type VARCHAR(32) NOT NULL DEFAULT 'MANUAL' COMMENT '异常来源类型', created_by VARCHAR(128) NULL COMMENT '创建人ID', + exception_at DATETIME(3) NOT NULL COMMENT '异常发生时间', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_exception_no(customer_id,exception_no), + UNIQUE KEY uk_ll_exception_delivery_return(customer_id,delivery_order_id,exception_type), + KEY idx_ll_exception_list(customer_id,status,exception_at), KEY idx_ll_exception_tail(customer_id,last_mile_order_no), + KEY idx_ll_exception_inbound(customer_id,inbound_code) +) ENGINE=InnoDB COMMENT='海外仓异常订单'; + +CREATE TABLE IF NOT EXISTS ll_customer_visit ( + id BIGINT NOT NULL COMMENT '客户回访记录ID', + customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + delivery_order_id BIGINT NOT NULL COMMENT '关联派送单ID', + delivery_no VARCHAR(64) NOT NULL COMMENT '关联派送单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', + contact_name VARCHAR(128) NULL COMMENT '客户联系人姓名快照', + contact_phone VARCHAR(64) NULL COMMENT '客户联系人电话快照', + destination VARCHAR(500) NULL COMMENT '派送目的仓编码汇总快照', + delivery_completed_at DATETIME(3) NOT NULL COMMENT '派送完成时间', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '回访状态:PENDING待回访或COMPLETED已回访', + selected_last_mile_order_id BIGINT NULL COMMENT '本次回访选择的尾程订单ID', + selected_last_mile_order_no VARCHAR(64) NULL COMMENT '本次回访选择的尾程订单号', + selected_inbound_order_id BIGINT NULL COMMENT '本次回访选择的入仓单ID', + selected_inbound_code VARCHAR(128) NULL COMMENT '本次回访选择的入仓码', + visit_result VARCHAR(32) NULL COMMENT '最近一次回访结果', + customer_feedback VARCHAR(2000) NULL COMMENT '最近一次客户反馈', + remark VARCHAR(1000) NULL COMMENT '最近一次回访备注', + attachment_json JSON NULL COMMENT '当前回访资料文件元数据列表', + visited_by VARCHAR(128) NULL COMMENT '最近一次回访操作人ID', + visited_at DATETIME(3) NULL COMMENT '最近一次回访时间', + visit_count INT NOT NULL DEFAULT 0 COMMENT '累计保存回访次数', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), + UNIQUE KEY uk_ll_customer_visit_delivery(customer_id,delivery_order_id), + KEY idx_ll_customer_visit_list(customer_id,status,delivery_completed_at), + KEY idx_ll_customer_visit_delivery_no(customer_id,delivery_no) +) ENGINE=InnoDB COMMENT='派送单维度客户回访记录'; CREATE TABLE IF NOT EXISTS ll_security_event_inbox ( event_id VARCHAR(64) NOT NULL COMMENT '跨区安全事件ID', event_type VARCHAR(96) NOT NULL COMMENT 'IAM安全事件类型', diff --git a/lastleg-service/src/main/resources/openapi/overseas-business-v1.yaml b/lastleg-service/src/main/resources/openapi/overseas-business-v1.yaml index 4749a8111..d1ffa4091 100644 --- a/lastleg-service/src/main/resources/openapi/overseas-business-v1.yaml +++ b/lastleg-service/src/main/resources/openapi/overseas-business-v1.yaml @@ -167,6 +167,89 @@ paths: - { name: createdFrom, in: query, schema: { type: string, format: date-time } } - { name: createdTo, in: query, schema: { type: string, format: date-time } } responses: { '200': { description: 统一 ApiResponse 包装的尾程订单分页;列表行 destinationType 为 FBA 或 OVERSEAS } } + /api/platform/v1/last-leg/orders/{id}: + get: + summary: 查询尾程订单详情 + description: 权限 platform:overseas-warehouse:last-mile-order:list;返回订单、货物、状态历史、费用明细和操作记录。 + security: [{ bearerAuth: [] }] + parameters: + - { name: id, in: path, required: true, schema: { type: integer, format: int64 } } + - { name: customerId, in: query, required: false, schema: { type: string, pattern: '^[1-9][0-9]*$' } } + responses: + '200': { description: 统一 ApiResponse 包装的尾程订单详情 } + '404': { description: 尾程订单不存在或不属于查询客户 } + /api/platform/v1/last-leg/orders/{id}/fees: + post: + summary: 新增尾程订单费用 + description: 权限 platform:overseas-warehouse:last-mile-order:edit;在订单客户范围内新增费用并记录操作人和处理结果。 + security: [{ bearerAuth: [] }] + parameters: + - { name: id, in: path, required: true, schema: { type: integer, format: int64 } } + - { name: customerId, in: query, required: false, schema: { type: string, pattern: '^[1-9][0-9]*$' } } + requestBody: + required: true + content: { application/json: { schema: { $ref: '#/components/schemas/LastMileFeeCreate' } } } + responses: + '200': { description: 新增后的费用明细, content: { application/json: { schema: { $ref: '#/components/schemas/LastMileFeeApiResponse' } } } } + '404': { description: 尾程订单不存在或不属于操作客户 } + /api/platform/v1/last-leg/orders/{id}/detained: + put: + summary: 设置或解除尾程订单暂扣 + description: 权限 platform:overseas-warehouse:last-mile-order:edit;库存同步转入或释放暂扣,并通过跨区事件回写国内入仓单。 + security: [{ bearerAuth: [] }] + parameters: + - { name: id, in: path, required: true, schema: { type: integer, format: int64 } } + - { name: customerId, in: query, required: false, schema: { type: string, pattern: '^[1-9][0-9]*$' } } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [detained, remark] + properties: + detained: { type: boolean } + remark: { type: string, minLength: 1, maxLength: 500 } + responses: { '200': { description: 暂扣状态已更新 } } + /api/platform/v1/last-leg/orders/{id}/pallet-merge-required: + put: + summary: 设置尾程订单合托标记 + description: 权限 platform:overseas-warehouse:last-mile-order:edit;标记后 PDA 扫描本单箱唛时返回合托提示;实际完成合托后不可取消。 + security: [{ bearerAuth: [] }] + parameters: + - { name: id, in: path, required: true, schema: { type: integer, format: int64 } } + - { name: customerId, in: query, required: false, schema: { type: string, pattern: '^[1-9][0-9]*$' } } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [palletMergeRequired] + properties: { palletMergeRequired: { type: boolean } } + responses: { '200': { description: 合托标记已更新 }, '409': { description: 已完成实际合托,不能修改标记 } } + /api/platform/v1/last-leg/orders/pallet-merge-required/batch: + put: + summary: 批量设置同组合托标记 + description: 权限 platform:overseas-warehouse:last-mile-order:edit;所选订单按同一合托批次原子标记或取消。 + security: [{ bearerAuth: [] }] + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [orderIds, palletMergeRequired] + properties: + orderIds: { type: array, minItems: 1, uniqueItems: true, items: { type: integer, format: int64 } } + palletMergeRequired: { type: boolean } + responses: { '200': { description: 批量合托标记已更新 } } + /api/platform/v1/last-leg/orders/export: + get: + summary: 导出筛选后的尾程订单 + description: 权限 platform:overseas-warehouse:last-mile-order:export;筛选参数与列表一致,返回 UTF-8 CSV。 + security: [{ bearerAuth: [] }] + responses: { '200': { description: CSV 文件, content: { text/csv: { schema: { type: string, format: binary } } } } } /api/platform/v1/last-leg/orders/container-summaries: get: summary: 分页查询柜级入库计划进度 @@ -762,6 +845,37 @@ components: planStatus: { type: string, nullable: true } planValid: { type: boolean } plannedArrivalAt: { type: string, format: date-time, nullable: true, description: ll_order.estimated_warehouse_at } + LastMileFeeCreate: + type: object + required: [feeItem, currency, amount, status] + properties: + feeItem: { type: string, minLength: 1, maxLength: 128 } + currency: { type: string, pattern: '^[A-Z]{3}$', example: USD } + amount: { type: number, format: decimal, minimum: 0.01 } + status: { type: string, enum: [PENDING_REVIEW, APPROVED, REJECTED] } + supplierDescription: { type: string, maxLength: 500 } + LastMileFee: + type: object + required: [id, customerId, orderId, feeItem, currency, amount, status, createdBy, createdAt, updatedAt] + properties: + id: { type: integer, format: int64 } + customerId: { type: string } + orderId: { type: integer, format: int64 } + feeItem: { type: string } + currency: { type: string } + amount: { type: number, format: decimal } + status: { type: string } + supplierDescription: { type: string, nullable: true } + createdBy: { type: string } + createdAt: { type: string, format: date-time } + updatedAt: { type: string, format: date-time } + LastMileFeeApiResponse: + type: object + required: [code, message, data] + properties: + code: { type: string } + message: { type: string } + data: { $ref: '#/components/schemas/LastMileFee' } PdaWarehouseScan: type: object required: [code] @@ -906,7 +1020,7 @@ components: timestamp: { type: string, format: date-time } ReceivingInbound: type: object - required: [inboundCode, destinationType, plannedPalletCount, totalPackages, receivedPackages, pendingPackages] + required: [inboundCode, destinationType, plannedPalletCount, totalPackages, receivedPackages, pendingPackages, palletMergeRequired] properties: inboundCode: { type: string } destinationType: { type: string, enum: [FBA, OVERSEAS] } @@ -920,6 +1034,8 @@ components: totalPackages: { type: integer } receivedPackages: { type: integer } pendingPackages: { type: integer } + palletMergeRequired: { type: boolean, description: 尾程订单是否要求PDA合托提醒 } + palletMergeHint: { type: string, nullable: true, description: 标记合托时供PDA直接展示的中文提示 } ReceivingWarehouseProfile: type: object required: [warehouseId, warehouseCode, warehouseName] @@ -996,7 +1112,7 @@ components: compatible: { type: boolean } ReceivingReceipt: type: object - required: [result, containerNo, inboundCode, destinationType, palletCode, acceptedPackages, receivedPackages, pendingPackages, sessionStatus] + required: [result, containerNo, inboundCode, destinationType, palletCode, acceptedPackages, receivedPackages, pendingPackages, sessionStatus, palletMergeRequired] properties: result: { type: string } containerNo: { type: string } @@ -1007,6 +1123,8 @@ components: receivedPackages: { type: integer } pendingPackages: { type: integer } sessionStatus: { type: string, enum: [SIGNED, RECEIVING] } + palletMergeRequired: { type: boolean } + palletMergeHint: { type: string, nullable: true } ReceivingCandidateListApiResponse: allOf: - { $ref: '#/components/schemas/ReceivingApiResponseBase' } @@ -1256,6 +1374,19 @@ components: overseasTrackingNo: { $ref: '#/components/schemas/NullableTextPatch' } estimatedArrivalAt: { type: string, format: date-time, nullable: true, description: 缺失不修改;null 或空串清空 } remark: { $ref: '#/components/schemas/NullableTextPatch' } + outboundDetails: + type: array + description: 出库明细,可一次提交多条;总出库量不得超过入库量 + items: + type: object + required: [quantity] + properties: + id: { type: string } + quantity: { type: integer, format: int64, minimum: 0 } + carrier: { type: string, nullable: true, maxLength: 128 } + trackingNo: { type: string, nullable: true, maxLength: 128 } + shippedAt: { type: string, format: date-time, nullable: true } + remark: { type: string, nullable: true, maxLength: 500 } CustomerOptionResponse: type: object properties: diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/LastlegOrderControllerTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/LastlegOrderControllerTest.java index 47d14efe8..1ea3eec8a 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/LastlegOrderControllerTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/LastlegOrderControllerTest.java @@ -18,6 +18,8 @@ import org.springframework.web.server.ResponseStatusException; import java.util.Set; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; @@ -31,7 +33,7 @@ class LastlegOrderControllerTest { private final LastlegOrderService service = mock(LastlegOrderService.class); private final LastlegOrderController controller = controller(); private final LastlegOrderRequests.Query query = new LastlegOrderRequests.Query( - 1, 20, null, null, null, null, null, null, null, null, null, null, null, null, null); + 1L, 20L, null, null, null, null, null, null, null, null, null, null, null, null, null); @Test void platformEndpointRejectsMerchantIdentity() { @@ -76,6 +78,32 @@ class LastlegOrderControllerTest { verify(service).containerProgressDetail("BOOKING-1"); } + @Test + void exportBuildsCsvBeforeReturningTheResponse() { + byte[] csv = "csv".getBytes(java.nio.charset.StandardCharsets.UTF_8); + when(service.exportCsv("200", query)).thenReturn(csv); + + var response = controller.export(user(0L, "PLATFORM"), "200", query); + + assertArrayEquals(csv, response.getBody()); + verify(service).exportCsv("200", query); + } + + @Test + void platformOperatorCanExportAllCustomersWithoutFilters() { + byte[] csv = "all customers".getBytes(java.nio.charset.StandardCharsets.UTF_8); + LastlegOrderRequests.Query emptyQuery = new LastlegOrderRequests.Query( + null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); + when(service.exportCsv(null, emptyQuery)).thenReturn(csv); + + var response = controller.export(user(0L, "PLATFORM"), null, emptyQuery); + + assertEquals(Long.valueOf(1), emptyQuery.page()); + assertEquals(Long.valueOf(20), emptyQuery.size()); + assertArrayEquals(csv, response.getBody()); + verify(service).exportCsv(isNull(), eq(emptyQuery)); + } + @Test void iamSecurityEventsAreRejectedBeforeLoadingCompletedConsumer() { EventEnvelope event = mock(EventEnvelope.class); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/MerchantLastlegOrderControllerTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/MerchantLastlegOrderControllerTest.java index c21728b09..f5dabbcb8 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/MerchantLastlegOrderControllerTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/MerchantLastlegOrderControllerTest.java @@ -18,7 +18,7 @@ class MerchantLastlegOrderControllerTest { private final LastlegOrderService service = mock(LastlegOrderService.class); private final MerchantLastlegOrderController controller = controller(); private final LastlegOrderRequests.Query query = new LastlegOrderRequests.Query( - 1, 20, null, null, null, null, null, null, null, null, null, null, null, null, null); + 1L, 20L, null, null, null, null, null, null, null, null, null, null, null, null, null); @Test void merchantListUsesOnlyAuthenticatedTenant() { diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/Phase4BrowserApiBoundaryTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/Phase4BrowserApiBoundaryTest.java index 850071823..f7b89b42b 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/controller/Phase4BrowserApiBoundaryTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/controller/Phase4BrowserApiBoundaryTest.java @@ -1,10 +1,13 @@ package com.baiyue.lastleg.controller; import com.baiyue.lastleg.delivery.controller.DeliveryTransferController; +import com.baiyue.lastleg.delivery.controller.OutboundPdaController; import com.baiyue.lastleg.dispatch.controller.DispatchController; import com.baiyue.lastleg.dispatch.controller.DispatchQueryController; import com.baiyue.lastleg.dispatch.controller.SmartDispatchController; import com.baiyue.lastleg.dispatch.pda.controller.DispatchPdaController; +import com.baiyue.lastleg.customervisit.controller.CustomerVisitController; +import com.baiyue.lastleg.exceptionorder.controller.ExceptionOrderController; import com.baiyue.lastleg.inboundplan.pda.controller.ReceivingContainerController; import com.baiyue.lastleg.inventory.command.controller.InventoryCommandController; import org.junit.jupiter.api.Test; @@ -39,14 +42,12 @@ class Phase4BrowserApiBoundaryTest { } @Test - void excludedOutboundControllersAreDisabledUnlessExplicitlyEnabledLater() { + void outboundControllersAreNotPropertyGated() { for (Class type : new Class[] { - DispatchController.class, DeliveryTransferController.class, DispatchPdaController.class + DispatchController.class, DeliveryTransferController.class, DispatchPdaController.class, + OutboundPdaController.class, CustomerVisitController.class, ExceptionOrderController.class }) { - ConditionalOnProperty condition = type.getAnnotation(ConditionalOnProperty.class); - assertTrue(condition != null, type.getSimpleName()); - assertTrue(Arrays.asList(condition.name()).contains("lastleg.future-outbound-api.enabled")); - assertFalse(condition.matchIfMissing()); + assertFalse(type.isAnnotationPresent(ConditionalOnProperty.class), type.getSimpleName()); } } @@ -65,11 +66,10 @@ class Phase4BrowserApiBoundaryTest { } @Test - void dispatchQueryIsEnabledByDefaultWithoutEnablingOutboundCommands() throws Exception { + void dispatchQueryAndSmartDispatchAreEnabledByDefault() throws Exception { String yaml = Files.readString(Path.of("src/main/resources/application.yml")); assertTrue(yaml.contains("enabled: ${LASTLEG_DISPATCH_QUERY_API_ENABLED:true}")); assertTrue(yaml.contains("enabled: ${LASTLEG_SMART_DISPATCH_API_ENABLED:true}")); - assertFalse(yaml.contains("future-outbound-api:")); } @Test diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/DeliveryLongIdJsonContractTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/DeliveryLongIdJsonContractTest.java new file mode 100644 index 000000000..fd4e31675 --- /dev/null +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/DeliveryLongIdJsonContractTest.java @@ -0,0 +1,71 @@ +package com.baiyue.lastleg.delivery; + +import com.baiyue.common.web.LongIdJacksonConfiguration; +import com.baiyue.lastleg.delivery.dto.DeliveryRequests; +import com.baiyue.lastleg.delivery.dto.DeliveryResponses; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("派送单长整型 ID JSON 契约") +class DeliveryLongIdJsonContractTest { + + private static final long DELIVERY_ID = 19_000_000_000_000_001L; + private static final long FEE_ID = 19_000_000_000_000_002L; + private static final long OPERATION_ID = 19_000_000_000_000_003L; + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + JacksonAutoConfiguration.class, LongIdJacksonConfiguration.class)); + + @Test + void deliveryResponseIdsShouldBeSerializedAsJsonStrings() { + contextRunner.run(context -> { + ObjectMapper objectMapper = context.getBean(ObjectMapper.class); + DeliveryResponses.DeliveryDetail detail = new DeliveryResponses.DeliveryDetail( + deliveryRow(), + List.of(), + List.of(new DeliveryResponses.DeliveryFeeRow( + FEE_ID, "RECEIVABLE", "DELIVERY_FEE", "USD", null, null)), + List.of(new DeliveryResponses.DeliveryOperationRow( + OPERATION_ID, null, "CREATED", "tester", "success"))); + + JsonNode json = objectMapper.readTree(objectMapper.writeValueAsString(detail)); + + assertThat(json.at("/order/id").isTextual()).isTrue(); + assertThat(json.at("/order/id").textValue()).isEqualTo(Long.toString(DELIVERY_ID)); + assertThat(json.at("/fees/0/id").textValue()).isEqualTo(Long.toString(FEE_ID)); + assertThat(json.at("/operations/0/id").textValue()).isEqualTo(Long.toString(OPERATION_ID)); + }); + } + + @Test + void quotedRequestIdShouldBeDeserializedToLong() { + contextRunner.run(context -> { + ObjectMapper objectMapper = context.getBean(ObjectMapper.class); + + DeliveryRequests.OneClickItem item = objectMapper.readValue( + "{\"inventoryPositionId\":\"19000000000000001\",\"packages\":\"2\",\"sku\":\"SKU-1\"}", + DeliveryRequests.OneClickItem.class); + + assertThat(item.inventoryPositionId()).isEqualTo(DELIVERY_ID); + assertThat(item.packages()).isEqualTo(2L); + }); + } + + private DeliveryResponses.DeliveryRow deliveryRow() { + return new DeliveryResponses.DeliveryRow( + DELIVERY_ID, + null, null, "PS-1", null, null, null, null, "CREATED", null, + null, null, null, null, null, null, null, null, null, null, null, + 0, 0, null, null, null, null, null, null, null, null); + } +} diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/service/DeliveryTransferServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/service/DeliveryTransferServiceTest.java index 29cbef8a2..17703c2da 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/service/DeliveryTransferServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/delivery/service/DeliveryTransferServiceTest.java @@ -7,6 +7,8 @@ import com.baiyue.lastleg.delivery.entity.DeliveryOrder; import com.baiyue.lastleg.delivery.entity.OutboundOrder; import com.baiyue.lastleg.delivery.entity.OutboundPalletItem; import com.baiyue.lastleg.delivery.mapper.DeliveryOrderMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryFeeMapper; +import com.baiyue.lastleg.delivery.mapper.DeliveryOperationMapper; import com.baiyue.lastleg.delivery.mapper.OutboundOrderMapper; import com.baiyue.lastleg.delivery.mapper.OutboundPalletItemMapper; import com.baiyue.lastleg.dispatch.entity.DispatchItem; @@ -19,6 +21,8 @@ import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; +import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; +import com.baiyue.lastleg.service.LastlegOrderNoGenerator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -28,6 +32,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.server.ResponseStatusException; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; @@ -47,10 +52,13 @@ class DeliveryTransferServiceTest { @Mock OutboundOrderMapper outboundOrders; @Mock OutboundPalletItemMapper outboundPallets; @Mock DeliveryOrderMapper deliveryOrders; + @Mock DeliveryFeeMapper deliveryFees; + @Mock DeliveryOperationMapper deliveryOperations; @Mock InboundPlanPalletAllocationMapper planAllocations; @Mock InboundPlanLineMapper planLines; @Mock LastlegOrderMapper tailOrders; @Mock LastlegOrderStatusHistoryMapper histories; + @Mock LastlegOrderDetailMapper tailOrderDetails; private DeliveryTransferService service; @@ -62,10 +70,14 @@ class DeliveryTransferServiceTest { ReflectionTestUtils.setField(service, "outboundOrders", outboundOrders); ReflectionTestUtils.setField(service, "outboundPallets", outboundPallets); ReflectionTestUtils.setField(service, "deliveryOrders", deliveryOrders); + ReflectionTestUtils.setField(service, "deliveryFees", deliveryFees); + ReflectionTestUtils.setField(service, "deliveryOperations", deliveryOperations); ReflectionTestUtils.setField(service, "planAllocations", planAllocations); ReflectionTestUtils.setField(service, "planLines", planLines); + ReflectionTestUtils.setField(service, "orderNoGenerator", new LastlegOrderNoGenerator()); ReflectionTestUtils.setField(service, "tailOrders", tailOrders); ReflectionTestUtils.setField(service, "histories", histories); + ReflectionTestUtils.setField(service, "tailOrderDetails", tailOrderDetails); } @Test @@ -79,7 +91,7 @@ class DeliveryTransferServiceTest { )); DeliveryResponses.TransferResult result = service.transfer("TENANT-A", "DSP-101", "U-1", - new DeliveryRequests.Transfer("transfer-101", "ready")); + transferRequest("transfer-101", "ready")); ArgumentCaptor outbound = ArgumentCaptor.forClass(OutboundOrder.class); ArgumentCaptor delivery = ArgumentCaptor.forClass(DeliveryOrder.class); @@ -92,10 +104,10 @@ class DeliveryTransferServiceTest { assertEquals(2, outbound.getValue().getPalletCount()); assertEquals(20, outbound.getValue().getPackageCount()); assertEquals("transfer-101", outbound.getValue().getIdempotencyKey()); - assertTrue(outbound.getValue().getOutboundNo().startsWith("OUT")); + assertTrue(outbound.getValue().getOutboundNo().matches("OB-WH-\\d{8}-\\d{6}")); assertEquals(outbound.getValue().getId(), delivery.getValue().getOutboundOrderId()); assertEquals("WAIT_DELIVERY", delivery.getValue().getStatus()); - assertTrue(delivery.getValue().getDeliveryNo().startsWith("DEL")); + assertTrue(delivery.getValue().getDeliveryNo().matches("DL-WH-\\d{8}-\\d{6}")); assertEquals(List.of("ONT8", "LAX9"), pallets.getAllValues().stream() .map(OutboundPalletItem::getDestinationWarehouseCode).toList()); assertEquals(List.of("IN-A", "IN-B"), pallets.getAllValues().stream() @@ -116,7 +128,7 @@ class DeliveryTransferServiceTest { when(deliveryOrders.lockByOutbound("TENANT-A", 301L)).thenReturn(delivery); DeliveryResponses.TransferResult result = service.transfer("TENANT-A", "DSP-101", "U-1", - new DeliveryRequests.Transfer(" transfer-101 ", null)); + transferRequest(" transfer-101 ", null)); assertTrue(result.replayed()); assertEquals("OUT-301", result.outboundNo()); @@ -135,7 +147,7 @@ class DeliveryTransferServiceTest { when(outboundOrders.findByIdempotency("TENANT-A", "shared-key")).thenReturn(other); ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> - service.transfer("TENANT-A", "DSP-102", "U-1", new DeliveryRequests.Transfer("shared-key", null))); + service.transfer("TENANT-A", "DSP-102", "U-1", transferRequest("shared-key", null))); assertEquals(409, exception.getStatusCode().value()); verify(outboundOrders, never()).insert((OutboundOrder) any()); @@ -155,7 +167,7 @@ class DeliveryTransferServiceTest { when(deliveryOrders.lockByOutbound("TENANT-A", 301L)).thenReturn(delivery); DeliveryResponses.TransferResult result = service.transfer("TENANT-A", "DSP-101", "U-1", - new DeliveryRequests.Transfer("transfer-101", null)); + transferRequest("transfer-101", null)); assertTrue(result.replayed()); assertEquals("OUT-301", result.outboundNo()); @@ -176,13 +188,15 @@ class DeliveryTransferServiceTest { when(dispatchItems.findByDispatchOrder("TENANT-A", 101L)).thenReturn(List.of(source)); when(tailOrders.selectList(any())).thenReturn(List.of(tailOrder)); - service.transfer("TENANT-A", "DSP-101", "U-1", new DeliveryRequests.Transfer("direct-order", null)); + service.transfer("TENANT-A", "DSP-101", "U-1", transferRequest("direct-order", null)); assertEquals("WAIT_OUTBOUND", tailOrder.getCurrentStatus()); assertNotNull(tailOrder.getOutboundOrderNo()); assertNotNull(tailOrder.getDeliveryOrderNo()); verify(tailOrders).updateById(tailOrder); - verify(histories).insert(any(LastlegOrderStatusHistory.class)); + ArgumentCaptor history = ArgumentCaptor.forClass(LastlegOrderStatusHistory.class); + verify(histories).insert(history.capture()); + assertEquals("TENANT-A", history.getValue().getCustomerId()); } @Test @@ -195,7 +209,7 @@ class DeliveryTransferServiceTest { when(dispatchItems.findByDispatchOrder("TENANT-A", 101L)).thenReturn(List.of(planned)); ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> - service.transfer("TENANT-A", "DSP-101", "U-1", new DeliveryRequests.Transfer("transfer-101", null))); + service.transfer("TENANT-A", "DSP-101", "U-1", transferRequest("transfer-101", null))); assertEquals(409, exception.getStatusCode().value()); assertTrue(exception.getReason().contains("received")); @@ -239,6 +253,12 @@ class DeliveryTransferServiceTest { return value; } + private static DeliveryRequests.Transfer transferRequest(String idempotencyKey, String remark) { + return new DeliveryRequests.Transfer(idempotencyKey, LocalDateTime.now(), "SELF_DELIVERY", null, null, + "ONE_PICKUP_ONE_DROP", "PALLET", null, null, null, null, null, null, null, remark, + List.of(new DeliveryRequests.FeeItem("DELIVERY_FEE", "USD", BigDecimal.ZERO, null))); + } + private static DispatchItem item(Long id, Long warehousePalletId, String palletNo, String destinationCode, String destinationName, String inboundCode, int packages) { DispatchItem value = new DispatchItem(); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/discard/service/CargoDiscardServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/discard/service/CargoDiscardServiceTest.java index ce9707e49..3fe43bd57 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/discard/service/CargoDiscardServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/discard/service/CargoDiscardServiceTest.java @@ -14,6 +14,7 @@ import com.baiyue.lastleg.dropship.entity.DropshipCarton; import com.baiyue.lastleg.dropship.entity.DropshipSkuInventory; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.mapper.LastlegOrderMapper; +import com.baiyue.lastleg.service.LastlegOrderNoGenerator; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -63,6 +64,7 @@ class CargoDiscardServiceTest { ReflectionTestUtils.setField(service, "histories", histories); ReflectionTestUtils.setField(service, "inventory", inventory); ReflectionTestUtils.setField(service, "adjustments", adjustments); + ReflectionTestUtils.setField(service, "orderNoGenerator", new LastlegOrderNoGenerator()); ReflectionTestUtils.setField(service, "orders", orders); ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper()); ReflectionTestUtils.setField(service, "feePerPackage", new BigDecimal("5.00")); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowServiceTest.java index 675bf5e58..df38d04ca 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPdaWorkflowServiceTest.java @@ -2,11 +2,17 @@ package com.baiyue.lastleg.dispatch.pda.service; import com.baiyue.lastleg.delivery.mapper.OutboundOrderMapper; import com.baiyue.lastleg.dispatch.entity.DispatchOrder; +import com.baiyue.lastleg.dispatch.entity.DispatchItem; +import com.baiyue.lastleg.dispatch.mapper.DispatchItemMapper; import com.baiyue.lastleg.dispatch.mapper.DispatchOrderMapper; import com.baiyue.lastleg.dispatch.pda.dto.DispatchPdaRequests; import com.baiyue.lastleg.dispatch.pda.dto.DispatchPdaResponses; import com.baiyue.lastleg.dispatch.pda.entity.DispatchPdaOperation; +import com.baiyue.lastleg.dispatch.pda.entity.DispatchPdaPick; +import com.baiyue.lastleg.dispatch.pda.entity.DispatchPdaTask; import com.baiyue.lastleg.dispatch.pda.mapper.DispatchPdaOperationMapper; +import com.baiyue.lastleg.dispatch.pda.mapper.DispatchPdaPickMapper; +import com.baiyue.lastleg.dispatch.pda.mapper.DispatchPdaTaskMapper; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -15,7 +21,10 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.server.ResponseStatusException; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.when; @@ -24,6 +33,16 @@ class DispatchPdaWorkflowServiceTest { @Mock private DispatchOrderMapper orders; @Mock private DispatchPdaOperationMapper operations; @Mock private OutboundOrderMapper outboundOrders; + @Mock private DispatchItemMapper items; + @Mock private DispatchPdaTaskMapper tasks; + @Mock private DispatchPdaPickMapper picks; + + @Test + void labelsTransferredPalletsWithOriginalAndCurrentFbaWarehouses() { + assertEquals("ONT8 -> ONT", DispatchPdaWorkflowService.warehouseRouteLabel("ONT8", "ONT")); + assertNull(DispatchPdaWorkflowService.warehouseRouteLabel("ONT8", "ont8")); + assertNull(DispatchPdaWorkflowService.warehouseRouteLabel(null, "ONT")); + } @Test void pushRequiresVehiclePlateBeforeCreatingPdaTask() { @@ -118,4 +137,34 @@ class DispatchPdaWorkflowServiceTest { assertEquals("DEPARTED", result.status()); assertEquals(true, result.replayed()); } + + @Test + void completingAnAutomaticallyCompletedTaskReturnsIdempotentSuccess() { + DispatchPdaWorkflowService service = new DispatchPdaWorkflowService(); + ReflectionTestUtils.setField(service, "orders", orders); + ReflectionTestUtils.setField(service, "operations", operations); + ReflectionTestUtils.setField(service, "items", items); + ReflectionTestUtils.setField(service, "tasks", tasks); + ReflectionTestUtils.setField(service, "picks", picks); + DispatchOrder order = new DispatchOrder(); + order.setId(1L); order.setCustomerId("100"); order.setDispatchNo("PC1"); + order.setStatus("READY_TO_DEPART"); order.setVehiclePlate("CA-100"); + DispatchPdaTask task = new DispatchPdaTask(); + task.setId(2L); task.setStatus("COMPLETED"); + DispatchItem item = new DispatchItem(); item.setPackageCount(6); + DispatchPdaPick pick = new DispatchPdaPick(); pick.setPackages(6L); + DispatchPdaRequests.CompletePicking request = new DispatchPdaRequests.CompletePicking("complete-1", "normal"); + when(orders.findByDispatchNo("100", "PC1")).thenReturn(order); + when(orders.lockById("100", 1L)).thenReturn(order); + when(operations.findKey("100", "complete-1")).thenReturn(null); + when(tasks.lockByDispatch("100", 1L)).thenReturn(task); + when(items.findByDispatchOrder("100", 1L)).thenReturn(List.of(item)); + when(picks.listTask("100", 2L)).thenReturn(List.of(pick)); + + DispatchPdaResponses.PickingCompleted result = service.completePicking("100", "PC1", request); + + assertEquals("READY_TO_DEPART", result.status()); + assertEquals(6, result.pickedPackages()); + assertEquals(true, result.replayed()); + } } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionServiceTest.java index ad9dcee13..dd7339d81 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/pda/service/DispatchPickOrderAttributionServiceTest.java @@ -75,8 +75,8 @@ class DispatchPickOrderAttributionServiceTest { "t1", "OUT1", "D1", List.of(pick), LocalDateTime.of(2026, 8, 17, 15, 0)); assertEquals(List.of(new DispatchPickOrderAttributionService.OrderPackages(101L, 4L)), result); - verify(history).insert(argThat((LastlegOrderStatusHistory value) -> "PARTIALLY_SHIPPED".equals(value.getToStatus()) - && value.getRemark().contains("6/10"))); + verify(history).insert(argThat((LastlegOrderStatusHistory value) -> "t1".equals(value.getCustomerId()) + && "PARTIALLY_SHIPPED".equals(value.getToStatus()) && value.getRemark().contains("6/10"))); } private DispatchPickOrderAttributionService service() { diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/service/DispatchServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/service/DispatchServiceTest.java index 95e14f0e3..46a243918 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/service/DispatchServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/dispatch/service/DispatchServiceTest.java @@ -56,6 +56,7 @@ class DispatchServiceTest { ReflectionTestUtils.setField(service, "candidates", candidates); ReflectionTestUtils.setField(service, "drafts", drafts); ReflectionTestUtils.setField(service, "draftContainers", draftContainers); + ReflectionTestUtils.setField(service, "orderNoGenerator", new com.baiyue.lastleg.service.LastlegOrderNoGenerator()); lenient().when(items.findByDispatchOrder(any(), any())).thenReturn(List.of()); } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/dropship/service/DropshipOrderServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/dropship/service/DropshipOrderServiceTest.java index 85564aa1f..9c37e00a0 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/dropship/service/DropshipOrderServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/dropship/service/DropshipOrderServiceTest.java @@ -97,7 +97,7 @@ class DropshipOrderServiceTest { when(cartons.listProfile("C-1", 2L)).thenReturn(List.of(secondCarton)); service.activateAfterStockIn("C-1", 10L, 50L, List.of( - receipt(101L, "WH-P-1", 201L, 1), receipt(102L, "WH-P-2", 202L, 1))); + receipt(101L, "SOURCE-P-1", 201L, 1), receipt(102L, "SOURCE-P-2", 202L, 1))); assertThat(first.getStatus()).isEqualTo("ACTIVE"); assertThat(first.getWarehousePalletId()).isEqualTo(101L); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderServiceTest.java new file mode 100644 index 000000000..6a9036e12 --- /dev/null +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/exceptionorder/service/ExceptionOrderServiceTest.java @@ -0,0 +1,74 @@ +package com.baiyue.lastleg.exceptionorder.service; + +import com.baiyue.lastleg.entity.LastlegInboundOrder; +import com.baiyue.lastleg.entity.LastlegOrder; +import com.baiyue.lastleg.exceptionorder.mapper.ExceptionOrderMapper; +import com.baiyue.lastleg.mapper.LastlegInboundOrderMapper; +import com.baiyue.lastleg.mapper.LastlegOrderMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class ExceptionOrderServiceTest { + @Mock private ExceptionOrderMapper exceptionOrders; + @Mock private LastlegOrderMapper lastMileOrders; + @Mock private LastlegInboundOrderMapper inboundOrders; + private ExceptionOrderService service; + + @BeforeEach + void setUp() { + service = new ExceptionOrderService(); + ReflectionTestUtils.setField(service, "exceptionOrders", exceptionOrders); + ReflectionTestUtils.setField(service, "lastMileOrders", lastMileOrders); + ReflectionTestUtils.setField(service, "inboundOrders", inboundOrders); + } + + @Test + void resolvesTheOneToOneInboundAndLastMileRelation() { + LastlegOrder tail = tail("LM-1", "IN-1"); + LastlegInboundOrder inbound = inbound("IN-1"); + when(lastMileOrders.selectList(any())).thenReturn(List.of(tail)); + when(inboundOrders.selectList(any())).thenReturn(List.of(inbound)); + + var relation = service.resolve(null, null, "IN-1", "LM-1"); + + assertThat(relation.lastMileOrderNo()).isEqualTo("LM-1"); + assertThat(relation.inboundCode()).isEqualTo("IN-1"); + assertThat(relation.customerId()).isEqualTo("1001"); + } + + @Test + void rejectsInputsThatExistButDoNotBelongTogether() { + when(lastMileOrders.selectList(any())).thenReturn(List.of(tail("LM-1", "IN-1"))); + when(inboundOrders.selectList(any())).thenReturn(List.of(inbound("IN-2"))); + + assertThatThrownBy(() -> service.resolve(null, null, "IN-2", "LM-1")) + .isInstanceOf(ResponseStatusException.class) + .hasMessageContaining("不是同一关联关系"); + } + + private LastlegOrder tail(String orderNo, String inboundCode) { + LastlegOrder value = new LastlegOrder(); + value.setId(12L); value.setCustomerId("1001"); value.setOrderNo(orderNo); value.setInboundCode(inboundCode); + value.setCustomerName("测试客户"); value.setPackageCount(10); + return value; + } + + private LastlegInboundOrder inbound(String inboundCode) { + LastlegInboundOrder value = new LastlegInboundOrder(); + value.setId(11L); value.setCustomerId("1001"); value.setInboundCode(inboundCode); value.setPackageCount(10); + return value; + } +} diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapperContractTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapperContractTest.java new file mode 100644 index 000000000..6a38fe3a9 --- /dev/null +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanOrderMapperContractTest.java @@ -0,0 +1,21 @@ +package com.baiyue.lastleg.inboundplan.mapper; + +import org.apache.ibatis.annotations.Select; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class InboundPlanOrderMapperContractTest { + @Test + void transferWarehouseIsTheEffectivePalletDestination() throws Exception { + Select select = InboundPlanOrderMapper.class + .getMethod("lockOrder", String.class, Long.class) + .getAnnotation(Select.class); + String sql = String.join(" ", select.value()); + + assertTrue(sql.contains("COALESCE(NULLIF(o.transfer_warehouse_id,''),MIN(NULLIF(d.destination_warehouse_id,'')))")); + assertTrue(sql.contains("COALESCE(NULLIF(o.transfer_warehouse_code,''),MIN(NULLIF(d.destination_warehouse_code,'')))")); + assertTrue(sql.contains("THEN 1 ELSE COUNT(DISTINCT")); + assertTrue(sql.contains("0 AS destinationMismatchCount")); + } +} diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapperContractTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapperContractTest.java new file mode 100644 index 000000000..a595bfbbc --- /dev/null +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/mapper/InboundPlanQueryMapperContractTest.java @@ -0,0 +1,21 @@ +package com.baiyue.lastleg.inboundplan.mapper; + +import org.apache.ibatis.annotations.Select; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class InboundPlanQueryMapperContractTest { + @Test + void platformListReadsVersionAndDoesNotRequireANullDispatchCustomer() throws Exception { + Select select = InboundPlanQueryMapper.class + .getMethod("rows", String.class, + com.baiyue.lastleg.inboundplan.dto.InboundPlanRequests.Query.class, + long.class, long.class) + .getAnnotation(Select.class); + String sql = String.join(" ", select.value()); + + assertTrue(sql.contains("p.version version")); + assertTrue(sql.contains("WHERE di.customer_id=#{customerId} AND di.order_id=l.order_id")); + } +} diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowServiceTest.java index 3a7267150..483e7e6cc 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundPdaWorkflowServiceTest.java @@ -12,6 +12,8 @@ import com.baiyue.lastleg.inboundplan.pda.entity.InboundPdaTask; import com.baiyue.lastleg.inboundplan.pda.gateway.WarehouseResourceGateway; import com.baiyue.lastleg.inboundplan.pda.gateway.UnloadPhotoFileGateway; import com.baiyue.lastleg.inboundplan.pda.repository.PdaWorkflowRepository; +import com.baiyue.lastleg.dropship.service.DropshipOrderService; +import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot; import com.baiyue.lastleg.entity.LastlegOrder; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.fasterxml.jackson.databind.ObjectMapper; @@ -51,6 +53,7 @@ class InboundPdaWorkflowServiceTest { @Mock InboundUnloadCompletedOutboxService unloadCompletedOutbox; @Mock LastlegOrderMapper orders; @Mock UnloadPhotoFileGateway unloadPhotoFiles; + @Mock DropshipOrderService dropshipOrders; private InboundPdaWorkflowService service; private InboundPlan plan; @@ -73,6 +76,7 @@ class InboundPdaWorkflowServiceTest { ReflectionTestUtils.setField(service, "unloadCompletedOutbox", unloadCompletedOutbox); ReflectionTestUtils.setField(service, "orders", orders); ReflectionTestUtils.setField(service, "unloadPhotoFiles", unloadPhotoFiles); + ReflectionTestUtils.setField(service, "dropshipOrders", dropshipOrders); ReflectionTestUtils.setField(service, "sourceObjectMapper", new ObjectMapper().findAndRegisterModules()); service.initializeObjectMapper(); plan = plan(); @@ -189,6 +193,20 @@ class InboundPdaWorkflowServiceTest { assertEquals(3, targetPallet.getActualPackageCount()); } + @Test + void cartonScanRejectsBatchQuantityAndRequiresOneRealCartonPerScan() { + InboundPlanCarton first = carton("BOX-BATCH-1", "UNASSIGNED", targetLine.getId(), "WH-A"); + InboundPlanCarton second = carton("BOX-BATCH-2", "UNASSIGNED", targetLine.getId(), "WH-A"); + second.setId(42L); + activeTask(); + when(repository.lockCarton("100", "BOX-BATCH-1")).thenReturn(first); + + assertThrows(ResponseStatusException.class, () -> service.scanCarton("100", "LIP1", + new PdaRequests.ScanCarton("BOX-BATCH-1", null, 2, "scan-batch", "7", "worker"))); + assertEquals("UNASSIGNED", first.getStatus()); + assertEquals("UNASSIGNED", second.getStatus()); + } + @Test void cartonScanRejectsDuplicateAndWrongDestination() { InboundPlanCarton repeated = carton("BOX-002", "ON_PALLET", targetLine.getId(), "WH-A"); @@ -225,7 +243,9 @@ class InboundPdaWorkflowServiceTest { @Test void countMustPrecedePickAndPickedBoxesDriveActualVolume() { + targetLine.setFbaWarehouseCode("ONT8"); activeTask(); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); target.setCountedPackageCount(0); when(repository.lockAllocation(plan.getId(), target.getId())).thenReturn(target); when(repository.line(plan.getId(), targetLine.getId())).thenReturn(targetLine); @@ -248,7 +268,9 @@ class InboundPdaWorkflowServiceTest { @Test void overPickReturnsConfirmationWithoutChangingAllocationOrPallet() { + targetLine.setFbaWarehouseCode("ONT8"); activeTask(); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); when(repository.lockAllocation(plan.getId(), target.getId())).thenReturn(target); when(repository.line(plan.getId(), targetLine.getId())).thenReturn(targetLine); candidateQueries(); @@ -459,8 +481,95 @@ class InboundPdaWorkflowServiceTest { verify(repository, never()).update(task); } + @Test + void partialCompleteStocksOnlyActualPickedPackagesAndKeepsPartialStatus() { + target.setPickedPackageCount(2); + activeTask(); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); + when(repository.allocations(plan.getId())).thenReturn(List.of(target)); + when(repository.pallets(plan.getId())).thenReturn(List.of(targetPallet)); + when(repository.cartons(plan.getId())).thenReturn(List.of()); + + var result = service.partialComplete("100", "LIP1", + new PdaRequests.ForceComplete("partial-1", "7", "worker")); + + assertEquals("PARTIAL_INBOUND", result.status()); + assertEquals(5, result.expectedPackageCount()); + assertEquals(2, result.pickedPackageCount()); + assertEquals("PARTIAL_INBOUND", plan.getStatus()); + verify(stockReceipts).stockIn(eq("100"), eq(plan), eq(task), eq(List.of(targetLine)), + eq(List.of(target)), eq(List.of(targetPallet)), any(), eq(true)); + verify(unloadCompletedOutbox).publish(eq(plan), eq(task), eq(List.of(101L))); + } + + @Test + void partialCompleteRequiresUnloadPhotosBeforeStockIn() { + target.setPickedPackageCount(2); + task.setUnloadPhotoFileIds(null); + task.setUnloadPhotoUploadedAt(null); + task.setEstimatedEmptyReturnAt(null); + activeTask(); + + ResponseStatusException exception = assertThrows(ResponseStatusException.class, + () -> service.partialComplete("100", "LIP1", + new PdaRequests.ForceComplete("partial-no-photo", "7", "worker"))); + + assertEquals(409, exception.getStatusCode().value()); + verify(stockReceipts, never()).stockIn(any(), any(), any(), any(), any(), any(), any(), eq(true)); + verify(unloadCompletedOutbox, never()).publish(any(), any(), any()); + } + + @Test + void dropshipTaskIsMarkedAndWholePalletPickReceivesThePlannedPalletAtOnce() { + plan.setSourceOrderId(101L); + LastlegOrder order = new LastlegOrder(); + order.setId(101L); order.setOrderType("ONE_CLICK_DROPSHIP"); + when(repository.order(101L)).thenReturn(order); + when(dropshipOrders.palletSnapshots("100", 101L)).thenReturn(List.of( + new DropshipPalletSnapshot("PLT-21", 3, + List.of(new DropshipPalletSnapshot.Sku("SKU-A", "Product A", 3, 30))))); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); + when(repository.allocations(plan.getId())).thenReturn(List.of(target)); + when(repository.pallets(plan.getId())).thenReturn(List.of(targetPallet)); + when(repository.task("100", plan.getId())).thenReturn(task); + activeTask(); + when(repository.lockAllocation(plan.getId(), target.getId())).thenReturn(target); + when(repository.line(plan.getId(), targetLine.getId())).thenReturn(targetLine); + when(repository.lockPallet(plan.getId(), targetPallet.getId())).thenReturn(targetPallet); + + var taskResult = service.task("100", "LIP1"); + var pickResult = service.pickWholePallet("100", "LIP1", target.getId(), + new PdaRequests.WholePalletOperation("whole-1", "7", "worker")); + + assertEquals("ONE_CLICK_DROPSHIP", taskResult.orderType()); + assertTrue(taskResult.wholePalletInbound()); + assertEquals("SKU-A", taskResult.dropshipPallets().get(0).skus().get(0).skuCode()); + assertEquals(targetPallet.getId(), taskResult.dropshipPallets().get(0).planPalletId()); + assertEquals(target.getId(), taskResult.dropshipPallets().get(0).allocationId()); + assertEquals("WHOLE_PALLET_PICKED", pickResult.result()); + assertEquals(3, target.getPickedPackageCount()); + assertEquals(3, targetPallet.getActualPackageCount()); + } + + @Test + void dropshipPlanRejectsOrdinaryPackagePicking() { + plan.setSourceOrderId(101L); + LastlegOrder order = new LastlegOrder(); + order.setId(101L); order.setOrderType("ONE_CLICK_DROPSHIP"); + when(repository.order(101L)).thenReturn(order); + + ResponseStatusException exception = assertThrows(ResponseStatusException.class, + () -> service.pick("100", "LIP1", target.getId(), + new PdaRequests.PackageOperation(1, "wrong-mode", "7", "worker"))); + + assertEquals(409, exception.getStatusCode().value()); + verify(repository, never()).lockAllocation(plan.getId(), target.getId()); + } + @Test void rejectsReuseOfIdempotencyKeyWithDifferentActionOrPayload() { + targetLine.setFbaWarehouseCode("ONT8"); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); InboundPdaOperation existing = new InboundPdaOperation(); existing.setAction("COUNT"); existing.setRequestHash("different"); @@ -472,7 +581,9 @@ class InboundPdaWorkflowServiceTest { @Test void completedTaskStillReplaysAnAlreadyPersistedPick() { + targetLine.setFbaWarehouseCode("ONT8"); activeTask(); + when(repository.lines(plan.getId())).thenReturn(List.of(targetLine)); when(repository.lockAllocation(plan.getId(), target.getId())).thenReturn(target); when(repository.line(plan.getId(), targetLine.getId())).thenReturn(targetLine); when(repository.lockPallet(plan.getId(), targetPallet.getId())).thenReturn(targetPallet); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptServiceTest.java index 65ee6556a..b14845ecc 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/InboundStockReceiptServiceTest.java @@ -19,6 +19,7 @@ import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; import com.baiyue.lastleg.dropship.service.DropshipOrderService; +import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -157,6 +158,21 @@ class InboundStockReceiptServiceTest { assertEquals(5L, requests.getAllValues().stream().mapToLong(InventoryCommandRequests.StockIn::packages).sum()); } + @Test + void stocksInToHeldInventoryWhenInboundOrderIsDetained() { + Fixture fixture = fixture(5, 5, 5, 10); + fixture.order.setDetained(true); + when(orders.selectBatchIds(any())).thenReturn(List.of(fixture.order)); + when(inventory.stockInHeld(eq("100"), eq("operator"), any(InventoryCommandRequests.StockIn.class))) + .thenReturn(new InventoryCommandResponse(8L, "STOCK_IN", 9L, 10L, 5, 0, 0, 0, false, false)); + + service.stockIn("100", fixture.plan, fixture.task, List.of(fixture.line), + List.of(fixture.allocation), List.of(fixture.pallet), LocalDateTime.now()); + + verify(inventory).stockInHeld(eq("100"), eq("operator"), any(InventoryCommandRequests.StockIn.class)); + verify(inventory, never()).stockIn(any(), any(), any()); + } + @Test void existingTaskReceiptMakesStockInReplayANoOp() { Fixture fixture = fixture(10, 5, 5, 10); @@ -200,6 +216,7 @@ class InboundStockReceiptServiceTest { void activatesDropshipAfterGenericStockInReceiptIsCreated() { Fixture fixture = fixture(5, 5, 5, 10); fixture.order.setOrderType("ONE_CLICK_DROPSHIP"); + when(dropshipOrders.palletSnapshots("100", 101L)).thenReturn(List.of(dropshipPallet("PLT-21", 5))); when(orders.selectBatchIds(any())).thenReturn(List.of(fixture.order)); when(inventory.stockIn(eq("100"), eq("operator"), any(InventoryCommandRequests.StockIn.class))) .thenReturn(new InventoryCommandResponse(8L, "STOCK_IN", 9L, 10L, 5, 0, 0, 0, false, false)); @@ -227,6 +244,45 @@ class InboundStockReceiptServiceTest { verify(receipts, never()).insert(any(InboundStockReceipt.class)); } + @Test + void partialStockInSynchronizesOrderWithoutActivatingDropship() { + Fixture fixture = fixture(5, 2, 2, 10); + fixture.order.setOrderType("ONE_CLICK_DROPSHIP"); + when(dropshipOrders.palletSnapshots("100", 101L)).thenReturn(List.of(dropshipPallet("PLT-21", 2))); + when(orders.selectBatchIds(any())).thenReturn(List.of(fixture.order)); + when(inventory.stockIn(eq("100"), eq("operator"), any(InventoryCommandRequests.StockIn.class))) + .thenReturn(new InventoryCommandResponse(8L, "STOCK_IN", 9L, 10L, 2, 0, 0, 0, false, false)); + + service.stockIn("100", fixture.plan, fixture.task, List.of(fixture.line), + List.of(fixture.allocation), List.of(fixture.pallet), LocalDateTime.now(), true); + + assertEquals("INBOUND_COMPLETED", fixture.order.getCurrentStatus()); + assertEquals("INBOUND_PLAN:PARTIAL_INBOUND", fixture.order.getSubOrderStatus()); + verify(orders).updateById(fixture.order); + verify(orderHistories).insert(any(LastlegOrderStatusHistory.class)); + verify(dropshipOrders, never()).activateAfterStockIn(any(), any(), any(), any()); + } + + @Test + void rejectsPartiallyPickedDropshipSourcePallet() { + Fixture fixture = fixture(5, 5, 2, 10); + fixture.order.setOrderType("ONE_CLICK_DROPSHIP"); + when(orders.selectBatchIds(any())).thenReturn(List.of(fixture.order)); + when(dropshipOrders.palletSnapshots("100", 101L)).thenReturn(List.of(dropshipPallet("PLT-21", 5))); + + ResponseStatusException exception = assertThrows(ResponseStatusException.class, + () -> service.stockIn("100", fixture.plan, fixture.task, List.of(fixture.line), + List.of(fixture.allocation), List.of(fixture.pallet), LocalDateTime.now(), true)); + + assertEquals(409, exception.getStatusCode().value()); + verify(inventory, never()).stockIn(any(), any(), any()); + } + + private DropshipPalletSnapshot dropshipPallet(String palletNo, long packages) { + return new DropshipPalletSnapshot(palletNo, packages, + List.of(new DropshipPalletSnapshot.Sku("SKU-1", "Product", packages, packages * 10))); + } + private Fixture fixture(int required, int planned, int picked, int quantity) { InboundPlan plan = new InboundPlan(); plan.setId(1L); plan.setPlanNo("IP-1"); plan.setOverseasWarehouseId(900L); InboundPdaTask task = new InboundPdaTask(); task.setId(2L); task.setOperatorId("operator"); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerServiceBehaviorTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerServiceBehaviorTest.java index 930923ecb..a485e8de5 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerServiceBehaviorTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerServiceBehaviorTest.java @@ -502,6 +502,20 @@ class ReceivingContainerServiceBehaviorTest { verifyNoInteractions(workflow, operations); } + @Test + void cartonResolutionReturnsInboundAndPalletMergeReminderFromTailLegOrder() { + stubCurrent("FBA", "CONT-A"); + LastlegOrder marked = order("FBA"); + marked.setPalletMergeFlag("REQUIRED"); + when(orders.selectById(1L)).thenReturn(marked); + + var resolved = service.resolveCarton("7", new ReceivingContainerRequests.CartonResolve("IN-1#2-1")); + + assertThat(resolved.inboundCode()).isEqualTo("IN-1"); + assertThat(resolved.palletMergeRequired()).isTrue(); + assertThat(resolved.palletMergeHint()).contains("合托"); + } + @Test void fbaCartonResolutionRejectsCrossContainerAndInvalidPrefixesAsConflicts() { stubCurrent("FBA", "CONT-A"); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanPaginationTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanPaginationTest.java index 75975a81d..3fdd7c816 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanPaginationTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanPaginationTest.java @@ -70,7 +70,7 @@ class InboundPlanPaginationTest { .getMethod("rows", String.class, InboundPlanRequests.Query.class, long.class, long.class) .getAnnotation(org.apache.ibatis.annotations.Select.class).value()) + InboundPlanQueryMapper.FROM_AND_FILTER; assertThat(sql).contains("a2.customer_id=#{customerId}", "ORDER BY a2.created_at DESC,ai2.id DESC LIMIT 1"); - assertThat(sql).contains("di.customer_id=#{customerId} AND di.order_id=l.order_id"); + assertThat(sql).contains("di.customer_id=#{customerId} AND di.order_id=l.order_id"); assertThat(sql).contains("q.planNo", "q.inboundCode", "q.status", "q.overseasWarehouseId", "q.destinationWarehouseCode", "q.warehouse", "q.fbaWarehouseCode", "q.appointmentStatus", "q.dispatchStatus", "q.createdFrom", "q.createdTo"); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanServiceTest.java index 13396760c..6fa7f21bd 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/InboundPlanServiceTest.java @@ -2,6 +2,7 @@ package com.baiyue.lastleg.inboundplan.service; import com.baiyue.lastleg.inboundplan.entity.InboundPlan; import com.baiyue.lastleg.inboundplan.dto.InboundPlanRequests; +import com.baiyue.lastleg.inboundplan.dto.UpdateInboundPlanRequest; import com.baiyue.lastleg.inboundplan.entity.InboundPlanLine; import com.baiyue.lastleg.inboundplan.entity.InboundPlanPallet; import com.baiyue.lastleg.inboundplan.entity.InboundPlanPalletAllocation; @@ -55,6 +56,7 @@ class InboundPlanServiceTest { ReflectionTestUtils.setField(service, "allocationMapper", allocationMapper); ReflectionTestUtils.setField(service, "orderMapper", orderMapper); ReflectionTestUtils.setField(service, "inventoryBalanceMapper", inventoryBalanceMapper); + ReflectionTestUtils.setField(service, "orderNoGenerator", new com.baiyue.lastleg.service.LastlegOrderNoGenerator()); org.mockito.Mockito.lenient().when(lineMapper.selectList(any())).thenReturn(List.of()); org.mockito.Mockito.lenient().when(palletMapper.selectList(any())).thenReturn(List.of()); org.mockito.Mockito.lenient().when(allocationMapper.selectList(any())).thenReturn(List.of()); @@ -182,13 +184,93 @@ class InboundPlanServiceTest { } @Test - void rejectsTransferWarehouseThatDiffersFromCargoSnapshots() { - SourceOrderSnapshot invalid = new SourceOrderSnapshot(1L, "100", "MSCU6639870", "RK-1", + void transferWarehouseOverridesOriginalCargoDestination() { + SourceOrderSnapshot transferred = new SourceOrderSnapshot(1L, "100", "MSCU6639870", "RK-1", "DESTINATION_ASSIGNED", null, BigDecimal.ONE, 1, "WH-B", "WH-B", "Warehouse B", 1, 1); - when(orderMapper.lockOrder("100", 1L)).thenReturn(List.of(invalid)); + when(orderMapper.lockOrder("100", 1L)).thenReturn(List.of(transferred)); + when(orderMapper.markPlanned(any(), any(), any(), org.mockito.ArgumentMatchers.anyInt(), any())) + .thenReturn(1); - assertThrows(ResponseStatusException.class, () -> service.convert("100", 1L)); - verify(planMapper, never()).insert(any(InboundPlan.class)); + service.convert("100", 1L); + + ArgumentCaptor pallet = ArgumentCaptor.forClass(InboundPlanPallet.class); + verify(palletMapper).insert(pallet.capture()); + assertEquals("WH-B", pallet.getValue().getDestinationWarehouseId()); + assertEquals("WH-B", pallet.getValue().getDestinationWarehouseCode()); + } + + @Test + void convertsDropshipSourcePalletsWithoutRedistributingTheirPackages() { + when(orderMapper.lockOrder("100", 2L)).thenReturn(List.of(order(2L, "RK-2", "1.0000", "WH-A"))); + when(orderMapper.markPlanned(any(), any(), any(), org.mockito.ArgumentMatchers.anyInt(), any())) + .thenReturn(1); + + service.convertDropship("100", 2L, List.of( + new InboundPlanService.WholePalletInput("SOURCE-P-1", 3), + new InboundPlanService.WholePalletInput("SOURCE-P-2", 7))); + + ArgumentCaptor pallets = ArgumentCaptor.forClass(InboundPlanPallet.class); + verify(palletMapper, times(2)).insert(pallets.capture()); + assertEquals(List.of("SOURCE-P-1", "SOURCE-P-2"), + pallets.getAllValues().stream().map(InboundPlanPallet::getPalletNo).toList()); + ArgumentCaptor allocations = + ArgumentCaptor.forClass(InboundPlanPalletAllocation.class); + verify(allocationMapper, times(2)).insert(allocations.capture()); + assertEquals(List.of(3, 7), allocations.getAllValues().stream() + .map(InboundPlanPalletAllocation::getPlannedPackageCount).toList()); + } + + @Test + void updateSynchronizesDestinationSnapshotToLinesAndPallets() { + InboundPlan plan = new InboundPlan(); + plan.setId(20L); + plan.setPlanNo("IP-20"); + plan.setCustomerId("100"); + plan.setStatus("PENDING_ASSIGNMENT"); + plan.setLineCount(1); + plan.setPalletCount(1); + plan.setVersion(3); + + InboundPlanLine line = new InboundPlanLine(); + line.setId(21L); + line.setCustomerId("100"); + line.setPlanId(20L); + line.setRequiredPackageCount(1); + line.setPlannedPalletCount(1); + line.setDestinationWarehouseId("OLD-WH"); + line.setDestinationWarehouseCode("OLD-CODE"); + line.setDestinationWarehouseName("Old warehouse"); + + InboundPlanPallet pallet = new InboundPlanPallet(); + pallet.setId(22L); + pallet.setCustomerId("100"); + pallet.setPlanId(20L); + pallet.setSequenceNo(1); + pallet.setActualPackageCount(0); + pallet.setDestinationWarehouseId("OLD-WH"); + pallet.setDestinationWarehouseCode("OLD-CODE"); + pallet.setDestinationWarehouseName("Old warehouse"); + + when(planMapper.selectOne(any())).thenReturn(plan); + when(lineMapper.selectList(any())).thenReturn(List.of(line), List.of(line)); + when(palletMapper.selectList(any())).thenReturn(List.of(pallet), List.of(pallet)); + when(allocationMapper.selectList(any())).thenReturn(List.of()); + + service.update("100", "IP-20", + new UpdateInboundPlanRequest("100", 193000000000200003L, + " US-LAX-01 ", " Los Angeles Warehouse ", 3)); + + ArgumentCaptor updatedLine = ArgumentCaptor.forClass(InboundPlanLine.class); + verify(lineMapper).updateById(updatedLine.capture()); + assertEquals("193000000000200003", updatedLine.getValue().getDestinationWarehouseId()); + assertEquals("US-LAX-01", updatedLine.getValue().getDestinationWarehouseCode()); + assertEquals("Los Angeles Warehouse", updatedLine.getValue().getDestinationWarehouseName()); + + ArgumentCaptor updatedPallet = ArgumentCaptor.forClass(InboundPlanPallet.class); + verify(palletMapper).updateById(updatedPallet.capture()); + assertEquals("193000000000200003", updatedPallet.getValue().getDestinationWarehouseId()); + assertEquals("US-LAX-01", updatedPallet.getValue().getDestinationWarehouseCode()); + assertEquals("Los Angeles Warehouse", updatedPallet.getValue().getDestinationWarehouseName()); } @Test diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/PalletAllocatorTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/PalletAllocatorTest.java index 663404360..8cc8eba1d 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/PalletAllocatorTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inboundplan/service/PalletAllocatorTest.java @@ -49,6 +49,16 @@ class PalletAllocatorTest { .map(PalletAllocator.AllocationDraft::plannedPackageCount).toList()); } + @Test + void keepsAtLeastTheFirstLegRecommendedPalletCount() { + PalletAllocator.Result result = PalletAllocator.allocate(List.of( + new PalletAllocator.LineInput(1L, "WH-A", new BigDecimal("1.5000"), 6, 3))); + + assertEquals(3, result.pallets().size()); + assertEquals(6, result.allocations().stream() + .mapToInt(PalletAllocator.AllocationDraft::plannedPackageCount).sum()); + } + private PalletAllocator.Result allocate(String volume) { return PalletAllocator.allocate(List.of( new PalletAllocator.LineInput(1L, "WH-A", new BigDecimal(volume), diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/inventory/command/InventoryCommandServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/inventory/command/InventoryCommandServiceTest.java index 5e2139074..bdc374e24 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/inventory/command/InventoryCommandServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/inventory/command/InventoryCommandServiceTest.java @@ -24,6 +24,8 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -57,6 +59,7 @@ class InventoryCommandServiceTest { ReflectionTestUtils.setField(service, "warehouseGateway", warehouseGateway); ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper()); lenient().when(operations.findIdempotency(any(), any())).thenReturn(null); + lenient().when(commands.lockInboundTransitBalances(any(), any(), any())).thenReturn(List.of()); } @Test @@ -117,6 +120,7 @@ class InventoryCommandServiceTest { balance.setSkuCode("SKU-1"); balance.setInTransitPackages(12L); when(balances.lockDimension("100", "WH-1", "ONT8", 101L, "IN-CODE-1")).thenReturn(balance); + when(commands.lockInboundTransitBalances("100", 101L, "IN-CODE-1")).thenReturn(List.of(balance)); InventoryCommandResponse result = service.stockIn("100", "7", stockIn("IN-1", "stock-in-transit", 12, 24)); @@ -125,6 +129,48 @@ class InventoryCommandServiceTest { assertEquals(12L, balance.getAvailablePackages()); } + @Test + void stockInTransfersInTransitFromTheOriginalOverseasWarehouse() { + InventoryBalance target = balance(200L, 0); + target.setSkuCode("SKU-1"); + target.setInTransitPackages(0L); + InventoryBalance source = balance(201L, 0); + source.setOverseasWarehouseId("WH-OLD"); + source.setSkuCode("SKU-1"); + source.setInTransitPackages(12L); + when(balances.lockDimension("100", "WH-1", "ONT8", 101L, "IN-CODE-1")).thenReturn(target); + when(commands.lockInboundTransitBalances("100", 101L, "IN-CODE-1")).thenReturn(List.of(source)); + + InventoryCommandResponse result = service.stockIn("100", "7", + stockIn("IN-1", "stock-in-cross-warehouse", 12, 24)); + + assertEquals(12, result.availablePackages()); + assertEquals(0L, source.getInTransitPackages()); + ArgumentCaptor written = ArgumentCaptor.forClass(InventoryLedger.class); + verify(ledgers, times(2)).insert(written.capture()); + assertEquals("STOCK_IN_TRANSIT_RELEASE", written.getAllValues().get(0).getBusinessType()); + assertEquals(-12L, written.getAllValues().get(0).getDeltaPackages()); + assertEquals("STOCK_IN", written.getAllValues().get(1).getBusinessType()); + assertEquals("IN_TRANSIT", written.getAllValues().get(1).getStatusFrom()); + } + + @Test + void stockInKeepsOutsideSourceWhenInTransitIsInsufficient() { + InventoryBalance target = balance(200L, 0); + target.setSkuCode("SKU-1"); + target.setInTransitPackages(2L); + when(balances.lockDimension("100", "WH-1", "ONT8", 101L, "IN-CODE-1")).thenReturn(target); + when(commands.lockInboundTransitBalances("100", 101L, "IN-CODE-1")).thenReturn(List.of(target)); + + service.stockIn("100", "7", stockIn("IN-1", "stock-in-partial-transit", 5, 10)); + + ArgumentCaptor ledger = ArgumentCaptor.forClass(InventoryLedger.class); + verify(ledgers).insert(ledger.capture()); + assertEquals("OUTSIDE", ledger.getValue().getStatusFrom()); + assertEquals(0L, target.getInTransitPackages()); + assertEquals(5L, target.getAvailablePackages()); + } + @Test void differentSkusShareInboundBalanceAndKeepSkuInLedger() { InventoryBalance balance = balance(200L, 4); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardServiceTest.java index 330c6b2fd..d5460b792 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/looseboard/service/LocalLooseboardServiceTest.java @@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.server.ResponseStatusException; @@ -31,6 +32,7 @@ import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; @@ -43,7 +45,7 @@ class LocalLooseboardServiceTest { @Mock LocalLooseboardOperationMapper operations; @Mock LastlegOrderMapper lastlegOrders; @Mock LastlegOrderStatusHistoryMapper lastlegHistories; - @Mock LastlegOrderNoGenerator lastlegOrderNoGenerator; + @Spy LastlegOrderNoGenerator lastlegOrderNoGenerator = new LastlegOrderNoGenerator(); private LocalLooseboardService service; private final Map operationRows = new HashMap<>(); @@ -57,7 +59,8 @@ class LocalLooseboardServiceTest { ReflectionTestUtils.setField(service, "lastlegHistories", lastlegHistories); ReflectionTestUtils.setField(service, "lastlegOrderNoGenerator", lastlegOrderNoGenerator); ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper().findAndRegisterModules()); - org.mockito.Mockito.lenient().when(lastlegOrderNoGenerator.next()).thenReturn("LMO-LOOSE-1"); + org.mockito.Mockito.lenient().when(lastlegOrderNoGenerator.next(anyString(), any(), anyLong())) + .thenReturn("LB-WH-20260901-000001"); org.mockito.Mockito.lenient().when(operations.findKey(anyString(), any(), anyString())) .thenAnswer(call -> operationRows.get(call.getArgument(2))); org.mockito.Mockito.lenient().when(operations.insert(any(LocalLooseboardOperation.class))).thenAnswer(call -> { diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementInventoryBoundaryTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementInventoryBoundaryTest.java index 7e5702813..6288bad5f 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementInventoryBoundaryTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementInventoryBoundaryTest.java @@ -23,6 +23,9 @@ class OverseasProcurementInventoryBoundaryTest { assertFalse(source.contains("availablePackages")); assertFalse(source.contains("addOutbound")); assertFalse(source.contains("OverseasProcurementOutbound")); - assertFalse(source.contains("outboundQuantity")); + String responses = Files.readString(Path.of( + "src/main/java/com/baiyue/lastleg/procurement/dto/OverseasProcurementResponses.java"), + StandardCharsets.UTF_8); + assertTrue(responses.contains("outboundQuantity")); } } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementSchemaContractTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementSchemaContractTest.java index b6c1159a0..07e85bdfd 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementSchemaContractTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/OverseasProcurementSchemaContractTest.java @@ -20,6 +20,7 @@ class OverseasProcurementSchemaContractTest { assertTrue(sql.contains("operation_type VARCHAR(32) NOT NULL")); assertTrue(sql.contains("review_idempotency_key VARCHAR(255) NULL")); assertTrue(sql.contains("review_request_hash CHAR(64) NULL")); + assertTrue(sql.contains("outbound_details JSON NULL COMMENT '出库明细'")); assertTrue(sql.contains("idempotency_key VARCHAR(255) NULL COMMENT '操作幂等键'")); assertTrue(sql.contains("change_detail JSON NULL")); org.junit.jupiter.api.Assertions.assertFalse(sql.contains("CREATE TABLE IF NOT EXISTS ll_overseas_procurement_outbound")); @@ -39,6 +40,8 @@ class OverseasProcurementSchemaContractTest { "ALTER TABLE ll_overseas_procurement_order ADD COLUMN review_idempotency_key VARCHAR(255) NULL")); assertTrue(sql.contains( "ALTER TABLE ll_overseas_procurement_order ADD COLUMN review_request_hash CHAR(64) NULL")); + assertTrue(sql.contains( + "ALTER TABLE ll_overseas_procurement_order ADD COLUMN outbound_details JSON NULL")); assertTrue(sql.contains( "table_name='ll_overseas_procurement_status_history' AND column_name='idempotency_key'")); } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/service/OverseasProcurementServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/service/OverseasProcurementServiceTest.java index a73d2f04a..f96d0f514 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/service/OverseasProcurementServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/procurement/service/OverseasProcurementServiceTest.java @@ -45,6 +45,7 @@ class OverseasProcurementServiceTest { ReflectionTestUtils.setField(service, "histories", histories); ReflectionTestUtils.setField(service, "warehouseDirectory", warehouseDirectory); ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper()); + ReflectionTestUtils.setField(service, "orderNoGenerator", new com.baiyue.lastleg.service.LastlegOrderNoGenerator()); } @Test @@ -63,9 +64,32 @@ class OverseasProcurementServiceTest { assertEquals(OverseasProcurementService.PENDING_REVIEW, detail.summary().status()); assertEquals(0, detail.summary().inboundQuantity()); + assertEquals(0, detail.summary().outboundQuantity()); assertEquals(0, detail.summary().inventoryQuantity()); } + @Test + void outboundQuantityAndDisplayStatusFollowOutboundDetails() { + OverseasProcurementOrder order = order(OverseasProcurementService.APPROVED, 90, 0); + when(orders.findNo("100", "HC1")).thenReturn(order); + when(histories.listOrder("100", 1L)).thenReturn(List.of()); + + order.setOutboundDetails("[]"); + assertEquals("PENDING_OUTBOUND", service.detail("100", "HC1").summary().status()); + + order.setOutboundDetails("[{\"id\":\"1\",\"quantity\":1}]"); + var partial = service.detail("100", "HC1").summary(); + assertEquals(1, partial.outboundQuantity()); + assertEquals(89, partial.inventoryQuantity()); + assertEquals("PARTIALLY_OUTBOUND", partial.status()); + + order.setOutboundDetails("[{\"id\":\"1\",\"quantity\":90}]"); + var completed = service.detail("100", "HC1").summary(); + assertEquals(90, completed.outboundQuantity()); + assertEquals(0, completed.inventoryQuantity()); + assertEquals("OUTBOUNDED", completed.status()); + } + @Test void pendingOrderCannotReceiveInventory() { OverseasProcurementOrder order = order(OverseasProcurementService.PENDING_REVIEW, 0, 0); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/relabel/service/RelabelApplicationServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/relabel/service/RelabelApplicationServiceTest.java index 97a8dad24..a6b93f678 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/relabel/service/RelabelApplicationServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/relabel/service/RelabelApplicationServiceTest.java @@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.server.ResponseStatusException; @@ -44,7 +45,7 @@ class RelabelApplicationServiceTest { @Mock RelabelStatusHistoryMapper histories; @Mock LastlegOrderMapper orders; @Mock LastlegOrderStatusHistoryMapper orderHistories; - @Mock LastlegOrderNoGenerator orderNoGenerator; + @Spy LastlegOrderNoGenerator orderNoGenerator = new LastlegOrderNoGenerator(); private RelabelApplicationService service; @@ -58,7 +59,6 @@ class RelabelApplicationServiceTest { ReflectionTestUtils.setField(service, "orderHistories", orderHistories); ReflectionTestUtils.setField(service, "orderNoGenerator", orderNoGenerator); ReflectionTestUtils.setField(service, "objectMapper", mapper()); - lenient().when(orderNoGenerator.next()).thenReturn("LMO-RELABEL-1"); } @Test @@ -91,10 +91,10 @@ class RelabelApplicationServiceTest { assertEquals("PENDING_ASSIGNMENT", tailOrder.getValue().getCurrentStatus()); assertEquals("RELABEL:PENDING_PROCESSING", tailOrder.getValue().getSubOrderStatus()); assertEquals("CUSTOMER", tailOrder.getValue().getSourceRegion()); - assertEquals("LMO-RELABEL-1", tailOrder.getValue().getOrderNo()); + assertTrue(tailOrder.getValue().getOrderNo().matches("RL-WH-\\d{8}-\\d{6}")); assertEquals(tailOrder.getValue().getInboundCode(), tailOrder.getValue().getSourceInboundId()); assertEquals(tailOrder.getValue().getInboundCode(), application.getValue().getRelabelNo()); - assertTrue(tailOrder.getValue().getInboundCode().startsWith("FBR")); + assertTrue(tailOrder.getValue().getInboundCode().startsWith("RL-")); assertEquals(10, tailOrder.getValue().getPackageCount()); assertEquals(6, application.getValue().getNotifiedPackageCount()); assertEquals("SKU-A", relabelItem.getValue().getSkuCode()); diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderNoGeneratorTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderNoGeneratorTest.java index 8c729bf5a..5465b15c9 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderNoGeneratorTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderNoGeneratorTest.java @@ -12,6 +12,7 @@ import org.springframework.test.util.ReflectionTestUtils; import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.times; @@ -53,4 +54,19 @@ class LastlegOrderNoGeneratorTest { when(sequenceMapper.currentValue()).thenReturn(10_000L); assertThrows(IllegalStateException.class, () -> generator.next(LocalDate.of(2026, 8, 27))); } + + @Test void actualContainerNumberIsStableAndShared() { + LastlegOrderNoGenerator generator = new LastlegOrderNoGenerator(); + assertTrue(generator.forContainerNo("2087379674555461633").matches("FL-WH-\\d{8}-\\d{6}")); + assertEquals(generator.forContainerNo("MSCU1"), new LastlegOrderNoGenerator().forContainerNo("MSCU1")); + assertNotEquals(generator.forContainerNo("MSCU1"), generator.forContainerNo("CAB1")); + assertTrue(generator.forContainerNo("MSCU1234567890").length() <= 32); + assertThrows(IllegalArgumentException.class, () -> generator.forContainerNo(" ")); + } + + @Test void formatsBusinessWarehouseAndDate() { + String value = new LastlegOrderNoGenerator().next("OUTBOUND", "warehouse-LAX-01", 123L); + assertTrue(value.matches("OB-LAX-\\d{8}-000123"), value); + assertTrue(value.length() <= 24); + } } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderServiceTest.java index 1dea430bd..81d06af63 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderServiceTest.java @@ -8,9 +8,12 @@ import com.baiyue.lastleg.entity.LastlegOrderDetail; import com.baiyue.lastleg.entity.LastlegOrderStatusHistory; import com.baiyue.lastleg.inboundplan.entity.InboundPlanCarton; import com.baiyue.lastleg.inboundplan.mapper.InboundPlanCartonMapper; +import com.baiyue.lastleg.inventory.command.service.InventoryCommandService; import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper; import com.baiyue.lastleg.mapper.LastlegOrderMapper; import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper; +import com.baiyue.lastleg.mapper.LastlegOrderFeeMapper; +import com.baiyue.lastleg.mapper.LastlegOrderOperationLogMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,11 +25,16 @@ import org.springframework.test.util.ReflectionTestUtils; import java.util.List; import java.time.LocalDateTime; +import java.math.BigDecimal; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -38,8 +46,12 @@ class LastlegOrderServiceTest { @Mock LastlegOrderMapper orders; @Mock LastlegOrderDetailMapper details; @Mock LastlegOrderStatusHistoryMapper histories; + @Mock LastlegOrderFeeMapper fees; + @Mock LastlegOrderOperationLogMapper operations; @Mock InboundPlanCartonMapper cartons; @Mock FbaWarehouseValidator fbaWarehouses; + @Mock InventoryCommandService inventory; + @Mock InboundOrderDetainedOutboxService detainedOutbox; LastlegOrderService service; @BeforeEach @@ -48,12 +60,16 @@ class LastlegOrderServiceTest { ReflectionTestUtils.setField(service, "orderMapper", orders); ReflectionTestUtils.setField(service, "detailMapper", details); ReflectionTestUtils.setField(service, "historyMapper", histories); + ReflectionTestUtils.setField(service, "feeMapper", fees); + ReflectionTestUtils.setField(service, "operationLogMapper", operations); ReflectionTestUtils.setField(service, "cartonMapper", cartons); ReflectionTestUtils.setField(service, "fbaWarehouseValidator", fbaWarehouses); lenient().when(fbaWarehouses.required(any(), any(), any())).thenAnswer(invocation -> new FbaWarehouseValidator.VerifiedFbaWarehouse( Long.parseLong(invocation.getArgument(0)), invocation.getArgument(1), invocation.getArgument(2), "US", "CA")); + ReflectionTestUtils.setField(service, "inventory", inventory); + ReflectionTestUtils.setField(service, "detainedOutbox", detainedOutbox); } @Test @@ -63,6 +79,8 @@ class LastlegOrderServiceTest { when(orders.selectOne(any())).thenReturn(order); when(details.selectList(any())).thenReturn(List.of()); when(histories.selectList(any())).thenReturn(List.of()); + when(fees.selectList(any())).thenReturn(List.of()); + when(operations.selectList(any())).thenReturn(List.of()); var response = service.detail("100", 11L); @@ -102,6 +120,52 @@ class LastlegOrderServiceTest { assertEquals("DESTINATION_ASSIGNED", history.getValue().getToStatus()); } + @Test + void createFeePersistsFeeAndOperationForOwnedOrder() { + when(orders.selectOne(any())).thenReturn(order(11L, "100", "PENDING_ASSIGNMENT", null)); + var request = new LastlegOrderRequests.CreateFee( + "清关费", "USD", new BigDecimal("420.00"), "PENDING_REVIEW", "报关代理"); + + var fee = service.createFee("100", 11L, request, "platform_admin"); + + assertEquals("100", fee.getCustomerId()); + assertEquals("清关费", fee.getFeeItem()); + assertEquals(new BigDecimal("420.00"), fee.getAmount()); + verify(fees).insert(any(com.baiyue.lastleg.entity.LastlegOrderFee.class)); + verify(operations).insert(any(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class)); + } + + @Test + void palletMergeMarkIsPersistedAndAuditedForPdaReminder() { + LastlegOrder order = order(11L, "100", "INBOUND_PLANNED", "IB-1"); + when(orders.selectOne(any())).thenReturn(order); + when(orders.lockById("100", 11L)).thenReturn(order); + when(orders.updatePalletMergeFlag(any(), anyLong(), any(), any())).thenReturn(1); + + service.setPalletMergeRequired("100", 11L, + new LastlegOrderRequests.SetPalletMergeRequired(true), "platform_admin"); + + verify(orders).updatePalletMergeFlag(org.mockito.ArgumentMatchers.eq("100"), + org.mockito.ArgumentMatchers.eq(11L), org.mockito.ArgumentMatchers.eq("REQUIRED"), any()); + ArgumentCaptor operation = + ArgumentCaptor.forClass(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class); + verify(operations).insert(operation.capture()); + assertEquals("标记合托", operation.getValue().getOperationNode()); + assertTrue(operation.getValue().getResult().contains("PDA")); + } + + @Test + void completedPhysicalMergeCannotBeUnmarked() { + LastlegOrder order = order(11L, "100", "INBOUND_PLANNED", "IB-1"); + order.setPalletMergeFlag("MERGED"); + when(orders.selectOne(any())).thenReturn(order); + when(orders.lockById("100", 11L)).thenReturn(order); + + assertThrows(ResponseStatusException.class, () -> service.setPalletMergeRequired("100", 11L, + new LastlegOrderRequests.SetPalletMergeRequired(false), "platform_admin")); + verify(orders, never()).updatePalletMergeFlag(any(), anyLong(), any(), any()); + } + @Test void transferRejectsAnOrderAlreadyBoundToInboundPlan() { LastlegOrder order = order(11L, "100", "DESTINATION_ASSIGNED", "IB-1"); @@ -145,7 +209,7 @@ class LastlegOrderServiceTest { @Test void pageUsesPaginationAndPlatformFiltersWithoutCustomerScope() { - var query = new LastlegOrderRequests.Query(3, 25, "LL-", "FIRST_LEG", "IN-", null, + var query = new LastlegOrderRequests.Query(3L, 25L, "LL-", "FIRST_LEG", "IN-", null, "", "PENDING_ASSIGNMENT", null, "LAX", null, null, null, null, null); var resultPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(3, 25); resultPage.setTotal(0); @@ -222,12 +286,69 @@ class LastlegOrderServiceTest { 15 - pending, 15 - pending, pending, LocalDateTime.of(2026, 10, 3, 9, 0), ready, reason); } + @Test + void pageReturnsPalletMergeRelatedInboundNumbers() { + LastlegOrder order = order(11L, "100", "INBOUND_COMPLETED", "IB-1"); + order.setPalletMergeFlag("MERGED"); + var resultPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(1, 10); + resultPage.setRecords(List.of(order)); + resultPage.setTotal(1); + when(orders.selectPage(any(), any())).thenReturn(resultPage); + when(orders.findPalletMergeRelations(List.of(11L))).thenReturn(List.of( + new LastlegOrderMapper.PalletMergeRelationRow(11L, "INBOUND-2", "PMG-1"), + new LastlegOrderMapper.PalletMergeRelationRow(11L, "INBOUND-3", "PMG-1"))); + var query = new LastlegOrderRequests.Query(1L, 10L, null, null, null, null, + null, null, null, null, null, null, null, null, null); + + var result = service.page(null, query); + + assertEquals("PMG-1", result.records().get(0).palletMergeBatchId()); + assertEquals(List.of("INBOUND-2", "INBOUND-3"), result.records().get(0).palletMergeInboundNos()); + } + + @Test + void holdingBeforeInboundPersistsTheOrderMarkerAndOperationRemark() { + LastlegOrder order = order(11L, "100", "PENDING_INBOUND", null); + when(orders.selectOne(any())).thenReturn(order); + when(orders.lockById("100", 11L)).thenReturn(order); + + service.setDetained("100", 11L, new LastlegOrderRequests.SetDetained(true, "等待质检"), "operator"); + + assertTrue(order.getDetained()); + verify(orders).updateById(order); + ArgumentCaptor operation = + ArgumentCaptor.forClass(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class); + verify(operations).insert(operation.capture()); + assertEquals("暂扣", operation.getValue().getOperationNode()); + assertEquals("等待质检", operation.getValue().getResult()); + verify(detainedOutbox).publish(eq(order), eq(true), eq("等待质检"), isNull(), eq("operator"), any(), anyLong()); + verify(inventory, never()).releaseInboundOrderHold(any(), any(), anyLong(), any(), any()); + } + + @Test + void releasingAfterInboundMovesAllOrderHeldInventoryToAvailable() { + LastlegOrder order = order(11L, "100", "INBOUND_COMPLETED", "IP-1"); + order.setOrderNo("LM-1"); + order.setDetained(true); + when(orders.selectOne(any())).thenReturn(order); + when(orders.lockById("100", 11L)).thenReturn(order); + + service.setDetained("100", 11L, new LastlegOrderRequests.SetDetained(false, "质检通过"), "operator"); + + verify(inventory).releaseInboundOrderHold("100", "operator", 11L, "LM-1", "质检通过"); + assertEquals(false, order.getDetained()); + verify(detainedOutbox).publish(eq(order), eq(false), eq("质检通过"), isNull(), eq("operator"), any(), anyLong()); + } + private static LastlegOrder order(long id, String customerId, String status, String inboundOrderNo) { LastlegOrder order = new LastlegOrder(); order.setId(id); order.setCustomerId(customerId); order.setCurrentStatus(status); order.setInboundOrderNo(inboundOrderNo); + order.setOrderNo("LM-" + id); + order.setSourceInboundId(String.valueOf(1000 + id)); + order.setInboundCode("IN-" + id); return order; } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LoadingCompletedConsumerTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LoadingCompletedConsumerTest.java index 0a986e943..77975a237 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/service/LoadingCompletedConsumerTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/service/LoadingCompletedConsumerTest.java @@ -5,7 +5,7 @@ class LoadingCompletedConsumerTest { private final Map frozenContainerNos=new HashMap<>(); @Mock LastlegEventInboxMapper inbox; @Mock LastlegOrderMapper orders; @Mock LastlegInboundOrderMapper inboundOrders; @Mock LastlegOrderDetailMapper details; @Mock LastlegOrderStatusHistoryMapper histories; @Mock InboundPlanCartonMapper cartons; @Mock DropshipOrderService dropshipOrders; @Mock InventoryCommandService inventory; @Mock com.baiyue.lastleg.shipping.mapper.ContainerShippingGateMapper shippingGates; @Mock LastlegOrderNoDailySequenceMapper sequences; ObjectMapper json=new ObjectMapper().findAndRegisterModules(); LoadingCompletedConsumer service; @BeforeEach void setup(){ service=new LoadingCompletedConsumer(); ReflectionTestUtils.setField(service,"inboxMapper",inbox); ReflectionTestUtils.setField(service,"orderMapper",orders); ReflectionTestUtils.setField(service,"inboundOrderMapper",inboundOrders); ReflectionTestUtils.setField(service,"detailMapper",details); ReflectionTestUtils.setField(service,"historyMapper",histories); ReflectionTestUtils.setField(service,"cartonMapper",cartons); ReflectionTestUtils.setField(service,"dropshipOrders",dropshipOrders); ReflectionTestUtils.setField(service,"inventory",inventory); ReflectionTestUtils.setField(service,"shippingGates",shippingGates); ReflectionTestUtils.setField(service,"objectMapper",json); LastlegOrderNoGenerator generator=new LastlegOrderNoGenerator(); ReflectionTestUtils.setField(generator,"sequenceMapper",sequences); ReflectionTestUtils.setField(service,"orderNoGenerator",generator); lenient().when(sequences.currentValue()).thenReturn(1L); lenient().doAnswer(i->{ LastlegOrder o=i.getArgument(0); o.setId(Math.abs(o.getSourceInboundId().hashCode())+1L); return 1; }).when(orders).insert(any(LastlegOrder.class)); lenient().when(orders.lockContainerNumbers(anyString())).thenReturn(List.of()); lenient().when(shippingGates.initialize(anyString(),anyString(),anyString(),anyString(),anyLong(),anyLong())).thenAnswer(i->{ String key=i.getArgument(0); String value=i.getArgument(3); return frozenContainerNos.putIfAbsent(key,value)==null?1:0; }); lenient().when(shippingGates.lock(anyString())).thenAnswer(i->{ String key=i.getArgument(0); String value=frozenContainerNos.get(key); if(value==null)return null; var gate=new com.baiyue.lastleg.shipping.entity.ContainerShippingGate(); gate.setContainerBookingId(key); gate.setContainerNo(value); return gate; }); } - @Test void createsPendingOrdersAndInTransitSnapshotsWithoutInboundPlans(){ EventEnvelope event=event(); assertEquals(2,service.consume(event)); assertThrows(NoSuchFieldException.class,()->LoadingCompletedConsumer.class.getDeclaredField("inboundPlans")); verify(orders,times(2)).insert(any(LastlegOrder.class)); ArgumentCaptor inTransit=ArgumentCaptor.forClass(InventoryCommandRequests.InTransit.class); verify(inventory,times(2)).registerInTransit(eq("100"),eq("SYSTEM"),inTransit.capture()); assertEquals(Set.of("RK1","RK2"),inTransit.getAllValues().stream().map(InventoryCommandRequests.InTransit::inboundOrderNo).collect(java.util.stream.Collectors.toSet())); verify(details,times(2)).insert(any(LastlegOrderDetail.class)); verify(histories,times(2)).insert(any(LastlegOrderStatusHistory.class)); ArgumentCaptor c=ArgumentCaptor.forClass(LastlegOrder.class); verify(orders,times(2)).insert(c.capture()); assertEquals(Set.of("in-1","in-2"),c.getAllValues().stream().map(LastlegOrder::getSourceInboundId).collect(java.util.stream.Collectors.toSet())); assertTrue(c.getAllValues().stream().allMatch(o->"PENDING_ASSIGNMENT".equals(o.getCurrentStatus()))); assertTrue(c.getAllValues().stream().allMatch(o->o.getInboundOrderNo()==null)); assertTrue(c.getAllValues().stream().allMatch(o->o.getId()!=null)); assertTrue(c.getAllValues().stream().allMatch(o->"MSCU6639870".equals(o.getContainerNo()))); assertTrue(c.getAllValues().stream().allMatch(o->"FBA".equals(o.getDestinationType()))); assertTrue(c.getAllValues().stream().allMatch(o->"Customer 100".equals(o.getCustomerName()))); assertTrue(c.getAllValues().stream().allMatch(o->"ow-1".equals(o.getSourceWarehouseId())&&o.getOverseasWarehouseId()==null)); ArgumentCaptor snapshots=ArgumentCaptor.forClass(LastlegInboundOrder.class); verify(inboundOrders,times(2)).insert(snapshots.capture()); assertTrue(snapshots.getAllValues().stream().allMatch(o->"100".equals(o.getCustomerId())&&"FBA".equals(o.getDestinationType()))); assertEquals(1,c.getAllValues().stream().map(LastlegOrder::getOrderNo).distinct().count()); assertTrue(c.getAllValues().get(0).getOrderNo().matches("LMO-[0-9]{6}-0001")); } + @Test void createsPendingOrdersAndInTransitSnapshotsWithoutInboundPlans(){ EventEnvelope event=event(); assertEquals(2,service.consume(event)); assertThrows(NoSuchFieldException.class,()->LoadingCompletedConsumer.class.getDeclaredField("inboundPlans")); verify(orders,times(2)).insert(any(LastlegOrder.class)); ArgumentCaptor inTransit=ArgumentCaptor.forClass(InventoryCommandRequests.InTransit.class); verify(inventory,times(2)).registerInTransit(eq("100"),eq("SYSTEM"),inTransit.capture()); assertEquals(Set.of("RK1","RK2"),inTransit.getAllValues().stream().map(InventoryCommandRequests.InTransit::inboundOrderNo).collect(java.util.stream.Collectors.toSet())); verify(details,times(2)).insert(any(LastlegOrderDetail.class)); verify(histories,times(2)).insert(any(LastlegOrderStatusHistory.class)); ArgumentCaptor c=ArgumentCaptor.forClass(LastlegOrder.class); verify(orders,times(2)).insert(c.capture()); assertEquals(Set.of("in-1","in-2"),c.getAllValues().stream().map(LastlegOrder::getSourceInboundId).collect(java.util.stream.Collectors.toSet())); assertTrue(c.getAllValues().stream().allMatch(o->"PENDING_ASSIGNMENT".equals(o.getCurrentStatus()))); assertTrue(c.getAllValues().stream().allMatch(o->o.getInboundOrderNo()==null)); assertTrue(c.getAllValues().stream().allMatch(o->o.getId()!=null)); assertTrue(c.getAllValues().stream().allMatch(o->"MSCU6639870".equals(o.getContainerNo()))); assertTrue(c.getAllValues().stream().allMatch(o->"FBA".equals(o.getDestinationType()))); assertTrue(c.getAllValues().stream().allMatch(o->"Customer 100".equals(o.getCustomerName()))); assertTrue(c.getAllValues().stream().allMatch(o->"ow-1".equals(o.getSourceWarehouseId())&&o.getOverseasWarehouseId()==null)); ArgumentCaptor snapshots=ArgumentCaptor.forClass(LastlegInboundOrder.class); verify(inboundOrders,times(2)).insert(snapshots.capture()); assertTrue(snapshots.getAllValues().stream().allMatch(o->"100".equals(o.getCustomerId())&&"FBA".equals(o.getDestinationType()))); assertEquals(1,c.getAllValues().stream().map(LastlegOrder::getOrderNo).distinct().count()); assertTrue(c.getAllValues().get(0).getOrderNo().matches("FL-WH-[0-9]{8}-[0-9]{6}")); } @Test void freezesFormalPlannedWarehouseArrivalFromLoadingEvent(){ EventEnvelope source=event(); JsonNode node=source.payload().deepCopy(); ((com.fasterxml.jackson.databind.node.ObjectNode)node).put("estimatedWarehouseArrivalAt","2026-10-03T09:00"); String hash=EventPayloadHash.sha256(EventPayloadCanonicalizer.canonicalize(node)); EventEnvelope changed=new EventEnvelope(source.eventId(),source.eventType(),source.eventVersion(),source.occurredAt(),source.sourceRegion(),source.targetRegion(),source.tenantId(),source.aggregateType(),source.aggregateId(),source.aggregateVersion(),source.handoverVersion(),source.traceId(),source.correlationId(),source.causationId(),node,hash); service.consume(changed); ArgumentCaptor captor=ArgumentCaptor.forClass(LastlegOrder.class); verify(orders,times(2)).insert(captor.capture()); assertTrue(captor.getAllValues().stream().allMatch(order->java.time.LocalDateTime.of(2026,10,3,9,0).equals(order.getEstimatedWarehouseAt()))); } @Test void loadingCreatesAwaitingPickupReadyGateWithoutPollutingShippingVersion(){ service.consume(event()); verify(shippingGates).initialize("book-1","100","","MSCU6639870",0L,1L); } @Test void rejectsUnnormalizedActualContainerNumberInsteadOfUsingCabinetNumber(){ EventEnvelope source=event(); JsonNode node=source.payload().deepCopy(); ((com.fasterxml.jackson.databind.node.ObjectNode)node).put("containerNo"," mscu6639870 ").put("cabinetNo","FALLBACK-CABINET"); String hash=EventPayloadHash.sha256(EventPayloadCanonicalizer.canonicalize(node)); EventEnvelope changed=new EventEnvelope(source.eventId(),source.eventType(),source.eventVersion(),source.occurredAt(),source.sourceRegion(),source.targetRegion(),source.tenantId(),source.aggregateType(),source.aggregateId(),source.aggregateVersion(),source.handoverVersion(),source.traceId(),source.correlationId(),source.causationId(),node,hash); assertThrows(IllegalArgumentException.class,()->service.consume(changed)); verify(inbox,never()).insert(any(LastlegEventInbox.class)); verify(orders,never()).insert(any(LastlegOrder.class)); } diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/service/TaillegRealConsumerAcceptanceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/service/TaillegRealConsumerAcceptanceTest.java index c3df43abc..4680db2b1 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/service/TaillegRealConsumerAcceptanceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/service/TaillegRealConsumerAcceptanceTest.java @@ -22,6 +22,8 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.jdbc.core.JdbcTemplate; +import com.baiyue.lastleg.shipping.mapper.ContainerShippingGateMapper; +import org.mockito.Mockito; import java.math.BigDecimal; import java.time.LocalDate; @@ -64,6 +66,10 @@ class TaillegRealConsumerAcceptanceTest { "com.baiyue.lastleg.inventory.command.mapper", "com.baiyue.lastleg.relabel.mapper", "com.baiyue.lastleg.transfer.mapper", "com.baiyue.lastleg.looseboard.mapper"}) static class AcceptanceApplication { + @Bean ContainerShippingGateMapper containerShippingGateMapper() { + return Mockito.mock(ContainerShippingGateMapper.class); + } + @Bean WarehouseResourceGateway warehouseResourceGateway() { return new WarehouseResourceGateway() { public WarehousePalletReference ensureDropshipPallet(String customerId, long warehouseId, String businessId) { diff --git a/lastleg-service/src/test/java/com/baiyue/lastleg/transfer/service/WarehouseTransferServiceTest.java b/lastleg-service/src/test/java/com/baiyue/lastleg/transfer/service/WarehouseTransferServiceTest.java index df28e085b..b9135807a 100644 --- a/lastleg-service/src/test/java/com/baiyue/lastleg/transfer/service/WarehouseTransferServiceTest.java +++ b/lastleg-service/src/test/java/com/baiyue/lastleg/transfer/service/WarehouseTransferServiceTest.java @@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.server.ResponseStatusException; @@ -46,6 +47,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.lenient; @@ -70,7 +73,7 @@ class WarehouseTransferServiceTest { @Mock RelabelApplicationMapper relabelApplications; @Mock RelabelItemMapper relabelItems; @Mock RelabelStatusHistoryMapper relabelHistories; - @Mock LastlegOrderNoGenerator orderNoGenerator; + @Spy LastlegOrderNoGenerator orderNoGenerator = new LastlegOrderNoGenerator(); @Mock InventoryCommandService inventory; @Mock InventoryCommandMapper inventoryCommands; private WarehouseTransferService service; @@ -94,7 +97,8 @@ class WarehouseTransferServiceTest { ReflectionTestUtils.setField(service, "inventory", inventory); ReflectionTestUtils.setField(service, "inventoryCommands", inventoryCommands); ReflectionTestUtils.setField(service, "objectMapper", new ObjectMapper().findAndRegisterModules()); - lenient().when(orderNoGenerator.next()).thenReturn("LMO-TRANSFER-1"); + lenient().when(orderNoGenerator.next(anyString(), any(), anyLong())) + .thenReturn("WT-WH-20260901-000001"); lenient().when(inventoryAllocations.selectList(any())).thenReturn(List.of()); lenient().when(transferReceipts.selectList(any())).thenReturn(List.of()); } @@ -116,7 +120,7 @@ class WarehouseTransferServiceTest { assertEquals(WarehouseTransferService.RETURN_RELABEL_ORDER_TYPE, tailOrder.getValue().getOrderType()); assertEquals("PENDING_ASSIGNMENT", tailOrder.getValue().getCurrentStatus()); assertEquals("WAREHOUSE_TRANSFER:PENDING_REVIEW;RELABEL:PENDING_PROCESSING", tailOrder.getValue().getSubOrderStatus()); - assertEquals("LMO-TRANSFER-1", tailOrder.getValue().getOrderNo()); + assertTrue(tailOrder.getValue().getOrderNo().matches("WT-WH-\\d{8}-\\d{6}")); assertNull(tailOrder.getValue().getTransferWarehouseId()); verify(sourceOrders).insert(any(WarehouseTransferSourceOrder.class)); verify(relabelApplications).insert(any(RelabelApplication.class)); diff --git a/order/AGENTS.md b/order/AGENTS.md index 897fd98c9..6022f661f 100644 --- a/order/AGENTS.md +++ b/order/AGENTS.md @@ -20,7 +20,7 @@ | 下单计价渠道校验 | `service/OrderPricingChannelGuard.java`、`client/FirstlegPricingClient.java` | | 下单仓库主数据守卫 | `service/OrderMasterDataGuard.java`、`client/WarehouseClient.java`;校验公共收货仓、目的仓类型、状态、地址与收货能力,并保存服务端快照 | | 统一订单七状态与操作映射 | `../../docs/统一订单主状态与操作契约.md`;`LOADED` 硬门禁和 `DELIVERED` 可信回传同时核对三期需求确认表与开工门禁 | -| 货物内部查询与箱唛绑定来源 | `controller/InternalOrderCargoController.java`;Firstleg 消费端为 `../firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java` | +| 货物内部查询与箱唛绑定来源 | `controller/InternalOrderCargoController.java` 向 Firstleg 提供箱唛生成的 SKU 快照来源;`CargoLabelBinding.skuNo` 对 `ONE_CLICK_DROPSHIP` 为强制字段。Firstleg 消费端为 `../firstleg/src/main/java/com/baiyue/firstleg/client/OrderCargoClient.java`,变更两端 DTO 必须同步。 | | 快照/Outbox | `service/`、`repository/`、`event/` | | DTO/持久化 | `dto/`、`entity/`、`mapper/`、`repository/` | | SQL/OpenAPI | `src/main/resources/db/`、`src/main/resources/openapi/` | @@ -32,6 +32,7 @@ - `inbound_code` 是跨客户全局唯一业务码;生成模式由 `InboundCodeModeProvider` 控制,序列与唯一约束不得退化为客户内唯一。修改格式、长度或生成时机时同步检查 Order SQL、Firstleg 入库快照、箱唛箱码、前端展示和全局唯一性契约测试。 - 下单和后续关键状态操作不得信任客户端仓库名称、类型或启用状态;公共仓库必须通过 Warehouse 内部接口按 ID 实时校验。私人地址不得伪装为公共目的仓,非私人目的地必须引用类型匹配的公共仓库;Warehouse 不可用时失败关闭,不回退到旧快照或模拟数据。 - Warehouse 往来单位、Firstleg 入库协作只通过客户端 DTO/内部令牌;修改契约需同步消费者。 +- 对 `ONE_CLICK_DROPSHIP`,货物明细 SKU 是箱唛绑定及海外一件代发库存追踪的前置条件,不得依赖货名、箱规或其他展示字段推导 SKU。修改 `CargoLabelBinding` 时同步更新 Firstleg 客户端、相关测试与服务间契约。 ## 全量验收导航 diff --git a/order/src/main/java/com/baiyue/order/controller/InternalOrderCargoController.java b/order/src/main/java/com/baiyue/order/controller/InternalOrderCargoController.java index bf34d41c5..aff44bcb5 100644 --- a/order/src/main/java/com/baiyue/order/controller/InternalOrderCargoController.java +++ b/order/src/main/java/com/baiyue/order/controller/InternalOrderCargoController.java @@ -74,8 +74,8 @@ public class InternalOrderCargoController { } return ApiResponse.success(orders.findCargoInheritedByOrder(tenantId, orderId).stream() .map(cargo -> new CargoLabelBinding(cargo.getId(), orderId, cargo.getItemNo(), - cargo.getCargoName(), cargo.getPackageCount(), cargo.getLengthCm(), cargo.getWidthCm(), - cargo.getHeightCm())) + cargo.getCargoName(), cargo.getSkuNo(), cargo.getPackageCount(), cargo.getLengthCm(), + cargo.getWidthCm(), cargo.getHeightCm())) .toList()); } @@ -155,8 +155,13 @@ public class InternalOrderCargoController { } public record CargoLabelBinding(Long cargoId, Long orderId, Integer itemNo, String cargoName, - Integer packageCount, java.math.BigDecimal lengthCm, + String skuNo, Integer packageCount, java.math.BigDecimal lengthCm, java.math.BigDecimal widthCm, java.math.BigDecimal heightCm) { + public CargoLabelBinding(Long cargoId, Long orderId, Integer itemNo, String cargoName, + Integer packageCount, java.math.BigDecimal lengthCm, + java.math.BigDecimal widthCm, java.math.BigDecimal heightCm) { + this(cargoId, orderId, itemNo, cargoName, null, packageCount, lengthCm, widthCm, heightCm); + } } public record MeasurementDeclarationSnapshot( diff --git a/pom.xml b/pom.xml index 20134475d..73064fb44 100644 --- a/pom.xml +++ b/pom.xml @@ -16,19 +16,6 @@ 柏悦头程平台 柏悦悦物流国内头程业务微服务平台 - - - baiyue-nexus-releases - 柏悦 Nexus 正式制品库 - http://8.163.30.128:18081/repository/maven-releases/ - - - baiyue-nexus-snapshots - 柏悦 Nexus 快照制品库 - http://8.163.30.128:18081/repository/maven-snapshots/ - - - platform-bom common-core @@ -69,6 +56,10 @@ 4.9.8.5 1.5.0 2.3.3 + 127.0.0.1:8848 + public + nacos + nacos @@ -128,7 +119,19 @@ - org.springframework.bootspring-boot-maven-plugin + + org.springframework.boot + spring-boot-maven-plugin + + + ${nacos.server-addr} + ${nacos.namespace} + BAIYUE + ${nacos.username} + ${nacos.password} + + + org.apache.maven.pluginsmaven-compiler-plugin ${maven.compiler.plugin.version} @@ -170,6 +173,33 @@ + + local + + 127.0.0.1:8848 + public + nacos + nacos + + + + dev + + dev-nacos.buggoglobal.com:8848 + ${env.DEV_NACOS_NAMESPACE} + nacos + ${env.DEV_NACOS_PASSWORD} + + + + test + + 8.163.30.128:8848 + d99d7998-5bb0-4ffe-a2a0-1a564ce14463 + nacos + ${env.TEST_NACOS_PASSWORD} + + static-analysis-report diff --git a/scripts/db/incremental.sql b/scripts/db/incremental.sql index 81332bff9..d4d10ae15 100644 --- a/scripts/db/incremental.sql +++ b/scripts/db/incremental.sql @@ -239,6 +239,9 @@ CREATE TABLE IF NOT EXISTS ll_inbound_plan ( UNIQUE KEY uk_ll_inbound_plan_container (tenant_id,container_no) ) ENGINE=InnoDB COMMENT='尾程入库计划表:每个租户的每个集装箱生成一张入库单'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inbound_plan' AND column_name='version'), 'SELECT 1', 'ALTER TABLE ll_inbound_plan ADD COLUMN version INT NOT NULL DEFAULT 0 COMMENT ''入库计划乐观锁版本'' AFTER updated_at')); +PREPARE ll_inbound_plan_version_ddl FROM @ddl; EXECUTE ll_inbound_plan_version_ddl; DEALLOCATE PREPARE ll_inbound_plan_version_ddl; + CREATE TABLE IF NOT EXISTS ll_inbound_plan_line ( id BIGINT NOT NULL COMMENT '入库计划明细ID(雪花ID)', plan_id BIGINT NOT NULL COMMENT '入库计划ID', @@ -440,6 +443,7 @@ CREATE TABLE IF NOT EXISTS ll_order ( relabel_order_no VARCHAR(128) NULL COMMENT '换标单号', relabel_status VARCHAR(32) NULL COMMENT '换标状态', pallet_merge_flag VARCHAR(32) NULL COMMENT '合托标记', + pallet_merge_batch_id VARCHAR(64) NULL COMMENT '平台预标记合托分组号', current_status VARCHAR(32) NOT NULL DEFAULT 'PENDING_ASSIGNMENT' COMMENT '当前业务状态', private_remark VARCHAR(1000) NULL COMMENT '备注(仅自己)', customer_remark VARCHAR(1000) NULL COMMENT '备注(客户)', @@ -457,6 +461,9 @@ CREATE TABLE IF NOT EXISTS ll_order ( KEY idx_ll_order_list (tenant_id,current_status,created_at) ) ENGINE=InnoDB COMMENT='尾程订单主表'; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_order' AND column_name='pallet_merge_batch_id'), 'SELECT 1', 'ALTER TABLE ll_order ADD COLUMN pallet_merge_batch_id VARCHAR(64) NULL COMMENT ''平台预标记合托分组号'' AFTER pallet_merge_flag')); +PREPARE ll_order_merge_batch_ddl FROM @ddl; EXECUTE ll_order_merge_batch_ddl; DEALLOCATE PREPARE ll_order_merge_batch_ddl; + CREATE TABLE IF NOT EXISTS ll_order_detail ( id BIGINT NOT NULL AUTO_INCREMENT COMMENT '订单明细主键', order_id BIGINT NOT NULL COMMENT '尾程订单ID', @@ -1386,7 +1393,7 @@ CREATE TABLE IF NOT EXISTS ll_overseas_procurement_order ( product_specification VARCHAR(500) NULL COMMENT '产品规格', product_url VARCHAR(2000) NULL COMMENT '产品链接', purchase_quantity BIGINT NOT NULL COMMENT '采购数量', budget_unit_price DECIMAL(18,2) NULL COMMENT '预算单价', currency CHAR(3) NOT NULL DEFAULT 'USD' COMMENT '币种', receiving_address VARCHAR(1000) NULL COMMENT '收货地址', contact_name VARCHAR(128) NOT NULL COMMENT '联系人姓名', contact_phone VARCHAR(64) NOT NULL COMMENT '联系人电话', purchase_warehouse_id VARCHAR(128) NULL COMMENT '采购入仓仓库ID', - purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', inbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '累计入库数量', outbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '累计出库数量', + purchase_warehouse_name VARCHAR(255) NULL COMMENT '采购入仓仓库名称', inbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '累计入库数量', outbound_details JSON NULL COMMENT '出库明细', outbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT '累计出库数量', domestic_carrier VARCHAR(128) NULL COMMENT '境内承运商', domestic_tracking_no VARCHAR(128) NULL COMMENT '境内物流单号', overseas_carrier VARCHAR(128) NULL COMMENT '海外承运商', overseas_tracking_no VARCHAR(128) NULL COMMENT '海外物流单号', estimated_arrival_at DATETIME(3) NULL COMMENT '预计到达时间', status VARCHAR(32) NOT NULL DEFAULT 'PENDING_REVIEW' COMMENT '业务状态', remark VARCHAR(1000) NULL COMMENT '备注', idempotency_key VARCHAR(255) NOT NULL COMMENT '业务幂等键', request_hash CHAR(64) NOT NULL COMMENT '请求内容哈希', @@ -3195,6 +3202,8 @@ SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE tab PREPARE schema_comments_025 FROM @ddl; EXECUTE schema_comments_025; DEALLOCATE PREPARE schema_comments_025; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_overseas_procurement_order' AND column_name='inbound_quantity' AND COALESCE(column_comment,'') <> '累计入库数量'), 'ALTER TABLE ll_overseas_procurement_order MODIFY COLUMN inbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT ''累计入库数量''', 'SELECT 1')); PREPARE schema_comments_025 FROM @ddl; EXECUTE schema_comments_025; DEALLOCATE PREPARE schema_comments_025; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_overseas_procurement_order' AND column_name='outbound_details'), 'SELECT 1', 'ALTER TABLE ll_overseas_procurement_order ADD COLUMN outbound_details JSON NULL COMMENT ''出库明细'' AFTER inbound_quantity')); +PREPARE schema_comments_025 FROM @ddl; EXECUTE schema_comments_025; DEALLOCATE PREPARE schema_comments_025; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_overseas_procurement_order' AND column_name='outbound_quantity' AND COALESCE(column_comment,'') <> '累计出库数量'), 'ALTER TABLE ll_overseas_procurement_order MODIFY COLUMN outbound_quantity BIGINT NOT NULL DEFAULT 0 COMMENT ''累计出库数量''', 'SELECT 1')); PREPARE schema_comments_025 FROM @ddl; EXECUTE schema_comments_025; DEALLOCATE PREPARE schema_comments_025; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_overseas_procurement_order' AND column_name='domestic_carrier' AND COALESCE(column_comment,'') <> '境内承运商'), 'ALTER TABLE ll_overseas_procurement_order MODIFY COLUMN domestic_carrier VARCHAR(128) NULL COMMENT ''境内承运商''', 'SELECT 1')); @@ -3805,7 +3814,107 @@ SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE tab AND table_name='ll_cargo_discard_application' AND column_name='review_idempotency_key'), 'SELECT 1', 'ALTER TABLE ll_cargo_discard_application ADD COLUMN review_idempotency_key VARCHAR(255) NULL COMMENT ''审核幂等键'' AFTER request_hash')); PREPARE phase4_discard_review_stmt FROM @ddl; EXECUTE phase4_discard_review_stmt; DEALLOCATE PREPARE phase4_discard_review_stmt; + +-- 一件代发出库与 PDA 按托盘拣货出库。 +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_order' AND column_name='outbound_type'), 'SELECT 1', 'ALTER TABLE ll_outbound_order ADD COLUMN outbound_type VARCHAR(32) NOT NULL DEFAULT ''DISPATCH_DELIVERY'' COMMENT ''出库类型'' AFTER status')); +PREPARE dropship_outbound_stmt FROM @ddl; EXECUTE dropship_outbound_stmt; DEALLOCATE PREPARE dropship_outbound_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_order' AND column_name='recipient_info'), 'SELECT 1', 'ALTER TABLE ll_outbound_order ADD COLUMN recipient_info VARCHAR(1000) NULL COMMENT ''一件代发收件信息'' AFTER outbound_type')); +PREPARE dropship_outbound_stmt FROM @ddl; EXECUTE dropship_outbound_stmt; DEALLOCATE PREPARE dropship_outbound_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='picked_package_count'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN picked_package_count INT NOT NULL DEFAULT 0 COMMENT ''已拣箱数'' AFTER package_count')); +PREPARE dropship_outbound_stmt FROM @ddl; EXECUTE dropship_outbound_stmt; DEALLOCATE PREPARE dropship_outbound_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='status'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN status VARCHAR(32) NOT NULL DEFAULT ''WAIT_PICK'' COMMENT ''拣货状态'' AFTER picked_package_count')); +PREPARE dropship_outbound_stmt FROM @ddl; EXECUTE dropship_outbound_stmt; DEALLOCATE PREPARE dropship_outbound_stmt; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_outbound_pallet_item' AND column_name='sku_code'), 'SELECT 1', 'ALTER TABLE ll_outbound_pallet_item ADD COLUMN sku_code VARCHAR(128) NULL COMMENT ''一件代发SKU'' AFTER status')); +PREPARE dropship_outbound_stmt FROM @ddl; EXECUTE dropship_outbound_stmt; DEALLOCATE PREPARE dropship_outbound_stmt; +ALTER TABLE ll_outbound_order MODIFY COLUMN dispatch_order_id BIGINT NULL COMMENT '派车单ID,一件代发为空', MODIFY COLUMN dispatch_no VARCHAR(64) NULL COMMENT '派车单号,一件代发为空'; +CREATE TABLE IF NOT EXISTS ll_outbound_pda_task (id BIGINT NOT NULL COMMENT '主键ID',customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键',outbound_order_id BIGINT NOT NULL COMMENT '出库单ID',task_no VARCHAR(64) NOT NULL COMMENT 'PDA任务号',status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态',pushed_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '推送时间',completed_at DATETIME(3) NULL COMMENT '完成时间',created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',PRIMARY KEY(id),UNIQUE KEY uk_ll_outbound_pda_task_order(customer_id,outbound_order_id),UNIQUE KEY uk_ll_outbound_pda_task_no(customer_id,task_no)) ENGINE=InnoDB COMMENT='一件代发出库PDA拣货任务'; +CREATE TABLE IF NOT EXISTS ll_outbound_pda_pick (id BIGINT NOT NULL COMMENT '主键ID',customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键',task_id BIGINT NOT NULL COMMENT 'PDA任务ID',outbound_pallet_item_id BIGINT NOT NULL COMMENT '出库托盘明细ID',inventory_position_id BIGINT NOT NULL COMMENT '库存仓位ID',packages BIGINT NOT NULL COMMENT '拣货箱数',idempotency_key VARCHAR(255) NOT NULL COMMENT '拣货幂等键',picked_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '拣货时间',PRIMARY KEY(id),UNIQUE KEY uk_ll_outbound_pda_pick_key(customer_id,idempotency_key)) ENGINE=InnoDB COMMENT='一件代发出库PDA托盘拣货记录'; SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_cargo_discard_application' AND column_name='review_request_hash'), 'SELECT 1', 'ALTER TABLE ll_cargo_discard_application ADD COLUMN review_request_hash CHAR(64) NULL COMMENT ''审核请求内容哈希'' AFTER review_idempotency_key')); PREPARE phase4_discard_review_stmt FROM @ddl; EXECUTE phase4_discard_review_stmt; DEALLOCATE PREPARE phase4_discard_review_stmt; +CREATE TABLE IF NOT EXISTS ll_order_fee ( + id BIGINT NOT NULL COMMENT '费用明细主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', fee_item VARCHAR(128) NOT NULL COMMENT '费用项目', + currency VARCHAR(3) NOT NULL COMMENT '币种代码', amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING_REVIEW' COMMENT '费用状态:PENDING_REVIEW待审核、APPROVED审核通过、REJECTED已驳回', + supplier_description VARCHAR(500) NULL COMMENT '供应商或费用说明', created_by VARCHAR(128) NOT NULL COMMENT '创建操作人', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), KEY idx_ll_order_fee_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单费用明细'; + +CREATE TABLE IF NOT EXISTS ll_order_operation_log ( + id BIGINT NOT NULL COMMENT '操作记录主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + order_id BIGINT NOT NULL COMMENT '尾程订单ID', operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', + operator_name VARCHAR(128) NOT NULL COMMENT '操作人名称', result VARCHAR(500) NOT NULL COMMENT '处理结果', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', + PRIMARY KEY(id), KEY idx_ll_order_operation_customer_order(customer_id,order_id,created_at) +) ENGINE=InnoDB COMMENT='尾程订单操作记录'; + +-- 派送单列表与详情快照、转派送费用及操作记录。 +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_delivery_order' AND column_name='overseas_warehouse_name'), 'SELECT 1', 'ALTER TABLE ll_delivery_order ADD COLUMN overseas_warehouse_name VARCHAR(255) NULL COMMENT ''海外仓名称快照'', ADD COLUMN customer_name VARCHAR(255) NULL COMMENT ''客户名称快照'', ADD COLUMN waybill_no VARCHAR(128) NULL COMMENT ''承运运单号'', ADD COLUMN delivery_method VARCHAR(32) NULL COMMENT ''派送方式'', ADD COLUMN carrier_name VARCHAR(255) NULL COMMENT ''派送承运商'', ADD COLUMN pickup_method VARCHAR(64) NULL COMMENT ''提货方式'', ADD COLUMN loading_type VARCHAR(64) NULL COMMENT ''装车类型'', ADD COLUMN vehicle_type VARCHAR(64) NULL COMMENT ''车辆类型'', ADD COLUMN vehicle_size VARCHAR(64) NULL COMMENT ''车辆大小'', ADD COLUMN vehicle_plate VARCHAR(64) NULL COMMENT ''车牌号'', ADD COLUMN driver_name VARCHAR(128) NULL COMMENT ''司机或车队名称'', ADD COLUMN driver_phone VARCHAR(64) NULL COMMENT ''车队联系电话'', ADD COLUMN appointment_account VARCHAR(128) NULL COMMENT ''预约账号'', ADD COLUMN seller_name VARCHAR(128) NULL COMMENT ''卖家姓名'', ADD COLUMN total_weight_kg DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总重量(千克)'', ADD COLUMN total_volume_cbm DECIMAL(18,4) NOT NULL DEFAULT 0 COMMENT ''总体积(立方米)'', ADD COLUMN planned_delivery_at DATETIME(3) NULL COMMENT ''预计派送时间'', ADD COLUMN dispatched_at DATETIME(3) NULL COMMENT ''实际派送时间'', ADD COLUMN expected_arrival_at DATETIME(3) NULL COMMENT ''预计到达时间'', ADD COLUMN signed_at DATETIME(3) NULL COMMENT ''签收时间'', ADD COLUMN remark VARCHAR(1000) NULL COMMENT ''派送备注''')); +PREPARE delivery_detail_stmt FROM @ddl; EXECUTE delivery_detail_stmt; DEALLOCATE PREPARE delivery_detail_stmt; +CREATE TABLE IF NOT EXISTS ll_delivery_fee (id BIGINT NOT NULL COMMENT '派送费用主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', fee_direction VARCHAR(16) NOT NULL DEFAULT 'PAYABLE' COMMENT '应收应付方向', fee_type VARCHAR(64) NOT NULL COMMENT '费用类型', currency VARCHAR(8) NOT NULL COMMENT '币种', amount DECIMAL(18,2) NOT NULL COMMENT '费用金额', remark VARCHAR(500) NULL COMMENT '费用备注', created_by VARCHAR(128) NULL COMMENT '创建人ID', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', PRIMARY KEY(id), KEY idx_ll_delivery_fee_order(customer_id,delivery_order_id,id)) ENGINE=InnoDB COMMENT='派送单费用明细'; +CREATE TABLE IF NOT EXISTS ll_delivery_operation (id BIGINT NOT NULL COMMENT '派送操作主键', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', delivery_order_id BIGINT NOT NULL COMMENT '派送单ID', operation_node VARCHAR(64) NOT NULL COMMENT '操作节点', operator_id VARCHAR(128) NULL COMMENT '操作人ID', operator_name VARCHAR(128) NULL COMMENT '操作人名称', result VARCHAR(500) NOT NULL COMMENT '处理结果', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '操作时间', PRIMARY KEY(id), KEY idx_ll_delivery_operation_order(customer_id,delivery_order_id,created_at,id)) ENGINE=InnoDB COMMENT='派送单操作记录'; + +CREATE TABLE IF NOT EXISTS ll_exception_order ( + id BIGINT NOT NULL COMMENT '异常订单ID', customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', exception_no VARCHAR(64) NOT NULL COMMENT '异常订单号', + exception_type VARCHAR(32) NOT NULL COMMENT '异常类型', status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '异常处理状态', reason VARCHAR(1000) NOT NULL COMMENT '异常原因', + last_mile_order_id BIGINT NULL COMMENT '关联尾程订单ID', last_mile_order_no VARCHAR(64) NULL COMMENT '关联尾程订单号', inbound_order_id BIGINT NULL COMMENT '关联入仓单ID', inbound_code VARCHAR(128) NULL COMMENT '关联入仓码', + delivery_order_id BIGINT NULL COMMENT '关联派送单ID', delivery_no VARCHAR(64) NULL COMMENT '关联派送单号', outbound_no VARCHAR(64) NULL COMMENT '关联出库单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', overseas_warehouse_id VARCHAR(128) NULL COMMENT '海外仓ID快照', overseas_warehouse_name VARCHAR(255) NULL COMMENT '海外仓名称快照', + sku_code VARCHAR(128) NULL COMMENT '异常货物SKU', package_count INT NULL COMMENT '异常箱数', return_direction VARCHAR(32) NULL COMMENT '退回方向', return_logistics_no VARCHAR(128) NULL COMMENT '退回物流单号', + handling_action VARCHAR(500) NULL COMMENT '异常处理措施', handler_id VARCHAR(128) NULL COMMENT '处理人ID', handler_name VARCHAR(128) NULL COMMENT '处理人名称', + source_type VARCHAR(32) NOT NULL DEFAULT 'MANUAL' COMMENT '异常来源类型', created_by VARCHAR(128) NULL COMMENT '创建人ID', + exception_at DATETIME(3) NOT NULL COMMENT '异常发生时间', created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), UNIQUE KEY uk_ll_exception_no(customer_id,exception_no), + UNIQUE KEY uk_ll_exception_delivery_return(customer_id,delivery_order_id,exception_type), + KEY idx_ll_exception_list(customer_id,status,exception_at), KEY idx_ll_exception_tail(customer_id,last_mile_order_no), + KEY idx_ll_exception_inbound(customer_id,inbound_code) +) ENGINE=InnoDB COMMENT='海外仓异常订单'; + +CREATE TABLE IF NOT EXISTS ll_customer_visit ( + id BIGINT NOT NULL COMMENT '客户回访记录ID', + customer_id VARCHAR(128) NOT NULL COMMENT '客户数据隔离键', + delivery_order_id BIGINT NOT NULL COMMENT '关联派送单ID', + delivery_no VARCHAR(64) NOT NULL COMMENT '关联派送单号', + customer_name VARCHAR(255) NULL COMMENT '客户名称快照', + contact_name VARCHAR(128) NULL COMMENT '客户联系人姓名快照', + contact_phone VARCHAR(64) NULL COMMENT '客户联系人电话快照', + destination VARCHAR(500) NULL COMMENT '派送目的仓编码汇总快照', + delivery_completed_at DATETIME(3) NOT NULL COMMENT '派送完成时间', + status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '回访状态:PENDING待回访或COMPLETED已回访', + selected_last_mile_order_id BIGINT NULL COMMENT '本次回访选择的尾程订单ID', + selected_last_mile_order_no VARCHAR(64) NULL COMMENT '本次回访选择的尾程订单号', + selected_inbound_order_id BIGINT NULL COMMENT '本次回访选择的入仓单ID', + selected_inbound_code VARCHAR(128) NULL COMMENT '本次回访选择的入仓码', + visit_result VARCHAR(32) NULL COMMENT '最近一次回访结果', + customer_feedback VARCHAR(2000) NULL COMMENT '最近一次客户反馈', + remark VARCHAR(1000) NULL COMMENT '最近一次回访备注', + attachment_json JSON NULL COMMENT '当前回访资料文件元数据列表', + visited_by VARCHAR(128) NULL COMMENT '最近一次回访操作人ID', + visited_at DATETIME(3) NULL COMMENT '最近一次回访时间', + visit_count INT NOT NULL DEFAULT 0 COMMENT '累计保存回访次数', + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间', + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间', + PRIMARY KEY(id), + UNIQUE KEY uk_ll_customer_visit_delivery(customer_id,delivery_order_id), + KEY idx_ll_customer_visit_list(customer_id,status,delivery_completed_at), + KEY idx_ll_customer_visit_delivery_no(customer_id,delivery_no) +) ENGINE=InnoDB COMMENT='派送单维度客户回访记录'; + +-- ============================================================================ +-- 2026-08-27 / local legacy inventory-ledger cleanup +-- ============================================================================ +USE baiyue_lastleg; + +-- These quantity columns are absent from the current ledger contract. Legacy +-- NOT NULL definitions without defaults block package-only ledger writes. +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inventory_ledger' AND column_name='before_quantity'), 'ALTER TABLE ll_inventory_ledger DROP COLUMN before_quantity', 'SELECT 1')); +PREPARE lastleg_ledger_legacy_001 FROM @ddl; EXECUTE lastleg_ledger_legacy_001; DEALLOCATE PREPARE lastleg_ledger_legacy_001; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inventory_ledger' AND column_name='delta_quantity'), 'ALTER TABLE ll_inventory_ledger DROP COLUMN delta_quantity', 'SELECT 1')); +PREPARE lastleg_ledger_legacy_002 FROM @ddl; EXECUTE lastleg_ledger_legacy_002; DEALLOCATE PREPARE lastleg_ledger_legacy_002; +SET @ddl = (SELECT IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='ll_inventory_ledger' AND column_name='after_quantity'), 'ALTER TABLE ll_inventory_ledger DROP COLUMN after_quantity', 'SELECT 1')); +PREPARE lastleg_ledger_legacy_003 FROM @ddl; EXECUTE lastleg_ledger_legacy_003; DEALLOCATE PREPARE lastleg_ledger_legacy_003; diff --git a/scripts/overseas-deployment-contract.test.mjs b/scripts/overseas-deployment-contract.test.mjs index 72155a2ea..3f4ddc974 100644 --- a/scripts/overseas-deployment-contract.test.mjs +++ b/scripts/overseas-deployment-contract.test.mjs @@ -5,6 +5,29 @@ import test from 'node:test' const read = async (path) => (await readFile(new URL(`../${path}`, import.meta.url), 'utf8')) .replaceAll('\r\n', '\n') +test('local, development and test Nacos publishing remain environment-isolated', async () => { + const publisher = await read('scripts/publish-nacos-config.ps1') + const local = [ + await read('config/nacos/local/gateway-overseas.yml'), + await read('config/nacos/local/lastleg-service.yml'), + await read('config/nacos/local/warehouse-overseas-service.yml'), + ].join('\n') + const remote = [ + await read('config/nacos/overseas/gateway-overseas.yml'), + await read('config/nacos/overseas/lastleg-service.yml'), + await read('config/nacos/overseas/warehouse-overseas-service.yml'), + ].join('\n') + + assert.match(publisher, /ValidateSet\('local', 'dev', 'development', 'test', 'production'\)/) + assert.match(publisher, /必须显式指定非 public 的 Nacos namespace/) + assert.match(local, /127\.0\.0\.1:9100/) + assert.match(local, /127\.0\.0\.1:9303/) + assert.doesNotMatch(remote, /127\.0\.0\.1|password:\s*123456/) + assert.match(remote, /http:\/\/lastleg-service:9303/) + assert.match(remote, /\$\{LASTLEG_DB_URL\}/) + assert.match(remote, /\$\{WAREHOUSE_DB_URL\}/) +}) + test('Jenkins pipelines recognize all overseas deployable modules', async () => { const controller = await read('Jenkinsfile') const service = await read('Jenkinsfile.service') diff --git a/scripts/publish-nacos-config.ps1 b/scripts/publish-nacos-config.ps1 new file mode 100644 index 000000000..0231e4130 --- /dev/null +++ b/scripts/publish-nacos-config.ps1 @@ -0,0 +1,68 @@ +param( + [ValidateSet('local', 'dev', 'development', 'test', 'production')] + [string]$Environment, + [ValidateSet('cn', 'overseas')] + [string]$Region = 'cn', + [string]$ServerAddr = '127.0.0.1:8848', + [string]$Group = 'BAIYUE', + [string]$Namespace +) + +$ErrorActionPreference = 'Stop' +$projectRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) + +if ($Environment -eq 'local') { + if ($Namespace -and $Namespace -ne 'public') { + throw 'local 环境只能发布到 public namespace' + } + $Namespace = 'public' + $configProfile = 'local' +} else { + if (-not $Namespace -or $Namespace -eq 'public') { + throw "$Environment/$Region 必须显式指定非 public 的 Nacos namespace" + } + $configProfile = $Region +} + +$configRoot = (Resolve-Path (Join-Path $projectRoot "config\nacos\$configProfile")).Path +$healthUrl = "http://$ServerAddr/nacos/v1/console/health/readiness" +$publishUrl = "http://$ServerAddr/nacos/v1/cs/configs" +$health = Invoke-WebRequest -UseBasicParsing -Uri $healthUrl -TimeoutSec 5 +if ($health.StatusCode -ne 200 -or $health.Content.Trim() -ne 'OK') { + throw "Nacos not ready: $healthUrl" +} + +$files = Get-ChildItem -LiteralPath $configRoot -File -Filter '*.yml' | Sort-Object Name +if ($files.Count -eq 0) { + throw "No Nacos configuration found: $configRoot" +} + +$published = 0 +foreach ($file in $files) { + $resolvedFile = (Resolve-Path -LiteralPath $file.FullName).Path + if (-not $resolvedFile.StartsWith($configRoot, [System.StringComparison]::OrdinalIgnoreCase)) { + throw "Unexpected config path: $resolvedFile" + } + $body = @{ + dataId = $file.Name + group = $Group + content = Get-Content -LiteralPath $resolvedFile -Raw -Encoding UTF8 + type = 'yaml' + } + if ($Namespace -ne 'public') { + $body.tenant = $Namespace + } + $pairs = foreach ($entry in $body.GetEnumerator()) { + [uri]::EscapeDataString([string]$entry.Key) + '=' + [uri]::EscapeDataString([string]$entry.Value) + } + $result = Invoke-RestMethod -Method Post -Uri $publishUrl ` + -ContentType 'application/x-www-form-urlencoded; charset=UTF-8' ` + -Body ($pairs -join '&') -TimeoutSec 10 + if ([string]$result -ne 'true') { + throw "Publish failed: $($file.Name), Nacos response: $result" + } + $published++ + Write-Host "Published $($file.Name) -> $Namespace / $Group" +} + +Write-Host "Published $Environment/$Region Nacos configuration: $published items." diff --git a/scripts/use-nacos-environment.ps1 b/scripts/use-nacos-environment.ps1 new file mode 100644 index 000000000..ee5f64734 --- /dev/null +++ b/scripts/use-nacos-environment.ps1 @@ -0,0 +1,74 @@ +param( + [ValidateSet('local', 'dev', 'development', 'test')] + [string]$Environment, + [ValidateSet('cn', 'overseas')] + [string]$Region = 'cn', + [string]$NacosServerAddr, + [string]$NacosNamespace, + [string]$NacosUsername, + [string]$NacosPassword +) + +$ErrorActionPreference = 'Stop' +$target = if ($Environment -eq 'development') { 'dev' } else { $Environment } + +$defaults = @{ + local = @{ Server = '127.0.0.1:8848'; CnNamespace = 'public' } + dev = @{ Server = '8.163.49.32:8848'; CnNamespace = $env:DEV_NACOS_NAMESPACE } + test = @{ Server = '8.163.30.128:8848'; CnNamespace = 'd99d7998-5bb0-4ffe-a2a0-1a564ce14463' } +} +$selected = $defaults[$target] +$serverAddr = if ($NacosServerAddr) { $NacosServerAddr } else { $selected.Server } +$namespace = if ($NacosNamespace) { + $NacosNamespace +} elseif ($target -eq 'local') { + 'public' +} elseif ($Region -eq 'overseas') { + $regionNamespace = [Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_OVERSEAS_NACOS_NAMESPACE") + if ($regionNamespace) { + $regionNamespace + } else { + [Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_NAMESPACE") + } +} else { + $selected.CnNamespace +} +$username = if ($NacosUsername) { + $NacosUsername +} elseif ([Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_USERNAME")) { + [Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_USERNAME") +} else { + 'nacos' +} +$password = if ($NacosPassword) { + $NacosPassword +} elseif ([Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_PASSWORD")) { + [Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_PASSWORD") +} elseif ($target -eq 'local') { + 'nacos' +} else { + $null +} + +if (-not $namespace) { + throw "$target/$Region 必须通过 -NacosNamespace 或对应的 *_NACOS_NAMESPACE 环境变量指定 namespace" +} +if (-not $password) { + throw "$target 必须通过 -NacosPassword 或 $($target.ToUpperInvariant())_NACOS_PASSWORD 提供 Nacos 密码" +} + +$env:NACOS_SERVER_ADDR = $serverAddr +$env:NACOS_NAMESPACE = $namespace +$env:NACOS_GROUP = 'BAIYUE' +$env:NACOS_USERNAME = $username +$env:NACOS_PASSWORD = $password +$env:SPRING_CLOUD_NACOS_CONFIG_NAMESPACE = $namespace +$env:SPRING_CLOUD_NACOS_DISCOVERY_NAMESPACE = $namespace +$env:SPRING_PROFILES_ACTIVE = if ($Region -eq 'overseas') { 'overseas' } elseif ($target -eq 'local') { 'dev' } else { 'cn' } +$env:BAIYUE_RUNTIME_ENVIRONMENT = $target +$env:BAIYUE_RUNTIME_REGION = $Region + +Write-Host "Runtime environment: $target / $Region" +Write-Host "Nacos: $serverAddr; namespace: $namespace; group: BAIYUE" +Write-Host "Spring profiles: $env:SPRING_PROFILES_ACTIVE" +Write-Host 'Nacos password is loaded into the current process and is not printed.' diff --git a/settings-central.xml b/settings-central.xml new file mode 100644 index 000000000..0a3cdbdb3 --- /dev/null +++ b/settings-central.xml @@ -0,0 +1,12 @@ + + + D:/firstlegRepo + + + aliyun-public + 阿里云 Maven 公共镜像 + https://maven.aliyun.com/repository/public + external:* + + + diff --git a/warehouse-overseas-service/pom.xml b/warehouse-overseas-service/pom.xml index acce7f683..6217a9f92 100644 --- a/warehouse-overseas-service/pom.xml +++ b/warehouse-overseas-service/pom.xml @@ -44,6 +44,10 @@ org.springframework.cloud spring-cloud-starter-openfeign + + org.springframework.cloud + spring-cloud-starter-loadbalancer + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config diff --git a/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryController.java b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryController.java new file mode 100644 index 000000000..b3f271267 --- /dev/null +++ b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryController.java @@ -0,0 +1,82 @@ +package com.baiyue.warehouse.controller; + +import com.baiyue.common.core.api.ApiResponse; +import com.baiyue.common.core.api.PageResult; +import com.baiyue.common.core.api.ResultCode; +import com.baiyue.common.core.exception.BizException; +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.warehouse.security.OverseasWarehousePermissions; +import com.baiyue.warehouse.dto.PalletRequests; +import com.baiyue.warehouse.entity.WarehousePalletEntity; +import com.baiyue.warehouse.service.WarehouseResourceService; +import com.baiyue.warehouse.service.DomesticWarehouseDirectory; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import jakarta.validation.Valid; + +import java.util.List; + +/** Exposes the domestic authoritative directory as read-only overseas resource options. */ +@RestController +@RequestMapping("/api/platform/v1/warehouse-admin") +@Tag(name = "Platform overseas warehouse directory") +public class PlatformWarehouseDirectoryController { + private final DomesticWarehouseDirectory directory; + private final WarehouseResourceService resources; + + public PlatformWarehouseDirectoryController(DomesticWarehouseDirectory directory, + WarehouseResourceService resources) { + this.directory = directory; + this.resources = resources; + } + + @GetMapping("/pallets") + @PreAuthorize("hasAuthority('" + OverseasWarehousePermissions.PALLET_LIST + "')") + @Operation(summary = "Page public pallets across warehouses", + description = "Required authority: " + OverseasWarehousePermissions.PALLET_LIST) + public ApiResponse> pallets( + @AuthenticationPrincipal AuthenticatedUser user, + @Valid PalletRequests.QueryRequest query) { + requirePlatform(user); + return ApiResponse.success(resources.publicPallets(WarehouseResourceService.PUBLIC_RESOURCE_SCOPE, query)); + } + + @GetMapping + @PreAuthorize("hasAuthority('" + OverseasWarehousePermissions.WAREHOUSE_LIST + "')") + @Operation(summary = "Page shared overseas receiving warehouses", + description = "Required authority: " + OverseasWarehousePermissions.WAREHOUSE_LIST) + public ApiResponse> warehouses( + @AuthenticationPrincipal AuthenticatedUser user, + @RequestParam(defaultValue = "1") long page, + @RequestParam(defaultValue = "200") long size) { + requirePlatform(user); + if (page < 1 || size < 1 || size > 200) { + throw new BizException(ResultCode.BAD_REQUEST, "分页参数无效"); + } + List all = directory.options().stream() + .map(value -> new WarehouseOption(value.warehouseId(), 0L, value.warehouseCode(), + value.warehouseName(), "OVERSEAS", value.status())) + .toList(); + long from = Math.min((page - 1) * size, all.size()); + long to = Math.min(from + size, all.size()); + long pages = all.isEmpty() ? 0 : (all.size() + size - 1) / size; + return ApiResponse.success(new PageResult<>(all.subList((int) from, (int) to), + page, size, all.size(), pages)); + } + + private static void requirePlatform(AuthenticatedUser user) { + if (user == null || !"PLATFORM".equals(user.appCode()) + || user.tenantId() == null || user.tenantId() != 0L) { + throw new BizException(ResultCode.FORBIDDEN, "仅允许平台端访问"); + } + } + + public record WarehouseOption(long id, long customerId, String warehouseCode, + String warehouseName, String warehouseType, String status) { } +} diff --git a/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/repository/WarehouseResourceRepository.java b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/repository/WarehouseResourceRepository.java index 32b854ea8..9ebed969c 100644 --- a/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/repository/WarehouseResourceRepository.java +++ b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/repository/WarehouseResourceRepository.java @@ -146,6 +146,18 @@ public class WarehouseResourceRepository { return PageResult.from(pallets.selectPage(new Page<>(query.page(), query.size()), wrapper)); } + public PageResult pagePublicPallets(long customerId, + PalletRequests.QueryRequest query) { + var wrapper = new LambdaQueryWrapper() + .eq(WarehousePalletEntity::getCustomerId, customerId) + .eq(StringUtils.hasText(query.palletType()), WarehousePalletEntity::getPalletType, query.palletType()) + .eq(StringUtils.hasText(query.status()), WarehousePalletEntity::getStatus, query.status()) + .eq(query.locationId() != null, WarehousePalletEntity::getLocationId, query.locationId()) + .like(StringUtils.hasText(query.keyword()), WarehousePalletEntity::getPalletCode, query.keyword()) + .orderByDesc(WarehousePalletEntity::getCreatedAt); + return PageResult.from(pallets.selectPage(new Page<>(query.page(), query.size()), wrapper)); + } + public void deletePallet(long id) { pallets.deleteById(id); } diff --git a/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/service/WarehouseResourceService.java b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/service/WarehouseResourceService.java index d44e16759..f4c47176f 100644 --- a/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/service/WarehouseResourceService.java +++ b/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/service/WarehouseResourceService.java @@ -218,6 +218,11 @@ public class WarehouseResourceService { return repository.pagePallets(customerId, warehouseId, query); } + public PageResult publicPallets(long customerId, + PalletRequests.QueryRequest query) { + return repository.pagePublicPallets(customerId, query); + } + @Transactional public void deletePallet(long customerId, long warehouseId, long id) { WarehousePalletEntity pallet = requirePallet(customerId, warehouseId, id); diff --git a/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryControllerTest.java b/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryControllerTest.java new file mode 100644 index 000000000..ae3f2b09e --- /dev/null +++ b/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/PlatformWarehouseDirectoryControllerTest.java @@ -0,0 +1,33 @@ +package com.baiyue.warehouse.controller; + +import com.baiyue.common.security.AuthenticatedUser; +import com.baiyue.warehouse.dto.SharedWarehouseReference; +import com.baiyue.warehouse.service.DomesticWarehouseDirectory; +import com.baiyue.warehouse.service.WarehouseResourceService; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class PlatformWarehouseDirectoryControllerTest { + @Test + void pagesPublicSharedWarehouseOptions() { + DomesticWarehouseDirectory directory = mock(DomesticWarehouseDirectory.class); + when(directory.options()).thenReturn(List.of( + new SharedWarehouseReference(21L, "US-LAX", "洛杉矶仓", "ACTIVE"))); + var controller = new PlatformWarehouseDirectoryController(directory, mock(WarehouseResourceService.class)); + var user = new AuthenticatedUser(1L, 0L, 0L, 2L, + "tester", "PLATFORM", "PLATFORM", Set.of()); + + var page = controller.warehouses(user, 1, 200).data(); + + assertEquals(1, page.total()); + assertEquals(21L, page.records().get(0).id()); + assertEquals(0L, page.records().get(0).customerId()); + assertEquals("OVERSEAS", page.records().get(0).warehouseType()); + } +} diff --git a/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/WarehouseResourcePermissionContractTest.java b/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/WarehouseResourcePermissionContractTest.java index 65588f077..c147dad87 100644 --- a/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/WarehouseResourcePermissionContractTest.java +++ b/warehouse-overseas-service/src/test/java/com/baiyue/warehouse/controller/WarehouseResourcePermissionContractTest.java @@ -32,6 +32,13 @@ class WarehouseResourcePermissionContractTest { assertPermission(methods.get("deletePallet"), PREFIX + "pallet:delete"); } + @Test + void warehouseDirectoryUsesCanonicalListAuthority() throws Exception { + assertPermission(PlatformWarehouseDirectoryController.class.getDeclaredMethod( + "warehouses", com.baiyue.common.security.AuthenticatedUser.class, long.class, long.class), + PREFIX + "warehouse:list"); + } + private static void assertPermission(Method method, String authority) { PreAuthorize preAuthorize = method.getAnnotation(PreAuthorize.class); Operation operation = method.getAnnotation(Operation.class);