!3 增加 血压血糖记录 饮食记录 运动打卡记录
Merge pull request !3 from SkinEast/zyt2026416
This commit is contained in:
@@ -96,6 +96,50 @@ class BloodRecordLogic extends BaseLogic
|
||||
* @return array
|
||||
*/
|
||||
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 = BloodRecord::where($where)
|
||||
->where('delete_time', null)
|
||||
->order('record_date', 'desc')
|
||||
->order('record_time', '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 [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取血糖趋势图数据
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public static function getBloodSugarTrend(array $params): array
|
||||
{
|
||||
$where = [];
|
||||
|
||||
@@ -107,20 +151,93 @@ class BloodRecordLogic extends BaseLogic
|
||||
$where[] = ['patient_id', '=', $params['patient_id']];
|
||||
}
|
||||
|
||||
$records = BloodRecord::where($where)
|
||||
->where('delete_time', null)
|
||||
->order('record_date', 'desc')
|
||||
->order('record_time', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
// 检查是否使用自定义日期范围
|
||||
if (isset($params['start_date']) && isset($params['end_date'])) {
|
||||
$startDate = intval($params['start_date']);
|
||||
$endDate = intval($params['end_date']);
|
||||
} else {
|
||||
$days = isset($params['days']) ? intval($params['days']) : 7;
|
||||
$startDate = strtotime("-{$days} days");
|
||||
$endDate = time();
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
foreach ($records as &$record) {
|
||||
if (!empty($record['record_date'])) {
|
||||
$record['record_date'] = date('Y-m-d', $record['record_date']);
|
||||
$query = BloodRecord::where($where)
|
||||
->where('delete_time', null)
|
||||
->where('record_date', '>=', $startDate)
|
||||
->order('record_date', 'asc');
|
||||
|
||||
// 如果有结束日期,添加结束日期条件
|
||||
if (isset($endDate)) {
|
||||
$query->where('record_date', '<=', $endDate);
|
||||
}
|
||||
|
||||
$records = $query->select()->toArray();
|
||||
|
||||
// 使用Map来存储每天的数据,确保同一天的多条记录能够正确处理
|
||||
$dateMap = [];
|
||||
|
||||
foreach ($records as $record) {
|
||||
$date = date('Y-m-d', $record['record_date']);
|
||||
|
||||
if (!isset($dateMap[$date])) {
|
||||
$dateMap[$date] = [
|
||||
'fasting' => null,
|
||||
'postprandial_2h' => null,
|
||||
'other' => null
|
||||
];
|
||||
}
|
||||
|
||||
// 空腹血糖(取第一个非空值)
|
||||
if (isset($record['fasting_blood_sugar']) && $record['fasting_blood_sugar'] > 0 && $dateMap[$date]['fasting'] === null) {
|
||||
$dateMap[$date]['fasting'] = $record['fasting_blood_sugar'];
|
||||
}
|
||||
|
||||
// 餐后2小时血糖(取第一个非空值)
|
||||
if (isset($record['postprandial_blood_sugar']) && $record['postprandial_blood_sugar'] > 0 && $dateMap[$date]['postprandial_2h'] === null) {
|
||||
$dateMap[$date]['postprandial_2h'] = $record['postprandial_blood_sugar'];
|
||||
}
|
||||
|
||||
// 其他血糖(取第一个非空值)
|
||||
if (isset($record['other_blood_sugar']) && $record['other_blood_sugar'] > 0 && $dateMap[$date]['other'] === null) {
|
||||
$dateMap[$date]['other'] = $record['other_blood_sugar'];
|
||||
}
|
||||
}
|
||||
|
||||
return $records;
|
||||
// 按日期排序
|
||||
ksort($dateMap);
|
||||
|
||||
$trend = [
|
||||
'dates' => [],
|
||||
'fasting' => [],
|
||||
'postprandial_2h' => [],
|
||||
'other' => []
|
||||
];
|
||||
|
||||
foreach ($dateMap as $date => $values) {
|
||||
$trend['dates'][] = $date;
|
||||
|
||||
if ($values['fasting'] !== null) {
|
||||
$trend['fasting'][] = [
|
||||
'date' => $date,
|
||||
'value' => $values['fasting']
|
||||
];
|
||||
}
|
||||
|
||||
if ($values['postprandial_2h'] !== null) {
|
||||
$trend['postprandial_2h'][] = [
|
||||
'date' => $date,
|
||||
'value' => $values['postprandial_2h']
|
||||
];
|
||||
}
|
||||
|
||||
if ($values['other'] !== null) {
|
||||
$trend['other'][] = [
|
||||
'date' => $date,
|
||||
'value' => $values['other']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $trend;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\adminapi\logic\tcm;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\tcm\DietRecord;
|
||||
|
||||
class DietRecordLogic extends BaseLogic
|
||||
{
|
||||
public static function add(array $params): bool
|
||||
{
|
||||
try {
|
||||
if (isset($params['record_date'])) {
|
||||
$params['record_date'] = strtotime($params['record_date']);
|
||||
}
|
||||
|
||||
DietRecord::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']);
|
||||
}
|
||||
|
||||
DietRecord::update($params);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function delete(array $params): bool
|
||||
{
|
||||
try {
|
||||
DietRecord::destroy($params['id']);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function detail($params): array
|
||||
{
|
||||
$record = DietRecord::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 = DietRecord::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 [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?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' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user