新增功能
This commit is contained in:
@@ -1066,7 +1066,6 @@ class DiagnosisLogic extends BaseLogic
|
||||
|
||||
// 获取access_token(如果是重试,强制刷新)
|
||||
$forceRefresh = $retryCount > 0;
|
||||
|
||||
$accessToken = self::getWxAccessToken($config['app_id'], $config['app_secret'], $forceRefresh);
|
||||
|
||||
if (!$accessToken) {
|
||||
@@ -1723,4 +1722,168 @@ class DiagnosisLogic extends BaseLogic
|
||||
curl_close($ch);
|
||||
return $result ? json_decode($result, true) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 医助理诊单统计(按部门维度,按人统计创建数量)
|
||||
* @param array $params start_time, end_time, days(可选,默认7)
|
||||
* @return array
|
||||
*/
|
||||
public static function assistantDiagnosisStats(array $params = [])
|
||||
{
|
||||
$days = isset($params['days']) ? (int)$params['days'] : 7;
|
||||
|
||||
$endTime = !empty($params['end_time']) ? strtotime($params['end_time']) : time();
|
||||
if ($days === 0) {
|
||||
$startTime = strtotime(date('Y-m-d'));
|
||||
} else {
|
||||
$days = $days > 0 ? min($days, 90) : 7;
|
||||
$startTime = !empty($params['start_time']) ? strtotime($params['start_time']) : strtotime("-{$days} days", $endTime);
|
||||
if ($startTime > $endTime) {
|
||||
$startTime = strtotime("-{$days} days", $endTime);
|
||||
}
|
||||
}
|
||||
|
||||
$depts = \app\common\model\dept\Dept::where('status', 1)
|
||||
->whereNull('delete_time')
|
||||
->order('sort desc, id asc')
|
||||
->column('name', 'id');
|
||||
|
||||
$assistantIds = \app\common\model\auth\AdminRole::where('role_id', 2)
|
||||
->column('admin_id');
|
||||
if (empty($assistantIds)) {
|
||||
return [
|
||||
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
|
||||
'today_total' => 0,
|
||||
'today_ranking' => [],
|
||||
'departments' => [],
|
||||
'ranking' => [],
|
||||
'chart' => ['xAxis' => [], 'series' => []],
|
||||
];
|
||||
}
|
||||
|
||||
$adminDepts = \app\common\model\auth\AdminDept::whereIn('admin_id', $assistantIds)
|
||||
->select()
|
||||
->toArray();
|
||||
$adminToDepts = [];
|
||||
foreach ($adminDepts as $ad) {
|
||||
$adminId = $ad['admin_id'];
|
||||
if (!isset($adminToDepts[$adminId])) {
|
||||
$adminToDepts[$adminId] = [];
|
||||
}
|
||||
$adminToDepts[$adminId][] = (int)$ad['dept_id'];
|
||||
}
|
||||
|
||||
$admins = \app\common\model\auth\Admin::whereIn('id', $assistantIds)
|
||||
->whereNull('delete_time')
|
||||
->column('name', 'id');
|
||||
|
||||
$countRows = Diagnosis::where('assistant_id', 'in', $assistantIds)
|
||||
->where('create_time', 'between', [$startTime, $endTime])
|
||||
->whereNull('delete_time')
|
||||
->field('assistant_id, count(*) as cnt')
|
||||
->group('assistant_id')
|
||||
->select()
|
||||
->toArray();
|
||||
$counts = [];
|
||||
foreach ($countRows as $row) {
|
||||
$counts[$row['assistant_id']] = (int)$row['cnt'];
|
||||
}
|
||||
|
||||
$todayStart = strtotime(date('Y-m-d'));
|
||||
$todayEnd = time();
|
||||
$todayCountRows = Diagnosis::where('assistant_id', 'in', $assistantIds)
|
||||
->where('create_time', 'between', [$todayStart, $todayEnd])
|
||||
->whereNull('delete_time')
|
||||
->field('assistant_id, count(*) as cnt')
|
||||
->group('assistant_id')
|
||||
->select()
|
||||
->toArray();
|
||||
$todayCounts = [];
|
||||
foreach ($todayCountRows as $row) {
|
||||
$todayCounts[$row['assistant_id']] = (int)$row['cnt'];
|
||||
}
|
||||
$todayTotal = array_sum($todayCounts);
|
||||
$todayRanking = [];
|
||||
foreach ($assistantIds as $adminId) {
|
||||
$cnt = (int)($todayCounts[$adminId] ?? 0);
|
||||
$todayRanking[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
usort($todayRanking, fn($a, $b) => $b['count'] <=> $a['count']);
|
||||
$todayRanking = array_values(array_filter($todayRanking, fn($a) => $a['count'] > 0));
|
||||
|
||||
$deptData = [];
|
||||
$assignedAdminIds = [];
|
||||
foreach ($depts as $deptId => $deptName) {
|
||||
$assistants = [];
|
||||
foreach ($adminToDepts as $adminId => $deptIds) {
|
||||
if (!in_array((int)$deptId, $deptIds)) {
|
||||
continue;
|
||||
}
|
||||
$assignedAdminIds[] = $adminId;
|
||||
$cnt = (int)($counts[$adminId] ?? 0);
|
||||
$assistants[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
$deptData[] = [
|
||||
'dept_id' => (int)$deptId,
|
||||
'dept_name' => $deptName,
|
||||
'assistants' => $assistants,
|
||||
'total' => array_sum(array_column($assistants, 'count')),
|
||||
];
|
||||
}
|
||||
$unassignedIds = array_diff($assistantIds, array_unique($assignedAdminIds));
|
||||
if (!empty($unassignedIds)) {
|
||||
$assistants = [];
|
||||
foreach ($unassignedIds as $adminId) {
|
||||
$cnt = (int)($counts[$adminId] ?? 0);
|
||||
$assistants[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => $cnt,
|
||||
];
|
||||
}
|
||||
$deptData[] = [
|
||||
'dept_id' => 0,
|
||||
'dept_name' => '未分配部门',
|
||||
'assistants' => $assistants,
|
||||
'total' => array_sum(array_column($assistants, 'count')),
|
||||
];
|
||||
}
|
||||
|
||||
$ranking = [];
|
||||
foreach ($assistantIds as $adminId) {
|
||||
$ranking[] = [
|
||||
'id' => (int)$adminId,
|
||||
'name' => $admins[$adminId] ?? '未知',
|
||||
'count' => (int)($counts[$adminId] ?? 0),
|
||||
];
|
||||
}
|
||||
usort($ranking, fn($a, $b) => $b['count'] <=> $a['count']);
|
||||
|
||||
$allAssistantNames = [];
|
||||
$allAssistantData = [];
|
||||
foreach ($ranking as $i => $a) {
|
||||
$allAssistantNames[] = ($i + 1) . '.' . $a['name'];
|
||||
$allAssistantData[] = $a['count'];
|
||||
}
|
||||
|
||||
return [
|
||||
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
|
||||
'today_total' => $todayTotal,
|
||||
'today_ranking' => $todayRanking,
|
||||
'departments' => $deptData,
|
||||
'ranking' => $ranking,
|
||||
'chart' => [
|
||||
'xAxis' => $allAssistantNames,
|
||||
'series' => [['name' => '诊单数', 'data' => $allAssistantData]],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\adminapi\logic\tcm;
|
||||
|
||||
use app\common\model\tcm\Prescription;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
|
||||
class PrescriptionLogic
|
||||
{
|
||||
private static $error = '';
|
||||
|
||||
public static function setError(string $msg): void
|
||||
{
|
||||
self::$error = $msg;
|
||||
}
|
||||
|
||||
public static function getError(): string
|
||||
{
|
||||
return self::$error;
|
||||
}
|
||||
|
||||
private static function generateSn(): string
|
||||
{
|
||||
return 'RX' . date('Ymd') . str_pad((string)mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加处方
|
||||
*/
|
||||
public static function add(array $params, int $adminId): ?int
|
||||
{
|
||||
$diagnosis = Diagnosis::find($params['diagnosis_id']);
|
||||
if (!$diagnosis) {
|
||||
self::setError('诊单不存在');
|
||||
return null;
|
||||
}
|
||||
|
||||
$herbs = $params['herbs'] ?? [];
|
||||
if (empty($herbs) || !is_array($herbs)) {
|
||||
self::setError('请添加中药');
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($herbs as $h) {
|
||||
if (empty($h['name'])) {
|
||||
self::setError('中药名称不能为空');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$sn = self::generateSn();
|
||||
while (Prescription::where('sn', $sn)->find()) {
|
||||
$sn = self::generateSn();
|
||||
}
|
||||
|
||||
$data = [
|
||||
'sn' => $sn,
|
||||
'diagnosis_id' => (int)$params['diagnosis_id'],
|
||||
'appointment_id' => (int)($params['appointment_id'] ?? 0),
|
||||
'patient_id' => (int)$diagnosis->patient_id,
|
||||
'patient_name' => $params['patient_name'] ?? $diagnosis->patient_name,
|
||||
'gender' => (int)($params['gender'] ?? $diagnosis->gender),
|
||||
'age' => (int)($params['age'] ?? $diagnosis->age ?? 0),
|
||||
'phone' => $params['phone'] ?? $diagnosis->phone ?? '',
|
||||
'visit_no' => $params['visit_no'] ?? $sn,
|
||||
'prescription_date' => $params['prescription_date'] ?? date('Y-m-d'),
|
||||
'pulse' => $params['pulse'] ?? $diagnosis->pulse ?? '',
|
||||
'tongue' => $params['tongue'] ?? $diagnosis->tongue_coating ?? '',
|
||||
'clinical_diagnosis' => $params['clinical_diagnosis'] ?? '',
|
||||
'herbs' => $herbs,
|
||||
'dose_count' => (int)($params['dose_count'] ?? 1),
|
||||
'usage_instruction' => $params['usage_instruction'] ?? '水煎服一日二次',
|
||||
'amount' => (float)($params['amount'] ?? 0),
|
||||
'doctor_name' => $params['doctor_name'] ?? '',
|
||||
'doctor_signature' => $params['doctor_signature'] ?? '',
|
||||
'template_id' => (int)($params['template_id'] ?? 0),
|
||||
'creator_id' => $adminId,
|
||||
];
|
||||
|
||||
$prescription = new Prescription();
|
||||
$prescription->save($data);
|
||||
return (int)$prescription->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处方详情
|
||||
*/
|
||||
public static function detail(int $id): ?array
|
||||
{
|
||||
$row = Prescription::find($id);
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
$arr = $row->toArray();
|
||||
$arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女';
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据诊单ID获取处方列表
|
||||
*/
|
||||
public static function listByDiagnosis(int $diagnosisId): array
|
||||
{
|
||||
return Prescription::where('diagnosis_id', $diagnosisId)
|
||||
->whereNull('delete_time')
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据预约ID获取处方
|
||||
*/
|
||||
public static function getByAppointment(int $appointmentId): ?array
|
||||
{
|
||||
$row = Prescription::where('appointment_id', $appointmentId)
|
||||
->whereNull('delete_time')
|
||||
->order('id', 'desc')
|
||||
->find();
|
||||
return $row ? $row->toArray() : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user