diff --git a/server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php b/server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php index e9d61061..444b28a8 100644 --- a/server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php +++ b/server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php @@ -1547,7 +1547,15 @@ class PrescriptionOrderLogic return false; } - self::writeLog($id, $adminId, $adminInfo, 'complete', '订单完成,状态变更为「已完成」,实付金额更新为 ¥' . $linkedPayPaidTotal); + $unassignSummary = self::clearDiagnosisAssistantOnComplete((int) $order->diagnosis_id); + + self::writeLog( + $id, + $adminId, + $adminInfo, + 'complete', + '订单完成,状态变更为「已完成」,实付金额更新为 ¥' . $linkedPayPaidTotal . $unassignSummary + ); $out = $order->toArray(); self::maskInternalCostIfNeeded($out, $adminInfo); @@ -1556,6 +1564,50 @@ class PrescriptionOrderLogic return $out; } + /** + * 完成订单时把关联诊单的 assistant_id 清空,让患者回到「待分配」状态 + * 仅当该患者没有其它进行中的处方订单时才清空,避免误覆盖 + * + * @return string 追加到操作日志的描述(无变更则返回空串) + */ + private static function clearDiagnosisAssistantOnComplete(int $diagnosisId): string + { + if ($diagnosisId <= 0) { + return ''; + } + + try { + $diag = Diagnosis::where('id', $diagnosisId)->whereNull('delete_time')->find(); + if (!$diag) { + return ''; + } + $oldAssistant = (string) ($diag['assistant_id'] ?? ''); + if ($oldAssistant === '' || $oldAssistant === '0') { + return ''; + } + + // 同一诊单下若仍有未完成/未取消的处方订单,则不清空 + $pendingCount = PrescriptionOrder::where('diagnosis_id', $diagnosisId) + ->whereNull('delete_time') + ->whereNotIn('fulfillment_status', [3, 4]) + ->count(); + if ($pendingCount > 0) { + return ''; + } + + Diagnosis::where('id', $diagnosisId) + ->whereNull('delete_time') + ->update(['assistant_id' => '']); + + return ';并已清空诊单医助分配(原 assistant_id=' . $oldAssistant . '),患者进入待分配状态'; + } catch (\Throwable $e) { + Log::warning('clearDiagnosisAssistantOnComplete failed: ' . $e->getMessage(), [ + 'diagnosis_id' => $diagnosisId, + ]); + return ''; + } + } + /** * 列表行是否展示「上传甘草药方」(需配置甘草、处方审核通过、未上传过、有权限;不要求支付审核)。 * diff --git a/server/config/console.php b/server/config/console.php index 0e803913..6e5036a2 100644 --- a/server/config/console.php +++ b/server/config/console.php @@ -24,5 +24,7 @@ return [ 'express:sync' => 'app\\command\\SyncTrackingNumbers', // 企业微信客户同步 'qywx:sync-customer' => 'app\\command\\QywxSyncCustomer', + // 甘草订单物流路由同步(GET_TASK_ROUTE_LIST) + 'gancao:sync-logistics' => 'app\\command\\GancaoSyncLogisticsRoute', ], ];