SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

ALTER TABLE `sog`.`glam_access_point` ADD COLUMN `ap_movement_last_pos` POINT NULL DEFAULT NULL  AFTER `ap_movement_radius` ;

ALTER TABLE `sog`.`glam_his_access_point` ADD COLUMN `ap_movement_radius` FLOAT(11) NULL DEFAULT 10  AFTER `ap_last_access_by` , ADD COLUMN `ap_movement_last_pos` POINT NULL DEFAULT NULL  AFTER `ap_movement_radius` ;

ALTER TABLE `sog`.`glam_supervisor_schedule` CHANGE COLUMN `transaction_location` `transaction_location` POINT NULL  ;

-- Inserts
INSERT INTO glam_notification (notification_id,notification_desc) VALUES 
	(47, 'Lock moved out of commissioned area.'),
	(46,'Lock back in commissioned area.'),
	(45,'Lock safe area updated.');
-- Trigger DDL Statements

DROP TRIGGER IF EXISTS `sog`.`apInsert` ;
DROP TRIGGER IF EXISTS `sog`.`apUpdate` ;
DROP TRIGGER IF EXISTS `sog`.`apDelete` ;

DELIMITER $$

USE `sog`$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `sog`.`apInsert`
AFTER INSERT ON `sog`.`glam_access_point`
FOR EACH ROW
BEGIN 
    INSERT INTO glam_his_access_point
    SET 	access_point_id = NEW.access_point_id, 
		access_point_serial_no= NEW.access_point_serial_no, 
		access_point_status= NEW.access_point_status, 
		access_point_com_status= NEW.access_point_com_status, 
		access_point_description= NEW.access_point_description, 
		access_point_comment= NEW.access_point_comment, 
		access_point_safe_status= NEW.access_point_safe_status, 
		access_point_is_owner= NEW.access_point_is_owner, 
		access_point_last_status_change= NEW.access_point_last_status_change, 
		is_deleted= NEW.is_deleted, 
		version= NEW.version, 
		last_changed_on= NEW.last_changed_on, 
		last_changed_by= NEW.last_changed_by, 
		ap_replacement_serial_no= NEW.ap_replacement_serial_no, 
		in_lockdown= NEW.in_lockdown, 
		access_point_rf_id= NEW.access_point_rf_id, 
		access_point_location= NEW.access_point_location, 
		glam_access_pointcol= NEW.glam_access_pointcol, 
		access_point_type= NEW.access_point_type, 
    ap_share_status= NEW.ap_share_status, 
    ap_key_replaced_serial_no=NEW.ap_key_replaced_serial_no,
    ap_key_replaced_latch=NEW.ap_key_replaced_latch,
    ap_last_access_by=NEW.ap_last_access_by,
    ap_movement_radius=NEW.ap_movement_radius,
    ap_movement_last_pos=NEW.ap_movement_last_pos,
		his_action = 'ADDED';

