ArticleChapterService.class.php
6.38 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/12
* Time: 10:46
*/
namespace Common\Service;
use Common\Model\ArticleChapterModel;
class ArticleChapterService extends AbstractService
{
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new ArticleChapterModel();
}
/**
* 格式化章节数据
* @author liyifei
* @param Int $article_id 课程ID
* @return array
*/
public function formatDBData($article_id)
{
$conds = ['article_id' => $article_id];
$orders = [
'parent_id' => 'asc',
'`order`' => 'asc',
];
$chapter_list = $this->list_by_conds($conds, null, $orders);
if (empty($chapter_list)) {
return [];
}
$formatData = [];
foreach ($chapter_list as $chapter) {
if ($chapter['parent_id'] == 0) {
$formatData[$chapter['article_chapter_id']] = [
'article_chapter_id' => $chapter['article_chapter_id'],
'chapter_name' => $chapter['chapter_name'],
'order' => $chapter['order'],
'source' => [],
];
}
if ($chapter['parent_id'] > 0) {
$formatData[$chapter['parent_id']]['source'][$chapter['article_chapter_id']] = [
'article_chapter_id' => $chapter['article_chapter_id'],
'source_id' => $chapter['source_id'],
'et_ids' => empty($chapter['et_ids']) ? [] : unserialize($chapter['et_ids']),
'rand_num' => $chapter['rand_num'],
'order' => $chapter['order'],
];
}
}
return $formatData;
}
/**
* 保存课程、章节关系
* @author liyifei
* @param int $article_id 课程ID
* @param array $chapters 章节信息
* @return bool
*/
public function saveData($article_id, $chapters)
{
// 筛选出被删除的章节
$chapters_old = $this->formatDBData($article_id);
if (!empty($chapters_old)) {
// 筛选出被删除的章节
$chapter_id_old = array_column($chapters_old, 'article_chapter_id');
$chapter_id_new = array_unique(array_column($chapters, 'article_chapter_id'));
// 新章节不参与对比(即章节ID:article_chapter_id=0)
$unset_key = array_search(0, $chapter_id_new);
if ($unset_key !== false) {
unset($chapter_id_new[$unset_key]);
}
$chapter_id_new = array_values($chapter_id_new);
$delete_chapter_ids = array_diff($chapter_id_old, $chapter_id_new);
// 对比相同章节的素材
foreach ($chapters as $chapter) {
foreach ($chapters_old as $chapter_old) {
// 跳过将被删除的章节
if (!empty($delete_chapter_ids) && in_array($chapter_old['article_chapter_id'], $delete_chapter_ids)) {
continue;
}
if ($chapter['article_chapter_id'] == $chapter_old['article_chapter_id']) {
// 筛选出被删除的素材
$source_key_old = array_column($chapter_old['source'], 'article_chapter_id');
$source_key_new = array_column($chapter['source'], 'article_chapter_id');
// 新素材不参与对比(即素材主键ID:article_chapter_id=0)
$unset_key = array_search(0, $source_key_new);
if ($unset_key !== false) {
unset($source_key_new[$unset_key]);
}
$source_key_new = array_values($source_key_new);
$diff_chapter_ids = array_diff($source_key_old, $source_key_new);
$delete_chapter_ids = array_merge($delete_chapter_ids, $diff_chapter_ids);
}
}
}
}
// 删除章节和素材
if (!empty($delete_chapter_ids)) {
// 删除素材
$this->delete_by_conds(['parent_id' => $delete_chapter_ids]);
// 删除章节、素材
$this->delete($delete_chapter_ids);
}
// 新增、修改章节
$insert_data = [];
foreach ($chapters as $chapter) {
$article_chapter_id = $chapter['article_chapter_id'];
// 新增章节,获取章节ID
if ($article_chapter_id == 0) {
$article_chapter_id = $this->insert([
'article_id' => $article_id,
'parent_id' => 0,
'source_id' => 0,
'chapter_name' => $chapter['chapter_name'],
'order' => $chapter['order'],
]);
} else { // 修改章节数据
$chapter_data = [
'article_id' => $article_id,
'parent_id' => 0,
'source_id' => 0,
'chapter_name' => $chapter['chapter_name'],
'order' => $chapter['order'],
];
$this->update_by_conds(['article_chapter_id' => $chapter['article_chapter_id']], $chapter_data);
}
// 组合写入素材数据的sql
foreach ($chapter['source'] as $source) {
$source_data = [
'article_id' => $article_id,
'parent_id' => $article_chapter_id,
'source_id' => $source['source_id'],
'chapter_name' => $chapter['chapter_name'],
'et_ids' => empty($source['et_ids']) ? '' : serialize($source['et_ids']),
'rand_num' => $source['rand_num'],
'order' => intval($source['order']),
];
// 新增素材
if ($source['article_chapter_id'] == 0) {
$insert_data[] = $source_data;
} else { // 修改素材
$this->update_by_conds(['article_chapter_id' => $source['article_chapter_id']], $source_data);
}
}
}
if (!empty($insert_data)) {
$this->insert_all($insert_data);
}
return true;
}
}