IndexController.class.php
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/7/16
* Time: 22:42
*/
namespace Frontend\Controller\UpdateDB;
use Common\Service\ArticleSourceService;
use Common\Service\ArticleChapterService;
use Common\Service\StudyRecordService;
class IndexController extends AbstractController
{
/**
* v2.0.1版本迭代,数据库升级脚本
* @author liyifei
*/
public function Index()
{
echo "升级 oa_course_article_chapter 表,请执行 Chapter 方法;<br />";
echo "升级 oa_course_study_record 表,请执行 StudyRecord 方法。<br />";
exit('SUCCESS');
}
/**
* 将oa_course_article_source表数据,转存至oa_course_article_chapter表,并新增章节数据
* @author liyifei
*/
public function Chapter()
{
$articleSourceServ = new ArticleSourceService();
$chapterServ = new ArticleChapterService();
$orders = [
'article_id' => 'asc',
'`order`' => 'asc',
'created' => 'asc',
];
$asList = $articleSourceServ->list_all(null, $orders);
if (!empty($asList)) {
// 获取并去重课程ID
$articleIds = array_column($asList, 'article_id');
$articleIds = array_values(array_unique($articleIds));
// 确认所有课程在章节表中无数据
$acList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id' => 0, 'source_id' => 0]);
if (!empty($acList)) {
$chapterArticleIds = array_column($acList, 'article_id');
$chapterArticleIds = array_values(array_unique($chapterArticleIds));
exit('以下课程ID已存在章节数据:' . implode(',', $chapterArticleIds));
}
// 为每个课程创建一个章节
$insertChapterData = [];
foreach ($articleIds as $articleId) {
$insertChapterData[] = [
'article_id' => $articleId,
'parent_id' => 0,
'source_id' => 0,
'chapter_name' => '章节1',
'et_ids' => '',
'rand_num' => 0,
'order' => 1,
];
}
$insertChapterRes = $chapterServ->insert_all($insertChapterData);
if (empty($insertChapterRes)) {
exit('为课程创建章节失败');
}
// 获取课程新创建的章节信息
$chapterList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id' => 0, 'source_id' => 0]);
if (!empty($chapterList)) {
$chapterList = array_combine_by_key($chapterList, 'article_id');
// 将课程素材写入章节表
$insertSourceData = [];
foreach ($asList as $source) {
$articleId = $source['article_id'];
$insertSourceData[] = [
'article_id' => $source['article_id'],
'parent_id' => $chapterList[$articleId]['article_chapter_id'],
'source_id' => $source['source_id'],
'chapter_name' => $chapterList[$articleId]['chapter_name'],
'et_ids' => '',
'rand_num' => 0,
'order' => $source['order'] + 1,
'status' => $source['status'],
'created' => $source['created'],
'updated' => $source['updated'],
];
}
$insertSourceRes = $chapterServ->insert_all($insertSourceData);
if (empty($insertSourceRes)) {
exit('为章节创建素材失败');
}
}
}
exit('SUCCESS');
}
/**
* 为学习记录表,录入章节信息
* @author liyifei
*/
public function StudyRecord()
{
$recordServ = new StudyRecordService();
$chapterServ = new ArticleChapterService();
// 获取未写入章节信息的学习记录
$recordList = $recordServ->list_by_conds(['article_chapter_id' => 0]);
if (!empty($recordList)) {
// 获取课程ID
$articleIds = array_column($recordList, 'article_id');
$articleIds = array_values(array_unique($articleIds));
// 获取素材ID
$sourceIds = array_column($recordList, 'source_id');
$sourceIds = array_values(array_unique($sourceIds));
// 获取课程对应的章节数据
$chapterList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id > ?' => 0, 'source_id' => $sourceIds]);
if (empty($chapterList)) {
exit('学习的素材无对应的章节数据,请先创建章节!');
}
// 为学习的课程素材,补上对应的章节ID
foreach ($recordList as $record) {
foreach ($chapterList as $chapter) {
if ($record['article_id'] == $chapter['article_id'] && $record['source_id'] == $chapter['source_id']) {
$data = [
'status' => $record['status'],
'updated' => $record['updated'],
'article_chapter_id' => $chapter['parent_id'],
];
$recordServ->update($record['study_record_id'], $data);
}
}
}
}
exit('SUCCESS');
}
}