首页 / 跨境电商轻量软件 / 实操指南:同步跨境电商库存数据的3个自动化技巧

实操指南:同步跨境电商库存数据的3个自动化技巧

实操指南:同步跨境电商库存数据的3个自动化技巧

引言:跨境电商库存同步的挑战与机遇

在跨境电商运营中,库存管理是连接供应链、销售渠道和客户体验的核心环节。随着业务规模扩大,卖家往往需要在多个平台(如Shopify、亚马逊、eBay等)同步销售商品,而手动更新库存数据不仅耗时耗力,还容易导致超卖或缺货问题。

WordPress作为全球最流行的开源内容管理系统,配合WooCommerce插件,已成为许多中小型跨境电商企业的首选平台。本文将基于WordPress/WooCommerce系统,为行业新人和程序员提供三种实用的库存数据自动化同步技巧,帮助您构建高效、可靠的库存管理系统。

技巧一:基于WP-Cron的定时库存同步系统

1.1 WP-Cron工作原理与配置

WordPress内置的WP-Cron系统允许我们安排定期执行的任务,是实现库存自动同步的基础。与系统级Cron不同,WP-Cron在每次页面访问时检查是否有待执行任务,更适合流量稳定的网站。

// 注册自定义Cron间隔
add_filter('cron_schedules', 'custom_cron_schedules');
function custom_cron_schedules($schedules) {
    $schedules['every_15_minutes'] = array(
        'interval' => 900, // 15分钟,以秒为单位
        'display'  => __('每15分钟')
    );
    $schedules['every_hour'] = array(
        'interval' => 3600, // 1小时
        'display'  => __('每小时')
    );
    return $schedules;
}

// 计划库存同步任务
register_activation_hook(__FILE__, 'inventory_sync_activation');
function inventory_sync_activation() {
    if (!wp_next_scheduled('daily_inventory_sync')) {
        wp_schedule_event(time(), 'every_hour', 'daily_inventory_sync');
    }
}

// 停用插件时清除计划任务
register_deactivation_hook(__FILE__, 'inventory_sync_deactivation');
function inventory_sync_deactivation() {
    wp_clear_scheduled_hook('daily_inventory_sync');
}

1.2 多平台库存API集成

实现库存同步的第一步是连接各个销售平台的API。以下是一个通用的API连接类示例:

class MultiPlatformInventoryAPI {
    private $platforms = [];
    
    public function __construct() {
        $this->platforms = [
            'woocommerce' => [
                'base_url' => get_site_url(),
                'consumer_key' => get_option('wc_api_key'),
                'consumer_secret' => get_option('wc_api_secret')
            ],
            'amazon' => [
                'marketplace_id' => get_option('amazon_marketplace_id'),
                'seller_id' => get_option('amazon_seller_id'),
                'access_key' => get_option('amazon_access_key'),
                'secret_key' => get_option('amazon_secret_key')
            ],
            'shopify' => [
                'shop_domain' => get_option('shopify_domain'),
                'api_key' => get_option('shopify_api_key'),
                'password' => get_option('shopify_api_password')
            ]
        ];
    }
    
    // 获取亚马逊库存
    public function get_amazon_inventory($sku) {
        $params = [
            'Action' => 'ListInventorySupply',
            'SellerSkus.member.1' => $sku,
            'QueryStartDateTime' => gmdate('Y-m-dTH:i:s.\0\0\0\Z', strtotime('-1 day')),
            'ResponseGroup' => 'Basic'
        ];
        
        // 构建亚马逊MWS签名请求
        $url = $this->generate_amazon_mws_url($params);
        $response = wp_remote_get($url, [
            'timeout' => 30,
            'headers' => [
                'User-Agent' => 'InventorySync/1.0 (Language=PHP)'
            ]
        ]);
        
        if (is_wp_error($response)) {
            error_log('亚马逊API错误: ' . $response->get_error_message());
            return false;
        }
        
        return $this->parse_amazon_response(wp_remote_retrieve_body($response));
    }
    
