新增功能

This commit is contained in:
Your Name
2026-03-14 15:39:34 +08:00
parent 4ddee40675
commit 4a853ba237
27 changed files with 2906 additions and 74 deletions
@@ -25,6 +25,17 @@ class OrderController extends BaseAdminController
return $this->dataLists(new OrderLists());
}
/**
* @notes 订单统计(挂号费/药品费用)
* @return \think\response\Json
*/
public function orderStats()
{
$params = $this->request->get();
$result = OrderLogic::orderStats($params);
return $this->data($result);
}
/**
* @notes 订单详情
* @return \think\response\Json
@@ -50,6 +50,17 @@ class DiagnosisController extends BaseAdminController
return $this->dataLists(new DiagnosisLists());
}
/**
* @notes 医助理诊单统计(按部门、按人)
* @return \think\response\Json
*/
public function assistantDiagnosisStats()
{
$params = $this->request->get();
$result = DiagnosisLogic::assistantDiagnosisStats($params);
return $this->data($result);
}
/**
* @notes 添加诊单
* @return \think\response\Json
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace app\adminapi\controller\tcm;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\tcm\PrescriptionLogic;
use app\adminapi\validate\tcm\PrescriptionValidate;
/**
* 中医处方单控制器
*/
class PrescriptionController extends BaseAdminController
{
/**
* @notes 添加处方
*/
public function add()
{
$params = (new PrescriptionValidate())->post()->goCheck('add');
$params['creator_id'] = $this->adminId;
$id = PrescriptionLogic::add($params, $this->adminId);
if ($id === null) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('保存成功', ['id' => $id]);
}
/**
* @notes 处方详情
*/
public function detail()
{
$params = (new PrescriptionValidate())->get()->goCheck('detail');
$detail = PrescriptionLogic::detail((int)$params['id']);
if (!$detail) {
return $this->fail('处方不存在');
}
return $this->data($detail);
}
/**
* @notes 根据诊单获取处方列表
*/
public function listByDiagnosis()
{
$diagnosisId = (int)($this->request->get('diagnosis_id') ?? 0);
if (!$diagnosisId) {
return $this->fail('诊单ID不能为空');
}
$list = PrescriptionLogic::listByDiagnosis($diagnosisId);
return $this->data($list);
}
/**
* @notes 根据预约获取处方
*/
public function getByAppointment()
{
$appointmentId = (int)($this->request->get('appointment_id') ?? 0);
if (!$appointmentId) {
return $this->fail('预约ID不能为空');
}
$prescription = PrescriptionLogic::getByAppointment($appointmentId);
return $this->data($prescription ?? []);
}
}
@@ -258,6 +258,12 @@ class AdminLogic extends BaseLogic
])->findOrEmpty($params['id'])->toArray();
if ($action == 'detail') {
$roleIds = AdminRole::where('admin_id', $params['id'])->column('role_id');
if (in_array(2, $roleIds)) {
$admin['diagnosis_count'] = \app\common\model\tcm\Diagnosis::where('assistant_id', $params['id'])
->whereNull('delete_time')
->count();
}
return $admin;
}
@@ -427,4 +427,213 @@ class OrderLogic
return [];
}
}
private static $orderTypeNames = [
0 => '退款统计',
1 => '挂号费',
2 => '问诊费',
3 => '药品费用',
4 => '首付费用',
5 => '尾款费用',
6 => '其他费用',
];
/**
* @notes 订单统计(按订单类型或退款)
* @param array $params order_type(0退款,1挂号费,2问诊费,3药品费用,4首付,5尾款,6其他), days(0今天,7,30)
* @return array
*/
public static function orderStats(array $params = [])
{
$orderType = isset($params['order_type']) ? (int)$params['order_type'] : 1;
if (!array_key_exists($orderType, self::$orderTypeNames)) {
$orderType = 1;
}
$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 = strtotime("-{$days} days", $endTime);
}
$startStr = date('Y-m-d H:i:s', $startTime);
$endStr = date('Y-m-d H:i:s', $endTime);
$depts = \app\common\model\dept\Dept::where('status', 1)
->whereNull('delete_time')
->order('sort desc, id asc')
->column('name', 'id');
$query = Order::where('create_time', 'between', [$startStr, $endStr])
->whereNull('delete_time');
if ($orderType === 0) {
$query->where('status', 4);
} else {
$query->where('order_type', $orderType)->where('status', 2);
}
$orderRows = $query
->field('creator_id, count(*) as cnt, sum(amount) as total_amount')
->group('creator_id')
->select()
->toArray();
$creatorIds = array_unique(array_column($orderRows, 'creator_id'));
$creatorIds = array_filter($creatorIds);
if (empty($creatorIds)) {
return [
'order_type' => $orderType,
'order_type_name' => self::$orderTypeNames[$orderType],
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
'today_total' => 0,
'today_amount' => '0.00',
'today_ranking' => [],
'departments' => [],
'ranking' => [],
'chart' => ['xAxis' => [], 'series' => []],
];
}
$counts = [];
$amounts = [];
foreach ($orderRows as $row) {
$counts[$row['creator_id']] = (int)$row['cnt'];
$amounts[$row['creator_id']] = round((float)$row['total_amount'], 2);
}
$adminDepts = \app\common\model\auth\AdminDept::whereIn('admin_id', $creatorIds)
->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', $creatorIds)
->whereNull('delete_time')
->column('name', 'id');
$todayStart = strtotime(date('Y-m-d'));
$todayEnd = time();
$todayStr = date('Y-m-d H:i:s', $todayStart);
$todayEndStr = date('Y-m-d H:i:s', $todayEnd);
$todayQuery = Order::where('create_time', 'between', [$todayStr, $todayEndStr])
->whereNull('delete_time');
if ($orderType === 0) {
$todayQuery->where('status', 4);
} else {
$todayQuery->where('order_type', $orderType)->where('status', 2);
}
$todayRows = $todayQuery
->field('creator_id, count(*) as cnt, sum(amount) as total_amount')
->group('creator_id')
->select()
->toArray();
$todayCounts = [];
$todayAmounts = [];
foreach ($todayRows as $row) {
$todayCounts[$row['creator_id']] = (int)$row['cnt'];
$todayAmounts[$row['creator_id']] = round((float)$row['total_amount'], 2);
}
$todayTotal = array_sum($todayCounts);
$todayAmount = array_sum($todayAmounts);
$todayRanking = [];
foreach ($creatorIds as $cid) {
$cnt = (int)($todayCounts[$cid] ?? 0);
$amt = (float)($todayAmounts[$cid] ?? 0);
if ($cnt > 0 || $amt > 0) {
$todayRanking[] = [
'id' => (int)$cid,
'name' => $admins[$cid] ?? '未知',
'count' => $cnt,
'amount' => number_format($amt, 2, '.', ''),
];
}
}
usort($todayRanking, fn($a, $b) => $b['count'] <=> $a['count']);
$deptData = [];
$assignedIds = [];
foreach ($depts as $deptId => $deptName) {
$members = [];
foreach ($adminToDepts as $adminId => $deptIdsArr) {
if (!in_array((int)$deptId, $deptIdsArr)) {
continue;
}
$assignedIds[] = $adminId;
$members[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
$deptTotal = array_sum(array_column($members, 'count'));
$deptAmount = array_sum(array_map(fn($m) => (float)$m['amount'], $members));
$deptData[] = [
'dept_id' => (int)$deptId,
'dept_name' => $deptName,
'members' => $members,
'total' => $deptTotal,
'amount' => number_format($deptAmount, 2, '.', ''),
];
}
$unassignedIds = array_diff($creatorIds, array_unique($assignedIds));
if (!empty($unassignedIds)) {
$members = [];
foreach ($unassignedIds as $adminId) {
$members[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
$deptData[] = [
'dept_id' => 0,
'dept_name' => '未分配部门',
'members' => $members,
'total' => array_sum(array_column($members, 'count')),
'amount' => number_format(array_sum(array_map(fn($m) => (float)$m['amount'], $members)), 2, '.', ''),
];
}
$ranking = [];
foreach ($creatorIds as $adminId) {
$ranking[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
usort($ranking, fn($a, $b) => $b['count'] <=> $a['count']);
$chartNames = [];
$chartData = [];
foreach ($ranking as $i => $r) {
$chartNames[] = ($i + 1) . '.' . $r['name'];
$chartData[] = $r['count'];
}
return [
'order_type' => $orderType,
'order_type_name' => self::$orderTypeNames[$orderType],
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
'today_total' => $todayTotal,
'today_amount' => number_format($todayAmount, 2, '.', ''),
'today_ranking' => $todayRanking,
'departments' => $deptData,
'ranking' => $ranking,
'chart' => [
'xAxis' => $chartNames,
'series' => [['name' => '单数', 'data' => $chartData]],
],
];
}
}
@@ -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;
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace app\adminapi\validate\tcm;
use app\common\validate\BaseValidate;
class PrescriptionValidate extends BaseValidate
{
protected $rule = [
'id' => 'require',
'diagnosis_id' => 'require|number',
'appointment_id' => 'number',
'patient_name' => 'require',
'clinical_diagnosis' => 'require',
'herbs' => 'require|array',
];
protected $message = [
'diagnosis_id.require' => '诊单ID不能为空',
'patient_name.require' => '患者姓名不能为空',
'clinical_diagnosis.require' => '临床诊断不能为空',
'herbs.require' => '请添加中药',
];
public function sceneAdd()
{
return $this->only(['diagnosis_id', 'patient_name', 'clinical_diagnosis', 'herbs']);
}
public function sceneDetail()
{
return $this->only(['id']);
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace app\common\model\tcm;
use app\common\model\BaseModel;
/**
* 中医处方单模型
*/
class Prescription extends BaseModel
{
protected $name = 'tcm_prescription';
protected $autoWriteTimestamp = true;
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
protected $deleteTime = 'delete_time';
protected $dateFormat = false;
protected $json = ['herbs'];
protected $jsonAssoc = true;
public function getGenderDescAttr($value, $data)
{
return ($data['gender'] ?? 0) == 1 ? '男' : '女';
}
}