-- 工段/生产预警钉钉定时推送配置
CREATE TABLE IF NOT EXISTS `work_alert_push_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `push_code` varchar(64) NOT NULL COMMENT '推送任务编码(对应XxlJob Handler)',
  `push_name` varchar(128) NOT NULL COMMENT '推送名称',
  `push_time` varchar(5) NOT NULL DEFAULT '08:30' COMMENT '每日推送时间 HH:mm',
  `ding_user_ids` varchar(2000) DEFAULT NULL COMMENT '钉钉用户StaffId，逗号分隔',
  `recipient_rules` text DEFAULT NULL COMMENT '分对象接收人JSON，如{"机一工段":"staffId1"}',
  `open_conversation_id` varchar(128) DEFAULT NULL COMMENT '群会话ID，空则使用默认配置',
  `robot_code` varchar(128) DEFAULT NULL COMMENT '机器人Code，空则使用安全库存机器人',
  `enabled` char(1) NOT NULL DEFAULT '0' COMMENT '是否启用 0否 1是',
  `last_push_date` date DEFAULT NULL COMMENT '上次推送日期(防重复)',
  `sort_order` int(11) DEFAULT 0 COMMENT '排序',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `create_by` varchar(64) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_by` varchar(64) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_push_code` (`push_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='预警钉钉定时推送配置';

-- 预置推送项（默认关闭，可在页面配置时间与人员后启用）
INSERT INTO `work_alert_push_config` (`push_code`, `push_name`, `push_time`, `enabled`, `sort_order`, `remark`, `create_time`) VALUES
('getKuCun', '安全库存预警', '08:00', '0', 10, '钉钉推送安全库存Excel', NOW()),
('getMassageDelayDate', '工段延期预警', '08:10', '0', 20, '各工段延期数据', NOW()),
('getMassageForMultipleWorkCenters', '多工段综合预警', '08:15', '0', 25, '关键词「预警」对应数据', NOW()),
('getMassageTransInPendingOut', '已转入未转出', '08:20', '0', 30, 'recipient_rules按工段长分人私发；留空则多Sheet群发', NOW()),
('getMassageSubcontractTransfer', '委外实际发出接收', '08:22', '0', 35, '委外工段(机加/装配/铆焊)工序委外转移C/G汇总；recipient_rules按工段长分人私发', NOW()),
('getMassageStartedNotTransIn', '开工未转入', '08:25', '0', 40, 'recipient_rules按工段长分人私发；留空则多Sheet群发', NOW()),
('getProcessTransferForm', '工序转移未及时', '08:30', '0', 50, '工序转移预警', NOW()),
('getProcessReportNotInspected', '汇报未质检', '08:35', '0', 61, '私发：填写质检人员StaffId', NOW()),
('getProcessReportNotStockedIn', '质检未入库', '08:36', '0', 62, '私发：填写库房人员StaffId', NOW()),
('getProductionOrder', '工序汇报预警(合并)', '08:37', '0', 60, '未质检+未入库双Sheet，兼容旧配置', NOW()),
('getPurchaseOrder2', '采购订单临期', '08:40', '0', 70, '采购订单钉钉推送', NOW()),
('getApplicationMessage', '采购申请临期', '08:45', '0', 80, '采购申请钉钉推送', NOW()),
('getPurchaseRequestWithoutPo', '采购申请未下推订单', '08:46', '0', 81, '临期采购申请无采购订单，私发刘然（配置 ding_user_ids）', NOW()),
('getPurchaseOrderReApproval', '采购订单重新审批', '08:47', '0', 82, '重新审核采购订单，私发刘然（配置 ding_user_ids）', NOW()),
('getConstructionDelay', '生产订单未开工延期', '08:50', '0', 90, '企业微信推送', NOW()),
('getUninspectedData', '质检不及时', '08:55', '0', 100, '质检不及时数据', NOW())
ON DUPLICATE KEY UPDATE push_name = VALUES(push_name);

-- 菜单（parent_id=1 为系统管理，请按实际环境调整）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
SELECT 23001, '预警推送配置', 1, 20, 'workAlertPushConfig', 'system/workAlertPushConfig/index', 1, 0, 'C', '0', '0', 'system:workAlertPushConfig:list', 'time-range', 'admin', NOW(), '钉钉预警定时推送时间与人员'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23001);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
SELECT 23002, '预警推送查询', 23001, 1, '#', '', 1, 0, 'F', '0', '0', 'system:workAlertPushConfig:query', '#', 'admin', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23002);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
SELECT 23003, '预警推送新增', 23001, 2, '#', '', 1, 0, 'F', '0', '0', 'system:workAlertPushConfig:add', '#', 'admin', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23003);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
SELECT 23004, '预警推送修改', 23001, 3, '#', '', 1, 0, 'F', '0', '0', 'system:workAlertPushConfig:edit', '#', 'admin', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23004);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
SELECT 23005, '预警推送删除', 23001, 4, '#', '', 1, 0, 'F', '0', '0', 'system:workAlertPushConfig:remove', '#', 'admin', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23005);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
SELECT 23006, '预警立即推送', 23001, 5, '#', '', 1, 0, 'F', '0', '0', 'system:workAlertPushConfig:trigger', '#', 'admin', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id = 23006);