    // 获取Shopify库存
    public function get_shopify_inventory($product_id) {
        $shopify_url = "https://{$this->platforms['shopify']['api_key']}:{$this->platforms['shopify']['password']}@{$this->platforms['shopify']['shop_domain']}/admin/api/2023-01/products/{$product_id}/inventory_levels.json";
        
        $response = wp_remote_get($shopify_url, [
            'timeout' => 20,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ]
        ]);
        
        if (is_wp_error($response)) {
            error_log('Shopify API错误: ' . $response->get_error_message());
            return false;
        }
        
        $data = json_decode(wp_remote_retrieve_body($response), true);
        return isset($data['inventory_levels'][0]['available']) ? $data['inventory_levels'][0]['available'] : 0;
    }
    
    // 更新WooCommerce库存
    public function update_woocommerce_inventory($product_id, $quantity) {
        $product = wc_get_product($product_id);
        if ($product) {
            $product->set_stock_quantity($quantity);
            $product->set_manage_stock(true);
            $product->save();
            
            // 记录库存变更
            $this->log_inventory_change($product_id, 'woocommerce', $quantity);
            return true;
        }
        return false;
    }
    
    // 库存变更日志
    private function log_inventory_change($product_id, $platform, $quantity) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'inventory_sync_logs',
            [
                'product_id' => $product_id,
                'platform' => $platform,
                'quantity' => $quantity,
                'sync_time' => current_time('mysql'),
                'status' => 'success'
            ]
        );
    }
}

1.3 实现定时同步逻辑

// 主同步函数
add_action('daily_inventory_sync', 'execute_inventory_sync');
function execute_inventory_sync() {
    $inventory_api = new MultiPlatformInventoryAPI();
    $sync_manager = new InventorySyncManager();
    
    // 获取需要同步的产品列表
    $products_to_sync = $sync_manager->get_products_needing_sync();
    
    foreach ($products_to_sync as $product) {
        $sku = $product['sku'];
        $product_id = $product['product_id'];
        
        // 1. 从主库存源获取最新库存(这里以亚马逊为例)
        $latest_inventory = $inventory_api->get_amazon_inventory($sku);
        
        if ($latest_inventory !== false) {
            // 2. 更新WooCommerce库存
            $inventory_api->update_woocommerce_inventory($product_id, $latest_inventory);
            
            // 3. 可选:更新其他平台库存
            // $inventory_api->update_shopify_inventory($product_id, $latest_inventory);
            
            // 标记产品已同步
            $sync_manager->mark_product_synced($product_id);
        }
        
        // 避免API速率限制,每次请求间添加延迟
        sleep(1);
    }
    
    // 发送同步完成通知
    $sync_manager->send_sync_notification(count($products_to_sync));
}

// 库存同步管理器
class InventorySyncManager {
    public function get_products_needing_sync() {
        global $wpdb;
        
        // 获取最近24小时内未同步的产品
        $query = "
            SELECT p.ID as product_id, pm.meta_value as sku
            FROM {$wpdb->posts} p
            INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
            LEFT JOIN {$wpdb->prefix}inventory_sync_logs l ON p.ID = l.product_id 
                AND l.sync_time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
            WHERE p.post_type = 'product'
            AND p.post_status = 'publish'
            AND pm.meta_key = '_sku'
            AND pm.meta_value != ''
            AND l.id IS NULL
            LIMIT 50
        ";
        
        return $wpdb->get_results($query, ARRAY_A);
    }
    
    public function mark_product_synced($product_id) {
        update_post_meta($product_id, '_last_inventory_sync', current_time('mysql'));
    }
    
    public function send_sync_notification($synced_count) {
        $admin_email = get_option('admin_email');
        $subject = '库存同步完成报告';
        $message = "库存同步任务已完成。nn";
        $message .= "同步时间: " . current_time('mysql') . "n";
        $message .= "同步产品数量: " . $synced_count . "n";
        $message .= "系统: " . get_site_url() . "n";
        
        wp_mail($admin_email, $subject, $message);
    }
}

