更新
This commit is contained in:
@@ -6,11 +6,12 @@ namespace app\adminapi\logic\tcm;
|
||||
|
||||
use app\adminapi\logic\firstvisit\MyPatientLogic;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
use app\common\service\mobilerecording\MobileRecordingStorage;
|
||||
use DomainException;
|
||||
use think\db\Query;
|
||||
use think\facade\Db;
|
||||
|
||||
/** Metadata only. This service never accepts, reads or uploads a recording or transcript. */
|
||||
/** Backward-compatible metadata API; media lifecycle follows the binding transaction. */
|
||||
final class MobileRecordingLogic
|
||||
{
|
||||
public const PAGE_PERMISSION = 'firstvisit.myPatient/lists';
|
||||
@@ -39,7 +40,8 @@ final class MobileRecordingLogic
|
||||
public static function bind(array $params, int $adminId): array
|
||||
{
|
||||
$p = self::bindingInput($params);
|
||||
return Db::transaction(static function () use ($p, $adminId): array {
|
||||
$garbage = [];
|
||||
$result = Db::transaction(static function () use ($p, $adminId, &$garbage): array {
|
||||
// Serialize mutations for this actor, including the first INSERT for an absent key.
|
||||
$info = self::actor($adminId, true);
|
||||
$existing = self::ownedQuery($adminId, $p)->lock(true)->find();
|
||||
@@ -52,6 +54,9 @@ final class MobileRecordingLogic
|
||||
$now = time();
|
||||
if ($existing) {
|
||||
$id = (int) $existing['id'];
|
||||
if ((int) $existing['diagnosis_id'] !== $p['diagnosis_id']) {
|
||||
$garbage = MobileRecordingMediaLogic::bindingMoved($id, $p['diagnosis_id']);
|
||||
}
|
||||
self::ownedQuery($adminId, $p)->where('id', $id)->update($p + ['updated_at' => $now]);
|
||||
$row = array_merge($existing, $p, ['updated_at' => $now]);
|
||||
} else {
|
||||
@@ -60,21 +65,27 @@ final class MobileRecordingLogic
|
||||
}
|
||||
return ['binding' => self::deviceRow($row, $patients[$p['diagnosis_id']])];
|
||||
});
|
||||
MobileRecordingStorage::cleanup($garbage);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function unbind(array $params, int $adminId): array
|
||||
{
|
||||
self::only($params, ['installation_id', 'recording_key']);
|
||||
$identity = self::identity($params);
|
||||
return Db::transaction(static function () use ($identity, $adminId): array {
|
||||
$garbage = [];
|
||||
$result = Db::transaction(static function () use ($identity, $adminId, &$garbage): array {
|
||||
$info = self::actor($adminId, true);
|
||||
$row = self::ownedQuery($adminId, $identity)->lock(true)->find();
|
||||
if ($row) {
|
||||
self::diagnosis((int) $row['diagnosis_id'], $adminId, $info, true);
|
||||
$garbage = MobileRecordingMediaLogic::bindingRemoved((int) $row['id']);
|
||||
self::ownedQuery($adminId, $identity)->where('id', (int) $row['id'])->delete();
|
||||
}
|
||||
return ['unbound' => true];
|
||||
});
|
||||
MobileRecordingStorage::cleanup($garbage);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function deviceLists(array $params, int $adminId): array
|
||||
@@ -111,10 +122,12 @@ final class MobileRecordingLogic
|
||||
$rows = $query->leftJoin([$adminTable => 'a'], 'a.id = b.admin_id')
|
||||
->field('b.id,b.diagnosis_id,b.source_platform,b.recorded_at,b.duration_ms,b.created_at,b.updated_at,a.name AS bound_by')
|
||||
->order('b.recorded_at', 'desc')->order('b.id', 'desc')->page($page, $size)->select()->toArray();
|
||||
$items = array_map(static function (array $row): array {
|
||||
$media = MobileRecordingMediaLogic::summaries(array_column($rows, 'id'));
|
||||
$items = array_map(static function (array $row) use ($media): array {
|
||||
$dto = self::metadata($row);
|
||||
$dto['bound_by'] = trim((string) ($row['bound_by'] ?? '')) ?: '已停用账号';
|
||||
$dto['playback_location'] = 'original_device';
|
||||
$dto += $media[$row['id']];
|
||||
$dto['playback_location'] = $dto['audio_available'] ? 'server' : 'original_device';
|
||||
return $dto;
|
||||
}, $rows);
|
||||
return ['lists' => $items, 'count' => $count, 'page_no' => $page, 'page_size' => $size];
|
||||
@@ -141,7 +154,7 @@ final class MobileRecordingLogic
|
||||
->where('installation_id', $identity['installation_id'])->where('recording_key', $identity['recording_key']);
|
||||
}
|
||||
|
||||
private static function diagnosis(int $id, int $adminId, array $info, bool $lock = false): array
|
||||
public static function diagnosis(int $id, int $adminId, array $info, bool $lock = false): array
|
||||
{
|
||||
$row = Db::name('tcm_diagnosis')->where('id', $id)->whereNull('delete_time')->where('status', 1)
|
||||
->lock($lock)->find();
|
||||
@@ -187,7 +200,7 @@ final class MobileRecordingLogic
|
||||
return substr($digits, 0, 3) . str_repeat('*', $length - 7) . substr($digits, -4);
|
||||
}
|
||||
|
||||
private static function identity(array $params): array
|
||||
public static function identity(array $params): array
|
||||
{
|
||||
$key = $params['recording_key'] ?? null;
|
||||
if (!is_string($key) || !preg_match('/^[0-9a-f]{64}$/D', $key)) {
|
||||
@@ -212,7 +225,7 @@ final class MobileRecordingLogic
|
||||
];
|
||||
}
|
||||
|
||||
private static function integer($value, int $min, int $max, string $label): int
|
||||
public static function integer($value, int $min, int $max, string $label): int
|
||||
{
|
||||
if (!(is_int($value) || is_string($value)) || !preg_match('/^(0|[1-9][0-9]{0,14})$/D', (string) $value)
|
||||
|| (int) $value < $min || (int) $value > $max) {
|
||||
@@ -221,7 +234,7 @@ final class MobileRecordingLogic
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
private static function only(array $params, array $allowed): void
|
||||
public static function only(array $params, array $allowed): void
|
||||
{
|
||||
if (array_diff(array_keys($params), $allowed) !== []) {
|
||||
throw new DomainException('请求包含不支持的字段');
|
||||
|
||||
Reference in New Issue
Block a user