文章目录
-
- 在当今电子商务和内容管理系统中,供应商管理是企业运营的重要环节。WordPress作为全球最流行的内容管理系统,通过定制开发供应商评估与管理模块,可以帮助企业高效管理供应商信息、评估供应商绩效并优化供应链管理。 核心需求分析: 供应商信息管理(基本信息、联系方式、产品类别) 评估指标自定义(质量、交货时间、价格、服务等) 评估记录与评分系统 供应商分类与筛选功能 数据可视化与报表生成 本教程将指导您从零开始开发一个完整的柔性供应商评估与管理模块,代码将包含详细注释,便于理解和二次开发。
- 首先,我们需要设计数据库表结构来存储供应商信息和评估数据。 <?php /** * 供应商评估与管理模块 - 数据库表创建 */ global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // 供应商基本信息表 $suppliers_table = $wpdb->prefix . 'supplier_management'; $suppliers_sql = "CREATE TABLE IF NOT EXISTS $suppliers_table ( supplier_id INT(11) NOT NULL AUTO_INCREMENT, supplier_name VARCHAR(255) NOT NULL, contact_person VARCHAR(100), email VARCHAR(100), phone VARCHAR(50), address TEXT, product_categories VARCHAR(255), registration_date DATETIME DEFAULT CURRENT_TIMESTAMP, status ENUM('active', 'inactive', 'pending') DEFAULT 'active', PRIMARY KEY (supplier_id) ) $charset_collate;"; // 评估指标表 $criteria_table = $wpdb->prefix . 'supplier_criteria'; $criteria_sql = "CREATE TABLE IF NOT EXISTS $criteria_table ( criteria_id INT(11) NOT NULL AUTO_INCREMENT, criteria_name VARCHAR(255) NOT NULL, description TEXT, weight DECIMAL(5,2) DEFAULT 1.00, max_score INT(11) DEFAULT 10, is_active TINYINT(1) DEFAULT 1, PRIMARY KEY (criteria_id) ) $charset_collate;"; // 评估记录表 $evaluations_table = $wpdb->prefix . 'supplier_evaluations'; $evaluations_sql = "CREATE TABLE IF NOT EXISTS $evaluations_table ( evaluation_id INT(11) NOT NULL AUTO_INCREMENT, supplier_id INT(11) NOT NULL, evaluator_id INT(11) NOT NULL, evaluation_date DATETIME DEFAULT CURRENT_TIMESTAMP, total_score DECIMAL(5,2), comments TEXT, PRIMARY KEY (evaluation_id), FOREIGN KEY (supplier_id) REFERENCES $suppliers_table(supplier_id) ON DELETE CASCADE ) $charset_collate;"; // 评估详情表 $evaluation_details_table = $wpdb->prefix . 'evaluation_details'; $evaluation_details_sql = "CREATE TABLE IF NOT EXISTS $evaluation_details_table ( detail_id INT(11) NOT NULL AUTO_INCREMENT, evaluation_id INT(11) NOT NULL, criteria_id INT(11) NOT NULL, score DECIMAL(5,2) NOT NULL, notes TEXT, PRIMARY KEY (detail_id), FOREIGN KEY (evaluation_id) REFERENCES $evaluations_table(evaluation_id) ON DELETE CASCADE, FOREIGN KEY (criteria_id) REFERENCES $criteria_table(criteria_id) ON DELETE CASCADE ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($suppliers_sql); dbDelta($criteria_sql); dbDelta($evaluations_sql); dbDelta($evaluation_details_sql); ?>
- 接下来,我们创建供应商管理的核心类,封装所有业务逻辑。 <?php /** * 供应商管理核心类 */ class Supplier_Management_System { private $db; public function __construct() { global $wpdb; $this->db = $wpdb; } /** * 添加新供应商 * @param array $data 供应商数据 * @return int|false 插入ID或false */ public function add_supplier($data) { $defaults = array( 'registration_date' => current_time('mysql'), 'status' => 'active' ); $data = wp_parse_args($data, $defaults); // 数据验证 if (empty($data['supplier_name'])) { return new WP_Error('empty_name', '供应商名称不能为空'); } if (!is_email($data['email'])) { return new WP_Error('invalid_email', '邮箱格式不正确'); } // 插入数据 $result = $this->db->insert( $this->db->prefix . 'supplier_management', $data, array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ); return $result ? $this->db->insert_id : false; } /** * 获取供应商列表 * @param array $args 查询参数 * @return array 供应商列表 */ public function get_suppliers($args = array()) { $defaults = array( 'status' => 'active', 'per_page' => 20, 'page' => 1, 'orderby' => 'registration_date', 'order' => 'DESC' ); $args = wp_parse_args($args, $defaults); $table_name = $this->db->prefix . 'supplier_management'; $offset = ($args['page'] - 1) * $args['per_page']; $sql = "SELECT * FROM $table_name WHERE 1=1"; // 添加状态筛选 if ($args['status']) { $sql .= $this->db->prepare(" AND status = %s", $args['status']); } // 添加搜索功能 if (!empty($args['search'])) { $search_term = '%' . $this->db->esc_like($args['search']) . '%'; $sql .= $this->db->prepare( " AND (supplier_name LIKE %s OR contact_person LIKE %s OR email LIKE %s)", $search_term, $search_term, $search_term ); } // 添加排序 $sql .= " ORDER BY {$args['orderby']} {$args['order']}"; // 添加分页 $sql .= $this->db->prepare(" LIMIT %d OFFSET %d", $args['per_page'], $offset); return $this->db->get_results($sql); } /** * 评估供应商 * @param int $supplier_id 供应商ID * @param int $evaluator_id 评估者ID * @param array $scores 评分数组 * @param string $comments 评语 * @return int|false 评估ID或false */ public function evaluate_supplier($supplier_id, $evaluator_id, $scores, $comments = '') { // 开始事务 $this->db->query('START TRANSACTION'); try { // 计算总分 $total_score = 0; $total_weight = 0; foreach ($scores as $criteria_id => $score_data) { // 获取指标权重 $criteria = $this->db->get_row($this->db->prepare( "SELECT weight, max_score FROM {$this->db->prefix}supplier_criteria WHERE criteria_id = %d", $criteria_id )); if ($criteria) { $normalized_score = ($score_data['score'] / $criteria->max_score) * 10; $weighted_score = $normalized_score * $criteria->weight; $total_score += $weighted_score; $total_weight += $criteria->weight; } } $final_score = $total_weight > 0 ? $total_score / $total_weight : 0; // 插入评估记录 $evaluation_data = array( 'supplier_id' => $supplier_id, 'evaluator_id' => $evaluator_id, 'total_score' => round($final_score, 2), 'comments' => $comments ); $this->db->insert( $this->db->prefix . 'supplier_evaluations', $evaluation_data, array('%d', '%d', '%f', '%s') ); $evaluation_id = $this->db->insert_id; // 插入评估详情 foreach ($scores as $criteria_id => $score_data) { $detail_data = array( 'evaluation_id' => $evaluation_id, 'criteria_id' => $criteria_id, 'score' => $score_data['score'], 'notes' => isset($score_data['notes']) ? $score_data['notes'] : '' ); $this->db->insert( $this->db->prefix . 'evaluation_details', $detail_data, array('%d', '%d', '%f', '%s') ); } // 提交事务 $this->db->query('COMMIT'); return $evaluation_id; } catch (Exception $e) { // 回滚事务 $this->db->query('ROLLBACK'); error_log('供应商评估失败: ' . $e->getMessage()); return false; } } /** * 获取供应商评估统计 * @param int $supplier_id 供应商ID * @return array 统计信息 */ public function get_supplier_statistics($supplier_id) { $stats = array(); // 获取平均分 $avg_score = $this->db->get_var($this->db->prepare( "SELECT AVG(total_score) FROM {$this->db->prefix}supplier_evaluations WHERE supplier_id = %d", $supplier_id )); $stats['average_score'] = round($avg_score ?: 0, 2); // 获取评估次数 $stats['evaluation_count'] = $this->db->get_var($this->db->prepare( "SELECT COUNT(*) FROM {$this->db->prefix}supplier_evaluations WHERE supplier_id = %d", $supplier_id )); // 获取各指标平均分 $criteria_scores = $this->db->get_results($this->db->prepare( "SELECT c.criteria_name, AVG(ed.score) as avg_score FROM {$this->db->prefix}evaluation_details ed JOIN {$this->db->prefix}supplier_criteria c ON ed.criteria_id = c.criteria_id JOIN {$this->db->prefix}supplier_evaluations e ON ed.evaluation_id = e.evaluation_id WHERE e.supplier_id = %d GROUP BY c.criteria_id", $supplier_id )); $stats['criteria_scores'] = $criteria_scores; return $stats; } } ?>
- 创建供应商管理的WordPress后台界面。 <?php /** * 供应商管理后台界面 */ class Supplier_Management_Admin { private $supplier_system; public function __construct() { $this->supplier_system = new Supplier_Management_System(); // 添加管理菜单 add_action('admin_menu', array($this, 'add_admin_menu')); // 注册样式和脚本 add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets')); } /** * 添加管理菜单 */ public function add_admin_menu() { add_menu_page( '供应商管理', '供应商管理', 'manage_options', 'supplier-management', array($this, 'render_main_page'), 'dashicons-businessperson', 30 ); add_submenu_page( 'supplier-management', '供应商列表', '供应商列表', 'manage_options', 'supplier-management', array($this, 'render_main_page') ); add_submenu_page( 'supplier-management', '添加供应商', '添加供应商', 'manage_options', 'supplier-add', array($this, 'render_add_page') ); add_submenu_page( 'supplier-management', '评估指标', '评估指标', 'manage_options', 'supplier-criteria', array($this, 'render_criteria_page') ); add_submenu_page( 'supplier-management', '评估报表', '评估报表', 'manage_options', 'supplier-reports', array($this, 'render_reports_page') ); } /** * 加载管理界面资源 */ public function enqueue_admin_assets($hook) { if (strpos($hook, 'supplier-') === false) { return; } // 加载Chart.js用于数据可视化 wp_enqueue_script( 'chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', array(), '3.7.0', true ); // 加载自定义样式 wp_enqueue_style( 'supplier-admin-style', plugin_dir_url(__FILE__) . 'css/supplier-admin.css', array(), '1.0.0' ); // 加载自定义脚本 wp_enqueue_script( 'supplier-admin-script', plugin_dir_url(__FILE__) . 'js/supplier-admin.js', array('jquery', 'chart-js'), '1.0.0', true ); } /** * 渲染主页面 */ public function render_main_page() { // 处理删除操作 if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['supplier_id'])) { $this->delete_supplier(intval($_GET['supplier_id'])); } // 获取供应商列表 $args = array(); if (isset($_GET['status'])) { $args['status'] = sanitize_text_field($_GET['status']); } if (isset($_GET['s'])) { $args['search'] = sanitize_text_field($_GET['s']); } $suppliers = $this->supplier_system->get_suppliers($args); ?> <div class="wrap"> <h1 class="wp-heading-inline">供应商管理</h1> <a href="<?php echo admin_url('admin.php?page=supplier-add'); ?>" class="page-title-action">添加供应商</a> <!-- 搜索和筛选表单 --> <form method="get" action="<?php echo admin_url('admin.php'); ?>"> <input type="hidden" name="page" value="supplier-management"> <div class="tablenav top"> <div class="alignleft actions"> <select name="status"> <option value="">所有状态</option> <option value="active" <?php selected(isset($_GET['status']) && $_GET['status'] == 'active'); ?>>活跃</option> <option value="inactive" <?php selected(isset($_GET['status']) && $_GET['status'] == 'inactive'); ?>>停用</option> </select> <input type="search" name="s" value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?>" placeholder="搜索供应商..."> <input type="submit" class="button" value="筛选"> </div> </div> </form> <!-- 供应商表格 --> <table class="wp-list-table widefat fixed striped"> <thead> <tr> <th>ID</th> <th>供应商名称</th> <th>联系人</th> <th>邮箱</th> <th>产品类别</th> <th>注册日期</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody> <?php if (empty($suppliers)): ?> <tr> <td colspan="8">暂无供应商数据</td> </tr> <?php else: ?> <?php foreach ($suppliers as $supplier): ?> <tr> <td><?php echo $supplier->supplier_id; ?></td> <td> <strong><?php echo esc_html($supplier->supplier_name); ?></strong> <div class="row-actions"> <span class="edit"> <a href="<?php echo admin_url('admin.php?page=supplier-add&supplier_id=' . $supplier->supplier_id); ?>">编辑</a> | </span> <span class="evaluate"> <a href="<?php echo admin_url('admin.php?page=supplier-evaluate&supplier_id=' . $supplier->supplier_id); ?>">评估</a> | </span> <span class="delete"> <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=supplier-management&action=delete&supplier_id=' . $supplier->supplier_id), 'delete_supplier'); ?>" onclick="return confirm('确定要删除这个供应商吗?')">删除</a> </span> </div> </td> <td><?php echo esc_html($supplier->contact_person); ?></td> <td><?php echo esc_html($supplier->email); ?></td> <td><?php echo esc_html($supplier->product_categories); ?></td> <?php echo date('Y-m-d', strtotime($supplier->registration_date)); ?></td> <td> <span class="status-badge status-<?php echo $supplier->status; ?>"> <?php $status_labels = array( 'active' => '活跃', 'inactive' => '停用', 'pending' => '待审核' ); echo $status_labels[$supplier->status] ?? $supplier->status; ?> </span> </td> <td> <a href="<?php echo admin_url('admin.php?page=supplier-reports&supplier_id=' . $supplier->supplier_id); ?>" class="button button-small">查看报表</a> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> <?php } /** * 渲染添加/编辑供应商页面 */ public function render_add_page() { $supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0; $supplier = null; // 如果是编辑模式,获取供应商数据 if ($supplier_id > 0) { global $wpdb; $supplier = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}supplier_management WHERE supplier_id = %d", $supplier_id )); } // 处理表单提交 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_supplier'])) { $this->save_supplier($_POST, $supplier_id); } ?> <div class="wrap"> <h1><?php echo $supplier_id ? '编辑供应商' : '添加新供应商'; ?></h1> <form method="post" action=""> <?php wp_nonce_field('save_supplier', 'supplier_nonce'); ?> <table class="form-table"> <tr> <th scope="row"><label for="supplier_name">供应商名称 *</label></th> <td> <input type="text" id="supplier_name" name="supplier_name" value="<?php echo $supplier ? esc_attr($supplier->supplier_name) : ''; ?>" class="regular-text" required> </td> </tr> <tr> <th scope="row"><label for="contact_person">联系人</label></th> <td> <input type="text" id="contact_person" name="contact_person" value="<?php echo $supplier ? esc_attr($supplier->contact_person) : ''; ?>" class="regular-text"> </td> </tr> <tr> <th scope="row"><label for="email">邮箱 *</label></th> <td> <input type="email" id="email" name="email" value="<?php echo $supplier ? esc_attr($supplier->email) : ''; ?>" class="regular-text" required> </td> </tr> <tr> <th scope="row"><label for="phone">电话</label></th> <td> <input type="tel" id="phone" name="phone" value="<?php echo $supplier ? esc_attr($supplier->phone) : ''; ?>" class="regular-text"> </td> </tr> <tr> <th scope="row"><label for="address">地址</label></th> <td> <textarea id="address" name="address" rows="3" class="large-text"><?php echo $supplier ? esc_textarea($supplier->address) : ''; ?></textarea> </td> </tr> <tr> <th scope="row"><label for="product_categories">产品类别</label></th> <td> <input type="text" id="product_categories" name="product_categories" value="<?php echo $supplier ? esc_attr($supplier->product_categories) : ''; ?>" class="regular-text"> <p class="description">多个类别用逗号分隔</p> </td> </tr> <tr> <th scope="row"><label for="status">状态</label></th> <td> <select id="status" name="status"> <option value="active" <?php selected($supplier && $supplier->status == 'active'); ?>>活跃</option> <option value="inactive" <?php selected($supplier && $supplier->status == 'inactive'); ?>>停用</option> <option value="pending" <?php selected($supplier && $supplier->status == 'pending'); ?>>待审核</option> </select> </td> </tr> </table> <p class="submit"> <input type="submit" name="submit_supplier" class="button button-primary" value="<?php echo $supplier_id ? '更新供应商' : '添加供应商'; ?>"> <a href="<?php echo admin_url('admin.php?page=supplier-management'); ?>" class="button">取消</a> </p> </form> </div> <?php } /** * 保存供应商信息 */ private function save_supplier($data, $supplier_id = 0) { if (!wp_verify_nonce($data['supplier_nonce'], 'save_supplier')) { wp_die('安全验证失败'); } $supplier_data = array( 'supplier_name' => sanitize_text_field($data['supplier_name']), 'contact_person' => sanitize_text_field($data['contact_person']), 'email' => sanitize_email($data['email']), 'phone' => sanitize_text_field($data['phone']), 'address' => sanitize_textarea_field($data['address']), 'product_categories' => sanitize_text_field($data['product_categories']), 'status' => sanitize_text_field($data['status']) ); global $wpdb; $table_name = $wpdb->prefix . 'supplier_management'; if ($supplier_id > 0) { // 更新现有供应商 $result = $wpdb->update( $table_name, $supplier_data, array('supplier_id' => $supplier_id), array('%s', '%s', '%s', '%s', '%s', '%s', '%s'), array('%d') ); if ($result !== false) { add_settings_error('supplier_messages', 'supplier_updated', '供应商信息已更新', 'success'); } } else { // 添加新供应商 $supplier_data['registration_date'] = current_time('mysql'); $result = $wpdb->insert( $table_name, $supplier_data, array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ); if ($result) { add_settings_error('supplier_messages', 'supplier_added', '供应商添加成功', 'success'); } } settings_errors('supplier_messages'); } /** * 渲染评估指标页面 */ public function render_criteria_page() { global $wpdb; // 处理指标操作 if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_criteria'])) { $this->add_criteria($_POST); } elseif (isset($_POST['update_criteria'])) { $this->update_criteria($_POST); } } // 处理删除操作 if (isset($_GET['action']) && $_GET['action'] == 'delete_criteria') { $this->delete_criteria(intval($_GET['criteria_id'])); } // 获取所有评估指标 $criteria = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}supplier_criteria ORDER BY criteria_id DESC"); ?> <div class="wrap"> <h1>评估指标管理</h1> <div class="criteria-management"> <!-- 添加新指标表单 --> <div class="postbox"> <h2 class="hndle"><?php echo isset($_GET['edit']) ? '编辑指标' : '添加新指标'; ?></h2> <div class="inside"> <form method="post" action=""> <?php wp_nonce_field('manage_criteria', 'criteria_nonce'); ?> <?php if (isset($_GET['edit'])): $edit_id = intval($_GET['edit']); $edit_criteria = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}supplier_criteria WHERE criteria_id = %d", $edit_id )); ?> <input type="hidden" name="criteria_id" value="<?php echo $edit_id; ?>"> <?php endif; ?> <table class="form-table"> <tr> <th scope="row"><label for="criteria_name">指标名称 *</label></th> <td> <input type="text" id="criteria_name" name="criteria_name" value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->criteria_name) : ''; ?>" class="regular-text" required> </td> </tr> <tr> <th scope="row"><label for="description">描述</label></th> <td> <textarea id="description" name="description" rows="3" class="large-text"><?php echo isset($edit_criteria) ? esc_textarea($edit_criteria->description) : ''; ?></textarea> </td> </tr> <tr> <th scope="row"><label for="weight">权重</label></th> <td> <input type="number" id="weight" name="weight" value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->weight) : '1.00'; ?>" step="0.01" min="0.1" max="5" class="small-text"> <p class="description">权重越高,该指标在总分中的占比越大(范围:0.1-5.0)</p> </td> </tr> <tr> <th scope="row"><label for="max_score">最高分</label></th> <td> <input type="number" id="max_score" name="max_score" value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->max_score) : '10'; ?>" min="1" max="100" class="small-text"> </td> </tr> <tr> <th scope="row"><label for="is_active">状态</label></th> <td> <label> <input type="checkbox" id="is_active" name="is_active" value="1" <?php checked(!isset($edit_criteria) || $edit_criteria->is_active == 1); ?>> 启用该指标 </label> </td> </tr> </table> <p class="submit"> <?php if (isset($_GET['edit'])): ?> <input type="submit" name="update_criteria" class="button button-primary" value="更新指标"> <a href="<?php echo remove_query_arg('edit'); ?>" class="button">取消</a> <?php else: ?> <input type="submit" name="add_criteria" class="button button-primary" value="添加指标"> <?php endif; ?> </p> </form> </div> </div> <!-- 指标列表 --> <div class="postbox"> <h2 class="hndle">评估指标列表</h2> <div class="inside"> <?php if (empty($criteria)): ?> <p>暂无评估指标,请先添加指标。</p> <?php else: ?> <table class="wp-list-table widefat fixed striped"> <thead> <tr> <th>ID</th> <th>指标名称</th> <th>描述</th> <th>权重</th> <th>最高分</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($criteria as $item): ?> <tr> <td><?php echo $item->criteria_id; ?></td> <td><strong><?php echo esc_html($item->criteria_name); ?></strong></td> <td><?php echo esc_html($item->description); ?></td> <td><?php echo $item->weight; ?></td> <td><?php echo $item->max_score; ?></td> <td> <span class="status-badge status-<?php echo $item->is_active ? 'active' : 'inactive'; ?>"> <?php echo $item->is_active ? '启用' : '停用'; ?> </span> </td> <td> <a href="<?php echo add_query_arg('edit', $item->criteria_id); ?>" class="button button-small">编辑</a> <a href="<?php echo wp_nonce_url( add_query_arg(array( 'action' => 'delete_criteria', 'criteria_id' => $item->criteria_id )), 'delete_criteria' ); ?>" class="button button-small button-link-delete" onclick="return confirm('确定要删除这个指标吗?')">删除</a> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </div> </div> </div> <?php } /** * 渲染报表页面 */ public function render_reports_page() { global $wpdb; $supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0; // 获取供应商统计信息 $stats = $this->supplier_system->get_supplier_statistics($supplier_id); // 获取所有供应商用于下拉选择 $suppliers = $wpdb->get_results("SELECT supplier_id, supplier_name FROM {$wpdb->prefix}supplier_management WHERE status = 'active' ORDER BY supplier_name"); ?> <div class="wrap"> <h1>供应商评估报表</h1> <!-- 供应商选择器 --> <div class="supplier-selector"> <form method="get" action="<?php echo admin_url('admin.php'); ?>"> <input type="hidden" name="page" value="supplier-reports"> <label for="supplier_id">选择供应商:</label> <select id="supplier_id" name="supplier_id" onchange="this.form.submit()"> <option value="">-- 请选择供应商 --</option> <?php foreach ($suppliers as $supplier): ?> <option value="<?php echo $supplier->supplier_id; ?>" <?php selected($supplier_id, $supplier->supplier_id); ?>> <?php echo esc_html($supplier->supplier_name); ?> </option> <?php endforeach; ?> </select> </form> </div> <?php if ($supplier_id > 0): ?> <?php $supplier_info = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}supplier_management WHERE supplier_id = %d", $supplier_id )); ?> <div class="report-header"> <h2><?php echo esc_html($supplier_info->supplier_name); ?> - 评估报表</h2> <p>联系人:<?php echo esc_html($supplier_info->contact_person); ?> | 邮箱:<?php echo esc_html($supplier_info->email); ?> | 注册日期:<?php echo date('Y-m-d', strtotime($supplier_info->registration_date)); ?></p> </div> <!-- 关键指标卡片 --> <div class="stats-cards"> <div class="card"> <h3>平均评分</h3> <div class="score"><?php echo $stats['average_score']; ?>/10</div> </div> <div class="card"> <h3>评估次数</h3> <div class="count"><?php echo $stats['evaluation_count']; ?> 次</div> </div> <div class="card"> <h3>当前状态</h3> <div class="status"> <span class="status-badge status-<?php echo $supplier_info->status; ?>"> <?php $status_labels = array( 'active' => '活跃', 'inactive' => '停用', 'pending' => '待审核' ); echo $status_labels[$supplier_info->status] ?? $supplier_info->status; ?> </span> </div> </div> </div> <!-- 各指标评分图表 --> <div class="chart-container"> <h3>各指标评分分析</h3> <canvas id="criteriaChart" width="400" height="200"></canvas> </div> <!-- 详细评估记录 --> <div class="evaluation-history"> <h
在当今电子商务和内容管理系统中,供应商管理是企业运营的重要环节。WordPress作为全球最流行的内容管理系统,通过定制开发供应商评估与管理模块,可以帮助企业高效管理供应商信息、评估供应商绩效并优化供应链管理。
核心需求分析:
- 供应商信息管理(基本信息、联系方式、产品类别)
- 评估指标自定义(质量、交货时间、价格、服务等)
- 评估记录与评分系统
- 供应商分类与筛选功能
- 数据可视化与报表生成
本教程将指导您从零开始开发一个完整的柔性供应商评估与管理模块,代码将包含详细注释,便于理解和二次开发。
首先,我们需要设计数据库表结构来存储供应商信息和评估数据。
<?php
/**
* 供应商评估与管理模块 - 数据库表创建
*/
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 供应商基本信息表
$suppliers_table = $wpdb->prefix . 'supplier_management';
$suppliers_sql = "CREATE TABLE IF NOT EXISTS $suppliers_table (
supplier_id INT(11) NOT NULL AUTO_INCREMENT,
supplier_name VARCHAR(255) NOT NULL,
contact_person VARCHAR(100),
email VARCHAR(100),
phone VARCHAR(50),
address TEXT,
product_categories VARCHAR(255),
registration_date DATETIME DEFAULT CURRENT_TIMESTAMP,
status ENUM('active', 'inactive', 'pending') DEFAULT 'active',
PRIMARY KEY (supplier_id)
) $charset_collate;";
// 评估指标表
$criteria_table = $wpdb->prefix . 'supplier_criteria';
$criteria_sql = "CREATE TABLE IF NOT EXISTS $criteria_table (
criteria_id INT(11) NOT NULL AUTO_INCREMENT,
criteria_name VARCHAR(255) NOT NULL,
description TEXT,
weight DECIMAL(5,2) DEFAULT 1.00,
max_score INT(11) DEFAULT 10,
is_active TINYINT(1) DEFAULT 1,
PRIMARY KEY (criteria_id)
) $charset_collate;";
// 评估记录表
$evaluations_table = $wpdb->prefix . 'supplier_evaluations';
$evaluations_sql = "CREATE TABLE IF NOT EXISTS $evaluations_table (
evaluation_id INT(11) NOT NULL AUTO_INCREMENT,
supplier_id INT(11) NOT NULL,
evaluator_id INT(11) NOT NULL,
evaluation_date DATETIME DEFAULT CURRENT_TIMESTAMP,
total_score DECIMAL(5,2),
comments TEXT,
PRIMARY KEY (evaluation_id),
FOREIGN KEY (supplier_id) REFERENCES $suppliers_table(supplier_id) ON DELETE CASCADE
) $charset_collate;";
// 评估详情表
$evaluation_details_table = $wpdb->prefix . 'evaluation_details';
$evaluation_details_sql = "CREATE TABLE IF NOT EXISTS $evaluation_details_table (
detail_id INT(11) NOT NULL AUTO_INCREMENT,
evaluation_id INT(11) NOT NULL,
criteria_id INT(11) NOT NULL,
score DECIMAL(5,2) NOT NULL,
notes TEXT,
PRIMARY KEY (detail_id),
FOREIGN KEY (evaluation_id) REFERENCES $evaluations_table(evaluation_id) ON DELETE CASCADE,
FOREIGN KEY (criteria_id) REFERENCES $criteria_table(criteria_id) ON DELETE CASCADE
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($suppliers_sql);
dbDelta($criteria_sql);
dbDelta($evaluations_sql);
dbDelta($evaluation_details_sql);
?>
接下来,我们创建供应商管理的核心类,封装所有业务逻辑。
<?php
/**
* 供应商管理核心类
*/
class Supplier_Management_System {
private $db;
public function __construct() {
global $wpdb;
$this->db = $wpdb;
}
/**
* 添加新供应商
* @param array $data 供应商数据
* @return int|false 插入ID或false
*/
public function add_supplier($data) {
$defaults = array(
'registration_date' => current_time('mysql'),
'status' => 'active'
);
$data = wp_parse_args($data, $defaults);
// 数据验证
if (empty($data['supplier_name'])) {
return new WP_Error('empty_name', '供应商名称不能为空');
}
if (!is_email($data['email'])) {
return new WP_Error('invalid_email', '邮箱格式不正确');
}
// 插入数据
$result = $this->db->insert(
$this->db->prefix . 'supplier_management',
$data,
array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
return $result ? $this->db->insert_id : false;
}
/**
* 获取供应商列表
* @param array $args 查询参数
* @return array 供应商列表
*/
public function get_suppliers($args = array()) {
$defaults = array(
'status' => 'active',
'per_page' => 20,
'page' => 1,
'orderby' => 'registration_date',
'order' => 'DESC'
);
$args = wp_parse_args($args, $defaults);
$table_name = $this->db->prefix . 'supplier_management';
$offset = ($args['page'] - 1) * $args['per_page'];
$sql = "SELECT * FROM $table_name WHERE 1=1";
// 添加状态筛选
if ($args['status']) {
$sql .= $this->db->prepare(" AND status = %s", $args['status']);
}
// 添加搜索功能
if (!empty($args['search'])) {
$search_term = '%' . $this->db->esc_like($args['search']) . '%';
$sql .= $this->db->prepare(
" AND (supplier_name LIKE %s OR contact_person LIKE %s OR email LIKE %s)",
$search_term, $search_term, $search_term
);
}
// 添加排序
$sql .= " ORDER BY {$args['orderby']} {$args['order']}";
// 添加分页
$sql .= $this->db->prepare(" LIMIT %d OFFSET %d", $args['per_page'], $offset);
return $this->db->get_results($sql);
}
/**
* 评估供应商
* @param int $supplier_id 供应商ID
* @param int $evaluator_id 评估者ID
* @param array $scores 评分数组
* @param string $comments 评语
* @return int|false 评估ID或false
*/
public function evaluate_supplier($supplier_id, $evaluator_id, $scores, $comments = '') {
// 开始事务
$this->db->query('START TRANSACTION');
try {
// 计算总分
$total_score = 0;
$total_weight = 0;
foreach ($scores as $criteria_id => $score_data) {
// 获取指标权重
$criteria = $this->db->get_row($this->db->prepare(
"SELECT weight, max_score FROM {$this->db->prefix}supplier_criteria WHERE criteria_id = %d",
$criteria_id
));
if ($criteria) {
$normalized_score = ($score_data['score'] / $criteria->max_score) * 10;
$weighted_score = $normalized_score * $criteria->weight;
$total_score += $weighted_score;
$total_weight += $criteria->weight;
}
}
$final_score = $total_weight > 0 ? $total_score / $total_weight : 0;
// 插入评估记录
$evaluation_data = array(
'supplier_id' => $supplier_id,
'evaluator_id' => $evaluator_id,
'total_score' => round($final_score, 2),
'comments' => $comments
);
$this->db->insert(
$this->db->prefix . 'supplier_evaluations',
$evaluation_data,
array('%d', '%d', '%f', '%s')
);
$evaluation_id = $this->db->insert_id;
// 插入评估详情
foreach ($scores as $criteria_id => $score_data) {
$detail_data = array(
'evaluation_id' => $evaluation_id,
'criteria_id' => $criteria_id,
'score' => $score_data['score'],
'notes' => isset($score_data['notes']) ? $score_data['notes'] : ''
);
$this->db->insert(
$this->db->prefix . 'evaluation_details',
$detail_data,
array('%d', '%d', '%f', '%s')
);
}
// 提交事务
$this->db->query('COMMIT');
return $evaluation_id;
} catch (Exception $e) {
// 回滚事务
$this->db->query('ROLLBACK');
error_log('供应商评估失败: ' . $e->getMessage());
return false;
}
}
/**
* 获取供应商评估统计
* @param int $supplier_id 供应商ID
* @return array 统计信息
*/
public function get_supplier_statistics($supplier_id) {
$stats = array();
// 获取平均分
$avg_score = $this->db->get_var($this->db->prepare(
"SELECT AVG(total_score) FROM {$this->db->prefix}supplier_evaluations WHERE supplier_id = %d",
$supplier_id
));
$stats['average_score'] = round($avg_score ?: 0, 2);
// 获取评估次数
$stats['evaluation_count'] = $this->db->get_var($this->db->prepare(
"SELECT COUNT(*) FROM {$this->db->prefix}supplier_evaluations WHERE supplier_id = %d",
$supplier_id
));
// 获取各指标平均分
$criteria_scores = $this->db->get_results($this->db->prepare(
"SELECT c.criteria_name, AVG(ed.score) as avg_score
FROM {$this->db->prefix}evaluation_details ed
JOIN {$this->db->prefix}supplier_criteria c ON ed.criteria_id = c.criteria_id
JOIN {$this->db->prefix}supplier_evaluations e ON ed.evaluation_id = e.evaluation_id
WHERE e.supplier_id = %d
GROUP BY c.criteria_id",
$supplier_id
));
$stats['criteria_scores'] = $criteria_scores;
return $stats;
}
}
?>
创建供应商管理的WordPress后台界面。
<?php
/**
* 供应商管理后台界面
*/
class Supplier_Management_Admin {
private $supplier_system;
public function __construct() {
$this->supplier_system = new Supplier_Management_System();
// 添加管理菜单
add_action('admin_menu', array($this, 'add_admin_menu'));
// 注册样式和脚本
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
}
/**
* 添加管理菜单
*/
public function add_admin_menu() {
add_menu_page(
'供应商管理',
'供应商管理',
'manage_options',
'supplier-management',
array($this, 'render_main_page'),
'dashicons-businessperson',
30
);
add_submenu_page(
'supplier-management',
'供应商列表',
'供应商列表',
'manage_options',
'supplier-management',
array($this, 'render_main_page')
);
add_submenu_page(
'supplier-management',
'添加供应商',
'添加供应商',
'manage_options',
'supplier-add',
array($this, 'render_add_page')
);
add_submenu_page(
'supplier-management',
'评估指标',
'评估指标',
'manage_options',
'supplier-criteria',
array($this, 'render_criteria_page')
);
add_submenu_page(
'supplier-management',
'评估报表',
'评估报表',
'manage_options',
'supplier-reports',
array($this, 'render_reports_page')
);
}
/**
* 加载管理界面资源
*/
public function enqueue_admin_assets($hook) {
if (strpos($hook, 'supplier-') === false) {
return;
}
// 加载Chart.js用于数据可视化
wp_enqueue_script(
'chart-js',
'https://cdn.jsdelivr.net/npm/chart.js',
array(),
'3.7.0',
true
);
// 加载自定义样式
wp_enqueue_style(
'supplier-admin-style',
plugin_dir_url(__FILE__) . 'css/supplier-admin.css',
array(),
'1.0.0'
);
// 加载自定义脚本
wp_enqueue_script(
'supplier-admin-script',
plugin_dir_url(__FILE__) . 'js/supplier-admin.js',
array('jquery', 'chart-js'),
'1.0.0',
true
);
}
/**
* 渲染主页面
*/
public function render_main_page() {
// 处理删除操作
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['supplier_id'])) {
$this->delete_supplier(intval($_GET['supplier_id']));
}
// 获取供应商列表
$args = array();
if (isset($_GET['status'])) {
$args['status'] = sanitize_text_field($_GET['status']);
}
if (isset($_GET['s'])) {
$args['search'] = sanitize_text_field($_GET['s']);
}
$suppliers = $this->supplier_system->get_suppliers($args);
?>
<div class="wrap">
<h1 class="wp-heading-inline">供应商管理</h1>
<a href="<?php echo admin_url('admin.php?page=supplier-add'); ?>" class="page-title-action">添加供应商</a>
<!-- 搜索和筛选表单 -->
<form method="get" action="<?php echo admin_url('admin.php'); ?>">
<input type="hidden" name="page" value="supplier-management">
<div class="tablenav top">
<div class="alignleft actions">
<select name="status">
<option value="">所有状态</option>
<option value="active" <?php selected(isset($_GET['status']) && $_GET['status'] == 'active'); ?>>活跃</option>
<option value="inactive" <?php selected(isset($_GET['status']) && $_GET['status'] == 'inactive'); ?>>停用</option>
</select>
<input type="search" name="s" value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?>" placeholder="搜索供应商...">
<input type="submit" class="button" value="筛选">
</div>
</div>
</form>
<!-- 供应商表格 -->
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>ID</th>
<th>供应商名称</th>
<th>联系人</th>
<th>邮箱</th>
<th>产品类别</th>
<th>注册日期</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php if (empty($suppliers)): ?>
<tr>
<td colspan="8">暂无供应商数据</td>
</tr>
<?php else: ?>
<?php foreach ($suppliers as $supplier): ?>
<tr>
<td><?php echo $supplier->supplier_id; ?></td>
<td>
<strong><?php echo esc_html($supplier->supplier_name); ?></strong>
<div class="row-actions">
<span class="edit">
<a href="<?php echo admin_url('admin.php?page=supplier-add&supplier_id=' . $supplier->supplier_id); ?>">编辑</a> |
</span>
<span class="evaluate">
<a href="<?php echo admin_url('admin.php?page=supplier-evaluate&supplier_id=' . $supplier->supplier_id); ?>">评估</a> |
</span>
<span class="delete">
<a href="<?php echo wp_nonce_url(admin_url('admin.php?page=supplier-management&action=delete&supplier_id=' . $supplier->supplier_id), 'delete_supplier'); ?>"
onclick="return confirm('确定要删除这个供应商吗?')">删除</a>
</span>
</div>
</td>
<td><?php echo esc_html($supplier->contact_person); ?></td>
<td><?php echo esc_html($supplier->email); ?></td>
<td><?php echo esc_html($supplier->product_categories); ?></td>
<?php echo date('Y-m-d', strtotime($supplier->registration_date)); ?></td>
<td>
<span class="status-badge status-<?php echo $supplier->status; ?>">
<?php
$status_labels = array(
'active' => '活跃',
'inactive' => '停用',
'pending' => '待审核'
);
echo $status_labels[$supplier->status] ?? $supplier->status;
?>
</span>
</td>
<td>
<a href="<?php echo admin_url('admin.php?page=supplier-reports&supplier_id=' . $supplier->supplier_id); ?>"
class="button button-small">查看报表</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<?php
}
/**
* 渲染添加/编辑供应商页面
*/
public function render_add_page() {
$supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0;
$supplier = null;
// 如果是编辑模式,获取供应商数据
if ($supplier_id > 0) {
global $wpdb;
$supplier = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}supplier_management WHERE supplier_id = %d",
$supplier_id
));
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_supplier'])) {
$this->save_supplier($_POST, $supplier_id);
}
?>
<div class="wrap">
<h1><?php echo $supplier_id ? '编辑供应商' : '添加新供应商'; ?></h1>
<form method="post" action="">
<?php wp_nonce_field('save_supplier', 'supplier_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="supplier_name">供应商名称 *</label></th>
<td>
<input type="text" id="supplier_name" name="supplier_name"
value="<?php echo $supplier ? esc_attr($supplier->supplier_name) : ''; ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row"><label for="contact_person">联系人</label></th>
<td>
<input type="text" id="contact_person" name="contact_person"
value="<?php echo $supplier ? esc_attr($supplier->contact_person) : ''; ?>"
class="regular-text">
</td>
</tr>
<tr>
<th scope="row"><label for="email">邮箱 *</label></th>
<td>
<input type="email" id="email" name="email"
value="<?php echo $supplier ? esc_attr($supplier->email) : ''; ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row"><label for="phone">电话</label></th>
<td>
<input type="tel" id="phone" name="phone"
value="<?php echo $supplier ? esc_attr($supplier->phone) : ''; ?>"
class="regular-text">
</td>
</tr>
<tr>
<th scope="row"><label for="address">地址</label></th>
<td>
<textarea id="address" name="address" rows="3" class="large-text"><?php
echo $supplier ? esc_textarea($supplier->address) : '';
?></textarea>
</td>
</tr>
<tr>
<th scope="row"><label for="product_categories">产品类别</label></th>
<td>
<input type="text" id="product_categories" name="product_categories"
value="<?php echo $supplier ? esc_attr($supplier->product_categories) : ''; ?>"
class="regular-text">
<p class="description">多个类别用逗号分隔</p>
</td>
</tr>
<tr>
<th scope="row"><label for="status">状态</label></th>
<td>
<select id="status" name="status">
<option value="active" <?php selected($supplier && $supplier->status == 'active'); ?>>活跃</option>
<option value="inactive" <?php selected($supplier && $supplier->status == 'inactive'); ?>>停用</option>
<option value="pending" <?php selected($supplier && $supplier->status == 'pending'); ?>>待审核</option>
</select>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="submit_supplier" class="button button-primary"
value="<?php echo $supplier_id ? '更新供应商' : '添加供应商'; ?>">
<a href="<?php echo admin_url('admin.php?page=supplier-management'); ?>" class="button">取消</a>
</p>
</form>
</div>
<?php
}
/**
* 保存供应商信息
*/
private function save_supplier($data, $supplier_id = 0) {
if (!wp_verify_nonce($data['supplier_nonce'], 'save_supplier')) {
wp_die('安全验证失败');
}
$supplier_data = array(
'supplier_name' => sanitize_text_field($data['supplier_name']),
'contact_person' => sanitize_text_field($data['contact_person']),
'email' => sanitize_email($data['email']),
'phone' => sanitize_text_field($data['phone']),
'address' => sanitize_textarea_field($data['address']),
'product_categories' => sanitize_text_field($data['product_categories']),
'status' => sanitize_text_field($data['status'])
);
global $wpdb;
$table_name = $wpdb->prefix . 'supplier_management';
if ($supplier_id > 0) {
// 更新现有供应商
$result = $wpdb->update(
$table_name,
$supplier_data,
array('supplier_id' => $supplier_id),
array('%s', '%s', '%s', '%s', '%s', '%s', '%s'),
array('%d')
);
if ($result !== false) {
add_settings_error('supplier_messages', 'supplier_updated', '供应商信息已更新', 'success');
}
} else {
// 添加新供应商
$supplier_data['registration_date'] = current_time('mysql');
$result = $wpdb->insert(
$table_name,
$supplier_data,
array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
if ($result) {
add_settings_error('supplier_messages', 'supplier_added', '供应商添加成功', 'success');
}
}
settings_errors('supplier_messages');
}
/**
* 渲染评估指标页面
*/
public function render_criteria_page() {
global $wpdb;
// 处理指标操作
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_criteria'])) {
$this->add_criteria($_POST);
} elseif (isset($_POST['update_criteria'])) {
$this->update_criteria($_POST);
}
}
// 处理删除操作
if (isset($_GET['action']) && $_GET['action'] == 'delete_criteria') {
$this->delete_criteria(intval($_GET['criteria_id']));
}
// 获取所有评估指标
$criteria = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}supplier_criteria ORDER BY criteria_id DESC");
?>
<div class="wrap">
<h1>评估指标管理</h1>
<div class="criteria-management">
<!-- 添加新指标表单 -->
<div class="postbox">
<h2 class="hndle"><?php echo isset($_GET['edit']) ? '编辑指标' : '添加新指标'; ?></h2>
<div class="inside">
<form method="post" action="">
<?php wp_nonce_field('manage_criteria', 'criteria_nonce'); ?>
<?php if (isset($_GET['edit'])):
$edit_id = intval($_GET['edit']);
$edit_criteria = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}supplier_criteria WHERE criteria_id = %d",
$edit_id
));
?>
<input type="hidden" name="criteria_id" value="<?php echo $edit_id; ?>">
<?php endif; ?>
<table class="form-table">
<tr>
<th scope="row"><label for="criteria_name">指标名称 *</label></th>
<td>
<input type="text" id="criteria_name" name="criteria_name"
value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->criteria_name) : ''; ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row"><label for="description">描述</label></th>
<td>
<textarea id="description" name="description" rows="3" class="large-text"><?php
echo isset($edit_criteria) ? esc_textarea($edit_criteria->description) : '';
?></textarea>
</td>
</tr>
<tr>
<th scope="row"><label for="weight">权重</label></th>
<td>
<input type="number" id="weight" name="weight"
value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->weight) : '1.00'; ?>"
step="0.01" min="0.1" max="5" class="small-text">
<p class="description">权重越高,该指标在总分中的占比越大(范围:0.1-5.0)</p>
</td>
</tr>
<tr>
<th scope="row"><label for="max_score">最高分</label></th>
<td>
<input type="number" id="max_score" name="max_score"
value="<?php echo isset($edit_criteria) ? esc_attr($edit_criteria->max_score) : '10'; ?>"
min="1" max="100" class="small-text">
</td>
</tr>
<tr>
<th scope="row"><label for="is_active">状态</label></th>
<td>
<label>
<input type="checkbox" id="is_active" name="is_active" value="1"
<?php checked(!isset($edit_criteria) || $edit_criteria->is_active == 1); ?>>
启用该指标
</label>
</td>
</tr>
</table>
<p class="submit">
<?php if (isset($_GET['edit'])): ?>
<input type="submit" name="update_criteria" class="button button-primary" value="更新指标">
<a href="<?php echo remove_query_arg('edit'); ?>" class="button">取消</a>
<?php else: ?>
<input type="submit" name="add_criteria" class="button button-primary" value="添加指标">
<?php endif; ?>
</p>
</form>
</div>
</div>
<!-- 指标列表 -->
<div class="postbox">
<h2 class="hndle">评估指标列表</h2>
<div class="inside">
<?php if (empty($criteria)): ?>
<p>暂无评估指标,请先添加指标。</p>
<?php else: ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>ID</th>
<th>指标名称</th>
<th>描述</th>
<th>权重</th>
<th>最高分</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($criteria as $item): ?>
<tr>
<td><?php echo $item->criteria_id; ?></td>
<td><strong><?php echo esc_html($item->criteria_name); ?></strong></td>
<td><?php echo esc_html($item->description); ?></td>
<td><?php echo $item->weight; ?></td>
<td><?php echo $item->max_score; ?></td>
<td>
<span class="status-badge status-<?php echo $item->is_active ? 'active' : 'inactive'; ?>">
<?php echo $item->is_active ? '启用' : '停用'; ?>
</span>
</td>
<td>
<a href="<?php echo add_query_arg('edit', $item->criteria_id); ?>"
class="button button-small">编辑</a>
<a href="<?php echo wp_nonce_url(
add_query_arg(array(
'action' => 'delete_criteria',
'criteria_id' => $item->criteria_id
)),
'delete_criteria'
); ?>"
class="button button-small button-link-delete"
onclick="return confirm('确定要删除这个指标吗?')">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
}
/**
* 渲染报表页面
*/
public function render_reports_page() {
global $wpdb;
$supplier_id = isset($_GET['supplier_id']) ? intval($_GET['supplier_id']) : 0;
// 获取供应商统计信息
$stats = $this->supplier_system->get_supplier_statistics($supplier_id);
// 获取所有供应商用于下拉选择
$suppliers = $wpdb->get_results("SELECT supplier_id, supplier_name FROM {$wpdb->prefix}supplier_management WHERE status = 'active' ORDER BY supplier_name");
?>
<div class="wrap">
<h1>供应商评估报表</h1>
<!-- 供应商选择器 -->
<div class="supplier-selector">
<form method="get" action="<?php echo admin_url('admin.php'); ?>">
<input type="hidden" name="page" value="supplier-reports">
<label for="supplier_id">选择供应商:</label>
<select id="supplier_id" name="supplier_id" onchange="this.form.submit()">
<option value="">-- 请选择供应商 --</option>
<?php foreach ($suppliers as $supplier): ?>
<option value="<?php echo $supplier->supplier_id; ?>"
<?php selected($supplier_id, $supplier->supplier_id); ?>>
<?php echo esc_html($supplier->supplier_name); ?>
</option>
<?php endforeach; ?>
</select>
</form>
</div>
<?php if ($supplier_id > 0): ?>
<?php
$supplier_info = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}supplier_management WHERE supplier_id = %d",
$supplier_id
));
?>
<div class="report-header">
<h2><?php echo esc_html($supplier_info->supplier_name); ?> - 评估报表</h2>
<p>联系人:<?php echo esc_html($supplier_info->contact_person); ?> |
邮箱:<?php echo esc_html($supplier_info->email); ?> |
注册日期:<?php echo date('Y-m-d', strtotime($supplier_info->registration_date)); ?></p>
</div>
<!-- 关键指标卡片 -->
<div class="stats-cards">
<div class="card">
<h3>平均评分</h3>
<div class="score"><?php echo $stats['average_score']; ?>/10</div>
</div>
<div class="card">
<h3>评估次数</h3>
<div class="count"><?php echo $stats['evaluation_count']; ?> 次</div>
</div>
<div class="card">
<h3>当前状态</h3>
<div class="status">
<span class="status-badge status-<?php echo $supplier_info->status; ?>">
<?php
$status_labels = array(
'active' => '活跃',
'inactive' => '停用',
'pending' => '待审核'
);
echo $status_labels[$supplier_info->status] ?? $supplier_info->status;
?>
</span>
</div>
</div>
</div>
<!-- 各指标评分图表 -->
<div class="chart-container">
<h3>各指标评分分析</h3>
<canvas id="criteriaChart" width="400" height="200"></canvas>
</div>
<!-- 详细评估记录 -->
<div class="evaluation-history">
<h


