189 lines
5.8 KiB
PHP
189 lines
5.8 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
// +----------------------------------------------------------------------
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
// +----------------------------------------------------------------------
|
|
// | author: likeadminTeam
|
|
// +----------------------------------------------------------------------
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\logic\tcm;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\auth\Admin;
|
|
use app\common\model\auth\AdminRole;
|
|
use app\common\model\tcm\Diagnosis;
|
|
use app\common\model\tcm\DiagnosisTodo;
|
|
|
|
/**
|
|
* 诊单待办事项逻辑
|
|
*
|
|
* 状态机:
|
|
* add() → status=0 (待执行)
|
|
* cron → status=1 (已发送) / status=3 (失败)
|
|
* cancel() → status=2 (已取消,仅 status=0 可取消)
|
|
*
|
|
* 鉴权:
|
|
* add:诊单需存在;数据权限交由路由层统一「tcm.diagnosis/dailyRecord」权限域控制,
|
|
* 并兼容历史 diagnosisTodo/* 接口路由。
|
|
* cancel:仅创建人本人或超级管理员 (role_id=1) 可取消,且仅 status=0 时。
|
|
*
|
|
* @package app\adminapi\logic\tcm
|
|
*/
|
|
class DiagnosisTodoLogic extends BaseLogic
|
|
{
|
|
/**
|
|
* @notes 新增待办
|
|
*
|
|
* @param array<string,mixed> $params 已通过 sceneAdd 验证
|
|
* @param int $adminId 当前 admin id
|
|
* @param array<string,mixed> $adminInfo request->adminInfo
|
|
*/
|
|
public static function add(array $params, int $adminId, array $adminInfo): bool
|
|
{
|
|
try {
|
|
$diagnosisId = (int) ($params['diagnosis_id'] ?? 0);
|
|
$diagnosis = Diagnosis::findOrEmpty($diagnosisId);
|
|
if ($diagnosis->isEmpty()) {
|
|
self::setError('诊单不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
$remindTime = (int) ($params['remind_time'] ?? 0);
|
|
if ($remindTime <= time()) {
|
|
self::setError('提醒时间必须晚于当前时间');
|
|
|
|
return false;
|
|
}
|
|
|
|
$creatorName = trim((string) ($adminInfo['name'] ?? ''));
|
|
if ($creatorName === '' && $adminId > 0) {
|
|
$creatorName = (string) Admin::where('id', $adminId)->value('name');
|
|
}
|
|
|
|
DiagnosisTodo::create([
|
|
'diagnosis_id' => $diagnosisId,
|
|
'patient_id' => (int) ($diagnosis->getAttr('patient_id') ?? 0),
|
|
'content' => trim((string) ($params['content'] ?? '')),
|
|
'remind_time' => $remindTime,
|
|
'status' => DiagnosisTodo::STATUS_PENDING,
|
|
'creator_id' => $adminId,
|
|
'creator_name' => $creatorName,
|
|
]);
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 取消待办(人工)
|
|
*/
|
|
public static function cancel(int $id, int $adminId): bool
|
|
{
|
|
try {
|
|
$todo = DiagnosisTodo::findOrEmpty($id);
|
|
if ($todo->isEmpty()) {
|
|
self::setError('待办不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
$status = (int) $todo->getAttr('status');
|
|
if ($status !== DiagnosisTodo::STATUS_PENDING) {
|
|
self::setError('该待办已不是「待执行」状态,无法取消');
|
|
|
|
return false;
|
|
}
|
|
|
|
$creatorId = (int) $todo->getAttr('creator_id');
|
|
$isSuper = self::isSuperAdmin($adminId);
|
|
if ($creatorId !== $adminId && !$isSuper) {
|
|
self::setError('仅创建人或超级管理员可取消');
|
|
|
|
return false;
|
|
}
|
|
|
|
$todo->save([
|
|
'status' => DiagnosisTodo::STATUS_CANCELLED,
|
|
'cancelled_at' => time(),
|
|
'cancelled_by' => $adminId,
|
|
]);
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 详情
|
|
*
|
|
* @return array<string,mixed>
|
|
*/
|
|
public static function detail(int $id, int $adminId = 0): array
|
|
{
|
|
$todo = DiagnosisTodo::findOrEmpty($id);
|
|
if ($todo->isEmpty()) {
|
|
return [];
|
|
}
|
|
$arr = $todo->append([
|
|
'status_text',
|
|
'remind_time_text',
|
|
'notified_at_text',
|
|
'cancelled_at_text',
|
|
])->toArray();
|
|
|
|
// 业务态:是否能被「我」取消(前端按钮显隐用)
|
|
$arr['can_cancel'] = self::canCancel($todo->toArray(), $adminId);
|
|
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* 是否超管:role_id=1
|
|
*/
|
|
public static function isSuperAdmin(int $adminId): bool
|
|
{
|
|
if ($adminId <= 0) {
|
|
return false;
|
|
}
|
|
$roleIds = AdminRole::where('admin_id', $adminId)->column('role_id');
|
|
|
|
return in_array(1, array_map('intval', $roleIds), true);
|
|
}
|
|
|
|
/**
|
|
* 业务判定:当前 admin 能否取消该待办
|
|
*
|
|
* @param array<string,mixed> $todoRow
|
|
*/
|
|
public static function canCancel(array $todoRow, int $adminId): bool
|
|
{
|
|
if ((int) ($todoRow['status'] ?? -1) !== DiagnosisTodo::STATUS_PENDING) {
|
|
return false;
|
|
}
|
|
if ($adminId <= 0) {
|
|
return false;
|
|
}
|
|
if ((int) ($todoRow['creator_id'] ?? 0) === $adminId) {
|
|
return true;
|
|
}
|
|
|
|
return self::isSuperAdmin($adminId);
|
|
}
|
|
}
|