-- 云仓一体变更：通知单扩展、待办路由、入出库聚合配置（未执行过分散脚本时整份执行一次即可）
SET NAMES utf8mb4;

-- ---------------------------------------------------------------------------
-- 1) 通知单 man_asn_order：云仓 doc 字段（回馈 DAP 原样带回）
-- ---------------------------------------------------------------------------
ALTER TABLE `man_asn_order`
  ADD COLUMN `doc_task_warehouse_no` varchar(64) DEFAULT NULL COMMENT '任务仓库(与云仓 docWarehouseNo)' AFTER `station_id`,
  ADD COLUMN `doc_org_no` varchar(64) DEFAULT NULL COMMENT '组织编码(orgNo)' AFTER `doc_task_warehouse_no`,
  ADD COLUMN `doc_in_warehouse_no` varchar(64) DEFAULT NULL COMMENT '入库仓编码(inWarehouseNo)' AFTER `doc_org_no`,
  ADD COLUMN `doc_out_warehouse_no` varchar(64) DEFAULT NULL COMMENT '出库仓编码(outWarehouseNo)' AFTER `doc_in_warehouse_no`;

-- ---------------------------------------------------------------------------
-- 2) 云仓上报待办 man_cloud_wms_notify_log：路由与调度（合并排除 sending、send_hold）
-- ---------------------------------------------------------------------------
ALTER TABLE `man_cloud_wms_notify_log`
  ADD COLUMN `source_order_no` varchar(128) DEFAULT NULL COMMENT '云仓单据号（入出库索引）' AFTER `biz_ref`,
  ADD COLUMN `inbound_flag` tinyint(4) DEFAULT NULL COMMENT '1入库 0出库' AFTER `source_order_no`,
  ADD COLUMN `ware_house_code` varchar(64) DEFAULT NULL COMMENT '云仓仓库编码' AFTER `inbound_flag`,
  ADD COLUMN `send_hold` tinyint(4) NOT NULL DEFAULT '0' COMMENT '1暂缓调度发送（manual/wait_order 未放行）' AFTER `ware_house_code`,
  ADD COLUMN `sending` tinyint(4) NOT NULL DEFAULT '0' COMMENT '1正在上报中，合并任务排除' AFTER `send_hold`,
  ADD KEY `idx_source_hold` (`source_order_no`, `send_hold`),
  ADD KEY `idx_send_sending` (`send_hold`, `sending`);

-- ---------------------------------------------------------------------------
-- 3) 系统配置：云仓入出库模式与 wait_order 聚合秒数（若 flag 已存在请先删重复或注释本段）
-- ---------------------------------------------------------------------------
INSERT INTO `sys_config` (`uuid`, `name`, `flag`, `type`, `val`, `content`, `status`, `deleted`, `tenant_id`, `create_by`, `create_time`, `update_by`, `update_time`, `memo`)
VALUES
(UPPER(UUID()), '云仓入出库回馈模式', 'CLOUD_WMS_INOUT_REPORT_MODE', 3, 'immediate', 'immediate 立即待办；wait_order 暂缓发送后由调度放行；manual 暂缓发送由通知单按钮放行；single 逐条上报不与同单合并', 1, 0, 1, NULL, NOW(), NULL, NOW(), '默认 immediate'),
(UPPER(UUID()), '云仓入出库聚合-空闲部分上报秒数', 'CLOUD_WMS_INOUT_AGG_IDLE_SECONDS', 2, '180', 'wait_order：超过该秒数无新实绩更新则放行暂缓待办（可与整单结束择先触发）', 1, 0, 1, NULL, NOW(), NULL, NOW(), '默认 180'),
(UPPER(UUID()), '云仓入出库聚合-整单完成防抖秒数', 'CLOUD_WMS_INOUT_AGG_COMPLETE_DEBOUNCE_SECONDS', 2, '8', '同订单无执行中任务后，再等待该秒数再整批上报', 1, 0, 1, NULL, NOW(), NULL, NOW(), '默认 8');

UPDATE `sys_config`
SET `content` = 'immediate 立即待办；wait_order 暂缓发送后由调度放行；manual 暂缓发送由通知单按钮放行；single 逐条上报不与同单合并',
    `update_time` = NOW()
WHERE `flag` = 'CLOUD_WMS_INOUT_REPORT_MODE'
  AND (`deleted` = 0 OR `deleted` IS NULL);
