<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.skcks.docking.gb28181.mocking.orm.mybatis.operation.MockingOperateTableMapper">
    <update id="createDeviceTable">
        CREATE TABLE IF NOT EXISTS `mocking_device` (
                                          `id` bigint(20) NOT NULL AUTO_INCREMENT,
                                          `device_code` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
                                          `gb_device_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
                                          `gb_channel_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
                                          `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
                                          `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
                                          `enable` tinyint(1) DEFAULT '1',
                                          PRIMARY KEY (`id`),
                                          UNIQUE KEY `device_code` (`device_code`),
                                          UNIQUE KEY `gb_device_id` (`gb_device_id`,`gb_channel_id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    </update>

    <update id="dropProcedureUpdateDeviceTable">
        DROP PROCEDURE IF EXISTS update_mocking_device;
    </update>

    <update id="createProcedureUpdateDeviceTable">
        CREATE PROCEDURE update_mocking_device()
        BEGIN
            IF ISNULL((SELECT 1 FROM information_schema.COLUMNS
                       WHERE TABLE_SCHEMA = DATABASE()
                         AND TABLE_NAME = "mocking_device"
                         AND COLUMN_NAME = "live_stream" LIMIT 1))  THEN
                ALTER TABLE `mocking_device` ADD COLUMN live_stream VARCHAR(255) DEFAULT NULL;
            END IF;
        END;
    </update>

    <update id="callProcedureUpdateDeviceTable">
        CALL update_mocking_device();
    </update>
</mapper>
