Files
tcpserver-flow/app/flow/vo/BatchNo.php
T
zimoyin d5991813a6 ai-refactor(flow): 调整抽象流程节点实现和依赖路径
- 修改 AbstractProcessNode 中 ProcessContext 的命名空间引用为 app\flow\context\ProcessContext
- 引入 app\flow\vo\CanHandleResult 用于节点处理结果表示
- 更新前置策略执行后对成功状态的判断,改为调用 isSuccess() 方法
- 增加日志记录细节,便于调试策略执行中断时的错误信息
- 优化代码注释,提升代码可读性和维护性
2026-03-11 00:49:02 +08:00

170 lines
3.8 KiB
PHP

<?php
namespace app\flow\vo;
use app\config\Config;
use app\repository\EctActionsRepository;
/**
* 批次号值对象
* 封装批次号相关操作(不可变)
*/
readonly class BatchNo
{
public function __construct(
/** 批次号值 */
public string $value = '',
) {}
/**
* 创建空批次号
*/
public static function empty(): self
{
return new self(value: '');
}
/**
* 从字符串创建
*/
public static function fromString(string $value): self
{
return new self(value: $value);
}
/**
* 生成新批次号
*/
public static function generate(?string $machineId = null): self
{
$config = Config::getInstance();
$machineId = $machineId ?? $config->machineId;
$existingBatchNo = EctActionsRepository::new()->findTodayActiveBatchNo($machineId);
$datePart = date('Ymd');
$sequence = 1;
if (!empty($existingBatchNo)) {
$existingDatePart = substr($existingBatchNo, 0, 8);
$existingSequence = substr($existingBatchNo, 10, 4);
if ($existingDatePart === $datePart && is_numeric($existingSequence)) {
$sequence = (int)$existingSequence + 1;
}
}
$sequencePart = str_pad($sequence, 4, '0', STR_PAD_LEFT);
return new self(value: $datePart . $machineId . $sequencePart);
}
/**
* 解析批次号结构
*
* @return array{date: string, machineId: string, sequence: int, dateFormatted: string}
*/
public function parse(): array
{
if (!$this->isValid()) {
return [
'date' => '',
'machineId' => '',
'sequence' => 0,
'dateFormatted' => '',
];
}
$datePart = substr($this->value, 0, 8);
$machineId = substr($this->value, 8, 2);
$sequence = (int)substr($this->value, 10, 4);
return [
'date' => $datePart,
'machineId' => $machineId,
'sequence' => $sequence,
'dateFormatted' => substr($datePart, 0, 4) . '-' . substr($datePart, 4, 2) . '-' . substr($datePart, 6, 2),
];
}
/**
* 验证批次号格式是否有效
*/
public function isValid(): bool
{
// 批次号格式: YYYYMMDD + 机器ID(2位) + 序号(4位) = 14位
if (strlen($this->value) !== 14) {
return false;
}
$datePart = substr($this->value, 0, 8);
$sequencePart = substr($this->value, 10, 4);
// 验证日期部分
if (!is_numeric($datePart)) {
return false;
}
// 验证序号部分
if (!is_numeric($sequencePart)) {
return false;
}
return true;
}
/**
* 是否为空批次号
*/
public function isEmpty(): bool
{
return empty($this->value);
}
/**
* 获取日期部分
*/
public function getDate(): string
{
$parsed = $this->parse();
return $parsed['dateFormatted'];
}
/**
* 获取机器ID
*/
public function getMachineId(): string
{
$parsed = $this->parse();
return $parsed['machineId'];
}
/**
* 获取序号
*/
public function getSequence(): int
{
$parsed = $this->parse();
return $parsed['sequence'];
}
/**
* 是否是今天的批次号
*/
public function isToday(): bool
{
if (!$this->isValid()) {
return false;
}
$datePart = substr($this->value, 0, 8);
return $datePart === date('Ymd');
}
/**
* 转换为字符串
*/
public function __toString(): string
{
return $this->value;
}
}