sql
用户表
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL,
role ENUM('admin', 'technician', 'user') NOT NULL DEFAULT 'user',
department VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
工单表
CREATE TABLE tickets (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
status ENUM('open', 'in_progress', 'resolved', 'closed') NOT NULL DEFAULT 'open',
priority ENUM('low', 'medium', 'high', 'urgent') NOT NULL DEFAULT 'medium',
category_id INT,
user_id INT NOT NULL,
assigned_to INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
closed_at TIMESTAMP NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (assigned_to) REFERENCES users(id),
FOREIGN KEY (category_id) REFERENCES categories(id)
);
工单分类表
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
工单评论表
CREATE TABLE ticket_comments (
id INT AUTO_INCREMENT PRIMARY KEY,
ticket_id INT NOT NULL,
user_id INT NOT NULL,
comment TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ticket_id) REFERENCES tickets(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
工单附件表
CREATE TABLE ticket_attachments (
id INT AUTO_INCREMENT PRIMARY KEY,
ticket_id INT NOT NULL,
user_id INT NOT NULL,
file_name VARCHAR(255) NOT NULL,
file_path VARCHAR(255) NOT NULL,
uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ticket_id) REFERENCES tickets(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
// 用户登录
public function login($username, $password) {
$stmt = $this-db-prepare(SELECT * FROM users WHERE username = :username);
$stmt-bindParam(':username', $username);
$stmt-execute();
$user = $stmt-fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
return $user;
}
return false;
}
// 获取用户信息
public function getUserById($id) {
$stmt = $this-db-prepare(SELECT * FROM users WHERE id = :id);
$stmt-bindParam(':id', $id);
$stmt-execute();
return $stmt-fetch(PDO::FETCH_ASSOC);
}
// 获取所有用户
public function getAllUsers() {
$stmt = $this-db-query(SELECT id, username, email, full_name, role, department FROM users);
return $stmt-fetchAll(PDO::FETCH_ASSOC);
}
}
4、工单模型 (models/Ticket.php)
php
<?php
class Ticket {
private $db;
public function __construct() {
$this-db = Database::getInstance()-getConnection();
}
// 创建工单
public function createTicket($data) {
$stmt = $this-db-prepare(INSERT INTO tickets (title, description, status, priority, category_id, user_id)
VALUES (:title, :description, :status, :priority, :category_id, :user_id));
// 获取工单详情
public function getTicketById($id) {
$stmt = $this-db-prepare(SELECT t.*, u.full_name as user_name,
a.full_name as assigned_to_name,
c.name as category_name
FROM tickets t
LEFT JOIN users u ON t.user_id = u.id
LEFT JOIN users a ON t.assigned_to = a.id
LEFT JOIN categories c ON t.category_id = c.id
WHERE t.id = :id);
$stmt-bindParam(':id', $id);
$stmt-execute();
return $stmt-fetch(PDO::FETCH_ASSOC);
}
// 获取所有工单
public function getAllTickets($limit = 20, $offset = 0) {
$stmt = $this-db-prepare(SELECT t.*, u.full_name as user_name,
a.full_name as assigned_to_name,
c.name as category_name
FROM tickets t
LEFT JOIN users u ON t.user_id = u.id
LEFT JOIN users a ON t.assigned_to = a.id
LEFT JOIN categories c ON t.category_id = c.id
ORDER BY t.created_at DESC
LIMIT :limit OFFSET :offset);
$stmt-bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt-bindParam(':offset', $offset, PDO::