-- ============================================================
-- nl_wx_app 小程序应用配置表（幂等）
-- 与 public/nl_admin.sql 末尾建表等价；已装库可单独执行本文件补表
-- AppSecret 等敏感字段由后台加密入库，不要在本文件写明文密钥
-- ============================================================

SET @db := DATABASE();
SET @exists := (
    SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'nl_wx_app'
);
SET @sql := IF(@exists = 0,
    'CREATE TABLE `nl_wx_app` (
      `id` int UNSIGNED NOT NULL AUTO_INCREMENT,
      `code` varchar(20) NOT NULL DEFAULT '''' COMMENT ''品牌标识 brm/wl'',
      `name` varchar(50) NOT NULL DEFAULT '''',
      `app_id` varchar(50) NOT NULL DEFAULT '''',
      `app_secret` varchar(255) NOT NULL DEFAULT '''' COMMENT ''密文存储'',
      `mch_id` varchar(50) NOT NULL DEFAULT '''' COMMENT ''微信支付商户号'',
      `mch_key` varchar(255) NOT NULL DEFAULT '''' COMMENT ''APIv3 密钥，密文存储'',
      `mch_serial_no` varchar(100) NOT NULL DEFAULT '''' COMMENT ''商户证书序列号'',
      `mch_private_key` text NULL COMMENT ''商户私钥，密文存储'',
      `platform_public_key` text NULL COMMENT ''微信支付平台证书公钥'',
      `notify_url` varchar(255) NOT NULL DEFAULT '''' COMMENT ''支付回调地址'',
      `template_code` varchar(50) NOT NULL DEFAULT '''' COMMENT ''默认装修模板 code'',
      `status` tinyint NOT NULL DEFAULT 0 COMMENT ''0=启用 1=停用'',
      `remark` varchar(255) NOT NULL DEFAULT '''',
      `created_at` int NOT NULL DEFAULT 0,
      `updated_at` int NOT NULL DEFAULT 0,
      `deleted_at` int NOT NULL DEFAULT 0,
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uk_wx_app_code`(`code` ASC) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = ''小程序应用配置'' ROW_FORMAT = DYNAMIC',
    'SELECT ''skip nl_wx_app (already exists)'' AS msg'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
