Merge branch 'master' into kpi-statis

This commit is contained in:
2026-04-24 12:19:04 +08:00
289 changed files with 1242 additions and 429 deletions
@@ -280,4 +280,61 @@ class DeptLogic extends BaseLogic
return self::getTree($data, $pid);
}
/**
* 指定部门及其全部下级部门 id(含自身)。筛选时选父级可匹配子级下成员(admin_dept.dept_id)。
*
* @return array<int>
*/
public static function getSelfAndDescendantIds(int $rootDeptId): array
{
if ($rootDeptId <= 0) {
return [];
}
$rows = Dept::field(['id', 'pid'])->select()->toArray();
if ($rows === []) {
return [$rootDeptId];
}
$validIds = [];
foreach ($rows as $r) {
$id = (int) ($r['id'] ?? 0);
if ($id > 0) {
$validIds[$id] = true;
}
}
if (!isset($validIds[$rootDeptId])) {
return [$rootDeptId];
}
$childrenByPid = [];
foreach ($rows as $r) {
$pid = (int) ($r['pid'] ?? 0);
$id = (int) ($r['id'] ?? 0);
if ($id <= 0) {
continue;
}
if (!isset($childrenByPid[$pid])) {
$childrenByPid[$pid] = [];
}
$childrenByPid[$pid][] = $id;
}
$out = [];
$queue = [$rootDeptId];
$seen = [];
while ($queue !== []) {
$id = array_shift($queue);
if (isset($seen[$id])) {
continue;
}
$seen[$id] = true;
$out[] = $id;
foreach ($childrenByPid[$id] ?? [] as $cid) {
$cid = (int) $cid;
if ($cid > 0 && !isset($seen[$cid])) {
$queue[] = $cid;
}
}
}
return $out;
}
}
@@ -92,7 +92,16 @@ class DiagnosisLogic extends BaseLogic
if (isset($params['diagnosis_date'])) {
$params['diagnosis_date'] = strtotime($params['diagnosis_date']);
}
// 表未增加 admin_id 列时勿写入,避免 SQL 报错(见 sql/1.9.20260421/add_tcm_diagnosis_admin_id.sql
// 注意:Model::getConnection() 在 think-orm 中返回连接名(string),须用 Db 查询对象取字段
if (array_key_exists('admin_id', $params)) {
$fieldNames = Db::name('tcm_diagnosis')->getTableFields();
if (! is_array($fieldNames) || ! in_array('admin_id', $fieldNames, true)) {
unset($params['admin_id']);
}
}
$model = Diagnosis::create($params);
// 自动为患者创建 TRTC 账号
@@ -13,6 +13,8 @@ use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\PrescriptionOrderLog;
use app\common\model\tcm\PrescriptionOrderPayOrder;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminDept;
use app\common\model\dept\Dept;
use app\common\service\ExpressTrackService;
use app\common\service\gancao\GancaoScmRecipelService;
use think\facade\Config;
@@ -32,6 +34,38 @@ class PrescriptionOrderLogic
return self::$error;
}
/**
* 部门自底向上链式名称,含父级(如:总部 / 华东 / 上海门诊)
*/
private static function buildDeptPath(int $deptId): string
{
if ($deptId <= 0) {
return '';
}
$names = [];
$id = $deptId;
for ($i = 0; $i < 50; $i++) {
$row = Dept::where('id', $id)->whereNull('delete_time')->field(['id', 'name', 'pid'])->find();
if ($row === null) {
break;
}
$data = $row->toArray();
array_unshift($names, (string) ($data['name'] ?? ''));
$pid = (int) ($data['pid'] ?? 0);
if ($pid <= 0) {
break;
}
$id = $pid;
}
if ($names === []) {
return '';
}
$names = array_filter($names, static fn (string $n): bool => $n !== '');
return implode(' / ', $names);
}
/**
* @param int[] $allowRoleIds
*/
@@ -607,6 +641,54 @@ class PrescriptionOrderLogic
unset($arr['prescription']['herbs']);
}
// 诊单医助 / 诊单创建人 及部门(多部门、含父级路径用「;」与「 / 」分隔);创建人为 tcm_diagnosis.admin_id
$arr['assistant_id'] = 0;
$arr['assistant_name'] = '';
$arr['assistant_dept_path'] = '';
$arr['diagnosis_creator_id'] = 0;
$arr['diagnosis_creator_name'] = '';
$arr['diagnosis_creator_dept_path'] = '';
$diagIdForMeta = (int) ($arr['diagnosis_id'] ?? 0);
if ($diagIdForMeta > 0) {
// 勿在 field 中写 admin_id:未执行建表补充列时会报 Unknown column
$dg = Diagnosis::where('id', $diagIdForMeta)->whereNull('delete_time')->find();
if ($dg !== null) {
$drow = $dg->toArray();
$astId = (int) ($drow['assistant_id'] ?? 0);
$arr['assistant_id'] = $astId;
if ($astId > 0) {
$an = Admin::where('id', $astId)->whereNull('delete_time')->value('name');
$arr['assistant_name'] = $an !== null && (string) $an !== '' ? (string) $an : '';
$deptIds = AdminDept::where('admin_id', $astId)->column('dept_id');
$pathSegments = [];
foreach ($deptIds as $did) {
$p = self::buildDeptPath((int) $did);
if ($p !== '') {
$pathSegments[] = $p;
}
}
$pathSegments = array_values(array_unique($pathSegments));
$arr['assistant_dept_path'] = $pathSegments === [] ? '' : implode('', $pathSegments);
}
$creId = (int) ($drow['admin_id'] ?? 0);
$arr['diagnosis_creator_id'] = $creId;
if ($creId > 0) {
$cn = Admin::where('id', $creId)->whereNull('delete_time')->value('name');
$arr['diagnosis_creator_name'] = $cn !== null && (string) $cn !== '' ? (string) $cn : '';
$deptIdsC = AdminDept::where('admin_id', $creId)->column('dept_id');
$pathSegmentsC = [];
foreach ($deptIdsC as $didC) {
$pc = self::buildDeptPath((int) $didC);
if ($pc !== '') {
$pathSegmentsC[] = $pc;
}
}
$pathSegmentsC = array_values(array_unique($pathSegmentsC));
$arr['diagnosis_creator_dept_path'] = $pathSegmentsC === [] ? '' : implode('', $pathSegmentsC);
}
}
}
return $arr;
}