Files
zyt/server/app/adminapi/logic/tcm/ExerciseRecordLogic.php
T
2026-05-11 17:49:38 +08:00

166 lines
4.9 KiB
PHP
Executable File

<?php
namespace app\adminapi\logic\tcm;
use app\common\logic\BaseLogic;
use app\common\model\tcm\ExerciseRecord;
class ExerciseRecordLogic extends BaseLogic
{
public static function add(array $params): bool
{
try {
if (isset($params['record_date'])) {
$params['record_date'] = strtotime($params['record_date']);
}
ExerciseRecord::create($params);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function edit(array $params): bool
{
try {
if (isset($params['record_date'])) {
$params['record_date'] = strtotime($params['record_date']);
}
ExerciseRecord::update($params);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function delete(array $params): bool
{
try {
ExerciseRecord::destroy($params['id']);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function detail($params): array
{
$record = ExerciseRecord::findOrEmpty($params['id'])->toArray();
if (!empty($record['record_date'])) {
$record['record_date'] = date('Y-m-d', $record['record_date']);
}
return $record;
}
public static function getRecordsByPatient(array $params): array
{
try {
$where = [];
if (isset($params['diagnosis_id'])) {
$where[] = ['diagnosis_id', '=', $params['diagnosis_id']];
}
if (isset($params['patient_id'])) {
$where[] = ['patient_id', '=', $params['patient_id']];
}
// 如果没有诊断ID或患者ID,返回空数组
if (empty($where)) {
return [];
}
$records = ExerciseRecord::where($where)
->where('delete_time', null)
->order('record_date', 'desc')
->select()
->toArray();
foreach ($records as &$record) {
if (!empty($record['record_date'])) {
$record['record_date'] = date('Y-m-d', $record['record_date']);
}
}
return $records;
} catch (\Exception $e) {
self::setError($e->getMessage());
return [];
}
}
public static function getExerciseTrend(array $params): array
{
try {
$where = [];
if (isset($params['diagnosis_id'])) {
$where[] = ['diagnosis_id', '=', $params['diagnosis_id']];
}
if (isset($params['patient_id'])) {
$where[] = ['patient_id', '=', $params['patient_id']];
}
// 如果没有诊断ID或患者ID,返回空数组
if (empty($where)) {
return [
'dates' => [],
'duration' => []
];
}
// 处理日期范围
if (isset($params['start_date']) && isset($params['end_date'])) {
$startDate = strtotime($params['start_date']);
$endDate = strtotime($params['end_date'] . ' 23:59:59');
} else {
$days = isset($params['days']) ? intval($params['days']) : 7;
$startDate = strtotime("-{$days} days");
$endDate = time();
}
$records = ExerciseRecord::where($where)
->where('delete_time', null)
->where('record_date', '>=', $startDate)
->where('record_date', '<=', $endDate)
->order('record_date', 'asc')
->select()
->toArray();
$trend = [
'dates' => [],
'duration' => []
];
foreach ($records as $record) {
$date = date('Y-m-d', $record['record_date']);
if (!in_array($date, $trend['dates'])) {
$trend['dates'][] = $date;
}
$trend['duration'][] = [
'date' => $date,
'value' => $record['duration']
];
}
return $trend;
} catch (\Exception $e) {
self::setError($e->getMessage());
return [
'dates' => [],
'duration' => []
];
}
}
}