File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Services/AdminReportService.php
Back
<?php /** * AdminReportService * Report queue management: list, assign, resolve, reject */ require_once __DIR__ . '/../Models/Report.php'; require_once __DIR__ . '/../Models/AdminAuditLog.php'; require_once __DIR__ . '/../../database/Database.php'; class AdminReportService { private Report $reportModel; private AdminAuditLog $auditLog; public function __construct() { $this->reportModel = new Report(); $this->auditLog = new AdminAuditLog(); } public function getReports(array $filters, int $page = 1, int $perPage = 20): array { $offset = ($page - 1) * $perPage; $reports = $this->reportModel->getReports($filters, $perPage, $offset); $total = $this->reportModel->countReports($filters); return [ 'reports' => $reports, 'total' => $total, 'page' => $page, 'per_page' => $perPage, 'total_pages' => max(1, (int)ceil($total / $perPage)), 'counts' => $this->reportModel->getStatusCounts(), ]; } public function getReportDetail(int $id): ?array { $report = $this->reportModel->findReport($id); if (!$report) { return null; } $report['target_info'] = $this->resolveTargetInfo($report['report_type'], (int)$report['target_id']); return $report; } public function assignAndReview(int $reportId, int $adminId): array { $report = $this->reportModel->findReport($reportId); if (!$report) return ['success' => false, 'message' => 'ไม่พบรายงาน']; if (!in_array($report['status'], ['pending', 'submitted'], true)) { return ['success' => false, 'message' => 'รายงานนี้ถูกดำเนินการไปแล้ว']; } $ok = $this->reportModel->updateStatus($reportId, 'reviewing', null, $adminId); return ['success' => $ok, 'message' => $ok ? 'รับรายงานนี้เพื่อตรวจสอบเรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function resolve(int $reportId, string $note, int $adminId): array { if (empty(trim($note))) { return ['success' => false, 'message' => 'กรุณาระบุผลการดำเนินการ']; } $ok = $this->reportModel->updateStatus($reportId, 'resolved', $note, $adminId); if ($ok) { $this->auditLog->record($adminId, 'report.resolve', 'reports', 'report', $reportId, "Resolved report #{$reportId}. Note: " . mb_substr($note, 0, 300)); } return ['success' => $ok, 'message' => $ok ? 'ปิดรายงานและบันทึกผลการดำเนินการเรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function reject(int $reportId, string $note, int $adminId): array { $ok = $this->reportModel->updateStatus($reportId, 'rejected', $note ?: 'ไม่ผ่านเกณฑ์การตรวจสอบ', $adminId); if ($ok) { $this->auditLog->record($adminId, 'report.reject', 'reports', 'report', $reportId, "Rejected report #{$reportId}"); } return ['success' => $ok, 'message' => $ok ? 'ปฏิเสธรายงานเรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } private function resolveTargetInfo(string $type, int $targetId): ?array { try { $db = Database::getInstance(); switch ($type) { case 'product': $p = $db->prepare("SELECT p.id, p.name, p.price, p.status, s.store_name FROM products p LEFT JOIN stores s ON p.store_id = s.id WHERE p.id = :id"); $p->execute([':id' => $targetId]); return $p->fetch(PDO::FETCH_ASSOC) ?: null; case 'seller': $s = $db->prepare("SELECT s.id, s.store_name, s.store_slug, s.status, u.username FROM stores s LEFT JOIN users u ON s.user_id = u.id WHERE s.id = :id"); $s->execute([':id' => $targetId]); return $s->fetch(PDO::FETCH_ASSOC) ?: null; case 'order': $o = $db->prepare("SELECT o.id, o.order_no, o.total_amount, o.order_status, o.payment_status FROM orders o WHERE o.id = :id"); $o->execute([':id' => $targetId]); return $o->fetch(PDO::FETCH_ASSOC) ?: null; case 'review': $r = $db->prepare("SELECT r.id, r.product_id, r.rating, r.comment, r.status, u.username FROM reviews r LEFT JOIN users u ON r.user_id = u.id WHERE r.id = :id"); $r->execute([':id' => $targetId]); return $r->fetch(PDO::FETCH_ASSOC) ?: null; } } catch (Exception $e) { // Ignore if target table query fails } return null; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.23 |
proxy
|
phpinfo
|
Settings