94 lines
2.5 KiB
PHP
94 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Client;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Care\CareProfile;
|
|
use App\Models\Care\CareRecord;
|
|
use App\Models\Care\HealthMetric;
|
|
use App\Models\Crm\Customer;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CareController extends Controller
|
|
{
|
|
/**
|
|
* 获取当前客户护理档案
|
|
*/
|
|
public function profile(Request $request): JsonResponse
|
|
{
|
|
/** @var Customer $customer */
|
|
$customer = $request->user();
|
|
|
|
$profile = CareProfile::withoutGlobalScope('store')
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if ($profile) {
|
|
$profile->load(['plans']);
|
|
}
|
|
|
|
return $this->success($profile);
|
|
}
|
|
|
|
/**
|
|
* 护理记录列表(分页)
|
|
*/
|
|
public function records(Request $request): JsonResponse
|
|
{
|
|
/** @var Customer $customer */
|
|
$customer = $request->user();
|
|
|
|
$profile = CareProfile::withoutGlobalScope('store')
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$profile) {
|
|
return $this->paginate(
|
|
CareRecord::withoutGlobalScope('store')->whereRaw('0=1')->paginate(10)
|
|
);
|
|
}
|
|
|
|
$query = CareRecord::withoutGlobalScope('store')
|
|
->where('care_profile_id', $profile->id)
|
|
->with('nurse:id,name');
|
|
|
|
if ($request->filled('type')) {
|
|
$query->where('type', (int) $request->input('type'));
|
|
}
|
|
|
|
return $this->paginate($query->latest('recorded_at')->paginate(10));
|
|
}
|
|
|
|
/**
|
|
* 健康指标列表(最近30条)
|
|
*/
|
|
public function metrics(Request $request): JsonResponse
|
|
{
|
|
/** @var Customer $customer */
|
|
$customer = $request->user();
|
|
|
|
$profile = CareProfile::withoutGlobalScope('store')
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$profile) {
|
|
return $this->success([]);
|
|
}
|
|
|
|
$query = HealthMetric::withoutGlobalScope('store')
|
|
->where('care_profile_id', $profile->id);
|
|
|
|
if ($request->filled('metric_type')) {
|
|
$query->where('metric_type', (string) $request->input('metric_type'));
|
|
}
|
|
|
|
$limit = (int) $request->input('limit', 30);
|
|
$limit = max(1, min($limit, 50));
|
|
|
|
$list = $query->orderByDesc('recorded_at')->limit($limit)->get();
|
|
|
|
return $this->success($list);
|
|
}
|
|
}
|