技巧二:基于Webhook的实时库存更新系统

2.1 Webhook原理与优势

与定时同步相比,Webhook提供了实时库存更新的能力。当主库存系统发生变化时,立即通知所有相关平台,实现真正的实时同步。

// 注册Webhook端点
add_action('rest_api_init', function() {
    register_rest_route('inventory-sync/v1', '/webhook', [
        'methods' => 'POST',
        'callback' => 'handle_inventory_webhook',
        'permission_callback' => 'verify_webhook_signature'
    ]);
});

// Webhook签名验证
function verify_webhook_signature($request) {
    $received_signature = $request->get_header('X-Webhook-Signature');
    $payload = $request->get_body();
    $expected_signature = hash_hmac('sha256', $payload, get_option('webhook_secret_key'));
    
    return hash_equals($expected_signature, $received_signature);
}

// 处理Webhook请求
function handle_inventory_webhook(WP_REST_Request $request) {
    $payload = json_decode($request->get_body(), true);
    
    // 验证必要字段
    if (!isset($payload['event']) || !isset($payload['data'])) {
        return new WP_REST_Response(['error' => '无效的请求格式'], 400);
    }
    
    $event = $payload['event'];
    $data = $payload['data'];
    
    switch ($event) {
        case 'inventory.updated':
            return process_inventory_update($data);
        case 'inventory.low_stock':
            return process_low_stock_alert($data);
        case 'product.created':
            return process_new_product($data);
        default:
            return new WP_REST_Response(['error' => '未知的事件类型'], 400);
    }
}

2.2 实时库存处理逻辑

function process_inventory_update($data) {
    $sku = $data['sku'];
    $new_quantity = $data['quantity'];
    $source = $data['source'] ?? 'unknown';
    
    // 查找WooCommerce产品
    $product_id = wc_get_product_id_by_sku($sku);
    
    if (!$product_id) {
        // 尝试通过其他标识符查找产品
        $product_id = find_product_by_custom_identifier($sku);
    }
    
    if ($product_id) {
        $product = wc_get_product($product_id);
        $old_quantity = $product->get_stock_quantity();
        
        // 更新库存
        $product->set_stock_quantity($new_quantity);
        $product->save();
        
        // 记录库存变更
        log_real_time_update($product_id, $old_quantity, $new_quantity, $source);
        
        // 触发相关操作
        if ($new_quantity == 0) {
            handle_out_of_stock($product_id);
        } elseif ($new_quantity < get_option('low_stock_threshold', 10)) {
            handle_low_stock($product_id, $new_quantity);
        }
        
        // 同步到其他平台(如果需要)
        sync_to_other_platforms($product_id, $new_quantity);
        
        return new WP_REST_Response([
            'success' => true,
            'message' => '库存更新成功',
            'product_id' => $product_id,
            'old_quantity' => $old_quantity,
            'new_quantity' => $new_quantity
        ], 200);
    }
    
    // 产品不存在,可能需要创建新产品
    if (get_option('auto_create_products', false)) {
        $new_product_id = create_product_from_webhook($data);
        return new WP_REST_Response([
            'success' => true,
            'message' => '产品已创建',
            'product_id' => $new_product_id
        ], 201);
    }
    
    return new WP_REST_Response([
        'error' => '产品未找到',
        'sku' => $sku
    ], 404);
}

// 实时同步到其他平台
function sync_to_other_platforms($product_id, $quantity) {
    $sync_settings = get_option('realtime_sync_settings', []);
    
    if (isset($sync_settings['shopify']['enabled']) && $sync_settings['shopify']['enabled']) {
        sync_to_shopify($product_id, $quantity);
    }
    
    if (isset($sync_settings['amazon']['enabled']) && $sync_settings['amazon']['enabled']) {
        sync_to_amazon($product_id, $quantity);
    }
    
    if (isset($sync_settings['ebay']['enabled']) && $sync_settings['ebay']['enabled']) {
        sync_to_ebay($product_id, $quantity);
    }
}

