The old 售后 flow was an order-refund appendage: tickets were hard-bound to an order number, users could only file them from 「我的订单」against a paid order, and the admin action bottomed out at 「标记订单已退款」. It could not carry account questions or feature requests that have nothing to do with an order. Replace it with a general ticket system modelled on 魔方财务云's idcsmart_ticket: title + department + description are required, linking an order is optional (user's own paid orders only). Add departments, custom statuses, preset replies and internal admin notes as four configuration surfaces. Conversation history moves off the ticket row's JSON log into dedicated ticket_reply rows, which unlocks quoted replies. Front end gets a 工单中心 panel under 我的服务; back end moves from 支付中心 → 售后服务 to users.php → 工单管理. Per CLAUDE.md's rewrite-over-compatibility rule: drop the old table, build new ones, delete the old code. No dual paths, no data migration. The upstream plugin's upstream/downstream delivery and internal-ticket conversion are deliberately left out — both are multi-provider platform concerns with no meaning on a single WordPress site. Data: order_aftersale becomes six aksrt_ticket* tables (ticket, ticket_reply, ticket_status, ticket_type, ticket_prereply, ticket_note). SCHEMA_VERSION 2.6.0 -> 3.0.0. Status and department seeds are idempotent; the default status 「待接单」is flagged as non-editable. Domain: new src/Ticket/ (13 files, all under 460 lines). Attachments reuse UserController::rest_upload_comment_image with a ticket-specific permission callback, mirroring the existing /report/upload-image route rather than growing a second upload path. Notifications go through AKSRT\Messages\Message::send() and deep-link to the right surface; email/SMS stay out of scope since the theme's mail and SMS stacks are separate implementations. Refund gate opens up: marking an order refunded no longer requires an in-flight ticket, only that the order is paid. Also generalise a hardcoded special case in tools/audit-admin-contracts.php — schema domain inference now reads the parent directory instead of matching "PostList" by name, so new leaf schema files need no per-name exception. Verification: php tools/audit-admin-contracts.php exit 0, errors=0 node --test (widget-system + admin) 664 pass / 0 fail php -l over all new/changed files clean node --check over all JS clean npm run build + build:public clean Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
159 lines
5.5 KiB
PHP
159 lines
5.5 KiB
PHP
<?php
|
|
/**
|
|
* 工单查询 trait:列表 WHERE 拼装、分页与统计。
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Ticket;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait TicketQuery {
|
|
|
|
/**
|
|
* 列表查询的 WHERE 片段与参数。
|
|
*
|
|
* 用户视角只可能看到自己的工单;管理视角可按用户 / 部门 / 状态 / 关键字过滤。
|
|
* 管理侧默认隐藏已完结状态(对齐参考实现的 whereNotIn([3,4]))。
|
|
*
|
|
* @param array<string,mixed> $args
|
|
* @return array{0:string,1:list<mixed>}
|
|
*/
|
|
private function ticket_where(array $args, bool $is_admin): array {
|
|
global $wpdb;
|
|
$where = [];
|
|
$params = [];
|
|
|
|
if (!$is_admin) {
|
|
$where[] = 'user_id = %d';
|
|
$params[] = (int) ($args['user_id'] ?? 0);
|
|
} else {
|
|
$user_id = (int) ($args['user_id'] ?? 0);
|
|
if ($user_id > 0) {
|
|
$where[] = 'user_id = %d';
|
|
$params[] = $user_id;
|
|
}
|
|
}
|
|
|
|
$statuses = [];
|
|
foreach ((array) ($args['status'] ?? []) as $status) {
|
|
$status = sanitize_key((string) $status);
|
|
if ($status !== '') {
|
|
$statuses[] = $status;
|
|
}
|
|
}
|
|
if ($statuses !== []) {
|
|
$where[] = 'status IN (' . implode(', ', array_fill(0, count($statuses), '%s')) . ')';
|
|
$params = array_merge($params, $statuses);
|
|
} elseif ($is_admin) {
|
|
$closed = TicketStatusService::is_closed(TicketStatusService::KEY_CLOSED);
|
|
if ($closed) {
|
|
$where[] = 'status <> %s';
|
|
$params[] = TicketStatusService::KEY_CLOSED;
|
|
}
|
|
}
|
|
|
|
$type_id = (int) ($args['ticket_type_id'] ?? 0);
|
|
if ($type_id > 0) {
|
|
$where[] = 'ticket_type_id = %d';
|
|
$params[] = $type_id;
|
|
}
|
|
|
|
$keywords = sanitize_text_field((string) ($args['keywords'] ?? ''));
|
|
if ($keywords !== '') {
|
|
$like = '%' . $wpdb->esc_like($keywords) . '%';
|
|
$where[] = '(ticket_num LIKE %s OR title LIKE %s)';
|
|
$params[] = $like;
|
|
$params[] = $like;
|
|
}
|
|
|
|
foreach (['date_from' => '>=', 'date_to' => '<='] as $key => $operator) {
|
|
$date = sanitize_text_field((string) ($args[$key] ?? ''));
|
|
if ($date !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) === 1) {
|
|
$where[] = 'created_at ' . $operator . ' %s';
|
|
$params[] = $operator === '>=' ? $date . ' 00:00:00' : $date . ' 23:59:59';
|
|
}
|
|
}
|
|
|
|
unset($wpdb);
|
|
return [
|
|
$where === [] ? '1 = 1' : implode(' AND ', $where),
|
|
$params,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $args
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
private function ticket_rows(array $args, bool $is_admin): array {
|
|
global $wpdb;
|
|
$table = \AKSRT\Core\DBMigrator::table('ticket');
|
|
if ($table === '') {
|
|
return [];
|
|
}
|
|
[$where, $params] = $this->ticket_where($args, $is_admin);
|
|
$limit = max(1, min(100, (int) ($args['limit'] ?? 20)));
|
|
$offset = max(0, (int) ($args['offset'] ?? 0));
|
|
$sql = "SELECT * FROM {$table} WHERE {$where} ORDER BY last_reply_time DESC, id DESC LIMIT %d OFFSET %d";
|
|
$params[] = $limit;
|
|
$params[] = $offset;
|
|
$rows = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A); // phpcs:ignore WordPress.DB.PreparedSQL
|
|
return is_array($rows) ? $rows : [];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $args
|
|
*/
|
|
private function ticket_count(array $args, bool $is_admin): int {
|
|
global $wpdb;
|
|
$table = \AKSRT\Core\DBMigrator::table('ticket');
|
|
if ($table === '') {
|
|
return 0;
|
|
}
|
|
[$where, $params] = $this->ticket_where($args, $is_admin);
|
|
$sql = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
|
|
$count = $params === []
|
|
? $wpdb->get_var($sql) // phpcs:ignore WordPress.DB.DirectDatabaseQuery
|
|
: $wpdb->get_var($wpdb->prepare($sql, $params)); // phpcs:ignore WordPress.DB.PreparedSQL
|
|
return (int) $count;
|
|
}
|
|
|
|
/**
|
|
* 各状态计数:侧栏统计卡与后台 badge 用。
|
|
* 用户视角只统计自己的;管理视角统计全站(可按部门过滤)。
|
|
*
|
|
* @param array<string,mixed> $args
|
|
* @return array<string,int> status_key => count
|
|
*/
|
|
private function ticket_statistic(array $args, bool $is_admin): array {
|
|
global $wpdb;
|
|
$table = \AKSRT\Core\DBMigrator::table('ticket');
|
|
$out = [];
|
|
foreach (array_keys(TicketStatusService::map()) as $key) {
|
|
$out[(string) $key] = 0;
|
|
}
|
|
if ($table === '') {
|
|
return $out;
|
|
}
|
|
|
|
$query_args = [
|
|
'user_id' => (int) ($args['user_id'] ?? 0),
|
|
'ticket_type_id' => $args['ticket_type_id'] ?? 0,
|
|
];
|
|
[$where, $params] = $this->ticket_where($query_args, $is_admin);
|
|
$sql = "SELECT status, COUNT(*) AS total FROM {$table} WHERE {$where} GROUP BY status";
|
|
$rows = $wpdb->get_results($params === [] ? $sql : $wpdb->prepare($sql, $params), ARRAY_A); // phpcs:ignore WordPress.DB.PreparedSQL
|
|
if (is_array($rows)) {
|
|
foreach ($rows as $row) {
|
|
$key = sanitize_key((string) ($row['status'] ?? ''));
|
|
if ($key !== '') {
|
|
$out[$key] = (int) ($row['total'] ?? 0);
|
|
}
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
}
|