92 lines
5.5 KiB
SQL
92 lines
5.5 KiB
SQL
USE baiyue_firstleg;
|
|
|
|
DROP PROCEDURE IF EXISTS add_simple_customs_column;
|
|
DELIMITER $$
|
|
CREATE PROCEDURE add_simple_customs_column(IN p_name VARCHAR(64), IN p_definition TEXT)
|
|
BEGIN
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = DATABASE() AND table_name = 'fl_container_document'
|
|
AND column_name = p_name) THEN
|
|
SET @ddl = CONCAT('ALTER TABLE fl_container_document ADD COLUMN ', p_name, ' ', p_definition);
|
|
PREPARE statement FROM @ddl; EXECUTE statement; DEALLOCATE PREPARE statement;
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
|
|
CALL add_simple_customs_column('file_hash', "VARCHAR(64) NULL COMMENT 'File服务SHA-256快照' AFTER file_name");
|
|
CALL add_simple_customs_column('completed_by', "BIGINT UNSIGNED NULL COMMENT '一键完成操作人ID' AFTER uploaded_by_name");
|
|
CALL add_simple_customs_column('completed_by_name', "VARCHAR(64) NULL COMMENT '一键完成操作人名称快照' AFTER completed_by");
|
|
CALL add_simple_customs_column('completed_at', "DATETIME(3) NULL COMMENT '一键完成时间' AFTER completed_by_name");
|
|
DROP PROCEDURE add_simple_customs_column;
|
|
|
|
SET @old_check = (SELECT CONSTRAINT_NAME FROM information_schema.table_constraints
|
|
WHERE table_schema = DATABASE() AND table_name = 'fl_container_document'
|
|
AND constraint_type = 'CHECK' AND constraint_name = 'chk_container_document_type');
|
|
SET @ddl = IF(@old_check IS NULL, 'SELECT 1',
|
|
'ALTER TABLE fl_container_document DROP CHECK chk_container_document_type');
|
|
PREPARE statement FROM @ddl; EXECUTE statement; DEALLOCATE PREPARE statement;
|
|
ALTER TABLE fl_container_document ADD CONSTRAINT chk_container_document_type
|
|
CHECK (document_type IN ('DESTINATION_CLEARANCE', 'CUSTOMS_DECLARATION', 'DELIVERY_ORDER', 'PICKUP_ORDER'));
|
|
|
|
-- Keep container identity/loading facts immutable while allowing release transport data to arrive later.
|
|
DROP TRIGGER IF EXISTS trg_container_booking_before_update;
|
|
DELIMITER $$
|
|
CREATE TRIGGER trg_container_booking_before_update BEFORE UPDATE ON fl_container_booking FOR EACH ROW
|
|
BEGIN
|
|
DECLARE current_plan_status VARCHAR(24);
|
|
SELECT MAX(plan_status) INTO current_plan_status FROM fl_loading_plan WHERE container_booking_id=OLD.id;
|
|
IF current_plan_status IN ('LOADING','COMPLETED') AND (
|
|
NOT (OLD.id <=> NEW.id) OR NOT (OLD.booking_no <=> NEW.booking_no)
|
|
OR NOT (OLD.cabinet_no <=> NEW.cabinet_no) OR NOT (OLD.channel_code <=> NEW.channel_code)
|
|
OR NOT (OLD.channel_name <=> NEW.channel_name) OR NOT (OLD.ocean_booking_id <=> NEW.ocean_booking_id)
|
|
OR NOT (OLD.ocean_booking_requirement_id <=> NEW.ocean_booking_requirement_id)
|
|
OR NOT (OLD.warehouse_id <=> NEW.warehouse_id) OR NOT (OLD.warehouse_name <=> NEW.warehouse_name)
|
|
OR NOT (OLD.container_type <=> NEW.container_type) OR NOT (OLD.shipping_company_id <=> NEW.shipping_company_id)
|
|
OR NOT (OLD.shipping_company_name <=> NEW.shipping_company_name)
|
|
OR NOT (OLD.departure_port_code <=> NEW.departure_port_code) OR NOT (OLD.departure_port_name <=> NEW.departure_port_name)
|
|
OR NOT (OLD.destination_port_code <=> NEW.destination_port_code) OR NOT (OLD.destination_port_name <=> NEW.destination_port_name)
|
|
OR NOT (OLD.destination_warehouse_code <=> NEW.destination_warehouse_code)
|
|
OR NOT (OLD.destination_warehouse_name <=> NEW.destination_warehouse_name)
|
|
OR NOT (OLD.capacity_cbm <=> NEW.capacity_cbm) OR NOT (OLD.max_weight_kg <=> NEW.max_weight_kg)
|
|
OR NOT (OLD.terminal_trucking_company_id <=> NEW.terminal_trucking_company_id)
|
|
OR NOT (OLD.terminal_trucking_company <=> NEW.terminal_trucking_company) OR NOT (OLD.loading_at <=> NEW.loading_at)
|
|
OR NOT (OLD.actual_sailing_at <=> NEW.actual_sailing_at)
|
|
OR NOT (OLD.estimated_port_arrival_at <=> NEW.estimated_port_arrival_at)
|
|
OR NOT (OLD.actual_port_arrival_at <=> NEW.actual_port_arrival_at)
|
|
OR NOT (OLD.estimated_warehouse_arrival_at <=> NEW.estimated_warehouse_arrival_at)
|
|
OR NOT (OLD.actual_warehouse_arrival_at <=> NEW.actual_warehouse_arrival_at)
|
|
OR NOT (OLD.created_by <=> NEW.created_by) OR NOT (OLD.created_by_name <=> NEW.created_by_name)
|
|
OR NOT (OLD.created_at <=> NEW.created_at)) THEN
|
|
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='装柜作业开始后仅允许修改集装箱号与放舱运输数据';
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
|
|
DROP TRIGGER IF EXISTS trg_container_document_before_insert;
|
|
DELIMITER $$
|
|
CREATE TRIGGER trg_container_document_before_insert BEFORE INSERT ON fl_container_document FOR EACH ROW
|
|
BEGIN
|
|
DECLARE current_plan_status VARCHAR(24);
|
|
SELECT MAX(plan_status) INTO current_plan_status FROM fl_loading_plan
|
|
WHERE container_booking_id = NEW.container_booking_id;
|
|
IF current_plan_status = 'COMPLETED'
|
|
AND NEW.document_type NOT IN ('DESTINATION_CLEARANCE', 'CUSTOMS_DECLARATION', 'DELIVERY_ORDER', 'PICKUP_ORDER') THEN
|
|
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '装柜完成后货柜物流文件不可新增';
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
|
|
DROP TRIGGER IF EXISTS trg_container_document_before_update;
|
|
DELIMITER $$
|
|
CREATE TRIGGER trg_container_document_before_update BEFORE UPDATE ON fl_container_document FOR EACH ROW
|
|
BEGIN
|
|
DECLARE current_plan_status VARCHAR(24);
|
|
SELECT MAX(plan_status) INTO current_plan_status FROM fl_loading_plan
|
|
WHERE container_booking_id = OLD.container_booking_id;
|
|
IF current_plan_status = 'COMPLETED'
|
|
AND OLD.document_type NOT IN ('DESTINATION_CLEARANCE', 'CUSTOMS_DECLARATION', 'DELIVERY_ORDER', 'PICKUP_ORDER') THEN
|
|
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '装柜完成后货柜物流文件不可修改';
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|