Files
zyt/server/app/common/service/assistantservice/AssistantServiceReminderRecipient.php
T
2026-11-12 09:07:07 +08:00

62 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\service\assistantservice;
use DomainException;
use think\facade\Db;
/** 模块跟进发给执行人;既有普通提醒仍使用原创建人。 */
final class AssistantServiceReminderRecipient
{
public static function resolve(array $todo): int
{
$creatorId = (int) ($todo['creator_id'] ?? 0);
$actionQuery = Db::name('assistant_service_action');
// 旧环境尚未安装模块时,保持普通提醒的收件人行为。
if (!in_array($actionQuery->getTable(), Db::connect()->getTables(), true)) {
return $creatorId;
}
return Db::transaction(static function () use ($todo, $creatorId): int {
$action = Db::name('assistant_service_action')->where('todo_id', (int) ($todo['id'] ?? 0))->lock(true)->find();
if (!$action) { return $creatorId; }
if (!in_array($action['business_state'], ['pending', 'in_progress'], true)
|| (int) $action['diagnosis_id'] !== (int) ($todo['diagnosis_id'] ?? 0)) {
throw new DomainException('服务跟进已结束或关联关系已变化,不发送提醒');
}
$job = Db::name('assistant_service_job')->where('id', (int) $action['job_id'])->lock(true)->find();
if (!$job || (int) $job['diagnosis_id'] !== (int) $action['diagnosis_id']) {
throw new DomainException('服务跟进缺少有效关联记录,不发送提醒');
}
$assigneeId = (int) $action['assignee_id'];
$payload = json_decode((string) ($job['payload_json'] ?? ''), true);
$subjectId = (int) ($payload['context']['subject_admin_id'] ?? 0);
if ($subjectId > 0) {
if ($subjectId !== $assigneeId) { throw new DomainException('服务跟进执行人与分析对象不一致,不发送提醒'); }
// 收件人不是后台登录操作者:核验实际医助身份和负责关系,不要求模块菜单权限。
$account = Db::name('admin')->where('id', $assigneeId)->where('disable', 0)->where('root', 0)
->whereNull('delete_time')->lock(true)->find();
$roleIds = Db::name('admin_role')->where('admin_id', $assigneeId)->lock(true)->column('role_id');
$roles = $roleIds ? Db::name('system_role')->whereIn('id', $roleIds)->whereNull('delete_time')->lock(true)->select()->toArray() : [];
$isAssistant = false; $isManager = false;
foreach ($roles as $role) {
$isAssistant = $isAssistant || (int) $role['id'] === 2 || trim((string) $role['name']) === '医助';
$isManager = $isManager || in_array((int) $role['id'], [3, 7, 8], true);
}
if (!$account || !$isAssistant || $isManager) { throw new DomainException('跟进执行人已非有效医助,不发送提醒'); }
$diagnosis = Db::name('tcm_diagnosis')->where('id', (int) $action['diagnosis_id'])->where('status', 1)
->whereNull('delete_time')->where('assistant_id', $assigneeId)->lock(true)->find();
if (!$diagnosis) { throw new DomainException('医助已不负责该患者,不发送提醒'); }
} else {
$diagnosis = AssistantServiceAccess::diagnosis((int) $action['diagnosis_id'], $assigneeId, 'view', true);
}
if ((int) $job['patient_id'] !== (int) $diagnosis['patient_id']
|| (int) ($todo['patient_id'] ?? 0) !== (int) $diagnosis['patient_id']) {
throw new DomainException('服务跟进患者关联已变化,不发送提醒');
}
return $assigneeId;
});
}
}