-- ====================================================
-- 停车券发放记录
-- 发布日期：2026-06-12
-- ====================================================

-- 1. 停车券发放记录表
CREATE TABLE `t_parking_coupon_record` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `code` varchar(64) NOT NULL COMMENT 'UUID唯一标识',
    `user_id` bigint NOT NULL COMMENT '用户ID',
    `station_id` varchar(64) DEFAULT NULL COMMENT '站点ID',
    `order_id` varchar(64) DEFAULT NULL COMMENT '触发发放的订单号',
    `target_url` varchar(2048) DEFAULT NULL COMMENT '停车券目标URL',
    `status` tinyint DEFAULT 0 COMMENT '0-未使用，1-已使用',
    `used_time` datetime DEFAULT NULL COMMENT '使用时间',
    `expire_time` datetime DEFAULT NULL COMMENT '过期时间',
    `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
    `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_code` (`code`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_station_id` (`station_id`),
    KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='停车券发放记录表';

-- 2. 权限：停车券发放记录
INSERT IGNORE INTO `t_permission` (`id`, `company_id`, `name`, `value`, `pid`, `weight`) VALUES
(NULL, NULL, '停车券管理', 'parkingCouponRecord', 0, 105),
(NULL, NULL, '发放记录', 'parkingCouponRecord.list', (SELECT id FROM (SELECT id FROM `t_permission` WHERE `value` = 'parkingCouponRecord') AS t), 1);

-- 3. 将新权限追加到超级管理员角色（role_id=1）
UPDATE `t_role` SET `permissions` = CONCAT(`permissions`, '|parkingCouponRecord|parkingCouponRecord.list')
WHERE `id` = 1 AND `permissions` NOT LIKE '%parkingCouponRecord%';
