2020-11-04 11:57:16 +08:00
|
|
|
<?php
|
2021-09-04 20:18:20 +08:00
|
|
|
declare(strict_types=1);
|
2020-11-04 11:57:16 +08:00
|
|
|
|
2020-11-04 16:47:17 +08:00
|
|
|
namespace App\Service;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
use App\Model\Article\Article;
|
|
|
|
use App\Model\Article\ArticleClass;
|
|
|
|
use App\Model\Article\ArticleDetail;
|
|
|
|
use App\Model\Article\ArticleTag;
|
|
|
|
use App\Model\Article\ArticleAnnex;
|
2020-11-04 22:58:49 +08:00
|
|
|
use App\Traits\PagingTrait;
|
|
|
|
use Hyperf\DbConnection\Db;
|
2020-11-05 17:40:51 +08:00
|
|
|
use Exception;
|
2020-11-04 22:58:49 +08:00
|
|
|
|
2020-11-29 14:44:11 +08:00
|
|
|
/**
|
|
|
|
* 笔记服务层
|
|
|
|
*
|
|
|
|
* @package App\Service
|
|
|
|
*/
|
2020-11-04 11:57:16 +08:00
|
|
|
class ArticleService extends BaseService
|
|
|
|
{
|
2020-11-04 22:58:49 +08:00
|
|
|
use PagingTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户文章标签列表
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
2021-04-22 16:14:34 +08:00
|
|
|
* @return array
|
2020-11-04 22:58:49 +08:00
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function getUserTags(int $user_id): array
|
2020-11-04 22:58:49 +08:00
|
|
|
{
|
2020-11-29 14:44:11 +08:00
|
|
|
$items = ArticleTag::where('user_id', $user_id)->orderBy('id', 'desc')->get(['id', 'tag_name', Db::raw('0 as count')])->toArray();
|
2021-04-22 16:14:34 +08:00
|
|
|
foreach ($items as $k => $item) {
|
2021-07-28 23:42:21 +08:00
|
|
|
$items[$k]['count'] = Article::where('user_id', $user_id)->whereRaw("FIND_IN_SET({$item['id']},tags_id)")->count();
|
2021-04-22 16:14:34 +08:00
|
|
|
}
|
2020-11-04 22:58:49 +08:00
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户文章列表
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $page 分页
|
|
|
|
* @param int $page_size 分页大小
|
|
|
|
* @param array $params 查询参数
|
2020-11-04 22:58:49 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function getUserArticleList(int $user_id, int $page, int $page_size, $params = []): array
|
2020-11-04 22:58:49 +08:00
|
|
|
{
|
|
|
|
$filed = ['article.id', 'article.class_id', 'article.title', 'article.image', 'article.abstract', 'article.updated_at', 'article_class.class_name', 'article.status'];
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
$model = Article::select($filed)
|
2020-11-04 22:58:49 +08:00
|
|
|
->leftJoin('article_class', 'article_class.id', '=', 'article.class_id');
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->where('article.user_id', $user_id);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
|
|
|
if ($params['find_type'] == 3) {
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->where('article.class_id', $params['class_id']);
|
2020-11-04 22:58:49 +08:00
|
|
|
} else if ($params['find_type'] == 4) {
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->whereRaw("FIND_IN_SET({$params['class_id']},tags_id)");
|
2020-11-04 22:58:49 +08:00
|
|
|
} else if ($params['find_type'] == 2) {
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->where('article.is_asterisk', 1);
|
2020-11-04 22:58:49 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->where('article.status', $params['find_type'] == 5 ? 2 : 1);
|
2020-11-04 22:58:49 +08:00
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
if (isset($params['keyword']) && !empty($params['keyword'])) {
|
|
|
|
$model->where('article.title', 'like', "%{$params['keyword']}%");
|
2020-11-04 22:58:49 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
$count = $model->count();
|
2021-04-20 16:30:57 +08:00
|
|
|
$rows = [];
|
2020-11-04 22:58:49 +08:00
|
|
|
if ($count > 0) {
|
|
|
|
if ($params['find_type'] == 1) {
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->orderBy('updated_at', 'desc');
|
2020-11-04 22:58:49 +08:00
|
|
|
$page_size = 20;
|
|
|
|
} else {
|
2021-09-04 20:18:20 +08:00
|
|
|
$model->orderBy('id', 'desc');
|
2020-11-04 22:58:49 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
$rows = $model->forPage($page, $page_size)->get()->toArray();
|
2020-11-04 22:58:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getPagingRows($rows, $count, $page, $page_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取文章详情
|
|
|
|
*
|
|
|
|
* @param int $article_id 文章ID
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-04 22:58:49 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function getArticleDetail(int $article_id, $user_id = 0): array
|
2020-11-04 22:58:49 +08:00
|
|
|
{
|
|
|
|
$info = Article::where('id', $article_id)->where('user_id', $user_id)->first(['id', 'class_id', 'tags_id', 'title', 'status', 'is_asterisk', 'created_at', 'updated_at']);
|
2021-09-04 20:18:20 +08:00
|
|
|
|
|
|
|
if (!$info) return [];
|
2020-11-04 22:58:49 +08:00
|
|
|
|
|
|
|
// 关联文章详情
|
|
|
|
$detail = $info->detail;
|
2021-09-04 20:18:20 +08:00
|
|
|
if (!$detail) return [];
|
2020-11-04 22:58:49 +08:00
|
|
|
|
|
|
|
$tags = [];
|
|
|
|
if ($info->tags_id) {
|
|
|
|
$tags = ArticleTag::whereIn('id', explode(',', $info->tags_id))->get(['id', 'tag_name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-04-20 16:30:57 +08:00
|
|
|
'id' => $article_id,
|
|
|
|
'class_id' => $info->class_id,
|
|
|
|
'title' => $info->title,
|
|
|
|
'md_content' => htmlspecialchars_decode($detail->md_content),
|
|
|
|
'content' => htmlspecialchars_decode($detail->content),
|
2020-11-04 22:58:49 +08:00
|
|
|
'is_asterisk' => $info->is_asterisk,
|
2021-04-20 16:30:57 +08:00
|
|
|
'status' => $info->status,
|
|
|
|
'created_at' => $info->created_at,
|
|
|
|
'updated_at' => $info->updated_at,
|
|
|
|
'tags' => $tags,
|
|
|
|
'files' => $this->findArticleAnnexAll($user_id, $article_id)
|
2020-11-04 22:58:49 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取笔记附件
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-04 22:58:49 +08:00
|
|
|
* @param int $article_id 笔记ID
|
2021-09-04 20:18:20 +08:00
|
|
|
* @return array
|
2020-11-04 22:58:49 +08:00
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function findArticleAnnexAll(int $user_id, int $article_id): array
|
2020-11-04 22:58:49 +08:00
|
|
|
{
|
|
|
|
return ArticleAnnex::where([
|
|
|
|
['user_id', '=', $user_id],
|
|
|
|
['article_id', '=', $article_id],
|
|
|
|
['status', '=', 1]
|
2022-01-16 10:29:16 +08:00
|
|
|
])->get(['id', 'suffix', 'size', 'original_name', 'created_at'])->toArray();
|
2020-11-04 22:58:49 +08:00
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
/**
|
|
|
|
* 编辑笔记分类
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
|
|
|
* @param int|string $class_id 分类ID
|
|
|
|
* @param string $class_name 分类名
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool|int
|
|
|
|
*/
|
2020-11-24 23:23:12 +08:00
|
|
|
public function editArticleClass(int $uid, $class_id, string $class_name)
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
if ($class_id) {
|
|
|
|
if (!ArticleClass::where('id', $class_id)->where('user_id', $uid)->where('is_default', 0)->update(['class_name' => $class_name])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class_id;
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:30:57 +08:00
|
|
|
$arr = [];
|
2020-11-05 17:40:51 +08:00
|
|
|
$items = ArticleClass::where('user_id', $uid)->get(['id', 'sort']);
|
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
$arr[] = ['id' => $item->id, 'sort' => $key + 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($items);
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
ArticleClass::where('id', $val['id'])->update(['sort' => $val['sort']]);
|
|
|
|
}
|
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
$res = ArticleClass::create(['user_id' => $uid, 'class_name' => $class_name, 'sort' => 1, 'created_at' => date("y-m-d H:i:s")]);
|
2020-11-05 17:40:51 +08:00
|
|
|
|
|
|
|
Db::commit();
|
2021-04-22 16:14:34 +08:00
|
|
|
return $res->id;
|
2020-11-05 17:40:51 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
2022-01-16 10:29:16 +08:00
|
|
|
|
|
|
|
logger()->info("编辑标签失败", [
|
|
|
|
"error" => $e->getMessage(),
|
|
|
|
"line" => $e->getLine(),
|
|
|
|
"file" => $e->getFile(),
|
|
|
|
]);
|
2020-11-05 17:40:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除笔记分类
|
|
|
|
*
|
2021-07-05 21:52:44 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $class_id 分类ID
|
|
|
|
* @return bool
|
2021-04-22 16:54:01 +08:00
|
|
|
* @throws Exception
|
2020-11-05 17:40:51 +08:00
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function delArticleClass(int $user_id, int $class_id): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-07-05 21:52:44 +08:00
|
|
|
$result = ArticleClass::where('id', $class_id)->where('user_id', $user_id)->first(['id', 'sort']);
|
|
|
|
if (!$result) return false;
|
2020-11-05 17:40:51 +08:00
|
|
|
|
2021-07-05 21:52:44 +08:00
|
|
|
$count = Article::where('user_id', $user_id)->where('class_id', $class_id)->count();
|
|
|
|
if ($count > 0) return false;
|
|
|
|
|
|
|
|
Db::transaction(function () use ($user_id, $class_id, $result) {
|
|
|
|
ArticleClass::where('id', $class_id)->where('user_id', $user_id)->where('is_default', 0)->delete();
|
|
|
|
ArticleClass::where('user_id', $user_id)->where('sort', '>', $result->sort)->decrement('sort');
|
|
|
|
});
|
2020-11-05 17:40:51 +08:00
|
|
|
|
2021-07-05 21:52:44 +08:00
|
|
|
return true;
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文集分类排序
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $class_id 文集分类ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $sort_type 排序方式
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function articleClassSort(int $user_id, int $class_id, int $sort_type): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
if (!$info = ArticleClass::select(['id', 'sort'])->where('id', $class_id)->where('user_id', $user_id)->first()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
// 向下排序
|
2020-11-05 17:40:51 +08:00
|
|
|
if ($sort_type == 1) {
|
|
|
|
$maxSort = ArticleClass::where('user_id', $user_id)->max('sort');
|
|
|
|
if ($maxSort == $info->sort) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
ArticleClass::where('user_id', $user_id)->where('sort', $info->sort + 1)->update([
|
|
|
|
'sort' => $info->sort
|
|
|
|
]);
|
|
|
|
|
|
|
|
ArticleClass::where('id', $class_id)->update([
|
|
|
|
'sort' => $info->sort + 1
|
|
|
|
]);
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if ($sort_type == 2) {//向上排序
|
|
|
|
$minSort = ArticleClass::where('user_id', $user_id)->min('sort');
|
|
|
|
if ($minSort == $info->sort) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
ArticleClass::where('user_id', $user_id)->where('sort', $info->sort - 1)->update([
|
|
|
|
'sort' => $info->sort
|
|
|
|
]);
|
|
|
|
|
|
|
|
ArticleClass::where('id', $class_id)->where('user_id', $user_id)->update([
|
|
|
|
'sort' => $info->sort - 1
|
|
|
|
]);
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 笔记分类合并
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $class_id 笔记分类ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $to_class_id 笔记分类ID
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function mergeArticleClass(int $user_id, int $class_id, int $to_class_id): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
$count = ArticleClass::whereIn('id', [$class_id, $to_class_id])->where('user_id', $user_id)->count();
|
|
|
|
if ($count < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
return (bool)Article::where('class_id', $class_id)->where('user_id', $user_id)->update([
|
2021-09-04 20:18:20 +08:00
|
|
|
'class_id' => $to_class_id,
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
2020-11-05 17:40:51 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 编辑笔记标签
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
|
|
|
* @param int $tag_id 标签ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param string $tag_name 标签名
|
|
|
|
* @return bool|int
|
|
|
|
*/
|
|
|
|
public function editArticleTag(int $uid, int $tag_id, string $tag_name)
|
|
|
|
{
|
|
|
|
$id = ArticleTag::where('user_id', $uid)->where('tag_name', $tag_name)->value('id');
|
|
|
|
if ($tag_id) {
|
|
|
|
if ($id && $id != $tag_id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ArticleTag::where('id', $tag_id)->where('user_id', $uid)->update(['tag_name' => $tag_name]) ? $tag_id : false;
|
|
|
|
} else {
|
2021-04-22 16:14:34 +08:00
|
|
|
// 判断新添加的标签名是否存在
|
|
|
|
if ($id) return false;
|
2020-11-05 17:40:51 +08:00
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
$insRes = ArticleTag::create(['user_id' => $uid, 'tag_name' => $tag_name, 'sort' => 1]);
|
2020-11-05 17:40:51 +08:00
|
|
|
if (!$insRes) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $insRes->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除笔记标签
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $tag_id 标签ID
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function delArticleTags(int $uid, int $tag_id): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
if (!ArticleTag::where('id', $tag_id)->where('user_id', $uid)->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = Article::where('user_id', $uid)->whereRaw("FIND_IN_SET({$tag_id},tags_id)")->count();
|
2021-04-22 16:14:34 +08:00
|
|
|
if ($count > 0) return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return (bool)ArticleTag::where('id', $tag_id)->where('user_id', $uid)->delete();
|
|
|
|
} catch (Exception $e) {
|
2020-11-05 17:40:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 编辑文章信息
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $article_id 文章ID
|
|
|
|
* @param array $data 文章数据
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function editArticle(int $user_id, int $article_id, $data = [])
|
|
|
|
{
|
|
|
|
if ($article_id) {
|
2021-04-22 16:14:34 +08:00
|
|
|
if (!Article::where('id', $article_id)->where('user_id', $user_id)->first()) {
|
2020-11-05 17:40:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
Article::where('id', $article_id)->where('user_id', $user_id)->update([
|
2021-04-20 16:30:57 +08:00
|
|
|
'class_id' => $data['class_id'],
|
|
|
|
'title' => $data['title'],
|
|
|
|
'abstract' => $data['abstract'],
|
|
|
|
'image' => $data['image'] ? $data['image'][0] : '',
|
2020-11-05 17:40:51 +08:00
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
|
|
|
|
ArticleDetail::where('article_id', $article_id)->update([
|
|
|
|
'md_content' => $data['md_content'],
|
2021-04-20 16:30:57 +08:00
|
|
|
'content' => $data['content']
|
2020-11-05 17:40:51 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
return $article_id;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$res = Article::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
'user_id' => $user_id,
|
|
|
|
'class_id' => $data['class_id'],
|
|
|
|
'title' => $data['title'],
|
|
|
|
'abstract' => $data['abstract'],
|
2020-11-05 17:40:51 +08:00
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
|
|
|
|
ArticleDetail::create([
|
|
|
|
'article_id' => $res->id,
|
|
|
|
'md_content' => $data['md_content'],
|
2021-04-20 16:30:57 +08:00
|
|
|
'content' => $data['content']
|
2020-11-05 17:40:51 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
return $res->id;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新笔记状态
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $article_id 笔记ID
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $status 笔记状态 1:正常 2:已删除
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function updateArticleStatus(int $user_id, int $article_id, int $status): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-09-04 20:18:20 +08:00
|
|
|
$data = [
|
|
|
|
'status' => $status,
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
];
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
if ($status == 2) {
|
|
|
|
$data['deleted_at'] = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2021-09-04 20:18:20 +08:00
|
|
|
return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update($data);
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 笔记移动至指定分类
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $article_id 笔记ID
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $class_id 笔记分类ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function moveArticle(int $user_id, int $article_id, int $class_id): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-09-04 20:18:20 +08:00
|
|
|
return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update([
|
|
|
|
'class_id' => $class_id,
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 笔记标记星号
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $article_id 笔记ID
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $type 1:标记星号 2:取消星号标记
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function setAsteriskArticle(int $user_id, int $article_id, int $type): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-04-22 16:14:34 +08:00
|
|
|
return (bool)Article::where('id', $article_id)->where('user_id', $user_id)->update([
|
2021-09-04 20:18:20 +08:00
|
|
|
'is_asterisk' => $type == 1 ? 1 : 0,
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
2020-11-05 17:40:51 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新笔记关联标签
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
|
|
|
* @param int $article_id 笔记ID
|
|
|
|
* @param array $tags 关联标签ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function updateArticleTag(int $uid, int $article_id, array $tags): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
2021-09-04 20:18:20 +08:00
|
|
|
return (bool)Article::where('id', $article_id)->where('user_id', $uid)->update([
|
|
|
|
'tags_id' => implode(',', $tags),
|
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 永久删除回收站中的笔记
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $article_id 笔记ID
|
|
|
|
* @return bool|int|mixed|null
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function foreverDelArticle(int $uid, int $article_id)
|
|
|
|
{
|
|
|
|
$info = Article::where('id', $article_id)->where('user_id', $uid)->where('status', 2)->first(['id', 'title']);
|
|
|
|
if (!$info) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$annex_files = $info->annexs()->get(['id', 'article_id', 'save_dir'])->toArray();
|
|
|
|
|
|
|
|
//判断笔记是否存在附件,不存在直接删除
|
|
|
|
if (count($annex_files) == 0) {
|
|
|
|
return $info->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$info->detail->delete();
|
|
|
|
|
|
|
|
if (!$info->delete()) {
|
|
|
|
throw new Exception('删除笔记失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ArticleAnnex::whereIn('id', array_column($annex_files, 'id'))->delete()) {
|
|
|
|
throw new Exception('删除笔记附件失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 从磁盘中永久删除文件附件
|
2021-04-22 16:14:34 +08:00
|
|
|
//foreach ($annex_files as $item) {
|
|
|
|
//Storage::disk('uploads')->delete($item['save_dir']);
|
|
|
|
//}
|
2020-11-05 17:40:51 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新笔记附件状态
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $annex_id 附件ID
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $status 附件状态 1:正常 2:已删除
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function updateArticleAnnexStatus(int $user_id, int $annex_id, int $status): bool
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
$data = ['status' => $status];
|
|
|
|
if ($status == 2) {
|
|
|
|
$data['deleted_at'] = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:14:34 +08:00
|
|
|
return (bool)ArticleAnnex::where('id', $annex_id)->where('user_id', $user_id)->update($data);
|
2020-11-05 17:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 回收站附件列表
|
|
|
|
*
|
|
|
|
* @param int $uid 用户ID
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-09-04 20:18:20 +08:00
|
|
|
public function recoverAnnexList(int $uid): array
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
return ArticleAnnex::join('article', 'article.id', '=', 'article_annex.article_id')
|
|
|
|
->where('article_annex.user_id', $uid)
|
|
|
|
->where('article_annex.status', 2)
|
|
|
|
->get([
|
|
|
|
'article_annex.id',
|
|
|
|
'article_annex.article_id',
|
|
|
|
'article.title',
|
|
|
|
'article_annex.original_name',
|
|
|
|
'article_annex.deleted_at'
|
|
|
|
])->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 永久删除笔记附件(从磁盘中永久删除)
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $uid 用户ID
|
2020-11-05 17:40:51 +08:00
|
|
|
* @param int $annex_id 笔记附件ID
|
2021-04-22 16:14:34 +08:00
|
|
|
* @return mixed
|
2020-11-05 17:40:51 +08:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function foreverDelAnnex(int $uid, int $annex_id)
|
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$info = ArticleAnnex::where('id', $annex_id)->where('user_id', $uid)->where('status', 2)->first(['id', 'path']);
|
2020-11-05 17:40:51 +08:00
|
|
|
if (!$info) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $info->delete();
|
|
|
|
}
|
2020-11-27 19:48:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加笔记附件
|
|
|
|
*
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param int $user_id 用户id
|
|
|
|
* @param int $article_id 笔记ID
|
|
|
|
* @param array $annex 笔记附件信息
|
2021-09-04 20:18:20 +08:00
|
|
|
* @return bool|int
|
2020-11-27 19:48:41 +08:00
|
|
|
*/
|
|
|
|
public function insertArticleAnnex(int $user_id, int $article_id, array $annex)
|
|
|
|
{
|
|
|
|
if (!Article::where('id', $article_id)->where('user_id', $user_id)->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = ArticleAnnex::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
'user_id' => $user_id,
|
|
|
|
'article_id' => $article_id,
|
2022-01-16 10:29:16 +08:00
|
|
|
'suffix' => $annex['suffix'],
|
|
|
|
'size' => $annex['size'],
|
|
|
|
'drive' => $annex['drive'],
|
|
|
|
'path' => $annex['path'],
|
2020-11-27 19:48:41 +08:00
|
|
|
'original_name' => $annex['original_name'],
|
2022-01-16 10:29:16 +08:00
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
2020-11-27 19:48:41 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $result ? $result->id : false;
|
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
}
|