CREATE TABLE IF NOT EXISTS `YB_TABLE_PREFIX_local_solution` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL DEFAULT '' COMMENT '方案名称',
    `application_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '方案应用标识',
    `plugin_file` varchar(100) NOT NULL DEFAULT '' COMMENT '安装命名空间文件名',
    `version` varchar(50) NOT NULL DEFAULT '1.0.0' COMMENT '当前配置版本或最近发布版本',
    `icon` varchar(255) NOT NULL DEFAULT '' COMMENT '图标',
    `description` text NULL COMMENT '描述',
    `data` mediumtext NULL COMMENT '扩展配置JSON',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态 0禁用 1启用',
    `publish_status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '发布状态 0未发布 1已发布 2有未发布变更',
    `last_publish_log_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '最近发布日志ID',
    `last_publish_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '最近发布时间',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    `delete_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '软删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_application_flag_mch_shop_delete` (`application_flag`, `mch_id`, `shop_id`, `delete_time`),
    KEY `idx_mch_shop` (`mch_id`, `shop_id`),
    KEY `idx_publish_status` (`publish_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案主表';

CREATE TABLE IF NOT EXISTS `YB_TABLE_PREFIX_local_solution_class` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `solution_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '本地方案ID',
    `pid` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '父级分类ID',
    `level` tinyint(3) unsigned NOT NULL DEFAULT 1 COMMENT '层级',
    `name` varchar(100) NOT NULL DEFAULT '' COMMENT '分类名称',
    `sort` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '排序',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态 0禁用 1启用',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    `delete_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '软删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_solution_pid` (`solution_id`, `pid`, `delete_time`),
    KEY `idx_solution_status` (`solution_id`, `status`, `delete_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案应用分类表';

CREATE TABLE IF NOT EXISTS `yb_local_solution_menus` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `solution_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '本地方案ID',
    `name` varchar(100) NOT NULL DEFAULT '' COMMENT '菜单名称',
    `application_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '方案应用标识',
    `plugin_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '来源插件标识',
    `path` varchar(255) NOT NULL DEFAULT '' COMMENT '前端路径',
    `component` varchar(255) NOT NULL DEFAULT '' COMMENT '前端组件',
    `url` varchar(255) NOT NULL DEFAULT '' COMMENT '接口权限URL',
    `mid` varchar(100) NOT NULL DEFAULT '' COMMENT '方案内菜单唯一标识',
    `pid` varchar(100) NOT NULL DEFAULT '' COMMENT '父级mid',
    `level` tinyint(3) unsigned NOT NULL DEFAULT 1 COMMENT '层级',
    `sort` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '排序',
    `logo` text NULL COMMENT '图标',
    `info` varchar(500) NOT NULL DEFAULT '' COMMENT '描述',
    `collapse` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '展开状态',
    `is_menu` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否菜单',
    `is_page` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否页面',
    `is_show` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否显示',
    `menu_type` varchar(50) NOT NULL DEFAULT 'admin' COMMENT '菜单类型',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态',
    `source` varchar(30) NOT NULL DEFAULT 'manual' COMMENT '来源 manual/runtime_plugin/default',
    `source_menu_mid` varchar(100) NOT NULL DEFAULT '' COMMENT '运行环境原菜单mid',
    `data` mediumtext NULL COMMENT '扩展JSON',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_solution_mid` (`solution_id`, `mid`),
    KEY `idx_solution_pid` (`solution_id`, `pid`),
    KEY `idx_solution_plugin` (`solution_id`, `plugin_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案菜单表';

CREATE TABLE IF NOT EXISTS `yb_local_solution_plugins` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `solution_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '本地方案ID',
    `class_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '应用管理分类ID',
    `is_sync_menu` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否已同步菜单',
    `sys_addons_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '运行环境sys_addons.id',
    `name` varchar(100) NOT NULL DEFAULT '' COMMENT '插件名称快照',
    `plugin_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '插件标识',
    `plugin_file` varchar(100) NOT NULL DEFAULT '' COMMENT '插件目录',
    `plugin_type` varchar(50) NOT NULL DEFAULT '' COMMENT '插件类型',
    `version` varchar(50) NOT NULL DEFAULT '' COMMENT '导入版本',
    `is_default` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否默认插件',
    `excludes` varchar(255) NOT NULL DEFAULT '' COMMENT '互斥插件',
    `menu_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '导入菜单数量',
    `menu_config` mediumtext NULL COMMENT '导入菜单JSON快照',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态 0已移除 1有效',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    `delete_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '软删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_solution_plugin` (`solution_id`, `plugin_flag`, `delete_time`),
    KEY `idx_solution_default` (`solution_id`, `is_default`),
    KEY `idx_solution_plugin_class` (`solution_id`, `class_id`, `delete_time`),
    KEY `idx_solution_plugin_menu` (`solution_id`, `is_sync_menu`, `delete_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案插件关联表';

CREATE TABLE IF NOT EXISTS `yb_local_solution_cloud_sync` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `solution_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '本地方案ID',
    `application_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '本地方案标识',
    `diy_application_flag` varchar(100) NOT NULL DEFAULT '' COMMENT '云端DIY应用标识',
    `cloud_user_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '壹佰云sys_user.id',
    `cloud_developer_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '壹佰云developer.id',
    `developer_flag` varchar(255) NOT NULL DEFAULT '' COMMENT 'DIY开发者标识',
    `cloud_team_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '壹佰云developer_team.id',
    `cloud_diy_application_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '壹佰云diy_application.id',
    `sync_status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0待同步 1成功 2失败',
    `sync_msg` varchar(500) NOT NULL DEFAULT '' COMMENT '同步结果消息',
    `retry_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '重试次数',
    `last_request` mediumtext NULL COMMENT '最近请求快照',
    `last_response` mediumtext NULL COMMENT '最近响应快照',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_solution_id` (`solution_id`),
    KEY `idx_application_flag` (`application_flag`),
    KEY `idx_sync_status` (`sync_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案云端开发者同步表';

CREATE TABLE IF NOT EXISTS `yb_local_solution_publish_log` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `solution_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '本地方案ID',
    `version` varchar(50) NOT NULL DEFAULT '' COMMENT '发布版本',
    `update_content` mediumtext NULL COMMENT '更新内容',
    `config` mediumtext NULL COMMENT '发布JSON完整内容',
    `file_path` varchar(500) NOT NULL DEFAULT '' COMMENT '本地文件路径',
    `file_url` varchar(500) NOT NULL DEFAULT '' COMMENT '文件URL',
    `file_hash` char(64) NOT NULL DEFAULT '' COMMENT 'JSON sha256',
    `plugin_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '插件数量',
    `menu_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '菜单数量',
    `operator_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '操作管理员',
    `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态 1正常 0删除',
    `mch_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '商户ID',
    `shop_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '店铺ID',
    `create_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
    `update_time` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_solution_version` (`solution_id`, `version`),
    KEY `idx_solution_time` (`solution_id`, `create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='本地解决方案发布记录表';
