-- =====================================================
-- 数据库结构更新补丁
-- 日期: 2026-06-20
-- 说明: 只更新结构，不改变现有数据
-- 目标数据库: order_flow (MySQL 8.0+)
-- =====================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- 1. 新增表: order_supplier_text_log (发厂文本日志)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `order_supplier_text_log` (
  `id` int NOT NULL AUTO_INCREMENT,
  `order_id` int NOT NULL,
  `supplier_id` int NOT NULL,
  `template_type` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `text_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `confirmed` int NOT NULL DEFAULT 0,
  `confirmed_by` int NULL DEFAULT NULL,
  `confirmed_at` datetime NULL DEFAULT NULL,
  `created_by` int NULL DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT (now()),
  `updated_at` datetime NOT NULL DEFAULT (now()),
  `deleted` int NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `ix_order_supplier_text_log_order_id`(`order_id` ASC) USING BTREE,
  INDEX `ix_order_supplier_text_log_supplier_id`(`supplier_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 2. 新增表: logistics_waybill (物流运单号)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `logistics_waybill` (
  `id` int NOT NULL AUTO_INCREMENT,
  `task_id` int NOT NULL,
  `order_id` int NOT NULL,
  `tracking_number` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `express_company` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `express_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `image_url` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `ocr_raw` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `status` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'pending',
  `created_by` int NULL DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT (now()),
  `updated_at` datetime NOT NULL DEFAULT (now()),
  `deleted` int NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `ix_logistics_waybill_task_id`(`task_id` ASC) USING BTREE,
  INDEX `ix_logistics_waybill_order_id`(`order_id` ASC) USING BTREE,
  INDEX `ix_logistics_waybill_tracking_number`(`tracking_number` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 3. 新增表: product_pricing_rule (产品定价规则)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `product_pricing_rule` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `product_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `pricing_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'area',
  `base_unit_price` decimal(18, 4) NOT NULL DEFAULT 0.0000,
  `pricing_unit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '㎡',
  `pricing_inputs` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `formula_expr` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `formula_constants` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `surcharge_json` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `formula_note` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `tax_rate` decimal(5, 2) NOT NULL DEFAULT 0.00,
  `tax_inclusive` int NOT NULL DEFAULT 0,
  `status` int NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT (now()),
  `updated_at` datetime NOT NULL DEFAULT (now()),
  `deleted` int NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ix_product_pricing_rule_product_id`(`product_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 4. 新增表: supplier_product_cost (供应商产品成本)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `supplier_product_cost` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `supplier_id` int NOT NULL,
  `supplier_model` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `our_model` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `thickness` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `weight_gsm` int NULL DEFAULT NULL,
  `base_fabric_weight` int NULL DEFAULT NULL,
  `cost_price` decimal(18, 4) NOT NULL,
  `cost_unit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '㎡',
  `is_primary` int NOT NULL DEFAULT 0,
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `status` int NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT (now()),
  `updated_at` datetime NOT NULL DEFAULT (now()),
  `deleted` int NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `ix_supplier_product_cost_product_id`(`product_id` ASC) USING BTREE,
  INDEX `ix_supplier_product_cost_supplier_id`(`supplier_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 5. 新增表: product_price_tier (产品价格层级)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `product_price_tier` (
  `id` int NOT NULL AUTO_INCREMENT,
  `product_id` int NOT NULL,
  `tier_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `tier_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `price` decimal(18, 4) NOT NULL,
  `price_unit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '㎡',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `status` int NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT (now()),
  `updated_at` datetime NOT NULL DEFAULT (now()),
  `deleted` int NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `ix_product_price_tier_product_id`(`product_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 6. 新增表: performance_stat_cache (业绩统计缓存)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `performance_stat_cache` (
  `id` int NOT NULL AUTO_INCREMENT,
  `stat_type` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `stat_period` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `category_id` int NULL DEFAULT NULL,
  `order_count` int NOT NULL DEFAULT 0,
  `order_amount` decimal(18, 2) NOT NULL DEFAULT 0.00,
  `commission_amount` decimal(18, 2) NOT NULL DEFAULT 0.00,
  `total_profit` decimal(18, 2) NOT NULL DEFAULT 0.00,
  `category_amounts_json` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL,
  `created_at` datetime NOT NULL DEFAULT (now()),
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `ix_performance_stat_cache_stat_type`(`stat_type` ASC) USING BTREE,
  INDEX `ix_performance_stat_cache_stat_period`(`stat_period` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 7. customer 表新增字段: price_tier
-- ----------------------------
ALTER TABLE `customer`
ADD COLUMN IF NOT EXISTS `price_tier` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'default' AFTER `customer_type`;

-- ----------------------------
-- 8. product 表新增字段
-- ----------------------------
ALTER TABLE `product`
ADD COLUMN IF NOT EXISTS `pricing_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `sale_price`,
ADD COLUMN IF NOT EXISTS `pricing_unit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '㎡' AFTER `pricing_type`,
ADD COLUMN IF NOT EXISTS `thickness` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `pricing_unit`,
ADD COLUMN IF NOT EXISTS `weight_gsm` int NULL DEFAULT NULL AFTER `thickness`,
ADD COLUMN IF NOT EXISTS `default_width_m` decimal(10, 4) NULL DEFAULT NULL AFTER `weight_gsm`,
ADD COLUMN IF NOT EXISTS `is_default` int NOT NULL DEFAULT 0 AFTER `status`;

-- ----------------------------
-- 9. sales_order 表新增字段: contract_amount, payment_method, tax_amount
-- ----------------------------
ALTER TABLE `sales_order`
ADD COLUMN IF NOT EXISTS `contract_amount` decimal(18, 2) NOT NULL DEFAULT 0.00 AFTER `factory_id`,
ADD COLUMN IF NOT EXISTS `payment_method` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `supplier_text_confirmed_by`,
ADD COLUMN IF NOT EXISTS `tax_amount` decimal(18, 2) NOT NULL DEFAULT 0.00 AFTER `payment_method`;

-- ----------------------------
-- 10. sales_order_item 表新增字段 (定价快照)
-- ----------------------------
ALTER TABLE `sales_order_item`
ADD COLUMN IF NOT EXISTS `pricing_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `other_fee_amount`,
ADD COLUMN IF NOT EXISTS `length_m` decimal(10, 4) NULL DEFAULT NULL AFTER `pricing_type`,
ADD COLUMN IF NOT EXISTS `width_m` decimal(10, 4) NULL DEFAULT NULL AFTER `length_m`,
ADD COLUMN IF NOT EXISTS `area_sqm` decimal(18, 4) NULL DEFAULT NULL AFTER `width_m`,
ADD COLUMN IF NOT EXISTS `surcharge_detail` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL AFTER `area_sqm`,
ADD COLUMN IF NOT EXISTS `processing_detail` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL AFTER `surcharge_detail`,
ADD COLUMN IF NOT EXISTS `supplier_id` int NULL DEFAULT NULL AFTER `processing_detail`,
ADD COLUMN IF NOT EXISTS `supplier_model` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `supplier_id`,
ADD COLUMN IF NOT EXISTS `price_tier` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `supplier_model`;

-- ----------------------------
-- 11. logistics_task 表新增字段: task_no, tracking_number, express_company
-- ----------------------------
ALTER TABLE `logistics_task`
ADD COLUMN IF NOT EXISTS `task_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `id`,
ADD COLUMN IF NOT EXISTS `tracking_number` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `status`,
ADD COLUMN IF NOT EXISTS `express_company` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `tracking_number`;

-- 添加唯一索引 (task_no)
-- 注意: 如果已有重复数据，需要先清理
-- CREATE UNIQUE INDEX `ix_logistics_task_task_no` ON `logistics_task`(`task_no`);

-- ----------------------------
-- 12. sys_user 表新增字段: open_id
-- ----------------------------
ALTER TABLE `sys_user`
ADD COLUMN IF NOT EXISTS `open_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `status`;

-- 添加索引
CREATE INDEX IF NOT EXISTS `ix_sys_user_open_id` ON `sys_user`(`open_id`);

-- ----------------------------
-- 13. 更新 alembic_version 表
-- ----------------------------
-- 注意: 如果 alembic_version 表中已有记录，需要先删除旧记录
DELETE FROM `alembic_version`;
INSERT INTO `alembic_version` (`version_num`) VALUES ('20260531_0001');

SET FOREIGN_KEY_CHECKS = 1;

-- =====================================================
-- 更新完成
-- =====================================================
