-- 删除旧的订单表（如果存在）
DROP TABLE IF EXISTS tb_order CASCADE;

-- 创建订单表
CREATE TABLE tb_order (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,
    
    -- 订单基础信息
    order_no VARCHAR(30) NOT NULL,
    order_type VARCHAR(20) NOT NULL,
    
    -- 买家信息
    buyer_type VARCHAR(20) NOT NULL,
    buyer_id BIGINT NOT NULL,
    
    -- 关联资源
    iot_card_id BIGINT,
    device_id BIGINT,
    
    -- 金额信息
    total_amount BIGINT NOT NULL,
    
    -- 支付信息
    payment_method VARCHAR(20),
    payment_status INT NOT NULL DEFAULT 1,
    paid_at TIMESTAMPTZ,
    
    -- 佣金信息
    commission_status INT NOT NULL DEFAULT 1,
    commission_config_version INT NOT NULL DEFAULT 0
);

-- 添加表注释
COMMENT ON TABLE tb_order IS '订单表';
COMMENT ON COLUMN tb_order.order_no IS '订单号(ORD+时间戳+6位随机数)';
COMMENT ON COLUMN tb_order.order_type IS '订单类型 single_card-单卡购买 device-设备购买';
COMMENT ON COLUMN tb_order.buyer_type IS '买家类型 personal-个人客户 agent-代理商';
COMMENT ON COLUMN tb_order.buyer_id IS '买家ID(个人客户ID或店铺ID)';
COMMENT ON COLUMN tb_order.iot_card_id IS 'IoT卡ID(单卡购买时有值)';
COMMENT ON COLUMN tb_order.device_id IS '设备ID(设备购买时有值)';
COMMENT ON COLUMN tb_order.total_amount IS '订单总金额(分)';
COMMENT ON COLUMN tb_order.payment_method IS '支付方式 wallet-钱包 wechat-微信 alipay-支付宝';
COMMENT ON COLUMN tb_order.payment_status IS '支付状态 1-待支付 2-已支付 3-已取消 4-已退款';
COMMENT ON COLUMN tb_order.paid_at IS '支付时间';
COMMENT ON COLUMN tb_order.commission_status IS '佣金状态 1-待计算 2-已计算';
COMMENT ON COLUMN tb_order.commission_config_version IS '佣金配置版本(订单创建时快照)';

-- 创建索引
CREATE UNIQUE INDEX idx_order_no ON tb_order(order_no) WHERE deleted_at IS NULL;
CREATE INDEX idx_order_buyer ON tb_order(buyer_type, buyer_id);
CREATE INDEX idx_order_payment_status ON tb_order(payment_status);
CREATE INDEX idx_order_iot_card_id ON tb_order(iot_card_id) WHERE iot_card_id IS NOT NULL;
CREATE INDEX idx_order_device_id ON tb_order(device_id) WHERE device_id IS NOT NULL;
CREATE INDEX idx_order_deleted_at ON tb_order(deleted_at);

-- 创建订单明细表
CREATE TABLE tb_order_item (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,
    
    order_id BIGINT NOT NULL,
    package_id BIGINT NOT NULL,
    package_name VARCHAR(100) NOT NULL,
    quantity INT NOT NULL DEFAULT 1,
    unit_price BIGINT NOT NULL,
    amount BIGINT NOT NULL
);

-- 添加表注释
COMMENT ON TABLE tb_order_item IS '订单明细表';
COMMENT ON COLUMN tb_order_item.order_id IS '订单ID';
COMMENT ON COLUMN tb_order_item.package_id IS '套餐ID';
COMMENT ON COLUMN tb_order_item.package_name IS '套餐名称(快照)';
COMMENT ON COLUMN tb_order_item.quantity IS '数量';
COMMENT ON COLUMN tb_order_item.unit_price IS '单价(分)';
COMMENT ON COLUMN tb_order_item.amount IS '小计金额(分)';

-- 创建索引
CREATE INDEX idx_order_item_order_id ON tb_order_item(order_id);
CREATE INDEX idx_order_item_package_id ON tb_order_item(package_id);
CREATE INDEX idx_order_item_deleted_at ON tb_order_item(deleted_at);
