-- 1. 添加库存字段
ALTER TABLE merchant_products 
ADD COLUMN qty INT NOT NULL DEFAULT 0 COMMENT '库存数量';

-- 2. 添加是否售罄字段
ALTER TABLE merchant_products 
ADD COLUMN is_sellout TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否售罄，0-未售罄，1-已售罄';

-- 3. 添加配送类型字段（使用MySQL的ENUM类型）
ALTER TABLE merchant_products 
ADD COLUMN delivery_type ENUM('DELIVERY', 'PICKUP') NOT NULL DEFAULT 'DELIVERY' COMMENT '配送类型，DELIVERY-配送到家，PICKUP-自提';

-- 4. 添加自提点字段
ALTER TABLE merchant_products 
ADD COLUMN pickup_place VARCHAR(200) NULL COMMENT '自提点';

-- 5. 添加配送时间类型字段（使用MySQL的ENUM类型）
ALTER TABLE merchant_products 
ADD COLUMN delivery_time_type ENUM('IMMEDIATE', 'SCHEDULED') NOT NULL DEFAULT 'IMMEDIATE' COMMENT '配送时间类型，IMMEDIATE-立即送，SCHEDULED-定时送';

-- 6. 添加配送日期字段
ALTER TABLE merchant_products 
ADD COLUMN delivery_date DATE NULL COMMENT '配送日期，仅对定时送有效';

-- 7. 创建索引以提高查询效率
CREATE INDEX idx_merchant_products_delivery_type ON merchant_products(delivery_type);
CREATE INDEX idx_merchant_products_delivery_time_type ON merchant_products(delivery_time_type);
CREATE INDEX idx_merchant_products_is_sellout ON merchant_products(is_sellout);


-- 为merchant_products表添加product_detail字段
ALTER TABLE merchant_products 
ADD COLUMN product_detail TEXT COMMENT '产品详细描述，Markdown格式';

-- 为merchant_products表添加purchase_note字段
ALTER TABLE merchant_products 
ADD COLUMN purchase_note TEXT COMMENT '购买须知，用于提供商品购买相关注意事项';

-- 为merchant_products表添加pickup_time字段（带时区信息）
ALTER TABLE merchant_products 
ADD COLUMN pickup_time DATETIME COMMENT '自提时间，用于指定商品自提的时间';

-- 为merchant_products表添加operation_type字段（枚举类型）
ALTER TABLE merchant_products 
ADD COLUMN operation_type ENUM('SELF_OPERATED', 'MERCHANT') NOT NULL DEFAULT 'MERCHANT' COMMENT '经营类型：自营或商家';


-- 为merchant_products表添加经纬度字段
ALTER TABLE merchant_products 
ADD COLUMN longitude DECIMAL(9,6) COMMENT '经度，精确到小数点后6位',
ADD COLUMN latitude DECIMAL(9,6) COMMENT '纬度，精确到小数点后6位';

-- 为merchant_products表添加已售数量字段
ALTER TABLE merchant_products 
ADD COLUMN sold_total INT NOT NULL DEFAULT 0 COMMENT '已售数量';

-- 为delivery_addresses表添加地址类型和经纬度字段
ALTER TABLE delivery_addresses 
ADD COLUMN address_type ENUM('PICKUP', 'COMMON') NOT NULL DEFAULT 'PICKUP' COMMENT '地址类型：代取或通用',
ADD COLUMN longitude DECIMAL(9,6) COMMENT '经度，精确到小数点后6位',
ADD COLUMN latitude DECIMAL(9,6) COMMENT '纬度，精确到小数点后6位';



-- 添加address_id字段（先不添加外键约束）
ALTER TABLE merchant_orders 
ADD COLUMN address_id INT COMMENT '收货地址ID' AFTER merchant_product_id;

-- 添加qty字段
ALTER TABLE merchant_orders 
ADD COLUMN qty INT NOT NULL DEFAULT 1 COMMENT '购买数量' AFTER address_id;

-- 添加unit_price字段
ALTER TABLE merchant_orders 
ADD COLUMN unit_price DECIMAL(10,2) NOT NULL COMMENT '产品单价' AFTER qty;

-- 添加merchant_id字段（先不添加外键约束）
ALTER TABLE merchant_orders 
ADD COLUMN merchant_id INT COMMENT '商家ID' AFTER user_id;

-- 从产品表中获取商家ID更新到订单表
UPDATE merchant_orders mo
INNER JOIN merchant_products mp ON mo.merchant_product_id = mp.id
SET mo.merchant_id = mp.merchant_id;

-- 添加外键约束
ALTER TABLE merchant_orders 
ADD CONSTRAINT fk_merchant_orders_merchant 
FOREIGN KEY (merchant_id) REFERENCES merchants(id);

-- 修改merchant_id为NOT NULL
ALTER TABLE merchant_orders 
MODIFY COLUMN merchant_id INT NOT NULL COMMENT '商家ID';


-- 更新现有记录的address_id为NULL
UPDATE merchant_orders 
SET address_id = NULL 
WHERE address_id IS NOT NULL;

-- 添加外键约束
ALTER TABLE merchant_orders 
ADD CONSTRAINT fk_merchant_orders_address 
FOREIGN KEY (address_id) REFERENCES delivery_addresses(id);

-- 修改address_id为NOT NULL
ALTER TABLE merchant_orders 
MODIFY COLUMN address_id INT NOT NULL COMMENT '收货地址ID';

-- 修改status字段的枚举类型
ALTER TABLE merchant_orders 
MODIFY COLUMN status ENUM('CREATED', 'PENDING', 'DELIVERING', 'PICKUP_READY', 'COMPLETED', 'CANCELLED', 'REFUNDING', 'REFUNDED') 
NOT NULL DEFAULT 'CREATED' COMMENT '订单状态';

-- ====FINISH 3.22====

ALTER TABLE merchant_products 
ADD COLUMN recommend BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否为平台推荐商品' AFTER delivery_date;

-- 1. 重命名pickup_time为pickup_time_from
ALTER TABLE merchant_products 
CHANGE COLUMN pickup_time pickup_time_from TIME COMMENT '自提开始时间';

-- 2. 添加pickup_time_to字段
ALTER TABLE merchant_products 
ADD COLUMN pickup_time_to TIME COMMENT '自提结束时间' AFTER pickup_time_from;

-- 添加配送类型快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_delivery_type ENUM('DELIVERY', 'PICKUP') COMMENT '配送类型快照' AFTER verify_user_id;

-- 添加自提点快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_pickup_place VARCHAR(200) COMMENT '自提点快照' AFTER product_delivery_type;

-- 添加自提开始时间快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_pickup_time_from TIME COMMENT '自提开始时间快照' AFTER product_pickup_place;

-- 添加自提结束时间快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_pickup_time_to TIME COMMENT '自提结束时间快照' AFTER product_pickup_time_from;

-- 添加配送时间类型快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_delivery_time_type ENUM('IMMEDIATE', 'SCHEDULED') COMMENT '配送时间类型快照' AFTER product_pickup_time_to;

-- 添加配送日期快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_delivery_date DATE COMMENT '配送日期快照' AFTER product_delivery_time_type;

-- 添加配送截止时间快照字段
ALTER TABLE merchant_orders 
ADD COLUMN product_delivery_deadline_time DATETIME COMMENT '配送截止时间快照' AFTER product_delivery_date;


--====FINISH 3.23====

ALTER TABLE merchants 
ADD COLUMN community_id INT COMMENT '所属小区' AFTER user_id,
ADD CONSTRAINT fk_merchant_community 
FOREIGN KEY (community_id) REFERENCES communities(id)