-- =============================================
-- 租户-项目两级架构 - 业务表增加 project_id 字段
-- 执行顺序：ADD NULL → UPDATE 回填 → MODIFY NOT NULL → ADD INDEX
-- 前置条件：02-default-data.sql 已执行，system_project 中已有默认项目数据
-- =============================================

-- -----------------------------------------------
-- 1. iot_device
-- -----------------------------------------------
ALTER TABLE iot_device ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE iot_device d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE iot_device MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE iot_device ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 2. ops_order
-- -----------------------------------------------
ALTER TABLE ops_order ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 3. ops_order_event
-- -----------------------------------------------
ALTER TABLE ops_order_event ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order_event d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order_event MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order_event ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 4. ops_order_dispatch
-- -----------------------------------------------
ALTER TABLE ops_order_dispatch ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order_dispatch d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order_dispatch MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order_dispatch ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 5. ops_order_queue
-- -----------------------------------------------
ALTER TABLE ops_order_queue ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order_queue d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order_queue MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order_queue ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 6. ops_bus_area
-- -----------------------------------------------
ALTER TABLE ops_bus_area ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_bus_area d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_bus_area MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_bus_area ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 7. ops_area_device_relation
-- -----------------------------------------------
ALTER TABLE ops_area_device_relation ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_area_device_relation d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_area_device_relation MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_area_device_relation ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 8. ops_order_security_ext
-- -----------------------------------------------
ALTER TABLE ops_order_security_ext ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order_security_ext d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order_security_ext MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order_security_ext ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 9. ops_area_security_user
-- -----------------------------------------------
ALTER TABLE ops_area_security_user ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_area_security_user d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_area_security_user MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_area_security_user ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 10. ops_order_clean_ext
-- -----------------------------------------------
ALTER TABLE ops_order_clean_ext ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_order_clean_ext d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_order_clean_ext MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_order_clean_ext ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 11. ops_cleaner_status
-- -----------------------------------------------
ALTER TABLE ops_cleaner_status ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_cleaner_status d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_cleaner_status MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_cleaner_status ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 12. ops_cleaner_performance_monthly
-- -----------------------------------------------
ALTER TABLE ops_cleaner_performance_monthly ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_cleaner_performance_monthly d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_cleaner_performance_monthly MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_cleaner_performance_monthly ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 13. ops_device_trajectory
-- -----------------------------------------------
ALTER TABLE ops_device_trajectory ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_device_trajectory d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_device_trajectory MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_device_trajectory ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 14. ops_inspection_record
-- -----------------------------------------------
ALTER TABLE ops_inspection_record ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_inspection_record d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_inspection_record MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_inspection_record ADD INDEX idx_project_id (project_id);

-- -----------------------------------------------
-- 15. ops_inspection_record_item
-- -----------------------------------------------
ALTER TABLE ops_inspection_record_item ADD COLUMN project_id BIGINT NULL COMMENT '项目编号';

UPDATE ops_inspection_record_item d
JOIN system_project p ON p.tenant_id = d.tenant_id AND p.code = 'DEFAULT' AND p.deleted = 0
SET d.project_id = p.id
WHERE d.project_id IS NULL;

ALTER TABLE ops_inspection_record_item MODIFY COLUMN project_id BIGINT NOT NULL COMMENT '项目编号';

ALTER TABLE ops_inspection_record_item ADD INDEX idx_project_id (project_id);
