!3 增加 血压血糖记录 饮食记录 运动打卡记录

Merge pull request !3 from SkinEast/zyt2026416
This commit is contained in:
天啊
2026-04-17 02:00:13 +00:00
committed by Gitee
13 changed files with 1429 additions and 27 deletions
@@ -78,4 +78,15 @@ class BloodRecordController extends BaseAdminController
$result = BloodRecordLogic::getRecordsByPatient($params);
return $this->data($result);
}
/**
* @notes 获取血糖趋势图数据
* @return \think\response\Json
*/
public function getBloodSugarTrend()
{
$params = $this->request->get();
$result = BloodRecordLogic::getBloodSugarTrend($params);
return $this->data($result);
}
}
@@ -0,0 +1,53 @@
<?php
namespace app\adminapi\controller\tcm;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\tcm\DietRecordLogic;
class DietRecordController extends BaseAdminController
{
public function add()
{
$params = $this->request->post();
$result = DietRecordLogic::add($params);
if ($result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(DietRecordLogic::getError());
}
public function edit()
{
$params = $this->request->post();
$result = DietRecordLogic::edit($params);
if ($result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(DietRecordLogic::getError());
}
public function delete()
{
$params = $this->request->post();
$result = DietRecordLogic::delete($params);
if ($result) {
return $this->success('删除成功', [], 1, 1);
}
return $this->fail(DietRecordLogic::getError());
}
public function detail()
{
$params = $this->request->get();
$result = DietRecordLogic::detail($params);
return $this->data($result);
}
public function getRecordsByPatient()
{
$params = $this->request->get();
$result = DietRecordLogic::getRecordsByPatient($params);
return $this->data($result);
}
}
@@ -0,0 +1,60 @@
<?php
namespace app\adminapi\controller\tcm;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\tcm\ExerciseRecordLogic;
class ExerciseRecordController extends BaseAdminController
{
public function add()
{
$params = $this->request->post();
$result = ExerciseRecordLogic::add($params);
if ($result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ExerciseRecordLogic::getError());
}
public function edit()
{
$params = $this->request->post();
$result = ExerciseRecordLogic::edit($params);
if ($result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ExerciseRecordLogic::getError());
}
public function delete()
{
$params = $this->request->post();
$result = ExerciseRecordLogic::delete($params);
if ($result) {
return $this->success('删除成功', [], 1, 1);
}
return $this->fail(ExerciseRecordLogic::getError());
}
public function detail()
{
$params = $this->request->get();
$result = ExerciseRecordLogic::detail($params);
return $this->data($result);
}
public function getRecordsByPatient()
{
$params = $this->request->get();
$result = ExerciseRecordLogic::getRecordsByPatient($params);
return $this->data($result);
}
public function getExerciseTrend()
{
$params = $this->request->get();
$result = ExerciseRecordLogic::getExerciseTrend($params);
return $this->data($result);
}
}
@@ -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' => []
];
}
}
}