63 lines
2.1 KiB
PHP
63 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\pharmacy;
|
|
|
|
use DomainException;
|
|
|
|
final class EjMedicineMappingPolicy
|
|
{
|
|
/** @param array<string,mixed> $local @param array<string,mixed> $remote */
|
|
public static function assertValid(array $local, array $remote): void
|
|
{
|
|
if ((int) ($local['id'] ?? 0) <= 0) {
|
|
throw new DomainException('本地药材不存在');
|
|
}
|
|
if (!empty($local['delete_time'])) {
|
|
throw new DomainException('本地药材已删除,不能建立映射');
|
|
}
|
|
if ((int) ($local['status'] ?? 0) !== 1) {
|
|
throw new DomainException('本地药材已停用,不能建立映射');
|
|
}
|
|
|
|
if (trim((string) ($remote['medicine_code'] ?? '')) === '') {
|
|
throw new DomainException('洛阳药房药材不存在');
|
|
}
|
|
if (!empty($remote['remote_deleted'])) {
|
|
throw new DomainException('洛阳药房药材已删除,不能建立映射');
|
|
}
|
|
if ((int) ($remote['status'] ?? 0) !== 1) {
|
|
throw new DomainException('洛阳药房药材已停用,不能建立映射');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $local
|
|
* @param array<string,mixed>|null $mapping
|
|
* @return array{mapping_id:int,already_unlinked:bool}
|
|
*/
|
|
public static function unlinkDecision(array $local, ?array $mapping): array
|
|
{
|
|
$localId = (int) ($local['id'] ?? 0);
|
|
if ($localId <= 0) {
|
|
throw new DomainException('本地药材不存在');
|
|
}
|
|
if ($mapping === null) {
|
|
return ['mapping_id' => 0, 'already_unlinked' => true];
|
|
}
|
|
if ((int) ($mapping['local_medicine_id'] ?? 0) !== $localId) {
|
|
throw new DomainException('药材映射归属不匹配');
|
|
}
|
|
|
|
$mappingId = (int) ($mapping['id'] ?? 0);
|
|
if ($mappingId <= 0) {
|
|
throw new DomainException('药材映射记录无效');
|
|
}
|
|
return [
|
|
'mapping_id' => $mappingId,
|
|
'already_unlinked' => (int) ($mapping['status'] ?? 0) !== 1 || !empty($mapping['delete_time']),
|
|
];
|
|
}
|
|
}
|