// Shopify实时同步
function sync_to_shopify($product_id, $quantity) {
    $product = wc_get_product($product_id);
    $shopify_id = get_post_meta($product_id, '_shopify_product_id', true);
    
    if (!$shopify_id) {
        // 如果没有Shopify ID,尝试通过SKU查找
        $shopify_id = find_shopify_product_by_sku($product->get_sku());
        if ($shopify_id) {
            update_post_meta($product_id, '_shopify_product_id', $shopify_id);
        } else {
            error_log("无法找到Shopify产品ID: " . $product->get_sku());
            return false;
        }
    }
    
    $shopify_api = new ShopifyAPI();
    return $shopify_api->update_inventory($shopify_id, $quantity);
}

2.3 Webhook管理界面

// 添加Webhook管理页面
add_action('admin_menu', 'inventory_webhook_admin_menu');
function inventory_webhook_admin_menu() {
    add_menu_page(
        '库存Webhook设置',
        '库存Webhook',
        'manage_options',
        'inventory-webhook',
        'webhook_settings_page',
        'dashicons-update',
        56
    );
}

function webhook_settings_page() {
    ?>
    <div class="wrap">
        <h1>库存Webhook设置</h1>
        
        <form method="post" action="options.php">
            <?php settings_fields('inventory_webhook_settings'); ?>
            <?php do_settings_sections('inventory_webhook_settings'); ?>
            
            <table class="form-table">
                <tr>
                    <th scope="row">Webhook状态</th>
                    <td>
                        <label>
                            <input type="checkbox" name="webhook_enabled" value="1" 
                                <?php checked(get_option('webhook_enabled'), 1); ?>>
                            启用实时库存同步
                        </label>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">Webhook URL</th>
                    <td>
                        <code><?php echo rest_url('inventory-sync/v1/webhook'); ?></code>
                        <p class="description">将此URL配置到您的库存管理系统</p>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">安全密钥</th>
                    <td>
                        <input type="text" name="webhook_secret_key" 
                            value="<?php echo esc_attr(get_option('webhook_secret_key', wp_generate_password(32))); ?>" 
                            class="regular-text">
                        <p class="description">用于验证Webhook请求的密钥</p>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">自动创建产品</th>
                    <td>
                        <label>
                            <input type="checkbox" name="auto_create_products" value="1" 
                                <?php checked(get_option('auto_create_products'), 1); ?>>
                            当收到未知SKU时自动创建产品
                        </label>
                    </td>
                </tr>
                
                <tr>
                    <th scope="row">实时同步平台</th>
                    <td>
                        <label>
                            <input type="checkbox" name="sync_shopify" value="1" 
                                <?php checked(get_option('sync_shopify'), 1); ?>>
                            同步到Shopify
                        </label><br>
                        <label>
                            <input type="checkbox" name="sync_amazon" value="1" 
                                <?php checked(get_option('sync_amazon'), 1); ?>>
                            同步到亚马逊
                        </label><br>
                        <label>
                            <input type="checkbox" name="sync_ebay" value="1" 
                                <?php checked(get_option('sync_ebay'), 1); ?>>
                            同步到eBay
                        </label>
                    </td>
                </tr>
            </table>
            
            <?php submit_button(); ?>
        </form>
        
        <h2>最近Webhook日志</h2>
        <?php display_webhook_logs(); ?>
    </div>
    <?php
}

技巧三:基于队列系统的异步库存

技巧三:基于队列系统的异步库存同步

3.1 队列系统在库存同步中的优势

当处理大量库存数据或需要与多个API交互时,同步操作可能导致超时或性能问题。队列系统将任务放入后台异步处理,提高系统稳定性和响应速度。

