refactor(context): 重命名上下文构建器及更新相关方法命名

- 将 ProcessContext 中的 builder() 重命名为 createModifyBuilder() 并添加调用栈日志
- ProcessContextBuilder 中所有 with* 方法统一改为 set* 命名风格
- Flow 各节点及流程处理器中调用 builder() 替换为 createModifyBuilder()
- 虚拟测试环境相关 ContextBuilder 中方法同步改名保持一致
- 优化流程节点中上下文修改代码调用,提升代码规范性
- 添加Config中加载自定义流程配置的占位注释及代码
- 无功能改动,纯代码风格及命名规范调整
This commit is contained in:
zimoyin
2026-03-13 18:51:02 +08:00
parent 8e617d06bc
commit 177c3ae9b2
27 changed files with 176 additions and 163 deletions
+2 -2
View File
@@ -245,7 +245,7 @@ class VirtualContextBuilder
/**
* 设置操作时长(秒)
*/
public function duration(int $seconds): self
public function setDuration(int $seconds): self
{
$this->processStatus = new ProcessStatus(
currentStep: $this->processStatus->currentStep,
@@ -277,7 +277,7 @@ class VirtualContextBuilder
/**
* 设置上一条操作记录(虚拟)
*/
public function previousAction(string $processName, string $batchNo = '', ?string $opEndtime = null): self
public function setPreviousAction(string $processName, string $batchNo = '', ?string $opEndtime = null): self
{
$action = new EctActions();
$action->process_name = $processName;
+48 -46
View File
@@ -12,7 +12,7 @@ use app\utils\Logger;
/**
* 虚拟流程处理器
*
*
* 用于测试环境的刷卡流程模拟器。
* 不依赖真实的 FlowProcessor,不连接数据库,不发送语音。
* 支持完整的刷卡流程模拟和验证。
@@ -22,19 +22,19 @@ class VirtualityFlowProcessor
private ProcessEngine $engine;
private array $environment;
private string $envPath;
/** @var array 当前操作员会话缓存 (readerId => operatorInfo) */
private array $operatorSessions = [];
/** @var ProcessContext[] 执行历史 */
private array $history = [];
/** @var array 当前内镜的流程状态 (endoscopeId => ProcessContext) */
private array $endoscopeStates = [];
/**
* 构造函数
*
*
* @param ProcessConfig|null $config 流程配置
* @param string|null $envPath 环境配置文件路径
*/
@@ -53,10 +53,10 @@ class VirtualityFlowProcessor
if (!file_exists($this->envPath)) {
throw new \RuntimeException("环境配置文件不存在: {$this->envPath}");
}
$content = file_get_contents($this->envPath);
$this->environment = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException("环境配置文件解析失败: " . json_last_error_msg());
}
@@ -100,21 +100,21 @@ class VirtualityFlowProcessor
/**
* 模拟刷人员卡
*
*
* @param string $operatorName 操作员名称(环境配置中定义)
* @param string $readerType 读卡器类型
* @return ProcessContext
*/
public function swipeOperatorCard(string $operatorName, string $readerType = '清洗'): ProcessContext
{
$operatorData = $this->environment['operators'][$operatorName]
$operatorData = $this->environment['operators'][$operatorName]
?? throw new \InvalidArgumentException("未找到操作员: {$operatorName}");
$readerData = $this->environment['readers'][$readerType]
$readerData = $this->environment['readers'][$readerType]
?? throw new \InvalidArgumentException("未找到读卡器: {$readerType}");
// 保存操作员会话
$this->operatorSessions[$readerData['id']] = $operatorData;
// 构建上下文
$context = VirtualContextBuilder::create($this->envPath)
->operator($operatorName)
@@ -122,92 +122,94 @@ class VirtualityFlowProcessor
->asOperatorCard()
->withEngineConfig($this->engine->getConfig())
->build();
// 人员卡不走流程链,直接返回
$result = $context->builder()
->voiceMessage('请刷内镜卡')
$result = $context->createModifyBuilder()
->setVoiceMessage('请刷内镜卡')
->build();
$this->history[] = $result;
return $result;
}
/**
* 模拟刷内镜卡
*
*
* @param string $endoscopeName 内镜名称(环境配置中定义)
* @param string $readerType 读卡器类型
* @return ProcessContext
*/
public function swipeEndoscopeCard(string $endoscopeName, string $readerType): ProcessContext
{
$readerData = $this->environment['readers'][$readerType]
$readerData = $this->environment['readers'][$readerType]
?? throw new \InvalidArgumentException("未找到读卡器: {$readerType}");
$endoscopeData = $this->environment['endoscopes'][$endoscopeName]
$endoscopeData = $this->environment['endoscopes'][$endoscopeName]
?? throw new \InvalidArgumentException("未找到内镜: {$endoscopeName}");
// 获取内镜当前状态
/** @var ProcessContext $currentState */
$currentState = $this->endoscopeStates[$endoscopeData['id']] ?? null;
// 构建上下文
$builder = VirtualContextBuilder::create($this->envPath)
->endoscope($endoscopeName)
->reader($readerType)
->withEngineConfig($this->engine->getConfig());
// 如果有操作员会话,添加操作员信息
if (isset($this->operatorSessions[$readerData['id']])) {
$opData = $this->operatorSessions[$readerData['id']];
$builder->customOperator($opData['id'], $opData['name'], $opData['rfid']);
}
// 继承之前的流程状态
if ($currentState !== null) {
$builder->currentStep($currentState->getCurrentStep())
->batchNo($currentState->getBatchNo())
->processType($currentState->getProcessType());
->batchNo($currentState->getBatchNo())
->setDuration(time() - strtotime($currentState->getActionStartTime()))
->processType($currentState->getProcessType());
// 设置 previousAction:上一步骤名称就是当前状态的 currentStep
if (!empty($currentState->getCurrentStep())) {
$builder->previousAction($currentState->getCurrentStep(), $currentState->getBatchNo());
$builder->setPreviousAction($currentState->getCurrentStep(), $currentState->getBatchNo());
}
} else {
// 新流程
$builder->newProcess();
}
$context = $builder->build();
// 未刷人员卡检查
if (!$context->hasOperator() && !isset($this->operatorSessions[$readerData['id']])) {
$result = $context->builder()
$result = $context->createModifyBuilder()
->error(\app\flow\enum\VoiceMessage::PLEASE_SWIPE_OPERATOR)
->build();
$this->history[] = $result;
return $result;
}
// 执行流程
$result = $this->engine->execute($context);
// 确保 previousAction 在结果中正确设置
// 如果执行成功且有上一步骤信息,确保 previousAction 被保留
if ($result->isSuccess() && $context->getPreviousAction() !== null && $result->getPreviousAction() === null) {
$result = $result->builder()
->withPreviousAction($context->getPreviousAction())
$result = $result->createModifyBuilder()
->setPreviousAction($context->getPreviousAction())
->build();
}
// 保存状态
$this->endoscopeStates[$endoscopeData['id']] = $result;
$this->history[] = $result;
return $result;
}
/**
* 快捷方法:完整刷卡(先刷人员卡再刷内镜卡)
*
*
* @param string $operatorName 操作员名称
* @param string $endoscopeName 内镜名称
* @param string $readerType 读卡器类型
@@ -220,23 +222,23 @@ class VirtualityFlowProcessor
$result = $this->swipeOperatorCard($operatorName, $readerType);
// 当前语音
Logger::info("当前语音:{$result->getFullVoice()}");
// 刷内镜卡
Logger::info("测试刷卡:{$endoscopeName}, {$readerType}");
$result = $this->swipeEndoscopeCard($endoscopeName, $readerType);
$result = $this->swipeEndoscopeCard($endoscopeName, $readerType);
Logger::info("当前流程:{}", [$result->getProcessType()]);
Logger::info("当前批次号:{}", [$result->getBatchNo()]);
Logger::info("当前步骤:{}", [$result->getCurrentStep()]);
Logger::info("上一个步骤类型:{}", [$result->getPreviousAction()->action_type_name?? "null"]);
Logger::info("上一个步骤:{}", [$result->getPreviousAction()->process_name??"null"]);
Logger::info("时长:{}", [$result->getDuration()?? "null"]);
Logger::info("上一个步骤类型:{}", [$result->getPreviousAction()->action_type_name ?? "null"]);
Logger::info("上一个步骤:{}", [$result->getPreviousAction()->process_name ?? "null"]);
Logger::info("时长:{}", [$result->getDuration() ?? "null"]);
Logger::info("当前语音:{$result->getFullVoice()}\n");
return $result;
}
/**
* 执行完整的手工洗流程
*
*
* @param string $operatorName 操作员名称
* @param string $endoscopeName 内镜名称
* @return array<string, ProcessContext> 每个步骤的执行结果
@@ -245,17 +247,17 @@ class VirtualityFlowProcessor
{
$steps = ['清洗', '漂洗', '消毒', '终末漂洗', '干燥'];
$results = [];
foreach ($steps as $step) {
$results[$step] = $this->swipe($operatorName, $endoscopeName, $step);
}
return $results;
}
/**
* 执行完整的机洗流程
*
*
* @param string $operatorName 操作员名称
* @param string $endoscopeName 内镜名称
* @return ProcessContext