This commit is contained in:
Your Name
2026-07-17 12:13:02 +08:00
parent 97fefbae8f
commit 4f95d9182e
4 changed files with 130 additions and 15 deletions
@@ -39,6 +39,7 @@ use think\facade\Log;
*
* 日志:无论分配与否,每条待指派诊单都会写入 tcm_diagnosis_auto_assign_log,记录原因(为什么分配 / 为什么不分配)。
* 成功分配同时写 tcm_diagnosis_assign_logis_inherit=0,计入次月接诊率分母),与手动指派同口径。
* 写入时 from_assistant_id 尽量带上「一中心」原医助(指派日志中最近的一中心身份,否则回退业务单创建人若属一中心),避免原医助恒为空。
*/
class AutoAssignPendingDiagnosis extends Command
{
@@ -499,9 +500,12 @@ class AutoAssignPendingDiagnosis extends Command
$relatedPoCreatorId = 0;
}
// 自动分给二中心时,原医助带上一中心身份(列表「原医助」不再恒为空)
$fromAssistantId = $this->resolveYiCenterPreviousAssistantId($diagnosisId, $relatedPoCreatorId);
Db::name('tcm_diagnosis_assign_log')->insert([
'diagnosis_id' => $diagnosisId,
'from_assistant_id' => 0,
'from_assistant_id' => $fromAssistantId,
'to_assistant_id' => $toAssistantId,
'operator_admin_id' => 0,
'operator_name' => '系统自动分配',
@@ -524,6 +528,88 @@ class AutoAssignPendingDiagnosis extends Command
}
}
/**
* 解析诊单对应的「一中心」原医助 id,供自动指派写入 from_assistant_id。
* 优先:指派日志由新到旧,取最近出现的一中心身份(先 to 后 from);
* 回退:最新业务单创建人若属一中心则用之。跳过二中心身份,避免原医助写成上一任二中心。
*/
private function resolveYiCenterPreviousAssistantId(int $diagnosisId, int $relatedPoCreatorId): int
{
if ($diagnosisId <= 0) {
return 0;
}
$yiDeptSet = DeptLogic::getYiCenterSubtreeDeptIdSet();
if ($yiDeptSet === []) {
return 0;
}
$logs = Db::name('tcm_diagnosis_assign_log')
->where('diagnosis_id', $diagnosisId)
->order('id', 'desc')
->field(['from_assistant_id', 'to_assistant_id'])
->select()
->toArray();
$candidateIds = [];
foreach ($logs as $log) {
$to = (int) ($log['to_assistant_id'] ?? 0);
$from = (int) ($log['from_assistant_id'] ?? 0);
if ($to > 0) {
$candidateIds[] = $to;
}
if ($from > 0) {
$candidateIds[] = $from;
}
}
if ($relatedPoCreatorId > 0) {
$candidateIds[] = $relatedPoCreatorId;
}
$candidateIds = array_values(array_unique(array_filter($candidateIds)));
if ($candidateIds === []) {
return 0;
}
$yiAdminSet = $this->filterAdminIdsInDeptSet($candidateIds, $yiDeptSet);
if ($yiAdminSet === []) {
return 0;
}
foreach ($logs as $log) {
$to = (int) ($log['to_assistant_id'] ?? 0);
if ($to > 0 && isset($yiAdminSet[$to])) {
return $to;
}
$from = (int) ($log['from_assistant_id'] ?? 0);
if ($from > 0 && isset($yiAdminSet[$from])) {
return $from;
}
}
if ($relatedPoCreatorId > 0 && isset($yiAdminSet[$relatedPoCreatorId])) {
return $relatedPoCreatorId;
}
return 0;
}
/**
* @param list<int> $adminIds
* @param array<int, true> $deptIdSet
*
* @return array<int, true> admin_id => true
*/
private function filterAdminIdsInDeptSet(array $adminIds, array $deptIdSet): array
{
if ($adminIds === [] || $deptIdSet === []) {
return [];
}
$rows = Db::name('admin_dept')
->whereIn('admin_id', $adminIds)
->whereIn('dept_id', array_keys($deptIdSet))
->column('admin_id');
return array_fill_keys(array_map('intval', $rows), true);
}
/**
* @param array<string,mixed> $diag 诊单行(含 id/patient_name/phone
* @param array<string,mixed> $extra action/assistant_id/assistant_name/tier/visit2_rate/round_no/reason