

--
-- 表的结构 `mall_stocks_locked`
--

CREATE TABLE IF NOT EXISTS `mall_stocks_locked` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `out_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `shop_id` bigint(20) NOT NULL,
  `country` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remain_num` bigint(20) NOT NULL,
  `used_num` bigint(20) NOT NULL,
  `ctime` datetime(3) DEFAULT NULL,
  `mtime` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_id` (`id`),
  KEY `idx_out_id` (`out_id`),
  KEY `idx_out_id_country_shop_id` (`out_id`,`country`,`shop_id`),
  KEY `idx_out_id_shop_id` (`out_id`,`shop_id`),
  KEY `idx_out_country` (`out_id`,`country`),
  KEY `idx_shop_out_country` (`shop_id`,`out_id`,`country`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='锁定库存表';

-- --------------------------------------------------------

--
-- 表的结构 `mall_stocks_locked_logs`
--

CREATE TABLE IF NOT EXISTS `mall_stocks_locked_logs` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `out_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `shop_id` bigint(20) NOT NULL,
  `country` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
  `order_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `type` tinyint(4) NOT NULL,
  `num` bigint(20) NOT NULL,
  `before_total` bigint(20) NOT NULL DEFAULT '0',
  `after_total` bigint(20) NOT NULL DEFAULT '0',
  `before_used` bigint(20) NOT NULL DEFAULT '0',
  `after_used` bigint(20) NOT NULL DEFAULT '0',
  `ctime` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_out_id` (`out_id`),
  KEY `idx_shop_id` (`shop_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_ctime` (`ctime`),
  KEY `idx_shop_id_order_id` (`shop_id`,`order_id`),
  KEY `idx_shop_id_order_id_type` (`shop_id`,`order_id`,`type`),
  KEY `idx_order_type` (`order_id`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='锁定库存变更日志表';

-- --------------------------------------------------------

--
-- 表的结构 `mall_stocks_logs`
--

CREATE TABLE IF NOT EXISTS `mall_stocks_logs` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `out_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'SKU外部编号',
  `shop_id` bigint(20) DEFAULT NULL,
  `warehouse_id` bigint(20) NOT NULL COMMENT '仓库ID',
  `order_id` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '订单ID',
  `type` tinyint(4) NOT NULL COMMENT '类型：1-入库，2-出库',
  `num` bigint(20) NOT NULL COMMENT '操作数量',
  `before_num` bigint(20) NOT NULL COMMENT '操作前数量',
  `after_num` bigint(20) NOT NULL COMMENT '操作后数量',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `sence` tinyint(4) NOT NULL COMMENT '操作场景',
  PRIMARY KEY (`id`),
  KEY `idx_out_id` (`out_id`),
  KEY `idx_shop_id` (`shop_id`),
  KEY `idx_warehouse_id` (`warehouse_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_type` (`type`),
  KEY `idx_shop_out_warehouse` (`shop_id`,`out_id`,`warehouse_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='库存变更日志表';

-- --------------------------------------------------------

--
-- 表的结构 `mall_stocks_pre`
--

CREATE TABLE IF NOT EXISTS `mall_stocks_pre` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `shop_id` bigint(20) NOT NULL,
  `out_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `country` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
  `num` bigint(20) NOT NULL,
  `order_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` tinyint(4) NOT NULL,
  `use_locked` tinyint(1) NOT NULL DEFAULT '0',
  `lock_shop_id` bigint(20) DEFAULT NULL,
  `expire_at` datetime(3) DEFAULT NULL,
  `ctime` datetime(3) DEFAULT NULL,
  `mtime` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_order_sku` (`order_id`,`out_id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_out_id_country` (`out_id`,`country`),
  KEY `idx_out_country_status` (`out_id`,`country`,`status`),
  KEY `idx_order_status` (`order_id`,`status`),
  KEY `idx_out_shop_country_status` (`out_id`,`shop_id`,`country`,`status`),
  KEY `idx_expire_status` (`expire_at`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='预占库存表';









INSERT INTO mall_stocks_logs (
    id, 
    out_id, 
    shop_id, 
    warehouse_id, 
    order_id, 
    type, 
    num, 
    before_num, 
    after_num, 
    create_time, 
    sence
)
SELECT 
    id, 
    out_id, 
    shop_id, 
    warehouse_id, 
    order_id, 
    1 AS type,  -- type=1 表示入库
    change_quantity AS num, 
    before_change AS before_num, 
    after_change AS after_num, 
    create_time, 
    scene AS sence
FROM 
    mall_stockchange_log
WHERE 
    status = 1;


-- 以下命令仅仅在跨境一番数据库手工执行
INSERT INTO mall_stocks_pre (
    id, 
    shop_id, 
    out_id, 
    country, 
    num, 
    order_id, 
    status, 
    use_locked, 
    lock_shop_id, 
    expire_at, 
    ctime, 
    mtime
)
SELECT 
    id, 
    shop_id, 
    out_id, 
    'JP' AS country,  -- country=JP
    change_quantity AS num, 
    order_id, 
    1 AS status,  -- status=1
    0 AS use_locked,  -- use_locked=0
    0 AS lock_shop_id,  -- lock_shop_id=0
    DATE_ADD(create_time, INTERVAL 30 MINUTE) AS expire_at,  -- create_time加半小时
    create_time AS ctime, 
    create_time AS mtime
FROM 
    mall_stockchange_log
WHERE 
    status = 0;