Notebook
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB

1 year ago
<?php
namespace Plugins\Notebook\Service\Admin;
use App\Exception\ApiMessageException;
1 year ago
use App\Service\Admin\AbstractAdminService;
use App\Service\Admin\Traits\DestroyServiceTrait;
use App\Service\Admin\Traits\StoreServiceTrait;
use App\Service\Admin\Traits\UpdateServiceTrait;
use App\Service\Admin\Traits\SwitchServiceTrait;
use App\Util\AdminAuth;
use Plugins\Notebook\Model\Notebook;
use App\Util\EasySearch;
1 year ago
use Plugins\Notebook\Model\NotebookUser;
1 year ago
/**
* Class NotebookService
*/
class NotebookService extends AbstractAdminService
{
use StoreServiceTrait, UpdateServiceTrait, DestroyServiceTrait, SwitchServiceTrait;
public function __construct()
{
$this->model = new Notebook();
}
/**
* @return array
*/
public function lists(): array
{
$query = $this->model
->leftJoin('administrators as a1', 'a1.id', '=', 'notebook.administrators_id')
->leftJoin('administrators as a2', 'a2.id', '=', 'notebook.latest_administrators_id')
1 year ago
->with(['user'])
->select(["notebook.id","notebook.title","notebook.content","notebook.mode","notebook.share_key","a1.name as create_name","a2.name as latest_name","notebook.create_time","notebook.update_time"]);
1 year ago
return EasySearch::create($query)->getData();
}
public function beforeStore(&$data): void
{
$data['administrators_id'] = AdminAuth::getInfo()->id;
}
public function beforeWrite(&$data, $id = null): void
{
$data['latest_administrators_id'] = AdminAuth::getInfo()->id;
}
public function beforeUpdate(&$data, $id): void
{
if (strtotime($data['update_time']) < strtotime($this->model->update_time)){
throw new ApiMessageException("文档内容不是最新版,请更新后编辑");
}
}
/**
* @param int $id
* @param string $update_time
* @return array|null
*/
public function getLatest(int $id, string $update_time): ?array
{
$notebook = $this->model->find($id);
if (strtotime($update_time) < strtotime($notebook->update_time)) {
$data = $notebook->toArray();
$data['latest_name'] = $notebook->latestAdmin->name;
}else{
$data = null;
}
return $data;
1 year ago
}
1 year ago
/**
* @param int $notebook_id
* @param array $userIds
*
* @return void
* @throws ApiMessageException
*/
public function addUser(int $notebook_id, array $userIds): void
{
$data = [];
foreach ($userIds as $user_id) {
$data[] = [
"notebook_id" => $notebook_id,
"administrators_id" => $user_id,
'create_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s'),
];
}
if (!NotebookUser::insert($data)){
throw new ApiMessageException('添加失败');
}
}
1 year ago
}