更新
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\adminapi\logic\tcm;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\tcm\PrescriptionLibrary;
|
||||
|
||||
/**
|
||||
* 处方库逻辑层
|
||||
*/
|
||||
class PrescriptionLibraryLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* @notes 添加处方库
|
||||
*/
|
||||
public static function add(array $params): ?int
|
||||
{
|
||||
try {
|
||||
// 处理药材数据
|
||||
if (isset($params['herbs']) && is_array($params['herbs'])) {
|
||||
$params['herbs'] = json_encode($params['herbs'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
$model = PrescriptionLibrary::create($params);
|
||||
return $model->id;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 编辑处方库
|
||||
*/
|
||||
public static function edit(array $params, int $adminId): bool
|
||||
{
|
||||
try {
|
||||
$model = PrescriptionLibrary::findOrEmpty($params['id']);
|
||||
if ($model->isEmpty()) {
|
||||
self::setError('处方不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 权限检查:只有创建者或公开的处方才能编辑
|
||||
if ($model->creator_id != $adminId && $model->is_public != 1) {
|
||||
self::setError('无权限编辑此处方');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 处理药材数据
|
||||
if (isset($params['herbs']) && is_array($params['herbs'])) {
|
||||
$params['herbs'] = json_encode($params['herbs'], JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
$model->save($params);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 删除处方库
|
||||
*/
|
||||
public static function delete(int $id, int $adminId): bool
|
||||
{
|
||||
try {
|
||||
$model = PrescriptionLibrary::findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
self::setError('处方不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 权限检查:只有创建者才能删除
|
||||
if ($model->creator_id != $adminId) {
|
||||
self::setError('无权限删除此处方');
|
||||
return false;
|
||||
}
|
||||
|
||||
$model->delete();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 处方库详情
|
||||
*/
|
||||
public static function detail(int $id, int $adminId): ?array
|
||||
{
|
||||
try {
|
||||
$model = PrescriptionLibrary::findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 权限检查:只有创建者或公开的处方才能查看
|
||||
if ($model->creator_id != $adminId && $model->is_public != 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $model->toArray();
|
||||
|
||||
// 解析药材JSON
|
||||
if (!empty($data['herbs'])) {
|
||||
$data['herbs'] = json_decode($data['herbs'], true);
|
||||
} else {
|
||||
$data['herbs'] = [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user