// 基于Action Scheduler的队列系统实现
class InventoryQueueManager {
    private $queue_name = 'inventory_sync_queue';
    
    public function __construct() {
        // 确保Action Scheduler已加载
        if (!function_exists('as_schedule_single_action')) {
            require_once plugin_dir_path(__FILE__) . 'action-scheduler/action-scheduler.php';
        }
    }
    
    // 添加库存同步任务到队列
    public function enqueue_inventory_sync($product_ids, $priority = 'normal') {
        if (empty($product_ids)) {
            return false;
        }
        
        // 将产品ID分批次处理(每批50个)
        $chunks = array_chunk($product_ids, 50);
        $scheduled_count = 0;
        
        foreach ($chunks as $chunk_index => $chunk) {
            $args = [
                'product_ids' => $chunk,
                'batch_number' => $chunk_index + 1,
                'total_batches' => count($chunks)
            ];
            
            // 根据优先级设置不同的执行时间
            $timestamp = time();
            if ($priority === 'high') {
                $timestamp += 60; // 1分钟后执行
            } elseif ($priority === 'normal') {
                $timestamp += 300; // 5分钟后执行
            } else {
                $timestamp += 1800; // 30分钟后执行
            }
            
            // 安排异步任务
            $action_id = as_schedule_single_action(
                $timestamp,
                'process_inventory_sync_batch',
                $args,
                $this->queue_name
            );
            
            if ($action_id) {
                $scheduled_count++;
                $this->log_queue_action('enqueued', $action_id, $args);
            }
        }
        
        return $scheduled_count;
    }
    
    // 处理批次任务
    public function process_batch($product_ids, $batch_number, $total_batches) {
        $results = [
            'success' => 0,
            'failed' => 0,
            'skipped' => 0,
            'details' => []
        ];
        
        $inventory_api = new MultiPlatformInventoryAPI();
        
        foreach ($product_ids as $product_id) {
            try {
                // 获取产品信息
                $product = wc_get_product($product_id);
                if (!$product) {
                    $results['skipped']++;
                    $results['details'][] = [
                        'product_id' => $product_id,
                        'status' => 'skipped',
                        'reason' => '产品不存在'
                    ];
                    continue;
                }
                
                $sku = $product->get_sku();
                if (empty($sku)) {
                    $results['skipped']++;
                    continue;
                }
                
                // 从主数据源获取库存
                $inventory_data = $this->fetch_inventory_from_primary_source($sku);
                
                if ($inventory_data && isset($inventory_data['quantity'])) {
                    // 更新本地库存
                    $old_quantity = $product->get_stock_quantity();
                    $product->set_stock_quantity($inventory_data['quantity']);
                    
                    // 如果有价格更新,也同步价格
                    if (isset($inventory_data['price'])) {
                        $product->set_regular_price($inventory_data['price']);
                    }
                    
                    $product->save();
                    
                    // 记录变更
                    $this->record_inventory_change(
                        $product_id,
                        $old_quantity,
                        $inventory_data['quantity'],
                        'queue_sync'
                    );
                    
                    $results['success']++;
                    $results['details'][] = [
                        'product_id' => $product_id,
                        'status' => 'success',
                        'old_quantity' => $old_quantity,
                        'new_quantity' => $inventory_data['quantity']
                    ];
                    
                    // 触发库存变更钩子
                    do_action('inventory_updated_via_queue', $product_id, $old_quantity, $inventory_data['quantity']);
                    
                } else {
                    $results['failed']++;
                    $results['details'][] = [
                        'product_id' => $product_id,
                        'status' => 'failed',
                        'reason' => '无法获取库存数据'
                    ];
                }
                
                // 避免API速率限制
                usleep(100000); // 100ms延迟
                
            } catch (Exception $e) {
                $results['failed']++;
                $results['details'][] = [
                    'product_id' => $product_id,
                    'status' => 'error',
                    'reason' => $e->getMessage()
                ];
                
                error_log('库存同步错误 - 产品ID: ' . $product_id . ' - ' . $e->getMessage());
            }
        }
        
        // 记录批次完成
        $this->log_batch_completion($batch_number, $total_batches, $results);
        
        // 如果是最后一个批次,发送汇总报告
        if ($batch_number === $total_batches) {
            $this->send_batch_completion_report($results);
        }
        
        return $results;
    }
    
