-- 修正已有菜单权限标识
update sys_menu set perms = 'workflow:definition:list' where menu_id = '11620';
update sys_menu set perms = 'workflow:instance:list' where menu_id = '11621';
update sys_menu set perms = 'workflow:instance:currentList' where menu_id = '11629';

-- 流程定义管理相关按钮
INSERT INTO sys_menu VALUES ('11644', '流程定义查询', '11620', '1', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:query', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11645', '流程定义新增', '11620', '2', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:add', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11646', '流程定义修改', '11620', '3', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:edit', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11647', '流程定义删除', '11620', '4', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:remove', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11648', '流程定义导出', '11620', '5', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:export', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11649', '流程定义导入', '11620', '6', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:import', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11650', '流程定义发布/取消发布', '11620', '7', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:publish', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11651', '流程定义复制', '11620', '8', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:copy', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11652', '流程定义激活/挂起', '11620', '9', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:definition:active', '#', 103, 1, sysdate(), null, null, '');

-- 流程实例管理相关按钮
INSERT INTO sys_menu VALUES ('11653', '流程实例查询', '11621', '1', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:query', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11654', '流程变量查询', '11621', '2', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:variableQuery', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11655', '流程变量修改', '11621', '3', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:variable', '#', 103, 1, sysdate(), null, null, '');

-- Device binding and device_no primary key migration.
ALTER TABLE app_device ADD COLUMN qrcode varchar(1000) NULL COMMENT '设备二维码' AFTER device_img;
ALTER TABLE app_watering_log ADD COLUMN remark varchar(500) NULL COMMENT '备注' AFTER trigger_type;
ALTER TABLE app_scheduling_device ADD COLUMN device_no varchar(64) NULL COMMENT '设备编号' AFTER schedule_id;
UPDATE app_scheduling_device sd
    INNER JOIN app_device d ON sd.device_id = d.id
SET sd.device_no = d.device_no
WHERE sd.device_no IS NULL OR sd.device_no = '';
UPDATE app_watering_log wl
    INNER JOIN app_device d ON wl.device_id = d.id
SET wl.device_no = d.device_no
WHERE wl.device_id IS NOT NULL AND (wl.device_no IS NULL OR wl.device_no = '');
ALTER TABLE app_scheduling_device DROP COLUMN device_id;
ALTER TABLE app_watering_log DROP COLUMN device_id;
ALTER TABLE app_device MODIFY COLUMN device_no varchar(64) NOT NULL COMMENT '设备编号';
ALTER TABLE app_device MODIFY COLUMN id bigint NOT NULL;
ALTER TABLE app_device DROP PRIMARY KEY;
ALTER TABLE app_device DROP COLUMN id;
ALTER TABLE app_device ADD PRIMARY KEY (device_no);
DELETE sd1 FROM app_scheduling_device sd1
    INNER JOIN app_scheduling_device sd2
    ON sd1.schedule_id = sd2.schedule_id
    AND sd1.device_no = sd2.device_no
    AND sd1.id > sd2.id;
ALTER TABLE app_scheduling_device ADD UNIQUE KEY uk_schedule_device (schedule_id, device_no);
INSERT INTO sys_menu VALUES ('11656', '流程实例激活/挂起', '11621', '4', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:active', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11657', '流程实例删除', '11621', '5', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:remove', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11658', '流程实例作废', '11621', '6', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:invalid', '#', 103, 1, sysdate(), null, null, '');
INSERT INTO sys_menu VALUES ('11659', '流程实例撤销', '11621', '7', '#', '', '', 1, 0, 'F', '0', '0', 'workflow:instance:cancel', '#', 103, 1, sysdate(), null, null, '');