END$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `apUpdate`
AFTER UPDATE ON `glam_access_point`
FOR EACH ROW
BEGIN 
	IF(NEW.is_deleted) THEN
		INSERT INTO glam_his_access_point
		SET 	access_point_id = NEW.access_point_id, 
			access_point_serial_no= NEW.access_point_serial_no, 
			access_point_status= NEW.access_point_status, 
			access_point_com_status= NEW.access_point_com_status, 
			access_point_description= NEW.access_point_description, 
			access_point_comment= NEW.access_point_comment, 
			access_point_safe_status= NEW.access_point_safe_status, 
			access_point_is_owner= NEW.access_point_is_owner, 
			access_point_last_status_change= NEW.access_point_last_status_change, 
			is_deleted= NEW.is_deleted, 
			version= NEW.version, 
			last_changed_on= NEW.last_changed_on, 
			last_changed_by= NEW.last_changed_by, 
			ap_replacement_serial_no= NEW.ap_replacement_serial_no, 
			in_lockdown= NEW.in_lockdown, 
			access_point_rf_id= NEW.access_point_rf_id, 
			access_point_location= NEW.access_point_location, 
			glam_access_pointcol= NEW.glam_access_pointcol, 
			access_point_type= NEW.access_point_type, 
      ap_share_status= NEW.ap_share_status, 
      ap_key_replaced_serial_no=NEW.ap_key_replaced_serial_no,
      ap_key_replaced_latch=NEW.ap_key_replaced_latch,
      ap_last_access_by=NEW.ap_last_access_by,      
      ap_movement_radius=NEW.ap_movement_radius,
      ap_movement_last_pos=NEW.ap_movement_last_pos,
			his_action= 'DELETED';
	ELSE
		INSERT INTO glam_his_access_point
		SET 	access_point_id = NEW.access_point_id, 
			access_point_serial_no= NEW.access_point_serial_no, 
			access_point_status= NEW.access_point_status, 
			access_point_com_status= NEW.access_point_com_status, 
			access_point_description= NEW.access_point_description, 
			access_point_comment= NEW.access_point_comment, 
			access_point_safe_status= NEW.access_point_safe_status, 
			access_point_is_owner= NEW.access_point_is_owner, 
			access_point_last_status_change= NEW.access_point_last_status_change, 
			is_deleted= NEW.is_deleted, 
			version= NEW.version, 
			last_changed_on= NEW.last_changed_on, 
			last_changed_by= NEW.last_changed_by, 
			ap_replacement_serial_no= NEW.ap_replacement_serial_no, 
			in_lockdown= NEW.in_lockdown, 
			access_point_rf_id= NEW.access_point_rf_id, 
			access_point_location= NEW.access_point_location, 
			glam_access_pointcol= NEW.glam_access_pointcol, 
			access_point_type= NEW.access_point_type,
      ap_share_status= NEW.ap_share_status, 
      ap_key_replaced_serial_no=NEW.ap_key_replaced_serial_no,
      ap_key_replaced_latch=NEW.ap_key_replaced_latch,
      ap_last_access_by=NEW.ap_last_access_by,
      ap_movement_radius=NEW.ap_movement_radius,
      ap_movement_last_pos=NEW.ap_movement_last_pos,
			his_action= 'UPDATED';
	END IF;

END$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `apDelete`
BEFORE DELETE ON `sog`.`glam_access_point`
FOR EACH ROW
BEGIN 
    INSERT INTO glam_his_access_point
    SET 	access_point_id = OLD.access_point_id, 
		access_point_serial_no= OLD.access_point_serial_no, 
		access_point_status= OLD.access_point_status, 
		access_point_com_status= OLD.access_point_com_status, 
		access_point_description= OLD.access_point_description, 
		access_point_comment= OLD.access_point_comment, 
		access_point_safe_status= OLD.access_point_safe_status, 
		access_point_is_owner= OLD.access_point_is_owner, 
		access_point_last_status_change= OLD.access_point_last_status_change, 
		is_deleted= OLD.is_deleted, 
		version= OLD.version, 
		last_changed_on= OLD.last_changed_on, 
		last_changed_by= OLD.last_changed_by, 
		ap_replacement_serial_no= OLD.ap_replacement_serial_no, 
		in_lockdown= OLD.in_lockdown, 
		access_point_rf_id= OLD.access_point_rf_id, 
		access_point_location= OLD.access_point_location, 
		glam_access_pointcol= OLD.glam_access_pointcol, 
		access_point_type= OLD.access_point_type, 
    ap_share_status= OLD.ap_share_status, 
    ap_key_replaced_serial_no=OLD.ap_key_replaced_serial_no,
    ap_key_replaced_latch=OLD.ap_key_replaced_latch,
    ap_last_access_by=OLD.ap_last_access_by,
    ap_movement_radius=OLD.ap_movement_radius,
    ap_movement_last_pos=OLD.ap_movement_last_pos,
		his_action = 'DELETED';

END$$




SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
