-- 生产库增量 SQL（2026-08-03）
-- 已排除：社区菜单、帖子分类及其权限 SQL。
-- 本脚本可重复执行，不会删除既有表或数据。

-- ============================================================
-- 1. 社区帖子推荐排序字段与索引
-- ============================================================
SET @has_post_sort = (
  SELECT COUNT(*)
  FROM `information_schema`.`columns`
  WHERE `table_schema` = DATABASE()
    AND `table_name` = 'la_community_post'
    AND `column_name` = 'sort'
);
SET @sql = IF(
  @has_post_sort = 0,
  'ALTER TABLE `la_community_post` ADD COLUMN `sort` int unsigned NOT NULL DEFAULT 0 COMMENT ''推荐流排序值，越大越靠前'' AFTER `is_topic`',
  'SELECT ''la_community_post.sort already exists'''
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @has_recommend_sort_index = (
  SELECT COUNT(*)
  FROM `information_schema`.`statistics`
  WHERE `table_schema` = DATABASE()
    AND `table_name` = 'la_community_post'
    AND `index_name` = 'idx_status_recommend_sort'
);
SET @sql = IF(
  @has_recommend_sort_index = 0,
  'ALTER TABLE `la_community_post` ADD INDEX `idx_status_recommend_sort` (`status`, `is_recommend`, `sort`, `create_time`)',
  'SELECT ''la_community_post.idx_status_recommend_sort already exists'''
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- 2. 联赛热门配置字段
-- ============================================================
SET @schema_name = DATABASE();
SET @has_is_hot = (
    SELECT COUNT(*)
    FROM `information_schema`.`COLUMNS`
    WHERE `TABLE_SCHEMA` = @schema_name
      AND `TABLE_NAME` = 'la_league'
      AND `COLUMN_NAME` = 'is_hot'
);
SET @alter_sql = IF(
    @has_is_hot = 0,
    'ALTER TABLE `la_league` ADD COLUMN `is_hot` tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否热门联赛: 0=否 1=是'' AFTER `is_show`',
    'SELECT ''la_league.is_hot already exists'''
);
PREPARE alter_stmt FROM @alter_sql;
EXECUTE alter_stmt;
DEALLOCATE PREPARE alter_stmt;

-- ============================================================
-- 3. 加密行情自选表及行情配置
-- ============================================================
CREATE TABLE IF NOT EXISTS `la_crypto_favorite` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  `user_id` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
  `symbol` varchar(20) NOT NULL DEFAULT '' COMMENT '币种代码',
  `status` tinyint UNSIGNED NOT NULL DEFAULT 1 COMMENT '自选状态 0-取消 1-自选',
  `create_time` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
  `update_time` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
  `delete_time` int NULL DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_symbol` (`user_id`, `symbol`),
  KEY `idx_user_status` (`user_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户加密行情自选表';

INSERT INTO `la_config` (`type`, `name`, `value`, `create_time`, `update_time`)
SELECT 'crypto_market', 'api_base', 'https://api.coingecko.com/api/v3', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (
  SELECT 1 FROM `la_config` WHERE `type` = 'crypto_market' AND `name` = 'api_base'
);

INSERT INTO `la_config` (`type`, `name`, `value`, `create_time`, `update_time`)
SELECT 'crypto_market', 'cache_seconds', '60', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (
  SELECT 1 FROM `la_config` WHERE `type` = 'crypto_market' AND `name` = 'cache_seconds'
);

SET @crypto_market_coins = JSON_ARRAY(
  JSON_OBJECT('id', 'bitcoin', 'symbol', 'BTC', 'name_zh', '比特币'),
  JSON_OBJECT('id', 'ethereum', 'symbol', 'ETH', 'name_zh', '以太坊'),
  JSON_OBJECT('id', 'binancecoin', 'symbol', 'BNB', 'name_zh', '币安币'),
  JSON_OBJECT('id', 'solana', 'symbol', 'SOL', 'name_zh', 'Solana'),
  JSON_OBJECT('id', 'ripple', 'symbol', 'XRP', 'name_zh', '瑞波币'),
  JSON_OBJECT('id', 'cardano', 'symbol', 'ADA', 'name_zh', '艾达币'),
  JSON_OBJECT('id', 'dogecoin', 'symbol', 'DOGE', 'name_zh', '狗狗币'),
  JSON_OBJECT('id', 'tron', 'symbol', 'TRX', 'name_zh', '波场'),
  JSON_OBJECT('id', 'avalanche-2', 'symbol', 'AVAX', 'name_zh', 'Avalanche'),
  JSON_OBJECT('id', 'chainlink', 'symbol', 'LINK', 'name_zh', 'Chainlink'),
  JSON_OBJECT('id', 'the-open-network', 'symbol', 'TON', 'name_zh', 'Toncoin'),
  JSON_OBJECT('id', 'shiba-inu', 'symbol', 'SHIB', 'name_zh', '柴犬币'),
  JSON_OBJECT('id', 'polkadot', 'symbol', 'DOT', 'name_zh', '波卡币'),
  JSON_OBJECT('id', 'sui', 'symbol', 'SUI', 'name_zh', 'Sui'),
  JSON_OBJECT('id', 'near', 'symbol', 'NEAR', 'name_zh', 'NEAR'),
  JSON_OBJECT('id', 'litecoin', 'symbol', 'LTC', 'name_zh', '莱特币'),
  JSON_OBJECT('id', 'bitcoin-cash', 'symbol', 'BCH', 'name_zh', '比特币现金'),
  JSON_OBJECT('id', 'uniswap', 'symbol', 'UNI', 'name_zh', 'Uniswap'),
  JSON_OBJECT('id', 'aave', 'symbol', 'AAVE', 'name_zh', 'Aave'),
  JSON_OBJECT('id', 'ethereum-classic', 'symbol', 'ETC', 'name_zh', '以太坊经典')
);

INSERT INTO `la_config` (`type`, `name`, `value`, `create_time`, `update_time`)
SELECT 'crypto_market', 'coins', CAST(@crypto_market_coins AS CHAR), UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (
  SELECT 1 FROM `la_config` WHERE `type` = 'crypto_market' AND `name` = 'coins'
);

UPDATE `la_decorate_tabbar`
SET `name` = '行情', `update_time` = UNIX_TIMESTAMP()
WHERE `link` LIKE '%pages%crypto%crypto%'
  AND `name` <> '行情';
