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.
 
 
 

180 lines
5.5 KiB

<?php
namespace Plugins\Notebook\Service\Admin;
use App\Exception\ApiMessageException;
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 App\Util\Hy;
use Hyperf\Database\Query\JoinClause;
use Plugins\Notebook\Enums\NotebookEnumMode;
use Plugins\Notebook\Enums\NotebookUserEnumMode;
use Plugins\Notebook\Model\Notebook;
use App\Util\EasySearch;
use Plugins\Notebook\Model\NotebookUser;
/**
* 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')
->leftJoin('notebook_user', function (JoinClause $clause){
$clause->on('notebook_user.notebook_id', '=', 'notebook.id')
->where('notebook_user.administrators_id', AdminAuth::getInfo()->id)
->whereNull('notebook_user.delete_time');
})
->where(function ($builder){
$builder->orWhere('notebook.administrators_id', AdminAuth::getInfo()->id)
->orWhere('notebook_user.administrators_id', AdminAuth::getInfo()->id)
->orWhere('notebook.mode', NotebookEnumMode::Public->value);
})
->orderBy('notebook.id', 'desc')
->with(['user'])
->select(["notebook.id","notebook.administrators_id","notebook.title","notebook_user.mode as user_mode","notebook.content","notebook.mode","notebook.share_key","a1.name as create_name","a2.name as latest_name","notebook.create_time","notebook.update_time"]);
$data = EasySearch::create($query)->getData();
$data['data']->each(function ($datum) {
if ($datum->administrators_id === AdminAuth::getInfo()->id) {
$datum->user_mode = "super";
} elseif ($datum->user_mode === NotebookUserEnumMode::ReadingAndWriting->value) {
$datum->user_mode = "write";
} else {
$datum->user_mode = "read";
}
});
return $data;
}
/**
* @param $data
*
* @return void
*/
public function beforeStore(&$data): void
{
$data['administrators_id'] = AdminAuth::getInfo()->id;
$data['mode'] = NotebookEnumMode::Private->value;
$data['share_key'] = substr(md5(time()), 0, 10);
}
/**
* @param $data
* @param $id
*
* @return void
*/
public function beforeWrite(&$data, $id = null): void
{
$data['latest_administrators_id'] = AdminAuth::getInfo()->id;
}
/**
* @param array $ids
*
* @return void
*/
public function beforeDestroy(array $ids)
{
$this->model->where('administrators_id', AdminAuth::getInfo()->id);
}
/**
* @param $data
* @param $id
*
* @return void
* @throws ApiMessageException
*/
public function beforeUpdate(&$data, $id): void
{
if (strtotime($data['update_time']) < strtotime($this->model->update_time)){
throw new ApiMessageException("文档内容不是最新版,请更新后编辑");
}
if ($this->model->administrators_id === AdminAuth::getInfo()->id) {
return;
}
if ($data['mode'] != $this->model->mode){
throw new ApiMessageException("对不起,你无修改权限");
}
$userMode = NotebookUser::where('notebook_id', $id)
->where('administrators_id', AdminAuth::getInfo()->id)
->value('mode');
if ($userMode !== NotebookUserEnumMode::ReadingAndWriting->value) {
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;
}
/**
* @param int $notebook_id
* @param array $userIds
*
* @return void
* @throws ApiMessageException
*/
public function addUser(int $notebook_id, array $userIds): void
{
$notebook = $this->model->find($notebook_id);
if ($notebook?->administrators_id != AdminAuth::getInfo()->id) {
throw new ApiMessageException("对不起,未找到该操作");
}
$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('添加失败');
}
}
}