get_charset_collate(); $prefix = $wpdb->prefix; // ---------- 1. 积分流水 ---------- $sql_credit_log = "CREATE TABLE {$prefix}aksrt_credit_log ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, delta INT NOT NULL, balance_after INT NOT NULL, reason VARCHAR(64) NOT NULL, remark VARCHAR(255) DEFAULT NULL, ref_type VARCHAR(32) DEFAULT NULL, ref_id BIGINT UNSIGNED DEFAULT NULL, ip VARCHAR(64) DEFAULT NULL, ua VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY idx_user_time (user_id, created_at), KEY idx_reason (reason) ) {$charset_collate};"; dbDelta($sql_credit_log); // ---------- 1b. 现金余额流水(站内钱包,与积分分离) ---------- $sql_balance_log = "CREATE TABLE {$prefix}aksrt_balance_log ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, delta DECIMAL(18,2) NOT NULL, balance_after DECIMAL(18,2) NOT NULL, reason VARCHAR(64) NOT NULL, remark VARCHAR(255) DEFAULT NULL, ref_type VARCHAR(32) DEFAULT NULL, ref_id BIGINT UNSIGNED DEFAULT NULL, ip VARCHAR(64) DEFAULT NULL, ua VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY idx_user_time (user_id, created_at), KEY idx_reason (reason) ) {$charset_collate};"; dbDelta($sql_balance_log); // ---------- 2. 签到 ---------- $sql_signin = "CREATE TABLE {$prefix}aksrt_signin_log ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, signin_date DATE NOT NULL, streak INT NOT NULL DEFAULT 1, reward INT NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY uniq_user_date (user_id, signin_date), KEY idx_user (user_id) ) {$charset_collate};"; dbDelta($sql_signin); // ---------- 3. 邀请 ---------- $sql_invite = "CREATE TABLE {$prefix}aksrt_invite ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, inviter_id BIGINT UNSIGNED NOT NULL, invitee_id BIGINT UNSIGNED DEFAULT NULL, invite_code VARCHAR(32) NOT NULL, status VARCHAR(16) NOT NULL DEFAULT 'pending', created_at DATETIME NOT NULL, rewarded_at DATETIME DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY uniq_code (invite_code), KEY idx_inviter (inviter_id), KEY idx_invitee (invitee_id), KEY idx_status (status) ) {$charset_collate};"; dbDelta($sql_invite); // ---------- 4. 下载 token ---------- $sql_dl_token = "CREATE TABLE {$prefix}aksrt_dl_token ( token CHAR(48) NOT NULL, user_id BIGINT UNSIGNED NOT NULL, post_id BIGINT UNSIGNED NOT NULL, source_index INT NOT NULL DEFAULT 0, cost INT NOT NULL DEFAULT 0, ip_bind VARCHAR(64) DEFAULT NULL, ua_hash CHAR(40) DEFAULT NULL, used_count INT NOT NULL DEFAULT 0, max_uses INT NOT NULL DEFAULT 1, expires_at DATETIME NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (token), KEY idx_expires (expires_at), KEY idx_user (user_id), KEY idx_post (post_id) ) {$charset_collate};"; dbDelta($sql_dl_token); // ---------- 5. 用户通知收件箱 ---------- $sql_notification = "CREATE TABLE {$prefix}aksrt_notification ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, type VARCHAR(32) NOT NULL, title VARCHAR(191) NOT NULL, description TEXT DEFAULT NULL, target_url TEXT DEFAULT NULL, is_read TINYINT(1) NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, read_at DATETIME DEFAULT NULL, PRIMARY KEY (id), KEY idx_user_created (user_id, created_at), KEY idx_user_read (user_id, is_read) ) {$charset_collate};"; dbDelta($sql_notification); // ---------- 6. 私信会话 ---------- $sql_conversation = "CREATE TABLE {$prefix}aksrt_conversation ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_a BIGINT UNSIGNED NOT NULL, user_b BIGINT UNSIGNED NOT NULL, last_message VARCHAR(255) DEFAULT '', last_message_at DATETIME DEFAULT NULL, unread_a INT NOT NULL DEFAULT 0, unread_b INT NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY uniq_pair (user_a, user_b), KEY idx_user_a (user_a), KEY idx_user_b (user_b), KEY idx_last_msg (last_message_at) ) {$charset_collate};"; dbDelta($sql_conversation); // ---------- 7. 私信消息 ---------- $sql_conv_message = "CREATE TABLE {$prefix}aksrt_conversation_message ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, conversation_id BIGINT UNSIGNED NOT NULL, sender_id BIGINT UNSIGNED NOT NULL, content TEXT NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY idx_conv_time (conversation_id, created_at), KEY idx_sender (sender_id) ) {$charset_collate};"; dbDelta($sql_conv_message); // ---------- 8. 订单表 ---------- $sql_order = "CREATE TABLE {$prefix}aksrt_order ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, order_number VARCHAR(32) NOT NULL, user_id BIGINT UNSIGNED NOT NULL, product_type VARCHAR(32) NOT NULL DEFAULT '', product_id BIGINT UNSIGNED NOT NULL DEFAULT 0, amount DECIMAL(10,2) NOT NULL DEFAULT 0, currency VARCHAR(8) NOT NULL DEFAULT 'CNY', status VARCHAR(20) NOT NULL DEFAULT 'pending', shipping_status TINYINT NOT NULL DEFAULT 0, payment_method VARCHAR(32) NOT NULL DEFAULT '', payment_transaction_id VARCHAR(128) NOT NULL DEFAULT '', created_at DATETIME NOT NULL, paid_at DATETIME DEFAULT NULL, delivered_at DATETIME DEFAULT NULL, completed_at DATETIME DEFAULT NULL, cancelled_at DATETIME DEFAULT NULL, refunded_at DATETIME DEFAULT NULL, expires_at DATETIME DEFAULT NULL, note TEXT DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY uniq_order_number (order_number), KEY idx_user (user_id), KEY idx_status (status), KEY idx_created (created_at), KEY idx_product (product_type, product_id) ) {$charset_collate};"; dbDelta($sql_order); // ---------- 9. 收益台账 ---------- $sql_revenue = "CREATE TABLE {$prefix}aksrt_revenue ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, order_number VARCHAR(32) NOT NULL DEFAULT '', amount DECIMAL(10,2) NOT NULL DEFAULT 0, type VARCHAR(32) NOT NULL DEFAULT 'income', status VARCHAR(20) NOT NULL DEFAULT 'pending', created_at DATETIME NOT NULL, settled_at DATETIME DEFAULT NULL, PRIMARY KEY (id), KEY idx_user (user_id), KEY idx_order (order_number), KEY idx_status (status), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_revenue); // ---------- 10. 提现台账 ---------- $sql_withdrawal = "CREATE TABLE {$prefix}aksrt_withdrawal ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, amount DECIMAL(10,2) NOT NULL DEFAULT 0, method VARCHAR(32) NOT NULL DEFAULT '', account_info TEXT DEFAULT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending', created_at DATETIME NOT NULL, processed_at DATETIME DEFAULT NULL, note TEXT DEFAULT NULL, PRIMARY KEY (id), KEY idx_user (user_id), KEY idx_status (status), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_withdrawal); // ---------- 11. 举报工单 ---------- $sql_report = "CREATE TABLE {$prefix}aksrt_report ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, reporter_id BIGINT UNSIGNED NOT NULL, target_type VARCHAR(20) NOT NULL, target_id BIGINT UNSIGNED NOT NULL, reason TEXT NOT NULL, category VARCHAR(40) NOT NULL DEFAULT '', status VARCHAR(20) NOT NULL DEFAULT 'pending', created_at DATETIME NOT NULL, updated_at DATETIME DEFAULT NULL, notes_json TEXT DEFAULT NULL, PRIMARY KEY (id), KEY idx_status (status), KEY idx_target (target_type, target_id), KEY idx_reporter (reporter_id), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_report); // ---------- 12. 用户限制/封禁 ---------- $sql_restriction = "CREATE TABLE {$prefix}aksrt_user_restriction ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, type VARCHAR(20) NOT NULL DEFAULT 'warn', reason TEXT NOT NULL, scope VARCHAR(20) NOT NULL DEFAULT 'global', expires_at DATETIME DEFAULT NULL, created_by BIGINT UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY idx_user (user_id), KEY idx_type (type), KEY idx_scope (scope), KEY idx_expires (expires_at), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_restriction); // ---------- 13. 商品变体/SKU ---------- $sql_variant = "CREATE TABLE {$prefix}aksrt_product_variant ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, product_id BIGINT UNSIGNED NOT NULL, sku VARCHAR(64) NOT NULL DEFAULT '', attributes TEXT DEFAULT NULL, price DECIMAL(10,2) NOT NULL DEFAULT 0, stock INT NOT NULL DEFAULT 0, image VARCHAR(512) NOT NULL DEFAULT '', sort_order INT NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY idx_product (product_id), KEY idx_sku (sku), KEY idx_sort (sort_order) ) {$charset_collate};"; dbDelta($sql_variant); // ── follow ── $sql_follow = "CREATE TABLE {$prefix}aksrt_follow ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, follower_id BIGINT UNSIGNED NOT NULL, following_id BIGINT UNSIGNED NOT NULL, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY idx_pair (follower_id, following_id), KEY idx_following (following_id), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_follow); // ── coupon ── $sql_coupon = "CREATE TABLE {$prefix}aksrt_coupon ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, code VARCHAR(60) NOT NULL DEFAULT '', type VARCHAR(20) NOT NULL DEFAULT 'fixed', value DECIMAL(10,2) NOT NULL DEFAULT 0, min_amount DECIMAL(10,2) NOT NULL DEFAULT 0, usage_limit INT UNSIGNED NOT NULL DEFAULT 0, used_count INT UNSIGNED NOT NULL DEFAULT 0, expires_at DATETIME DEFAULT NULL, per_user_limit INT UNSIGNED NOT NULL DEFAULT 0, product_scope TEXT DEFAULT NULL, max_discount DECIMAL(10,2) NOT NULL DEFAULT 0, status VARCHAR(20) NOT NULL DEFAULT 'active', created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY idx_code (code), KEY idx_status (status), KEY idx_expires (expires_at) ) {$charset_collate};"; dbDelta($sql_coupon); // ── coupon_usage (R68) ── $sql_coupon_usage = "CREATE TABLE {$prefix}aksrt_coupon_usage ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, coupon_id BIGINT UNSIGNED NOT NULL DEFAULT 0, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, order_number VARCHAR(32) NOT NULL DEFAULT '', discount DECIMAL(10,2) NOT NULL DEFAULT 0, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY idx_coupon (coupon_id), KEY idx_user (user_id), KEY idx_order (order_number) ) {$charset_collate};"; dbDelta($sql_coupon_usage); // ── after_sale ── $sql_after_sale = "CREATE TABLE {$prefix}aksrt_after_sale ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, order_number VARCHAR(32) NOT NULL DEFAULT '', user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, type VARCHAR(20) NOT NULL DEFAULT 'refund', reason TEXT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending', amount DECIMAL(10,2) NOT NULL DEFAULT 0, note TEXT DEFAULT NULL, operator_id BIGINT UNSIGNED DEFAULT NULL, operator_note TEXT DEFAULT NULL, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at DATETIME DEFAULT NULL, resolved_at DATETIME DEFAULT NULL, PRIMARY KEY (id), KEY idx_order (order_number), KEY idx_user (user_id), KEY idx_status (status), KEY idx_type (type), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_after_sale); // ── order_shipping ── $sql_shipping = "CREATE TABLE {$prefix}aksrt_order_shipping ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, order_number VARCHAR(32) NOT NULL DEFAULT '', carrier VARCHAR(60) NOT NULL DEFAULT '', tracking_no VARCHAR(128) NOT NULL DEFAULT '', status VARCHAR(20) NOT NULL DEFAULT 'shipped', note TEXT DEFAULT NULL, operator_id BIGINT UNSIGNED DEFAULT NULL, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at DATETIME DEFAULT NULL, delivered_at DATETIME DEFAULT NULL, PRIMARY KEY (id), KEY idx_order (order_number), KEY idx_status (status), KEY idx_tracking (tracking_no), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_shipping); // ── charge_card (zibll-style typed card_pass) ── $sql_charge_card = "CREATE TABLE {$prefix}aksrt_charge_card ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, card VARCHAR(60) NOT NULL DEFAULT '', password VARCHAR(80) NOT NULL DEFAULT '', type VARCHAR(20) NOT NULL DEFAULT 'balance', amount DECIMAL(10,2) NOT NULL DEFAULT 0, balance DECIMAL(10,2) NOT NULL DEFAULT 0, status VARCHAR(20) NOT NULL DEFAULT 'active', meta LONGTEXT DEFAULT NULL, order_number VARCHAR(32) NOT NULL DEFAULT '', used_by BIGINT UNSIGNED DEFAULT NULL, used_at DATETIME DEFAULT NULL, expires_at DATETIME DEFAULT NULL, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', note TEXT DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY idx_password (password), KEY idx_card (card), KEY idx_type (type), KEY idx_status (status), KEY idx_used_by (used_by), KEY idx_order_number (order_number), KEY idx_expires (expires_at) ) {$charset_collate};"; dbDelta($sql_charge_card); // ── rebate_log ── $sql_rebate_log = "CREATE TABLE {$prefix}aksrt_rebate_log ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, order_number VARCHAR(32) NOT NULL DEFAULT '', source_user_id BIGINT UNSIGNED DEFAULT NULL, amount DECIMAL(10,2) NOT NULL DEFAULT 0, type VARCHAR(32) NOT NULL DEFAULT 'order', status VARCHAR(20) NOT NULL DEFAULT 'pending', created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', settled_at DATETIME DEFAULT NULL, note TEXT DEFAULT NULL, PRIMARY KEY (id), KEY idx_user (user_id), KEY idx_order (order_number), KEY idx_source (source_user_id), KEY idx_status (status), KEY idx_type (type), KEY idx_created (created_at) ) {$charset_collate};"; dbDelta($sql_rebate_log); // ── order_items (§6.5 多商品多商家) ── $sql_order_items = "CREATE TABLE {$prefix}aksrt_order_item ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, order_number VARCHAR(32) NOT NULL DEFAULT '', product_id BIGINT UNSIGNED NOT NULL DEFAULT 0, product_title VARCHAR(255) NOT NULL DEFAULT '', options_key VARCHAR(255) NOT NULL DEFAULT '', options_label VARCHAR(255) NOT NULL DEFAULT '', quantity INT UNSIGNED NOT NULL DEFAULT 1, unit_price DECIMAL(10,2) NOT NULL DEFAULT 0, total_price DECIMAL(10,2) NOT NULL DEFAULT 0, discount_amount DECIMAL(10,2) NOT NULL DEFAULT 0, shipping_fee DECIMAL(10,2) NOT NULL DEFAULT 0, pay_price DECIMAL(10,2) NOT NULL DEFAULT 0, shipping_type VARCHAR(20) NOT NULL DEFAULT 'express', auto_delivery_content TEXT DEFAULT NULL, discount_hit TEXT DEFAULT NULL, gift_data TEXT DEFAULT NULL, image VARCHAR(512) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY idx_order (order_number), KEY idx_product (product_id) ) {$charset_collate};"; dbDelta($sql_order_items); // ── card_key 卡密表 (§6.2 自动发货) ── $sql_card_key = "CREATE TABLE {$prefix}aksrt_card_key ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, product_id BIGINT UNSIGNED NOT NULL DEFAULT 0, options_key VARCHAR(255) NOT NULL DEFAULT '', card_code TEXT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'available', order_number VARCHAR(32) NOT NULL DEFAULT '', used_by BIGINT UNSIGNED DEFAULT NULL, used_at DATETIME DEFAULT NULL, created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY idx_product (product_id), KEY idx_options (options_key(50)), KEY idx_status (status), KEY idx_order (order_number) ) {$charset_collate};"; dbDelta($sql_card_key); } }