StudyCountController.class.php
5.6 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
<?php
/**
* Created by PhpStorm.
* User: zhonglei
* Date: 17/10/17
* Time: 11:49
*/
namespace Apicp\Controller\CourseData;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\DataHelper;
use Common\Model\CourseClassModel;
use Common\Model\CourseArticleModel;
use Common\Model\CourseCountModel;
class StudyCountController extends \Apicp\Controller\AbstractController
{
/**
* StudyCount
* @author zhonglei
* @desc 学习人数情况统计报表接口
* @param Int class_id 分类ID
* @param String article_title 课程名称
* @param Int created_start 发布时间戳
* @param Int created_end 发布时间戳
* @param Int starttime:true 开始时间戳
* @param Int endtime:true 结束时间戳
* @return array
array(
'days' => array( // X轴日期
'2017.10.1',
),
'unstudy' => array( // 未学习人数数据
'100',
),
'studying' => array( // 学习中人数数据
'50',
),
'complete' => array( // 已完成人数数据
'90',
),
)
*/
public function Index_post()
{
$this->_result = [
'days' => [],
'unstudy' => [],
'studying' => [],
'complete' => [],
];
// 请求数据
$post_data = I('post.');
// 验证规则
$rules = [
'class_id' => 'integer',
'article_title' => 'max:64',
'created_start' => 'integer',
'created_end' => 'integer',
'starttime' => 'require|integer',
'endtime' => 'require|integer',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $post_data;
$validate->validateParams($rules);
// 获取分类数据
$classModel = new CourseClassModel();
$class_list = $classModel->list_all();
if (empty($class_list)) {
return;
}
$class_data = array_combine_by_key($class_list, 'class_id');
// 格式化分类数据
foreach ($class_data as $class_id => $class) {
$class_data[$class['parent_id']]['children'][$class_id] = &$class_data[$class_id];
}
if (isset($post_data['class_id'])) {
$class_id = $post_data['class_id'];
$ids = [];
// 未找到分类
if (!isset($class_data[$class_id])) {
return;
}
// 获取分类自身ID和所有子分类ID
$this->_getSelfChildId($class_data[$class_id], $ids);
$post_data['class_id'] = $ids;
}
$dataHelper = &DataHelper::instance();
// 搜索条件
$conds = ['course_type' => Constant::COURSE_TYPE_NORMAL];
if (isset($post_data['class_id'])) {
$conds['class_id'] = $post_data['class_id'];
}
if (isset($post_data['article_title'])) {
$conds['article_title like ?'] = "%{$post_data['article_title']}%";
}
if (isset($post_data['created_start'])) {
$conds['created >= ?'] = $dataHelper->formatStarttime($post_data['created_start']);
}
if (isset($post_data['created_end'])) {
$conds['created <= ?'] = $dataHelper->formatEndtime($post_data['created_end']);
}
// 获取课程列表
$articleModel = new CourseArticleModel();
$article_list = $articleModel->list_by_conds($conds);
if (empty($article_list)) {
return;
}
$article_ids = array_column($article_list, 'article_id');
$starttime = $post_data['starttime'];
$endtime = $post_data['endtime'];
// 获取统计数据
$countModel = new CourseCountModel();
$count_list = $countModel->list_by_conds([
'article_id' => $article_ids,
'count_date >= ?' => rgmdate($starttime, 'Y-m-d'),
'count_date <= ?' => rgmdate($endtime, 'Y-m-d'),
]);
$count_data = [];
$keys = ['user_total', 'studying_total', 'complete_total', 'unstudy_total'];
// 格式化数据
foreach ($count_list as $v) {
$date = $v['count_date'];
if (!isset($count_data[$date])) {
foreach ($keys as $k) {
$count_data[$date][$k] = 0;
}
}
foreach ($keys as $k) {
$count_data[$date][$k] += $v[$k];
}
}
// 整合数据
$this->_result['days'] = $dataHelper->getEachDay($starttime, $endtime);
foreach ($this->_result['days'] as $day) {
$this->_result['unstudy'][] = isset($count_data[$day]) ? $count_data[$day]['unstudy_total'] : 0;
$this->_result['studying'][] = isset($count_data[$day]) ? $count_data[$day]['studying_total'] : 0;
$this->_result['complete'][] = isset($count_data[$day]) ? $count_data[$day]['complete_total'] : 0;
}
}
/**
* 获取分类自身ID和所有子分类ID
* @author zhonglei
* @param array $class 分类数据
* @param array $ids 分类自身ID和所有子分类ID数组(引用)
* @return void
*/
private function _getSelfChildId($class, &$ids)
{
$ids[] = $class['class_id'];
// 包含子级分类
if (isset($class['children'])) {
foreach ($class['children'] as $child) {
$this->_getSelfChildId($child, $ids);
}
}
}
}