feat: 实现TCP Server
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\config\StepConfig;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\strategies\ProcessStrategyInterface;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 流程节点抽象基类
|
||||
* 实现责任链的基础逻辑
|
||||
* 如果要新增标准流程的节点,请继承本类并实现抽象方法。并且需要配置 ProcessConfig 。无论是运行时新增节点还是静态修改节点配置,都会在运行时生效
|
||||
*/
|
||||
abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
{
|
||||
/**
|
||||
* 下一个处理节点
|
||||
*/
|
||||
protected ?ProcessNodeInterface $next = null;
|
||||
|
||||
/**
|
||||
* 是否启用
|
||||
*/
|
||||
protected bool $enabled = true;
|
||||
|
||||
/**
|
||||
* 策略列表
|
||||
* @var ProcessStrategyInterface[]
|
||||
*/
|
||||
protected array $strategies = [];
|
||||
|
||||
/**
|
||||
* 节点配置
|
||||
*/
|
||||
protected ?StepConfig $config = null;
|
||||
|
||||
/**
|
||||
* 设置下一个节点
|
||||
*/
|
||||
public function setNext(ProcessNodeInterface $next): ProcessNodeInterface
|
||||
{
|
||||
$this->next = $next;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一个节点
|
||||
*/
|
||||
public function getNext(): ?ProcessNodeInterface
|
||||
{
|
||||
return $this->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从当前节点开始处理流程链
|
||||
*/
|
||||
public function handle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 如果节点被禁用,直接传递给下一个节点
|
||||
if (!$this->isEnabled()) {
|
||||
Logger::debug('[{}-Node] 节点已禁用,跳过', [$this->getCode()]);
|
||||
return $this->passToNext($context);
|
||||
}
|
||||
|
||||
// 执行前置策略
|
||||
$context = $this->executeBeforeStrategies($context);
|
||||
|
||||
// 如果前置策略返回错误,不再继续
|
||||
if (!$context->success) {
|
||||
Logger::debug('[{}-Node] 前置策略拦截 error={}', [
|
||||
$this->getCode(),
|
||||
$context->errorMessage,
|
||||
]);
|
||||
return $context;
|
||||
}
|
||||
|
||||
|
||||
// 如果不能处理当前步骤,传递给下一个节点
|
||||
if (!$this->canHandle($context)) {
|
||||
Logger::debug('[{}-Node] 不能处理当前步骤,跳过', [$this->getCode()]);
|
||||
return $this->passToNext($context);
|
||||
}
|
||||
|
||||
// 输出当前节点
|
||||
Logger::debug('[{}-Node] 开始处理 step={} batch={}', [
|
||||
$this->getCode(),
|
||||
$context->currentStep,
|
||||
$context->batchNo ?: '-',
|
||||
]);
|
||||
|
||||
|
||||
// 执行节点具体处理逻辑
|
||||
$context = $this->doHandle($context);
|
||||
|
||||
Logger::debug('[{}-Node] 处理完成 step={} batch={} success={}', [
|
||||
$this->getCode(),
|
||||
$context->currentStep,
|
||||
$context->batchNo ?: '-',
|
||||
$context->success,
|
||||
]);
|
||||
|
||||
// 执行后置策略
|
||||
$context = $this->executeAfterStrategies($context);
|
||||
// 后置策略拦截
|
||||
if (!$context->success) {
|
||||
Logger::debug('[{}-Node] 后置策略拦截 error={}', [
|
||||
$this->getCode(),
|
||||
$context->errorMessage,
|
||||
]);
|
||||
return $context;
|
||||
}
|
||||
|
||||
$nextNode = $this->getNext();
|
||||
// 跳过节点逻辑
|
||||
for ($i = 0; $i < $context->skipNodeCount; $i++) {
|
||||
Logger::debug('[{}-Node] 跳过节点 code={}', [$this->getCode(), $nextNode->getCode()]);
|
||||
$nextNode = $nextNode->getNext();
|
||||
}
|
||||
|
||||
// 传递给下一个节点
|
||||
return empty($nextNode) ? $context : $nextNode->handle($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array 需要的前置节点列表
|
||||
*/
|
||||
public function getRequiredNodes($default = []): array
|
||||
{
|
||||
return (!empty($this->getConfig()->required)) ? $this->getConfig()->required : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是需要的前置节点
|
||||
* @param string $currentStep
|
||||
* @param array $default
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequiredNode(string $currentStep, array $default = []): bool
|
||||
{
|
||||
return in_array($currentStep, $this->getRequiredNodes($default));
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前刷的读卡器类型,是否和当前节点的配置匹配
|
||||
*/
|
||||
public function isMatchReaderType(ProcessContext $context): bool
|
||||
{
|
||||
return $this->getCode() === $context->readerType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传递给下一个节点
|
||||
*/
|
||||
protected function passToNext(ProcessContext $context): ProcessContext
|
||||
{
|
||||
if ($this->next !== null) {
|
||||
return $this->next->handle($context);
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止传递给下一个节点
|
||||
*/
|
||||
protected function stopNext(ProcessContext $context): ProcessContext
|
||||
{
|
||||
$context->skipNodeCount = count($this->getRemainingNodes());
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取剩余节点
|
||||
*/
|
||||
protected function getRemainingNodes(): array
|
||||
{
|
||||
$remainingNodes = [];
|
||||
$node = $this->next;
|
||||
while ($node !== null) {
|
||||
$remainingNodes[] = $node;
|
||||
$node = $node->getNext();
|
||||
}
|
||||
return $remainingNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行前置策略
|
||||
*
|
||||
* 在节点核心逻辑执行之前,按顺序执行所有标记为 'before' 阶段的策略
|
||||
*
|
||||
* 执行流程:
|
||||
* 1. 遍历所有已注册的策略
|
||||
* 2. 筛选出阶段为 'before' 的策略
|
||||
* 3. 依次执行策略的 execute 方法
|
||||
* 4. 如果某个策略导致上下文错误(!$context->isSuccess()),立即中断后续策略执行
|
||||
*
|
||||
* 前置策略的应用:
|
||||
* - 时间验证:检查步骤执行时间是否符合要求
|
||||
* - 权限检查:验证操作员是否有权限执行该步骤
|
||||
* - 状态校验:确认流程状态是否允许进入当前步骤
|
||||
* - 数据准备:为节点处理准备必要的数据
|
||||
*
|
||||
* @param ProcessContext $context 流程上下文
|
||||
*
|
||||
* @return ProcessContext 经过策略处理后的上下文
|
||||
* - 如果策略执行成功,返回修改后的上下文
|
||||
* - 如果策略执行失败,返回包含错误信息的上下文
|
||||
*
|
||||
* @see ProcessStrategyInterface::execute() 策略执行接口
|
||||
* @see ProcessStrategyInterface::getPhase() 获取策略执行阶段
|
||||
*/
|
||||
protected function executeBeforeStrategies(ProcessContext $context): ProcessContext
|
||||
{
|
||||
foreach ($this->strategies as $strategy) {
|
||||
if ($strategy->getPhase() === 'before') {
|
||||
$context = $strategy->execute($context, $this);
|
||||
if (!$context->success) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行后置策略
|
||||
*/
|
||||
protected function executeAfterStrategies(ProcessContext $context): ProcessContext
|
||||
{
|
||||
foreach ($this->strategies as $strategy) {
|
||||
if ($strategy->getPhase() === 'after') {
|
||||
Logger::debug('[{}-Node] 执行后置策略 code={}', [$this->getCode(), $strategy::class]);
|
||||
$context = $strategy->execute($context, $this);
|
||||
}
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体的处理逻辑,由子类实现
|
||||
*/
|
||||
abstract protected function doHandle(ProcessContext $context): ProcessContext;
|
||||
|
||||
/**
|
||||
* 是否启用
|
||||
*/
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用
|
||||
*/
|
||||
public function setEnabled(bool $enabled): ProcessNodeInterface
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加策略
|
||||
*/
|
||||
public function addStrategy(ProcessStrategyInterface $strategy): self
|
||||
{
|
||||
$this->strategies[] = $strategy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置
|
||||
*/
|
||||
public function setConfig(StepConfig $config): self
|
||||
{
|
||||
$this->config = $config;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*/
|
||||
public function getConfig(): StepConfig
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
* 默认实现:检查当前步骤是否匹配节点编码
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
return $context->currentStep === $this->getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前对象地址的哈希值
|
||||
*/
|
||||
public function _hash(): string
|
||||
{
|
||||
return spl_object_hash($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
abstract static public function getName(): string;
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
abstract public function getCode(): string;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\repository\EctActionsRepository;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 最后节点,用于扫尾
|
||||
*
|
||||
*/
|
||||
class CloseNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return "Close";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*
|
||||
* 所有需要数据库记录的场景都需要经过本节点检查
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$context->success || $context->needDatabaseOperation || !empty($context->voiceMessage)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑:最后节点处理
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
if (!$context->success || $context->needDatabaseOperation || !empty($context->voiceMessage)) return $context;
|
||||
// 无节点命中
|
||||
Logger::debug('当前刷卡无节点命中 currentStep={} readerType={} expectedNextStep={}', [
|
||||
$context->currentStep ?: '(空)',
|
||||
$context->readerType,
|
||||
$context->expectedNextStep
|
||||
]);
|
||||
// 如果有预期的下一步,则返回错误
|
||||
if (!empty($context->expectedNextStep) && $context->expectedNextStep != VoiceMessage::NONE) {
|
||||
Logger::debug("节点期望: {$context->expectedNextStep->value}");
|
||||
return $context->setError($context->expectedNextStep);
|
||||
}
|
||||
// 异常流程
|
||||
Logger::error("异常流程,所有节点处理完成,无匹配节点并且无预期的下一步");
|
||||
$context->setError(VoiceMessage::UNKNOWN_ERROR);
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
|
||||
/**
|
||||
* 消毒节点
|
||||
* 处理消毒步骤
|
||||
*/
|
||||
class DisinfectNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '消毒';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '消毒';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
if ($context->currentStep === RinseNode::getName()) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_DISINFECT;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上一个步骤必须是漂洗 或者 晨洗
|
||||
if (!$this->isRequiredNode($context->currentStep, [RinseNode::getName(), MorningWashNode::getName()])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 更新步骤
|
||||
$context->currentStep = '消毒';
|
||||
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 干燥节点
|
||||
* 处理干燥步骤
|
||||
*
|
||||
*/
|
||||
class DryNode extends AbstractProcessNode
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return "干燥";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
if ($context->currentStep === FinalRinseNode::getName()) {
|
||||
if (!$context->success) Logger::debug("[DryNode] 刷卡错误,当前步骤是终末漂洗,但是刷的读卡器类型不是终末漂洗,对用户进行语音提示刷终末漂洗读卡器");
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_DRY;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上一个步骤必须是终末漂洗
|
||||
if (!$this->isRequiredNode($context->currentStep, [FinalRinseNode::getName()])) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_DISINFECT;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 更新步骤
|
||||
$context->currentStep = '干燥';
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\repository\EctActionsRepository;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 重复检查节点
|
||||
* 用于检测当前操作是否与历史记录重复
|
||||
*
|
||||
*/
|
||||
class DuplicateCheckNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return "重复检查";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*
|
||||
* 所有需要数据库记录的场景都需要经过本节点检查
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
return $context->previousAction->process_name === $context->readerType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑:检查重复操作
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
$context->setError(VoiceMessage::DUPLICATE_SWIPING);
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
|
||||
/**
|
||||
* 结束节点
|
||||
* 处理流程结束步骤
|
||||
*/
|
||||
class EndNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '结束';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '结束';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
if ($context->currentStep === DryNode::getName() && $context->currentStep === FinalRinseNode::getName()) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_END;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上一个步骤必须是干燥、终末漂洗或机洗
|
||||
$validSteps = ['干燥', '终末漂洗', '机洗'];
|
||||
if ($this->isRequiredNode($context->currentStep, ['干燥', '终末漂洗', '机洗'])) {
|
||||
if ($context->currentStep === FinalRinseNode::getName()) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_WASH;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 更新步骤
|
||||
$context->currentStep = '结束';
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 终末漂洗节点
|
||||
* 处理终末漂洗步骤
|
||||
*
|
||||
*/
|
||||
class FinalRinseNode extends AbstractProcessNode
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '终末漂洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '终末漂洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
if ($context->currentStep === DisinfectNode::getName()) {
|
||||
if (!$context->success) Logger::debug("[FinalRinseNode] 刷卡错误,当前步骤是消毒,但是刷的读卡器类型不是消毒,对用户进行语音提示刷消毒读卡器");
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_FINAL_RINSE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上一个步骤必须是消毒或机洗
|
||||
return $this->isRequiredNode($context->currentStep, ['消毒', '机洗']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 更新步骤
|
||||
$context->currentStep = '终末漂洗';
|
||||
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
|
||||
/**
|
||||
* 机洗节点
|
||||
* 处理机器清洗步骤,可插入到手工流程中
|
||||
*/
|
||||
class MachineWashNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '机洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '机洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
if ($context->currentStep === WashNode::getName()) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_MACHINE_WASH;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 需要晨洗但未完成:提示先进行晨洗
|
||||
if ($context->needMorningWash && !$context->morningWashed) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_MORNING_WASH;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 机洗可以在多个步骤后执行:空步骤(新流程)、结束、内镜取出、清洗,晨洗
|
||||
if (!$this->isRequiredNode($context->currentStep, ['', '结束', '内镜取出', '清洗', MachineWashNode::getName()])) {
|
||||
if ($context->currentStep === EndNode::getName()) $context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_MACHINE_WASH;
|
||||
else $context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_END;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 设置流程类型为机洗
|
||||
$context->processType = '机洗';
|
||||
|
||||
// 更新步骤
|
||||
$context->currentStep = '机洗';
|
||||
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
// 更新批次为机洗,
|
||||
$context->processType = '机洗';
|
||||
$context->dbOperation = DbOperationType::UPDATE;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 晨洗节点(虚拟读卡器)
|
||||
* 处理晨洗流程的开始
|
||||
*/
|
||||
class MorningWashNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return "晨洗";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
// 只有需要晨洗且未完成晨洗时才处理
|
||||
if (!$context->needMorningWash || $context->morningWashed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查当前读卡器类型是否匹配
|
||||
if (!$this->isRequiredNode($context->readerType, ['漂洗', '机洗'])){
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_MORNING_WASH;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
Logger::debug("处理晨洗节点");
|
||||
// 标记晨洗已开始
|
||||
$context->morningWashed = true;
|
||||
|
||||
// 设置流程类型
|
||||
if ($context->readerType === '机洗') {
|
||||
$context->processType = '机洗(晨洗)';
|
||||
} else {
|
||||
$context->processType = '手工洗(晨洗)';
|
||||
}
|
||||
|
||||
// 更新当前步骤
|
||||
$context->currentStep = self::getName();
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\ProcessContext;
|
||||
|
||||
/**
|
||||
* 流程节点接口
|
||||
* 责任链模式的核心接口
|
||||
*/
|
||||
interface ProcessNodeInterface
|
||||
{
|
||||
/**
|
||||
* 设置下一个节点
|
||||
* @param ProcessNodeInterface $next 下一个处理节点
|
||||
* @return ProcessNodeInterface 返回自身,支持链式调用
|
||||
*/
|
||||
public function setNext(ProcessNodeInterface $next): ProcessNodeInterface;
|
||||
|
||||
/**
|
||||
* 获取下一个节点
|
||||
* @return ProcessNodeInterface|null
|
||||
*/
|
||||
public function getNext(): ?ProcessNodeInterface;
|
||||
|
||||
/**
|
||||
* 处理流程
|
||||
* @param ProcessContext $context 流程上下文
|
||||
* @return ProcessContext 处理后的上下文
|
||||
*/
|
||||
public function handle(ProcessContext $context): ProcessContext;
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string;
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
* @return string
|
||||
*/
|
||||
public function getCode(): string;
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
* @param ProcessContext $context
|
||||
* @return bool
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool;
|
||||
|
||||
/**
|
||||
* 是否启用该节点
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled(): bool;
|
||||
|
||||
/**
|
||||
* 设置是否启用
|
||||
* @param bool $enabled
|
||||
* @return ProcessNodeInterface
|
||||
*/
|
||||
public function setEnabled(bool $enabled): ProcessNodeInterface;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
|
||||
/**
|
||||
* 漂洗节点
|
||||
* 处理漂洗步骤
|
||||
*/
|
||||
class RinseNode extends AbstractProcessNode
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '漂洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '漂洗';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
// 期望当前读卡器为漂洗
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
// 当前步骤是清洗且读卡器不符:说明清洗完了应该刷漂洗
|
||||
if ($context->currentStep === WashNode::getName()) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_RINSE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上一个步骤必须是清洗
|
||||
if (!$this->isRequiredNode($context->currentStep, [WashNode::getName()])) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_WASH;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 更新步骤
|
||||
$context->currentStep = '漂洗';
|
||||
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\config\Config;
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 入库节点
|
||||
* 处理内镜存储入库步骤
|
||||
* 双读卡器模式专用:只在 storage_single_reader = false 时生效
|
||||
*/
|
||||
class StorageInNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '内镜放入';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '内镜放入';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
* 双读卡器模式:只在非单读卡器模式下生效
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$singleReaderMode = $config->storageSingleReader;
|
||||
|
||||
// 单读卡器模式不处理,由 StorageNode 统一处理
|
||||
if ($singleReaderMode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读卡器不是内镜放入类型,不处理
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取内镜当前存储状态
|
||||
$isInStorage = $context->isInStorage ?? false;
|
||||
|
||||
// 如果内镜已在库中,则当前应该是出库操作,不处理
|
||||
if ($isInStorage) {
|
||||
Logger::debug('[StorageInNode] 内镜已在库中,转由出库节点处理');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查前置步骤要求
|
||||
$validSteps = ['', '结束', '内镜取出', '测漏正常', '测漏异常'];
|
||||
if (!in_array($context->currentStep, $validSteps)) {
|
||||
Logger::debug('[StorageInNode] 当前步骤 {} 不符合入库条件', [$context->currentStep]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 设置流程类型为存储
|
||||
$context->processType = '存储';
|
||||
|
||||
// 更新步骤
|
||||
$context->currentStep = self::getName();
|
||||
|
||||
// 标记入库状态
|
||||
$context->isInStorage = true;
|
||||
$context->storageInTime = date('Y-m-d H:i:s');
|
||||
|
||||
// 设置数据库操作
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
Logger::debug('[StorageInNode] 内镜入库成功 endoscope={}', [$context->endoscopeName]);
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\config\Config;
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 存储节点
|
||||
* 统一处理内镜存储的入库和出库操作
|
||||
*
|
||||
* 单读卡器模式:通过配置控制,一个读卡器交替执行入库/出库
|
||||
* 双读卡器模式:分别使用"内镜放入"和"内镜取出"两个读卡器
|
||||
*/
|
||||
class StorageNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '存储';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '存储';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*
|
||||
* 单读卡器模式:读卡器类型是'内镜放入'或'内镜取出',根据 isInStorage 状态判断执行入库还是出库
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$singleReaderMode = $config->storageSingleReader;
|
||||
|
||||
// 非单读卡器模式不处理
|
||||
if (!$singleReaderMode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读卡器类型必须是'内镜放入'或'内镜取出'
|
||||
if (!in_array($context->readerType, ['内镜放入', '内镜取出'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$isInStorage = $context->isInStorage ?? false;
|
||||
|
||||
if ($isInStorage) {
|
||||
// 内镜已在库中,执行出库
|
||||
$validSteps = ['内镜放入', '结束'];
|
||||
if (!in_array($context->currentStep, $validSteps)) {
|
||||
Logger::debug('[StorageNode] 当前步骤 {} 不符合出库条件', [$context->currentStep]);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// 内镜不在库中,执行入库
|
||||
$validSteps = ['', '结束', '内镜取出', '测漏正常', '测漏异常'];
|
||||
if (!in_array($context->currentStep, $validSteps)) {
|
||||
Logger::debug('[StorageNode] 当前步骤 {} 不符合入库条件', [$context->currentStep]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
* 根据 isInStorage 状态判断执行入库还是出库
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 设置流程类型为存储
|
||||
$context->processType = '存储';
|
||||
|
||||
// 根据当前状态判断执行入库还是出库(canHandle 已经验证过状态)
|
||||
if (!$context->isInStorage) {
|
||||
// 入库操作
|
||||
$context->currentStep = '内镜放入';
|
||||
$context->isInStorage = true;
|
||||
$context->storageInTime = date('Y-m-d H:i:s');
|
||||
Logger::debug('[StorageNode] 内镜入库成功 endoscope={}', [$context->endoscopeName]);
|
||||
} else {
|
||||
// 出库操作
|
||||
$context->currentStep = '内镜取出';
|
||||
$context->isInStorage = false;
|
||||
Logger::debug('[StorageNode] 内镜出库成功 endoscope={}', [$context->endoscopeName]);
|
||||
}
|
||||
|
||||
// 设置数据库操作
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\config\Config;
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 出库节点
|
||||
* 处理内镜存储出库步骤
|
||||
* 双读卡器模式专用:只在 storage_single_reader = false 时生效
|
||||
*/
|
||||
class StorageOutNode extends AbstractProcessNode
|
||||
{
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return '内镜取出';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return '内镜取出';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
* 双读卡器模式:只在非单读卡器模式下生效
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
$singleReaderMode = $config->storageSingleReader;
|
||||
|
||||
// 单读卡器模式不处理,由 StorageNode 统一处理
|
||||
if ($singleReaderMode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读卡器不是内镜取出类型,不处理
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取内镜当前存储状态
|
||||
$isInStorage = $context->isInStorage ?? false;
|
||||
|
||||
// 如果内镜不在库中,则当前应该是入库操作,不处理
|
||||
if (!$isInStorage) {
|
||||
Logger::debug('[StorageOutNode] 内镜不在库中,转由入库节点处理');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查前置步骤要求:必须在库中才能出库
|
||||
$validSteps = ['内镜放入', '结束'];
|
||||
if (!in_array($context->currentStep, $validSteps)) {
|
||||
Logger::debug('[StorageOutNode] 当前步骤 {} 不符合出库条件', [$context->currentStep]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 设置流程类型为存储
|
||||
$context->processType = '存储';
|
||||
|
||||
// 更新步骤
|
||||
$context->currentStep = self::getName();
|
||||
|
||||
// 标记出库状态
|
||||
$context->isInStorage = false;
|
||||
|
||||
// 设置数据库操作
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
Logger::debug('[StorageOutNode] 内镜出库成功 endoscope={}', [$context->endoscopeName]);
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\DbOperationType;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\VoiceMessage;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
* 清洗节点
|
||||
* 处理手工清洗步骤
|
||||
*/
|
||||
class WashNode extends AbstractProcessNode
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取节点名称
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return "清洗";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节点编码
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
return self::getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
{
|
||||
|
||||
// 读卡器不是本节点,不处理
|
||||
if (!$this->isMatchReaderType($context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 需要晨洗但未完成:提示先进行晨洗
|
||||
if ($context->needMorningWash && !$context->morningWashed) {
|
||||
$context->expectedNextStep = VoiceMessage::PLEASE_SWIPE_MORNING_WASH;
|
||||
return false;
|
||||
}
|
||||
|
||||
$validCurrentSteps = ['', '结束', '内镜取出', '内镜放入', '测漏正常', '晨洗'];
|
||||
if (!in_array($context->currentStep, $validCurrentSteps)) {
|
||||
// 读卡器是清洗但步骤不对(如终末漂洗时刷清洗),提示应该先刷结束
|
||||
// $context->expectedNextStep = "清洗应在流程开始时刷,当前步骤为{$context->currentStep},请先刷结束卡重新开始";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体处理逻辑
|
||||
*/
|
||||
protected function doHandle(ProcessContext $context): ProcessContext
|
||||
{
|
||||
// 设置流程类型
|
||||
if (empty($context->processType) || $context->processType === '晨洗') {
|
||||
$context->processType = '手工洗';
|
||||
}
|
||||
|
||||
// 更新步骤
|
||||
$context->currentStep = self::getName();
|
||||
|
||||
|
||||
$context->needDatabaseOperation = true;
|
||||
$context->dbOperation = DbOperationType::INSERT;
|
||||
$context->needWebSocketNotify = true;
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user