    // 从主数据源获取库存
    private function fetch_inventory_from_primary_source($sku) {
        $primary_source = get_option('primary_inventory_source', 'erp');
        
        switch ($primary_source) {
            case 'erp':
                return $this->fetch_from_erp_api($sku);
            case 'amazon':
                $api = new MultiPlatformInventoryAPI();
                return ['quantity' => $api->get_amazon_inventory($sku)];
            case 'shopify':
                $api = new MultiPlatformInventoryAPI();
                $product_id = wc_get_product_id_by_sku($sku);
                return ['quantity' => $api->get_shopify_inventory($product_id)];
            case 'custom':
                return apply_filters('fetch_custom_inventory', $sku);
            default:
                return null;
        }
    }
    
    // ERP API集成示例
    private function fetch_from_erp_api($sku) {
        $erp_endpoint = get_option('erp_api_endpoint');
        $api_key = get_option('erp_api_key');
        
        if (empty($erp_endpoint) || empty($api_key)) {
            return null;
        }
        
        $response = wp_remote_get($erp_endpoint . '/inventory', [
            'timeout' => 15,
            'headers' => [
                'Authorization' => 'Bearer ' . $api_key,
                'Content-Type' => 'application/json'
            ],
            'body' => [
                'sku' => $sku,
                'location_id' => get_option('default_warehouse_id', 1)
            ]
        ]);
        
        if (is_wp_error($response)) {
            error_log('ERP API错误: ' . $response->get_error_message());
            return null;
        }
        
        $data = json_decode(wp_remote_retrieve_body($response), true);
        
        if (isset($data['success']) && $data['success']) {
            return [
                'quantity' => $data['data']['available_quantity'],
                'price' => $data['data']['price'] ?? null,
                'reserved' => $data['data']['reserved_quantity'] ?? 0
            ];
        }
        
        return null;
    }
}

3.2 高级队列管理与监控

// 队列监控和管理类
class InventoryQueueMonitor {
    private $db;
    
    public function __construct() {
        global $wpdb;
        $this->db = $wpdb;
        $this->create_queue_tables();
    }
    
