From c4259d78f30731c304fbbcce23bf044a10be7812 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Wed, 31 Jan 2024 18:01:25 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=88=A0=E9=99=A4chapter=E4=B8=ADresource=5F?= =?UTF-8?q?count,consult=5Fcount=E5=B1=9E=E6=80=A7=202.=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1course=E4=B8=ADresource=5Fcount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- app/Console/Migrations/V20230817240809.php | 58 ----------------- app/Http/Admin/Services/Consult.php | 58 ++++++----------- app/Http/Home/Views/consult/add.volt | 1 - app/Http/Home/Views/consult/show.volt | 16 ++--- app/Models/Chapter.php | 14 ----- app/Repos/Chapter.php | 18 ------ app/Services/Logic/Chapter/BasicInfo.php | 6 -- db/migrations/20240111185333.php | 73 ++++++++++++++++++++++ db/migrations/20240111185633.php | 32 ---------- 10 files changed, 97 insertions(+), 181 deletions(-) delete mode 100644 app/Console/Migrations/V20230817240809.php create mode 100644 db/migrations/20240111185333.php delete mode 100644 db/migrations/20240111185633.php diff --git a/README.md b/README.md index ff312fb0..e33aaf68 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Tips: 请用手机注册一个新账号,用户中心 -> 关注订阅,扫码 - 后台框架:[phalcon 3.4.5](https://phalcon.io) - 前端框架:[layui 2.9.3](https://layui.dev) -- 全文检索:[xunsearch 1.4.9](http://www.xunsearch.com) +- 全文检索:[xunsearch 1.4.17](http://www.xunsearch.com) - 基础依赖:[php7.3](https://php.net), [mysql5.7](https://mysql.com), [redis5.0](https://redis.io) ### 项目文档 diff --git a/app/Console/Migrations/V20230817240809.php b/app/Console/Migrations/V20230817240809.php deleted file mode 100644 index 0da4357b..00000000 --- a/app/Console/Migrations/V20230817240809.php +++ /dev/null @@ -1,58 +0,0 @@ -handleCourseResourceCount(); - } - - protected function handleCourseResourceCount() - { - $courses = CourseModel::find(); - - if ($courses->count() == 0) return; - - foreach ($courses as $course) { - if ($course->resource_count > 0) { - $this->recountCourseResources($course); - } - } - } - - protected function recountCourseResources(CourseModel $course) - { - $courseRepo = new CourseRepo(); - - $lessons = $courseRepo->findLessons($course->id); - - $chapterRepo = new ChapterRepo(); - - $resourceCount = 0; - - if ($lessons->count() > 0) { - foreach ($lessons as $lesson) { - if ($lesson->deleted == 0) { - $resourceCount += $chapterRepo->countResources($lesson->id); - } - } - } - - $course->resource_count = $resourceCount; - - $course->update(); - } - -} \ No newline at end of file diff --git a/app/Http/Admin/Services/Consult.php b/app/Http/Admin/Services/Consult.php index 9a44d10a..9cc67cf4 100644 --- a/app/Http/Admin/Services/Consult.php +++ b/app/Http/Admin/Services/Consult.php @@ -10,12 +10,10 @@ namespace App\Http\Admin\Services; use App\Builders\ConsultList as ConsultListBuilder; use App\Http\Admin\Services\Traits\AccountSearchTrait; use App\Library\Paginator\Query as PagerQuery; -use App\Models\Chapter as ChapterModel; use App\Models\Consult as ConsultModel; use App\Models\Course as CourseModel; use App\Models\Reason as ReasonModel; use App\Models\User as UserModel; -use App\Repos\Chapter as ChapterRepo; use App\Repos\Consult as ConsultRepo; use App\Repos\Course as CourseRepo; use App\Services\Logic\Consult\ConsultInfo as ConsultInfoService; @@ -104,6 +102,8 @@ class Consult extends Service { $consult = $this->findOrFail($id); + $course = $this->findCourse($consult->course_id); + $post = $this->request->getPost(); $validator = new ConsultValidator(); @@ -138,7 +138,7 @@ class Consult extends Service $this->handleReplyNotice($consult); } - $this->recountItemConsults($consult); + $this->recountCourseConsults($course); $this->eventsManager->fire('Consult:afterUpdate', $this, $consult); @@ -153,7 +153,9 @@ class Consult extends Service $consult->update(); - $this->recountItemConsults($consult); + $course = $this->findCourse($consult->course_id); + + $this->recountCourseConsults($course); $sender = $this->getLoginUser(); @@ -170,7 +172,9 @@ class Consult extends Service $consult->update(); - $this->recountItemConsults($consult); + $course = $this->findCourse($consult->course_id); + + $this->recountCourseConsults($course); $this->eventsManager->fire('Consult:afterRestore', $this, $consult); } @@ -202,7 +206,9 @@ class Consult extends Service $this->eventsManager->fire('Consult:afterReject', $this, $consult); } - $this->recountItemConsults($consult); + $course = $this->findCourse($consult->course_id); + + $this->recountCourseConsults($course); return $consult; } @@ -237,7 +243,9 @@ class Consult extends Service $this->handleConsultRejectedNotice($consult, $sender); } - $this->recountItemConsults($consult); + $course = $this->findCourse($consult->course_id); + + $this->recountCourseConsults($course); } } @@ -259,7 +267,10 @@ class Consult extends Service $consult->update(); $this->handleConsultDeletedNotice($consult, $sender); - $this->recountItemConsults($consult); + + $course = $this->findCourse($consult->course_id); + + $this->recountCourseConsults($course); } } @@ -277,13 +288,6 @@ class Consult extends Service return $courseRepo->findById($id); } - protected function findChapter($id) - { - $chapterRepo = new ChapterRepo(); - - return $chapterRepo->findById($id); - } - protected function handleReplyNotice(ConsultModel $consult) { $notice = new ConsultReplyNotice(); @@ -306,19 +310,6 @@ class Consult extends Service } - protected function recountItemConsults(ConsultModel $consult) - { - if ($consult->course_id > 0) { - $course = $this->findCourse($consult->course_id); - $this->recountCourseConsults($course); - } - - if ($consult->chapter_id > 0) { - $chapter = $this->findChapter($consult->chapter_id); - $this->recountChapterConsults($chapter); - } - } - protected function recountCourseConsults(CourseModel $course) { $courseRepo = new CourseRepo(); @@ -330,17 +321,6 @@ class Consult extends Service $course->update(); } - protected function recountChapterConsults(ChapterModel $chapter) - { - $chapterRepo = new ChapterRepo(); - - $consultCount = $chapterRepo->countConsults($chapter->id); - - $chapter->consult_count = $consultCount; - - $chapter->update(); - } - protected function handleConsults($pager) { if ($pager->total_items > 0) { diff --git a/app/Http/Home/Views/consult/add.volt b/app/Http/Home/Views/consult/add.volt index 6e1f5706..040225f7 100644 --- a/app/Http/Home/Views/consult/add.volt +++ b/app/Http/Home/Views/consult/add.volt @@ -20,7 +20,6 @@
-
diff --git a/app/Http/Home/Views/consult/show.volt b/app/Http/Home/Views/consult/show.volt index d2d1fe11..4b980b27 100644 --- a/app/Http/Home/Views/consult/show.volt +++ b/app/Http/Home/Views/consult/show.volt @@ -5,18 +5,10 @@ {% set consult.answer = consult.answer ? consult.answer : '请耐心等待回复吧' %}
- {% if consult.course.id is defined %} -
-
课程:
-
{{ consult.course.title }}
-
- {% endif %} - {% if consult.chapter.id is defined %} -
-
章节:
-
{{ consult.chapter.title }}
-
- {% endif %} +
+
课程:
+
{{ consult.course.title }}
+
咨询:
{{ consult.question }}
diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index d04fea92..ed910a9c 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -147,13 +147,6 @@ class Chapter extends Model */ public $deleted = 0; - /** - * 资源数 - * - * @var int - */ - public $resource_count = 0; - /** * 课时数 * @@ -168,13 +161,6 @@ class Chapter extends Model */ public $user_count = 0; - /** - * 咨询数 - * - * @var int - */ - public $consult_count = 0; - /** * 评论数 * diff --git a/app/Repos/Chapter.php b/app/Repos/Chapter.php index 8f1e63e1..144f8f6e 100644 --- a/app/Repos/Chapter.php +++ b/app/Repos/Chapter.php @@ -15,8 +15,6 @@ use App\Models\ChapterRead as ChapterReadModel; use App\Models\ChapterUser as ChapterUserModel; use App\Models\ChapterVod as ChapterVodModel; use App\Models\Comment as CommentModel; -use App\Models\Consult as ConsultModel; -use App\Models\Resource as ResourceModel; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -186,14 +184,6 @@ class Chapter extends Repository ]); } - public function countConsults($chapterId) - { - return (int)ConsultModel::count([ - 'conditions' => 'chapter_id = :chapter_id: AND published = 1 AND deleted = 0', - 'bind' => ['chapter_id' => $chapterId], - ]); - } - public function countUsers($chapterId) { return (int)ChapterUserModel::count([ @@ -218,12 +208,4 @@ class Chapter extends Repository ]); } - public function countResources($chapterId) - { - return (int)ResourceModel::count([ - 'conditions' => 'chapter_id = :chapter_id:', - 'bind' => ['chapter_id' => $chapterId], - ]); - } - } diff --git a/app/Services/Logic/Chapter/BasicInfo.php b/app/Services/Logic/Chapter/BasicInfo.php index 9ee4de76..d81a9eba 100644 --- a/app/Services/Logic/Chapter/BasicInfo.php +++ b/app/Services/Logic/Chapter/BasicInfo.php @@ -85,9 +85,7 @@ class BasicInfo extends LogicService 'published' => $chapter->published, 'deleted' => $chapter->deleted, 'play_urls' => $playUrls, - 'resource_count' => $chapter->resource_count, 'comment_count' => $chapter->comment_count, - 'consult_count' => $chapter->consult_count, 'user_count' => $chapter->user_count, 'like_count' => $chapter->like_count, 'create_time' => $chapter->create_time, @@ -118,9 +116,7 @@ class BasicInfo extends LogicService 'start_time' => $live->start_time, 'end_time' => $live->end_time, 'status' => $live->status, - 'resource_count' => $chapter->resource_count, 'comment_count' => $chapter->comment_count, - 'consult_count' => $chapter->consult_count, 'user_count' => $chapter->user_count, 'like_count' => $chapter->like_count, 'create_time' => $chapter->create_time, @@ -142,9 +138,7 @@ class BasicInfo extends LogicService 'content' => $read->content, 'published' => $chapter->published, 'deleted' => $chapter->deleted, - 'resource_count' => $chapter->resource_count, 'comment_count' => $chapter->comment_count, - 'consult_count' => $chapter->consult_count, 'user_count' => $chapter->user_count, 'like_count' => $chapter->like_count, 'create_time' => $chapter->create_time, diff --git a/db/migrations/20240111185333.php b/db/migrations/20240111185333.php new file mode 100644 index 00000000..723f926d --- /dev/null +++ b/db/migrations/20240111185333.php @@ -0,0 +1,73 @@ +alterChapterTable(); + $this->handleChapterUsers(); + $this->recountCourseResources(); + } + + protected function alterChapterTable() + { + $table = $this->table('kg_chapter'); + + if ($table->hasColumn('resource_count')) { + $table->removeColumn('resource_count'); + } + + if ($table->hasColumn('consult_count')) { + $table->removeColumn('consult_count'); + } + + $table->save(); + } + + /** + * 纠正 chapter_user 表中 plan_id = 0 的数据 + * + * @return void + */ + protected function handleChapterUsers() + { + $sql = 'UPDATE kg_chapter_user AS a JOIN kg_course_user AS b + ON a.course_id = b.course_id AND a.user_id = b.user_id + SET a.plan_id = b.plan_id WHERE a.plan_id = 0'; + + $this->query($sql); + } + + protected function recountCourseResources() + { + $courses = $this->getQueryBuilder() + ->select(['id']) + ->from('kg_course') + ->execute()->fetchAll(PDO::FETCH_ASSOC); + + if (count($courses) == 0) return; + + foreach ($courses as $course) { + $resourceCount = $this->getQueryBuilder() + ->select(['id']) + ->from('kg_resource') + ->where(['course_id' => $course['id']]) + ->execute()->count(); + + $this->getQueryBuilder() + ->update('kg_course') + ->set('resource_count', $resourceCount) + ->where(['id' => $course['id']]) + ->execute(); + } + } + +} diff --git a/db/migrations/20240111185633.php b/db/migrations/20240111185633.php deleted file mode 100644 index 75e433d9..00000000 --- a/db/migrations/20240111185633.php +++ /dev/null @@ -1,32 +0,0 @@ -handleChapterUsers(); - } - - /** - * 纠正 chapter_user 表中 plan_id = 0 的数据 - * - * @return void - */ - protected function handleChapterUsers() - { - $sql = 'UPDATE kg_chapter_user AS a JOIN kg_course_user AS b - ON a.course_id = b.course_id AND a.user_id = b.user_id - SET a.plan_id = b.plan_id WHERE a.plan_id = 0'; - - $this->query($sql); - } - -}