feat: 实现TCP Server

This commit is contained in:
zimoyin
2026-03-02 21:59:43 +08:00
parent 043306819b
commit a79dfae57d
144 changed files with 15785 additions and 140 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace app\repository;
use support\Model;
abstract class BaseRepository
{
protected Model $model;
public static abstract function new(): BaseRepository;
}
+309
View File
@@ -0,0 +1,309 @@
<?php
namespace app\repository;
use app\flow\ProcessContext;
use app\flow\DbOperationType;
use app\model\EctActions;
use app\utils\Logger;
/**
* EctActions 数据操作仓库
* 封装 ect_actions 表的所有读写操作
*/
class EctActionsRepository extends BaseRepository
{
public function __construct()
{
$this->model = new EctActions();
}
public static function new(): static
{
return new self();
}
/**
* 从数据库查询最大的批次号
* 用于分布式场景下保证批次号一致性
*
* @param string $machineId
* @return string|null 批次号,未找到返回 null
*/
public function findTodayActiveBatchNo(string $machineId): ?string
{
try {
// 1. 精准限定今日时间范围(避免跨天数据)
$todayStart = date('Y-m-d 00:00:00');
$todayEnd = date('Y-m-d 23:59:59');
$todayDate = date('Ymd');
$record = EctActions::where('process_name', '<>', '结束')
->whereBetween('created_at', [$todayStart, $todayEnd])
->where('op_batchno', 'like', $todayDate . $machineId . '%') // 匹配今日日期+指定机器ID开头的批次号
->select('op_batchno')
->orderBy('op_batchno', 'desc') // 降序排列,最大的序列号(批次号)排在最前
->lockForUpdate()
->first(); // 获取第一条(即序列号最大的)记录
// 3. 严谨的空值判断
if (empty($record) || empty(trim($record->op_batchno))) {
return null;
}
return (string)trim($record->op_batchno);
} catch (\Exception $e) {
// 记录异常(可选:根据项目日志规范调整)
Logger::error('查询最大批次号失败:', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return null;
}
}
/**
* 查询内镜最后一条洗消操作记录
*
* @param string $endoscopeId 内镜ID
* @param array $excludeActionTypes 排除的操作类型(如 [0, 7, 8])
* @return EctActions|null
*/
public function findLastAction(string $endoscopeId, array $excludeActionTypes = []): ?EctActions
{
$query = EctActions::where('endoscope_id', $endoscopeId);
if (!empty($excludeActionTypes)) {
$query->whereNotIn('action_type', $excludeActionTypes);
}
/** @var EctActions|null */
return $query->orderBy('action_id', 'desc')->first();
}
/**
* 查询今日洗消记录数
*
* @param string $endoscopeId 内镜ID
* @param array $excludeActionTypes 排除的操作类型 0诊疗 1手工洗 2机洗 3 手工洗(晨洗)4机洗(晨洗)5手工洗(加强)6机洗(加强)7测漏 8存储
* @return int
*/
public function countTodayActions(string $endoscopeId, string $todayStart, array $excludeActionTypes = []): int
{
$todayStart = date('Y-m-d H:i:s', strtotime($todayStart));
$query = EctActions::where('endoscope_id', $endoscopeId)
->where('created_at', '>=', $todayStart);
if (!empty($excludeActionTypes)) {
$query->whereNotIn('action_type', $excludeActionTypes);
}
// 遍历
$count = 0;
$today = date('Ymd');
$query->lazy()->each(function (EctActions $record) use (&$count, $today) {
$batchInfo = ProcessContext::parseBatchNo($record->op_batchno);
$dateTime = strtotime($batchInfo['date']);
if (empty($batchInfo['date']) || $dateTime === false) return;
$recordDate = date('Ymd', $dateTime);
if ($recordDate >= $today) {
$count += 1;
}
});
return $count;
}
/**
* 查询最后一次存储入库时间
*
* @param string $endoscopeId 内镜ID
* @return string|null op_starttime,未找到返回 null
*/
public function findLastStorageTime(string $endoscopeId): ?string
{
$record = EctActions::where('endoscope_id', $endoscopeId)
->where('action_type', 8)
->where('process_name', '内镜放入')
->select('op_starttime')
->orderBy('action_id', 'desc')
->first();
if ($record === null) {
return null;
}
return (string)$record->op_starttime;
}
/**
* 查询内镜当前是否在存储柜中
* 根据最后一次存储操作判断:入库则在库中,出库则不在
*
* @param string $endoscopeId 内镜ID
* @return bool true=在库中,false=不在库中
*/
public function isEndoscopeInStorage(string $endoscopeId): bool
{
$record = EctActions::where('endoscope_id', $endoscopeId)
->where('action_type', 8)
->whereIn('process_name', ['内镜放入', '内镜取出'])
->select('process_name', 'op_starttime')
->orderBy('action_id', 'desc')
->first();
if ($record === null) {
return false;
}
return $record->process_name === '内镜放入';
}
/**
* 查询最后一次存储操作记录
*
* @param string $endoscopeId 内镜ID
* @return array|null ['process_name' => ..., 'op_starttime' => ...] 或 null
*/
public function findLastStorageAction(string $endoscopeId): ?array
{
$record = EctActions::where('endoscope_id', $endoscopeId)
->where('action_type', 8)
->whereIn('process_name', ['内镜放入', '内镜取出'])
->select('process_name', 'op_starttime', 'action_id')
->orderBy('action_id', 'desc')
->first();
if ($record === null) {
return null;
}
return [
'process_name' => (string)$record->process_name,
'op_starttime' => (string)$record->op_starttime,
'action_id' => (int)$record->action_id,
];
}
/**
* 查询批次号对应的操作员信息
*
* @param string $batchNo 批次号
* @return array|null [字段名=> 值, ...] 或 null
*/
public function findOperatorByBatchNo(string $batchNo): ?array
{
$record = EctActions::where('op_batchno', $batchNo)
->whereNotNull('opuser_id')
->select('opuser_id', 'opuser_name', 'opuser_rfid', 'opuser_type')
->first();
if ($record === null) {
return null;
}
return [
'id' => (string)$record->opuser_id,
'name' => (string)$record->opuser_name,
'rfid' => (string)$record->opuser_rfid,
'type' => (int)$record->opuser_type,
];
}
/**
* 插入一条洗消记录
*
* @param array $data 字段数组
* @return EctActions
*/
public function insert(array $data): EctActions
{
return EctActions::create($data);
}
/**
* 从 ProcessContext 保存洗消记录
*
* @param ProcessContext $context 流程上下文
* @param int $opuserType 操作员类型
* @return EctActions|null
*/
public function saveFromContext(ProcessContext $context, int $opuserType): ?EctActions
{
$op_endtime = null;
$process_order = 1;
if ($context->currentStep === '结束') {
$op_endtime = date('Y-m-d H:i:s');
}
if (!empty($context->previousAction)) {
$process_order = (int)($context->previousAction->process_order ?? 1);
}
if (in_array(DbOperationType::INSERT, $context->dbOperation)) {
return $this->insert([
'op_batchno' => $context->batchNo,
'action_type' => $this->mapActionType($context->processType),
'action_type_name' => $context->processType,
'process_name' => $context->currentStep,
'process_order' => $process_order + 1,
'op_morning' => $context->needMorningWash ? 1 : 0,
'op_enhance' => $context->needEnhanceWash ? 1 : 0,
'reader_id' => (int)$context->readerId ?: null,
'reader_no' => $context->readerNo ?: null,
'opuser_type' => $opuserType,
'opuser_id' => (int)$context->operatorId ?: null,
'opuser_rfid' => $context->operatorRfid ?: null,
'opuser_name' => $context->operatorName ?: null,
'endoscope_id' => (int)$context->endoscopeId ?: null,
'endoscope_rfid' => $context->cardNo ?: null,
'endoscope_name' => $context->endoscopeName ?: null,
'op_starttime' => $context->actionStartTime ?: date('Y-m-d H:i:s'),
'op_endtime' => $op_endtime,
'op_duration' => $context->duration ?: null,
'created_at' => date('Y-m-d H:i:s'),
]);
} elseif (in_array(DbOperationType::UPDATE, $context->dbOperation)) {
EctActions::where('op_batchno', $context->batchNo)
->where('endoscope_id', $context->endoscopeId)
->update([
'action_type' => $this->mapActionType($context->processType),
'action_type_name' => $context->processType,
]);
}
return null;
}
/**
* 映射流程类型到 action_type
* 0诊疗 1手工洗 2机洗 3 手工洗(晨洗)4机洗(晨洗)5手工洗(加强)6机洗(加强)
*/
protected function mapActionType(string $processType): int
{
$mapping = [
'诊疗' => 0,
'手工洗' => 1,
'手工洗(晨洗)' => 3,
'手工洗(加强)' => 5,
'机洗' => 2,
'机洗(晨洗)' => 4,
'机洗(加强)' => 6,
'测漏' => 7,
'存储' => 8,
];
return $mapping[$processType] ?? 1;
}
/**
* 更新批次号最后一条记录的结束时间和时长
*
* @param string $batchNo 批次号
* @param string $endTime 结束时间
* @param int $duration 时长(秒)
* @return int 影响行数
*/
public function updateEndTime(string $batchNo, string $endTime, int $duration): int
{
return EctActions::where('op_batchno', $batchNo)
->orderBy('action_id', 'desc')
->limit(1)
->update([
'op_endtime' => $endTime,
'op_duration' => $duration,
]);
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace app\repository;
use app\model\EctMetaEndoscope;
/**
* 内镜数据仓库
* 封装 ect_meta_endoscope 表的查询操作
*/
class EndoscopeRepository extends BaseRepository
{
public function __construct()
{
$this->model = new EctMetaEndoscope();
}
public static function new(): static
{
return new self();
}
/**
* 通过 RFID 卡号查询内镜信息
* 同时匹配主卡号(endoscope_rfid)和备用卡号(rfid3
*
* @param string $cardNo RFID 卡号(大写)
* @return EctMetaEndoscope|null
*/
public function findByCardNo(string $cardNo): ?EctMetaEndoscope
{
$cardNo = strtoupper($cardNo);
/** @var EctMetaEndoscope|null */
return EctMetaEndoscope::where('endoscope_rfid', $cardNo)
->orWhere('rfid3', $cardNo)
->first();
}
}
@@ -0,0 +1,78 @@
<?php
namespace app\repository;
use app\model\EctMetaProcess;
class ProcessDurationRepository extends BaseRepository
{
public function __construct()
{
$this->model = new EctMetaProcess();
}
public static function new(): BaseRepository
{
return new self();
}
public function getProcessDurations(): array
{
$result = [];
$processes = EctMetaProcess::all();
foreach ($processes as $process) {
$processType = $process->process_type ?? '';
$processName = $process->process_name ?? '';
$processDuration = $process->process_duration ?? 0;
if (!isset($result[$processType])) $result[$processType] = [];
$result[$processType][$processName] = $processDuration;
}
return $result;
}
/**
* 查询某流程类型下某步骤的时长(秒)
*
* @param string $processType 流程类型,如"手工洗"、"机洗(晨洗)"等
* @param string $processName 步骤名称,如"清洗"、"消毒"等
* @return int|null 返回 process_duration(秒),未找到返回 null
*/
public function getDurationByProcessTypeAndName(string $processType, string $processName): ?int
{
$record = EctMetaProcess::where('process_type', $processType)
->where('process_name', $processName)
->where('status', 1)
->select('process_duration')
->first();
if ($record === null) {
return null;
}
return (int) $record->process_duration;
}
/**
* 查询某流程类型下所有步骤的时长,返回 [步骤名称 => 秒数] 映射
*
* @param string $processType 流程类型,如"手工洗"、"机洗(晨洗)"等
* @return array<string, int> [process_name => process_duration]
*/
public function getDurationsByProcessType(string $processType): array
{
$records = EctMetaProcess::where('process_type', $processType)
->where('status', 1)
->select('process_name', 'process_duration')
->get();
$result = [];
foreach ($records as $record) {
$result[$record->process_name] = (int) $record->process_duration;
}
return $result;
}
}
+69
View File
@@ -0,0 +1,69 @@
<?php
namespace app\repository;
use app\model\EctMetaFacilities;
use app\model\EctMetaProcess;
use app\model\EctMetaRfidreader;
/**
* 读卡器数据仓库
* 封装 ect_meta_rfidreader / ect_meta_facilities / ect_meta_process 的联合查询
*/
class ReaderRepository extends BaseRepository
{
public function __construct()
{
$this->model = new EctMetaRfidreader();
}
public static function new(): static
{
return new self();
}
/**
* 通过读卡器编号查询读卡器所在步骤信息
*
* 联合查询路径:
* ect_meta_rfidreader.reader_no
* → ect_meta_facilities.reader_id
* → ect_meta_process.process_id
* → process_name(步骤名称)
*
* @param string $readerNo 读卡器编号(大写)
* @return array{readerId: string, readerType: string}|null
* readerId = reader_id(读卡器ID
* readerType = process_name(该读卡器所在步骤,如"清洗")
*/
public function findReaderInfo(string $readerNo): ?array
{
$readerNo = strtoupper($readerNo);
$reader = EctMetaRfidreader::where('reader_no', $readerNo)
->select('reader_id')
->first();
if ($reader === null) {
return null;
}
$readerId = (string) $reader->reader_id;
$facility = EctMetaFacilities::where('reader_id', $readerId)
->select('process_id')
->first();
if ($facility === null) {
return ['readerId' => $readerId, 'readerType' => ''];
}
$process = EctMetaProcess::where('process_id', $facility->process_id)
->select('process_name')
->first();
$readerType = (string)$process?->process_name;
return ['readerId' => $readerId, 'readerType' => $readerType];
}
}
+119
View File
@@ -0,0 +1,119 @@
<?php
namespace app\repository;
use app\model\EctUser;
use app\repository\bean\User;
use app\flow\exception\IllegalUsageException;
use app\repository\exception\ResultNotAsExpectedException;
use support\Model;
class UserRepository extends BaseRepository
{
public function __construct(){
$this->model = new EctUser();
}
/**
* @param int $user_id
* @return User
* @throws IllegalUsageException
*/
public function getUser(int $user_id): User
{
$result = EctUser::where('user_id', $user_id)->first();
if ($result === null) throw new IllegalUsageException();
return new User($result->user_id, $result->user_name, $result->role_id, $result->department_id, $result->user_rfid);
}
/**
* 返回用户的rfid
*/
public function getRfid(User $user): string
{
return EctUser::where('user_id', $user->user_id)->first();
}
/**
* 根据用户名返回用户
* @param string $name
* @return array<User>
* @throws IllegalUsageException
*/
public function getUserByName(string $name): array
{
$list = [];
$ectUsers = EctUser::where('user_name', $name)->get();
if ($ectUsers === null) throw new IllegalUsageException();
foreach ($ectUsers as $ectUser) {
$list[] = new User($ectUser->user_id, $ectUser->user_name, $ectUser->role_id, $ectUser->department_id, $ectUser->user_rfid);
}
return $list;
}
/**
* @param int $id
* @return array<User>
* @throws IllegalUsageException
*/
public function getUserById(int $id): array
{
$ectUsers = EctUser::where('user_id', $id)->first();
if ($ectUsers === null) throw new IllegalUsageException();
$list = [];
foreach ($ectUsers as $ectUser) {
$list[] = new User($ectUser->user_id, $ectUser->user_name, $ectUser->role_id, $ectUser->department_id, $ectUser->user_rfid);
}
return $list;
}
/**
* @return array<User> 用户列表
*/
public function getAll(): array
{
$list = [];
$all = EctUser::all();
foreach ($all as $user) {
$list[] = new User($user->user_id, $user->user_name, $user->role_id, $user->department_id, $user->user_rfid);
}
return $list;
}
/**
* 根据 RFID 卡号查询操作员(人员卡识别用)
*
* @param string $rfid RFID 卡号(自动转大写匹配)
* @return EctUser|null 找到返回模型,找不到返回 null
* @throws ResultNotAsExpectedException
*/
public function findByRfid(string $rfid): ?EctUser
{
$rfid = strtoupper($rfid);
$isAllDigit = ctype_digit($rfid);
$rfids = EctUser::where(function ($query) use ($rfid, $isAllDigit) {
$query->where('user_rfid', $rfid)
->orWhere('rfid3', $rfid);
if ($isAllDigit) {
$rfidWithoutZero = ltrim($rfid, '0');
$rfidWithoutZero = $rfidWithoutZero === '' ? '0' : $rfidWithoutZero;
$query->orWhere('user_rfid', $rfidWithoutZero)
->orWhere('rfid3', $rfidWithoutZero);
}
})->distinct()
->get();
// 如果数量不对就抛出异常
if (count($rfids) > 1 || count($rfids) == 0) {
throw new ResultNotAsExpectedException("RFID Conflict, rfid {$rfid} count: " . count($rfids));
}
return $rfids[0];
}
public static function new(): BaseRepository
{
return new self();
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace app\repository\bean;
class User
{
/**
* @var int auto id
*/
public int $user_id {
get => $this->user_id;
}
/**
* @var string user name
*/
public string $name {
get => $this->name;
}
/**
* @var int role id
*/
public int $roleId {
get => $this->role->value;
}
public UserRole $role{
get => $this->role;
}
/**
* @var int department id
*/
public int $departmentId {
get => $this->departmentId;
}
/**
* @var string 人员卡编码
*/
public string $rfidCode {
get => $this->rfidCode;
}
public function __construct(int $user_id, string $name, int $roleId, int $departmentId, string $rfid)
{
$this->user_id = $user_id;
$this->name = $name;
$this->role = UserRole::from($roleId);
$this->departmentId = $departmentId;
$this->rfidCode = $rfid;
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace app\repository\bean;
// 枚举
enum UserRole: int
{
case ADMIN = 1;
/**
* 医生
*/
case DOCTOR = 2;
/**
* 清洗工
*/
case CLEANER = 3;
/**
* 护士
*/
case NURSE = 4;
}
@@ -0,0 +1,17 @@
<?php
namespace app\repository\exception;
use RuntimeException;
class NotFoundUserException extends RuntimeException
{
public function __construct(
$message = 'Not Found User in database',
int $code = 404,
?\Throwable $previous = null
)
{
parent::__construct($message, $code, $previous);
}
}
@@ -0,0 +1,17 @@
<?php
namespace app\repository\exception;
use RuntimeException;
class ResultNotAsExpectedException extends RuntimeException
{
public function __construct(
$message = 'Result not as expected',
int $code = 404,
?\Throwable $previous = null
)
{
parent::__construct($message, $code, $previous);
}
}