mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
1.删除chapter中resource_count,consult_count属性
2.重新统计course中resource_count
This commit is contained in:
parent
9b7a244fa3
commit
c4259d78f3
@ -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)
|
||||
|
||||
### 项目文档
|
||||
|
@ -1,58 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 深圳市酷瓜软件有限公司
|
||||
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Console\Migrations;
|
||||
|
||||
use App\Models\Course as CourseModel;
|
||||
use App\Repos\Chapter as ChapterRepo;
|
||||
use App\Repos\Course as CourseRepo;
|
||||
|
||||
class V20230817240809 extends Migration
|
||||
{
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
|
@ -20,7 +20,6 @@
|
||||
<div class="layui-input-block">
|
||||
<button class="layui-btn" lay-submit="true" lay-filter="go">提交</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
|
||||
<input type="hidden" name="chapter_id" value="{{ request.get('chapter_id') }}">
|
||||
<input type="hidden" name="course_id" value="{{ request.get('course_id') }}">
|
||||
</div>
|
||||
</div>
|
||||
|
@ -5,18 +5,10 @@
|
||||
{% set consult.answer = consult.answer ? consult.answer : '请耐心等待回复吧' %}
|
||||
|
||||
<div class="consult-info">
|
||||
{% if consult.course.id is defined %}
|
||||
<div class="item">
|
||||
<div class="label">课程:</div>
|
||||
<div class="title">{{ consult.course.title }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if consult.chapter.id is defined %}
|
||||
<div class="item">
|
||||
<div class="label">章节:</div>
|
||||
<div class="title">{{ consult.chapter.title }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="item">
|
||||
<div class="label">课程:</div>
|
||||
<div class="title">{{ consult.course.title }}</div>
|
||||
</div>
|
||||
<div class="item">
|
||||
<div class="label">咨询:</div>
|
||||
<div class="content">{{ consult.question }}</div>
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 评论数
|
||||
*
|
||||
|
@ -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],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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,
|
||||
|
73
db/migrations/20240111185333.php
Normal file
73
db/migrations/20240111185333.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2024 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class V20240111185333 extends AbstractMigration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2024 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class V20240111185633 extends AbstractMigration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user