ai-refactor(flow): 调整抽象流程节点实现和依赖路径
- 修改 AbstractProcessNode 中 ProcessContext 的命名空间引用为 app\flow\context\ProcessContext - 引入 app\flow\vo\CanHandleResult 用于节点处理结果表示 - 更新前置策略执行后对成功状态的判断,改为调用 isSuccess() 方法 - 增加日志记录细节,便于调试策略执行中断时的错误信息 - 优化代码注释,提升代码可读性和维护性
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace app\flow\strategies;
|
||||
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\nodes\ProcessNodeInterface;
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,8 +4,9 @@ namespace app\flow\strategies;
|
||||
|
||||
use app\flow\config\MorningMode;
|
||||
use app\flow\config\MorningWashConfig;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\nodes\ProcessNodeInterface;
|
||||
use app\flow\vo\MorningWashStatus;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
@@ -30,16 +31,17 @@ class MorningWashStrategy extends AbstractStrategy
|
||||
*/
|
||||
protected function doExecute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext
|
||||
{
|
||||
$isEnd = $context->currentStep == '结束';
|
||||
$isEnd = $context->getCurrentStep() == '结束';
|
||||
$needMorning = $this->checkNeedMorningWash($context) && $isEnd;
|
||||
$context->needMorningWash = $needMorning;
|
||||
|
||||
$morningWash = new MorningWashStatus(
|
||||
needMorningWash: $needMorning,
|
||||
morningWashed: !$needMorning,
|
||||
startTime: $context->getMorningWash()->startTime,
|
||||
todayWashRecords: $context->getMorningWash()->todayWashRecords
|
||||
);
|
||||
|
||||
// 如果不需要晨洗,标记为已完成
|
||||
if (!$needMorning) {
|
||||
$context->morningWashed = true;
|
||||
}
|
||||
|
||||
return $context;
|
||||
return $context->builder()->withMorningWash($morningWash)->build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,9 +64,9 @@ class MorningWashStrategy extends AbstractStrategy
|
||||
*/
|
||||
protected function checkByStorageTime(ProcessContext $context): bool
|
||||
{
|
||||
$storageInTime = $context->storageInTime;
|
||||
$lastActionType = $context->previousAction->action_type_name;
|
||||
$lastProcessName = $context->previousAction->process_name;
|
||||
$storageInTime = $context->getStorage()->inTime;
|
||||
$lastActionType = $context->getPreviousAction()?->action_type_name;
|
||||
$lastProcessName = $context->getPreviousAction()?->process_name;
|
||||
|
||||
// 如果最后一次操作是存储且已取出
|
||||
if ($lastActionType === '存储' && $lastProcessName === '内镜取出') {
|
||||
@@ -94,7 +96,7 @@ class MorningWashStrategy extends AbstractStrategy
|
||||
*/
|
||||
protected function hasWashRecordToday(ProcessContext $context): bool
|
||||
{
|
||||
return $context->todayWashRecords === 0;
|
||||
return $context->getMorningWash()->todayWashRecords === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +105,7 @@ class MorningWashStrategy extends AbstractStrategy
|
||||
protected function checkBySpecificTypes(ProcessContext $context): bool
|
||||
{
|
||||
$specificTypes = $this->morningWashConfig->getExpand('specific_types', []);
|
||||
return in_array($context->endoscopeType, $specificTypes);
|
||||
return in_array($context->getEndoscope()->type, $specificTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace app\flow\strategies;
|
||||
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\nodes\ProcessNodeInterface;
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,9 +4,9 @@ namespace app\flow\strategies;
|
||||
|
||||
use app\config\Config;
|
||||
use app\flow\config\TimeValidationConfig;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\nodes\ProcessNodeInterface;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\flow\enum\VoiceMessage;
|
||||
use app\repository\ProcessDurationRepository;
|
||||
use app\utils\Logger;
|
||||
|
||||
@@ -63,8 +63,8 @@ class TimeValidationStrategy extends AbstractStrategy
|
||||
protected function doExecute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext
|
||||
{
|
||||
$stepCode = $node->getCode();
|
||||
$currentStep = $context->currentStep;
|
||||
$processType = $context->processType;
|
||||
$currentStep = $context->getCurrentStep();
|
||||
$processType = $context->getProcessType();
|
||||
|
||||
Logger::debug("开始执行时间验证策略,步骤:{$stepCode},流程类型:{$processType}");
|
||||
$configDuration = $this->timeValidationConfig->getDuration($stepCode,$processType);
|
||||
@@ -76,9 +76,9 @@ class TimeValidationStrategy extends AbstractStrategy
|
||||
} else {
|
||||
Logger::debug("步骤:{$stepCode},流程类型:{$processType},无配置时长,从数据库查询");
|
||||
// 从数据库按流程类型精确查询
|
||||
$requiredDuration = $this->getDurationFromDb($stepCode, $context->processType);
|
||||
$requiredDuration = $this->getDurationFromDb($stepCode, $context->getProcessType());
|
||||
if ($requiredDuration > 0) {
|
||||
$context->setStepDuration($stepCode, $requiredDuration);
|
||||
$context = $context->builder()->withStepDuration($stepCode, $requiredDuration)->build();
|
||||
} else {
|
||||
// 最后使用上下文已缓存值
|
||||
$requiredDuration = $context->getStepDuration($stepCode);
|
||||
@@ -91,7 +91,7 @@ class TimeValidationStrategy extends AbstractStrategy
|
||||
}
|
||||
|
||||
// 获取上次该步骤的操作时间
|
||||
$duration = $context->duration;
|
||||
$duration = $context->getDuration();
|
||||
|
||||
// 没有上次记录(第一次操作),允许通过
|
||||
if (empty($duration)) {
|
||||
@@ -105,7 +105,7 @@ class TimeValidationStrategy extends AbstractStrategy
|
||||
$voice = VoiceMessage::NOT_ENOUGH_TIME->value;
|
||||
$voice = str_replace('{step}', $stepCode, $voice);
|
||||
$voice = str_replace('{time}', $requiredDuration - $duration, $voice);
|
||||
$context->setCustomError($voice);
|
||||
$context = $context->builder()->customError($voice)->build();
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
@@ -119,8 +119,8 @@ class TimeValidationStrategy extends AbstractStrategy
|
||||
*/
|
||||
public function isApplicable(ProcessContext $context, ProcessNodeInterface $node): bool
|
||||
{
|
||||
if ($context->currentStep != $context->previousAction->process_name) return false;
|
||||
if (!$this->timeValidationConfig->hasStep($node->getCode(), $context->processType)) return false;
|
||||
if ($context->getCurrentStep() != $context->getPreviousAction()?->process_name) return false;
|
||||
if (!$this->timeValidationConfig->hasStep($node->getCode(), $context->getProcessType())) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace app\flow\strategies;
|
||||
|
||||
use app\flow\config\VoiceTemplatesConfig;
|
||||
use app\flow\exception\IllegalUsageException;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\nodes\ProcessNodeInterface;
|
||||
use app\utils\Logger;
|
||||
|
||||
@@ -31,22 +31,21 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
protected function doExecute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext
|
||||
{
|
||||
// 如果存在流程中自定义语音,就直接输出
|
||||
if (!empty($context->voiceMessage)) {
|
||||
if (!empty($context->getVoice()->message)) {
|
||||
Logger::warn(
|
||||
"流程中存在自定义语音或存在多次设置语音,语音应该在 VoiceGenerationStrategy 策略中配置,否则不能拦截与自定义配置",
|
||||
new IllegalUsageException("In the existing process, there is custom voice, which should be configured in the VoiceGenerationStrategy strategy; otherwise, it cannot be intercepted and customized")
|
||||
);
|
||||
$context->setVoice($context->voiceMessage);
|
||||
return $context;
|
||||
return $context->builder()->voiceMessage($context->getVoice()->message)->build();
|
||||
}
|
||||
|
||||
// 如果已经有错误,生成错误语音
|
||||
// 否则生成正常流程语音
|
||||
$voice = !$context->success ? $this->generateErrorVoice($context) : $this->generateNormalVoice($context, $node);
|
||||
$voice = !$context->isSuccess() ? $this->generateErrorVoice($context) : $this->generateNormalVoice($context, $node);
|
||||
|
||||
|
||||
// 应用语音模板
|
||||
foreach ($context->voiceTemplateParams as $key => $val) {
|
||||
foreach ($context->getVoice()->templateParams as $key => $val) {
|
||||
$replaceVal = match (true) {
|
||||
is_array($val) => implode(',', $val),
|
||||
is_bool($val) => $val ? 'true' : 'false',
|
||||
@@ -56,9 +55,8 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
$voice = str_replace('{' . $key . '}', $replaceVal, $voice);
|
||||
}
|
||||
Logger::debug("应用语音模板后的内容: {$voice}");
|
||||
$context->setVoice($voice);
|
||||
|
||||
return $context;
|
||||
return $context->builder()->voiceMessage($voice)->build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +66,7 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
{
|
||||
$stepCode = $node->getCode();
|
||||
$stepName = $node->getName();
|
||||
$processType = $context->processType;
|
||||
$processType = $context->getProcessType();
|
||||
|
||||
// 根据流程类型选择模板
|
||||
$templateKey = $this->getTemplateKey($processType, $stepCode, $context);
|
||||
@@ -94,12 +92,12 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
*/
|
||||
protected function generateErrorVoice(ProcessContext $context): string
|
||||
{
|
||||
$errorMessage = $context->errorMessage;
|
||||
$errorMessage = $context->getVoice()->errorMessage;
|
||||
$errorTemplates = $this->voiceTemplatesConfig->voiceMessage;
|
||||
|
||||
$errorMsg = $errorTemplates[$errorMessage->name] ?? $errorMessage->value;
|
||||
if (empty($errorMsg)) {
|
||||
$errorMsg = $context->voiceMessage;
|
||||
$errorMsg = $context->getVoice()->message;
|
||||
Logger::debug("错误信息配置未命中,使用自定义语音: {$errorMsg}");
|
||||
} else {
|
||||
Logger::debug("错误信息配置命中: {$errorMsg}");
|
||||
@@ -129,7 +127,7 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
protected function getTemplateKey(string $processType, string $stepCode, ProcessContext $context): string
|
||||
{
|
||||
// 晨洗流程中的清洗/机洗步骤
|
||||
if ($context->needMorningWash) {
|
||||
if ($context->getMorningWash()->needMorningWash) {
|
||||
return 'morning_wash';
|
||||
}
|
||||
|
||||
@@ -144,11 +142,11 @@ class VoiceGenerationStrategy extends AbstractStrategy
|
||||
{
|
||||
$reminds = [];
|
||||
|
||||
if ($context->needLeakTestRemind) {
|
||||
if ($context->needLeakTestRemind()) {
|
||||
$reminds[] = ',清洗开始前,请测漏';
|
||||
}
|
||||
|
||||
if ($context->needStorageRemind) {
|
||||
if ($context->needStorageRemind()) {
|
||||
$reminds[] = ',未登记取出';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user