    // 创建队列监控表
    private function create_queue_tables() {
        $charset_collate = $this->db->get_charset_collate();
        $table_name = $this->db->prefix . 'inventory_queue_logs';
        
        $sql = "CREATE TABLE IF NOT EXISTS $table_name (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            action_id varchar(100) NOT NULL,
            product_count int(11) NOT NULL,
            status varchar(20) NOT NULL,
            priority varchar(20) DEFAULT 'normal',
            scheduled_at datetime DEFAULT NULL,
            started_at datetime DEFAULT NULL,
            completed_at datetime DEFAULT NULL,
            success_count int(11) DEFAULT 0,
            failed_count int(11) DEFAULT 0,
            error_message text,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY status (status),
            KEY scheduled_at (scheduled_at)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
    // 监控队列状态
    public function get_queue_status() {
        $stats = [
            'pending' => 0,
            'processing' => 0,
            'completed' => 0,
            'failed' => 0,
            'total' => 0
        ];
        
        // 检查Action Scheduler队列
        $pending_actions = $this->db->get_var(
            "SELECT COUNT(*) FROM {$this->db->prefix}actionscheduler_actions 
             WHERE hook = 'process_inventory_sync_batch' 
             AND status IN ('pending', 'in-progress')"
        );
        
        $stats['pending'] = intval($pending_actions);
        
        // 从自定义日志表获取统计
        $log_stats = $this->db->get_results(
            "SELECT status, COUNT(*) as count 
             FROM {$this->db->prefix}inventory_queue_logs 
             WHERE DATE(created_at) = CURDATE() 
             GROUP BY status",
            ARRAY_A
        );
        
        foreach ($log_stats as $stat) {
            $stats[$stat['status']] = intval($stat['count']);
            $stats['total'] += intval($stat['count']);
        }
        
        return $stats;
    }
    
    // 重试失败的任务
    public function retry_failed_tasks($hours_ago = 24) {
        $failed_tasks = $this->db->get_results(
            $this->db->prepare(
                "SELECT * FROM {$this->db->prefix}inventory_queue_logs 
                 WHERE status = 'failed' 
                 AND created_at >= DATE_SUB(NOW(), INTERVAL %d HOUR)
                 ORDER BY created_at DESC",
                $hours_ago
            ),
            ARRAY_A
        );
        
        $retry_count = 0;
        $queue_manager = new InventoryQueueManager();
        
        foreach ($failed_tasks as $task) {
            // 解析原始产品ID
            $original_args = maybe_unserialize($task['action_data']);
            if (isset($original_args['product_ids'])) {
                // 重新加入队列,提高优先级
                $queue_manager->enqueue_inventory_sync(
                    $original_args['product_ids'],
                    'high'
                );
                
                // 更新原任务状态
                $this->db->update(
                    $this->db->prefix . 'inventory_queue_logs',
                    ['status' => 'retrying'],
                    ['id' => $task['id']]
                );
                
                $retry_count++;
            }
        }
        
        return $retry_count;
    }
    
    // 生成队列报告
    public function generate_queue_report($period = 'daily') {
        $report = [
            'period' => $period,
            'generated_at' => current_time('mysql'),
            'summary' => [],
            'performance' => [],
            'issues' => []
        ];
        
        switch ($period) {
            case 'daily':
                $date_condition = "DATE(created_at) = CURDATE()";
                break;
            case 'weekly':
                $date_condition = "YEARWEEK(created_at) = YEARWEEK(CURDATE())";
                break;
            case 'monthly':
                $date_condition = "MONTH(created_at) = MONTH(CURDATE()) 
                                   AND YEAR(created_at) = YEAR(CURDATE())";
                break;
            default:
                $date_condition = "DATE(created_at) = CURDATE()";
        }
        
        // 获取汇总统计
        $summary = $this->db->get_row(
            "SELECT 
                COUNT(*) as total_tasks,
                SUM(success_count) as total_success,
                SUM(failed_count) as total_failed,
                AVG(TIMESTAMPDIFF(SECOND, started_at, completed_at)) as avg_duration
             FROM {$this->db->prefix}inventory_queue_logs 
             WHERE $date_condition",
            ARRAY_A
        );
        
        $report['summary'] = $summary;
        
        // 获取性能数据
        $performance = $this->db->get_results(
            "SELECT 
                HOUR(started_at) as hour,
                COUNT(*) as tasks_completed,
                AVG(TIMESTAMPDIFF(SECOND, started_at, completed_at)) as avg_duration,
                (SUM(failed_count) / (SUM(success_count) + SUM(failed_count))) * 100 as failure_rate
             FROM {$this->db->prefix}inventory_queue_logs 
             WHERE $date_condition AND completed_at IS NOT NULL
             GROUP BY HOUR(started_at)
             ORDER BY hour",
            ARRAY_A
        );
        
        $report['performance'] = $performance;
        
        // 识别常见问题
        $common_errors = $this->db->get_results(
            "SELECT 
                error_message,
                COUNT(*) as error_count
             FROM {$this->db->prefix}inventory_queue_logs 
             WHERE $date_condition 
             AND status = 'failed'
             AND error_message IS NOT NULL
             AND error_message != ''
             GROUP BY error_message
             ORDER BY error_count DESC
             LIMIT 10",
            ARRAY_A
        );
        
        $report['issues'] = $common_errors;
        
        return $report;
    }
}

3.3 队列系统管理界面

// 队列管理页面
add_action('admin_menu', 'inventory_queue_admin_menu');
function inventory_queue_admin_menu() {
    add_submenu_page(
        'inventory-webhook',
        '队列管理系统',
        '队列管理',
        'manage_options',
        'inventory-queue',
        'queue_management_page'
    );
}

function queue_management_page() {
    $queue_monitor = new InventoryQueueMonitor();
    $queue_stats = $queue_monitor->get_queue_status();
    
    // 处理操作请求
    if (isset($_POST['action'])) {
        check_admin_referer('queue_management');
        
        switch ($_POST['action']) {
            case 'manual_sync':
                $product_ids = isset($_POST['product_ids']) ? 
                    array_map('intval', explode(',', $_POST['product_ids'])) : 
                    get_all_product_ids();
                
                $queue_manager = new InventoryQueueManager();
                $result = $queue_manager->enqueue_inventory_sync($product_ids, 'high');
                
                add_settings_error(
                    'queue_messages',
                    'manual_sync_success',
                    sprintf('已添加 %d 个产品到同步队列', $result),
                    'success'
                );
                break;
                
            case 'retry_failed':
                $retry_count = $queue_monitor->retry_failed_tasks();
                
                add_settings_error(
                    'queue_messages',
                    'retry_success',
                    sprintf('已重新排队 %d 个失败任务', $retry_count),
                    'success'
                );
                break;
                
            case 'clear_queue':
                $this->clear_pending_actions();
                
                add_settings_error(
                    'queue_messages',
                    'clear_success',
                    '已清空待处理队列',
                    'success'
                );
                break;
        }
    }
    
    // 获取队列报告
    $daily_report = $queue_monitor->generate_queue_report('daily');
    ?>
    
    <div class="wrap">
        <h1>库存同步队列管理</h1>
        
        <?php settings_errors('queue_messages'); ?>
        
        <div class="queue-stats-container">
            <h2>队列状态概览</h2>
            <div class="stats-grid">
                <div class="stat-card pending">
                    <h3>待处理</h3>
                    <div class="stat-value"><?php echo $queue_stats['pending']; ?></div>
                </div>
                <div class="stat-card processing">
                    <h3>处理中</h3>
                    <div class="stat-value"><?php echo $queue_stats['processing']; ?></div>
                </div>
                <div class="stat-card completed">
                    <h3>已完成</h3>
                    <div class="stat-value"><?php echo $queue_stats['completed']; ?></div>
                </div>
                <div class="stat-card failed">
                    <h3>失败</h3>
                    <div class="stat-value"><?php echo $queue_stats['failed']; ?></div>
                </div>
            </div>
        </div>
        
        <div class="queue-actions">
            <h2>队列操作</h2>
            
            <form method="post" style="margin-bottom: 20px;">
                <?php wp_nonce_field('queue_management'); ?>
                <input type="hidden" name="action" value="manual_sync">
                
                <h3>手动同步</h3>
                <p>
                    <label for="product_ids">产品ID(逗号分隔,留空同步所有产品):</label><br>
                    <input type="text" id="product_ids" name="product_ids" 
                           style="width: 100%; max-width: 500px;" 
                           placeholder="例如: 123,456,789">
                </p>
                <p>
                    <button type="submit" class="button button-primary">
                        添加到同步队列
                    </button>
                </p>
            </form>
            
            <form method="post" style="margin-bottom: 20px;">
                <?php wp_nonce_field('queue_management'); ?>
                <input type="hidden" name="action" value="retry_failed">
                
本文来自网络,不代表柔性供应链服务中心立场,转载请注明出处:https://mall.org.cn/208.html

EXCHANGES®作者

上一篇
下一篇

为您推荐

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@exchanges.center

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部