-- ====================================================
-- v6: 设备故障提醒系统
-- 包含故障订阅表和故障记录表
-- ====================================================

-- ----------------------------
-- 1. 故障通知订阅表
-- ----------------------------
CREATE TABLE IF NOT EXISTS `t_fault_subscriber` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `company_id` BIGINT DEFAULT NULL COMMENT '公司ID（租户隔离）',
    `openid` VARCHAR(64) NOT NULL COMMENT '微信公众号openid',
    `nickname` VARCHAR(64) DEFAULT NULL COMMENT '微信昵称（冗余显示）',
    `station_id` VARCHAR(32) NOT NULL COMMENT '站点ID',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态：0-已解绑，1-已订阅',
    `subscribe_time` DATETIME DEFAULT NULL COMMENT '绑定时间',
    `unbind_time` DATETIME DEFAULT NULL COMMENT '解绑时间',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_openid_station` (`openid`, `station_id`),
    KEY `idx_station_id` (`station_id`),
    KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='故障通知订阅表';

-- ----------------------------
-- 2. 故障记录表
-- ----------------------------
CREATE TABLE IF NOT EXISTS `t_fault_record` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `company_id` BIGINT DEFAULT NULL COMMENT '公司ID（租户隔离）',
    `station_id` VARCHAR(32) NOT NULL COMMENT '站点ID',
    `device_name` VARCHAR(64) NOT NULL COMMENT '设备名称',
    `fault_type` VARCHAR(32) NOT NULL COMMENT '故障类型：offline-离线, water_shortage-缺水, foam_shortage-缺泡沫',
    `fault_time` DATETIME NOT NULL COMMENT '故障发生时间',
    `fault_reason` VARCHAR(255) DEFAULT NULL COMMENT '故障原因',
    `is_notified` TINYINT NOT NULL DEFAULT 0 COMMENT '是否已通知：0-未通知，1-已通知',
    `notify_time` DATETIME DEFAULT NULL COMMENT '通知发送时间',
    `is_recovered` TINYINT NOT NULL DEFAULT 0 COMMENT '是否已恢复：0-未恢复，1-已恢复',
    `recover_time` DATETIME DEFAULT NULL COMMENT '恢复时间',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_station_device` (`station_id`, `device_name`),
    KEY `idx_fault_type` (`fault_type`),
    KEY `idx_is_notified` (`is_notified`),
    KEY `idx_is_recovered` (`is_recovered`),
    KEY `idx_fault_time` (`fault_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备故障记录表';

-- ----------------------------
-- 3. 公众号模版消息配置（故障提醒）
-- 注意：templateId 需要在微信公众平台后台申请后填入
-- ----------------------------
INSERT INTO `t_mp_msg_template` (`template_id`, `biz_type`, `title`, `primary_industry`, `deputy_industry`, `content`, `example`, `page_path`, `create_time`, `update_time`)
VALUES (
    'Th-izNrL4o2X_zshLfMUk4H8ulhQSnfUSCnmwMq4T-Y',
    'FAULT_ALERT',
    '设备故障提醒',
    'IT科技',
    'IT软件与服务',
    '{{thing26.DATA}}{{thing9.DATA}}{{thing5.DATA}}{{time2.DATA}}',
    '站点：XX洗车站点\n设备：设备001\n故障类型：设备离线\n故障时间：2025-01-01 12:00:00',
    '',
    NOW(),
    NOW()
);

-- ----------------------------
-- 4. 公众号模版消息配置（故障恢复提醒）
-- ----------------------------
INSERT INTO `t_mp_msg_template` (`template_id`, `biz_type`, `title`, `primary_industry`, `deputy_industry`, `content`, `example`, `page_path`, `create_time`, `update_time`)
VALUES (
    'hnAaDKy92EySs2Pz778RPevh91qUVaSADSQAUzyeE1Y',
    'FAULT_RECOVER',
    '设备故障恢复提醒',
    'IT科技',
    'IT软件与服务',
    '{{thing14.DATA}}{{thing3.DATA}}{{thing2.DATA}}{{time6.DATA}}',
    '站点：XX洗车站点\n设备：设备001\n故障类型：设备离线\n恢复时间：2025-01-01 14:00:00',
    '',
    NOW(),
    NOW()
);

-- ----------------------------
-- 5. 权限：故障订阅管理
-- ----------------------------
INSERT IGNORE INTO `t_permission` (`id`, `company_id`, `name`, `value`, `pid`, `weight`) VALUES
(NULL, NULL, '故障订阅管理', 'faultSubscriber', 0, 60),
(NULL, NULL, '订阅列表', 'faultSubscriber.list', (SELECT id FROM (SELECT id FROM `t_permission` WHERE `value` = 'faultSubscriber') AS t), 1),
(NULL, NULL, '解绑订阅', 'faultSubscriber.delete', (SELECT id FROM (SELECT id FROM `t_permission` WHERE `value` = 'faultSubscriber') AS t), 2),
(NULL, NULL, '生成二维码', 'faultSubscriber.qrcode', (SELECT id FROM (SELECT id FROM `t_permission` WHERE `value` = 'faultSubscriber') AS t), 3),
(NULL, NULL, '故障记录', 'faultRecord.list', (SELECT id FROM (SELECT id FROM `t_permission` WHERE `value` = 'faultSubscriber') AS t), 4);

-- 将新权限追加到超级管理员角色（role_id=1）的 permissions 字段中（分隔符为 |）
UPDATE `t_role` SET `permissions` = CONCAT(
    IFNULL(`permissions`, ''),
    '|faultSubscriber|faultSubscriber.list|faultSubscriber.delete|faultSubscriber.qrcode|faultRecord.list'
) WHERE `id` = 1;

-- ----------------------------
-- 6. 数据字典：故障类型
-- ----------------------------
INSERT INTO `t_data_dict` (`code`, `name`, `value`, `weight`, `remark`, `color`) VALUES
('FaultRecord.faultType', '设备离线', 'offline', 1, '故障类型', 'danger'),
('FaultRecord.faultType', '缺水', 'water_shortage', 2, '故障类型', 'warning'),
('FaultRecord.faultType', '缺泡沫', 'foam_shortage', 3, '故障类型', 'warning');
