增加 血压血糖记录 饮食记录 运动打卡记录
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user