ReadListController.class.php
9.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/13
* Time: 11:48
*/
namespace Apicp\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\User;
use Common\Service\ArticleService;
use Common\Service\ReadService;
use Common\Service\RightService;
class ReadListController extends \Apicp\Controller\AbstractController
{
/**
* ReadList
* @desc 阅读列表
* @param Int article_id:true 新闻ID
* @param Int read_type:true 阅读类型(1=未读,2=已读)
* @param Int page 页码
* @param Int limit 每页数据条数
* @return array 阅读列表
* array(
'article_id' => 123, // 新闻ID
'read_type' => 1, // 阅读类型(1=未读,2=已读)
'title' => '', // 新闻标题
'send_time' => '', // 更新时间
'page' => '', // 页码
'limit' => '', // 每页数据条数
'read_total' => '', // 已读总数
'unread_total' => '', // 未读总数
'list' => array(
'username' => '张三', // 姓名
'dp_name' => array('技术部'), // 所属部门
'job' => 'PHP', // 职位
'mobile' => '15821392414', // 手机号码
'created' => '1234566898988', // 阅读时间(毫秒级时间戳)
),
);
*/
public function Index_post()
{
// 验证规则
$rules = [
'article_id' => 'require|integer',
'read_type' => 'require|integer',
'page' => 'integer',
'limit' => 'integer',
'dp_ids' => 'array',
'job_ids' => 'array',
'role_ids' => 'array',
'username' => 'max:64',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 默认值
$postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$postData['limit'] = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
// 查询条件
$uidCond = $this->formatConds($postData);
// 取新闻信息
$articleServ = new ArticleService();
$newsInfo = $articleServ->get($postData['article_id']);
if (empty($newsInfo)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
$list = [];
$total = 0;
$readServ = new ReadService();
$read_total = 0;
$unread_total = 0;
if ($uidCond !== false) {
// 已读
$readConds = ['article_id' => $postData['article_id']];
$read_total = $readServ->count_by_conds($readConds);
if (!empty($uidCond)) {
$readConds['uid'] = $uidCond;
}
if ($postData['read_type'] == Constant::READ_STATUS_IS_YES) {
list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
$readList = $readServ->list_by_conds($readConds, [$start, $perpage]);
$userServ = &User::instance();
$userlist = $userServ->listByUid(array_column($readList, 'uid'));
$userlist = array_combine_by_key($userlist, 'memUid');
foreach ($readList as &$item) {
$uid = $item['uid'];
$list[] = [
'username' => $userlist[$uid]['memUsername'],
'dp_name' => empty($userlist[$uid]['dpName']) ?
[] : array_column($userlist[$uid]['dpName'], 'dpName'),
'job' => $userlist[$uid]['memJob'],
'role' => $userlist[$uid]['memRole'],
'mobile' => $userlist[$uid]['memMobile'],
'created' => $item['created']
];
}
$total = $read_total;
}
// 未读
list($uids_all, $uids_read, $uids_unread, $unread_total) =
$this->getReadData($postData['article_id'], $uidCond);
if ($postData['read_type'] == Constant::READ_STATUS_IS_NO) {
$total = count($uids_unread);
// 取未读人员信息
if ($uids_unread) {
$userServ = &User::instance();
$userList = $userServ->listByConds(['memUids' => $uids_unread], $postData['page'], $postData['limit']);
if ($userList) {
foreach ($userList['list'] as $v) {
$list[] = [
'uid' => $v['memUid'],
'username' => $v['memUsername'],
'dp_name' => empty($v['dpName']) ? [] : array_column($v['dpName'], 'dpName'),
'job' => $v['memJob'],
'role' => $v['memRole'],
'mobile' => $v['memMobile'],
];
}
}
}
}
}
$this->_result = [
'article_id' => $postData['article_id'],
'read_type' => $postData['read_type'],
'limit' => $postData['limit'],
'page' => $postData['page'],
'title' => $newsInfo['title'],
'send_time' => $newsInfo['send_time'],
'read_total' => $read_total,
'unread_total' => $unread_total,
'total' => $total,
'list' => $list,
];
}
/**
* 将搜索条件转化为用户UID
* @author tangxingguo
* @param array $postData 用户提交的数据
* @return array|bool
*/
private function formatConds($postData)
{
$condUids = [];
$userServ = &User::instance();
if (isset($postData['username'])) {
// UC
$userConds = ['memUsername' => $postData['username']];
$list = $userServ->listAll($userConds);
$condUids = array_column($list, 'memUid');
}
// 部门、岗位、角色搜索交集
$right = array_intersect_key_reserved($postData, ['dp_ids', 'job_ids', 'role_ids'], true);
// 部门/岗位存在 名字存在
if (!empty($right) && isset($postData['username'])) {
$rightServ = new RightService();
$rights = $rightServ->formatPostData($right);
$unitUids = $rightServ->getUidsByRight($rights);
if (!empty($condUids)) {
// 筛选已经有值,取交集
$condUids = array_intersect($condUids, $unitUids);
} else {
//$condUids = $unitUids;
return false;
}
}
// 部门/岗位存在 名字存在 人名不存在
if (!empty($right) && !isset($postData['username'])) {
$rightServ = new RightService();
$rights = $rightServ->formatPostData($right);
$unitUids = $rightServ->getUidsByRight($rights);
$condUids = $unitUids;
}
// 如果有查询 并且 结果为空
if ((!empty($right) || !empty($postData['username'])) && empty($condUids)) {
return false;
}
return $condUids;
}
/**
* 获取新闻可读、已读、未读人员ID数组
* @author zhonglei
* @param int $article_id 新闻ID
* @return array [0 => 可读人员ID, 1 => 已读人员ID, 2 => 未读人员ID]
*/
public function getReadData($article_id, $uidCond)
{
$uids_all = [];
$uids_read = [];
$uids_unread = [];
// 查询条件
$conds = ['article_id' => $article_id];
// 获取权限数据
$rightServ = new RightService();
$right_list = $rightServ->list_by_conds($conds);
$rights = $rightServ->formatDBData($right_list);
if (empty($rights)) {
return [$uids_all, $uids_read, $uids_unread];
}
// 获取可读数据
$uids_all = $rightServ->getUidsByRight($rights);
// 获取已读数据
$readServ = new ReadService();
if (!empty($uidCond)) {
$conds['uid'] = $uidCond;
}
$study_list = $readServ->list_by_conds($conds);
$uids_read = array_column($study_list, 'uid');
$uids_unread = array_values(
array_diff(
// 如果外部传了条件
!empty($uidCond) ? array_intersect($uids_all, $uidCond) : $uids_all,
$uids_read
)
);
// 已经学习人数 (跟传入条件无关)
$studyListWithOutConds = $readServ->list_by_conds(['article_id' => $article_id]);
$studyListWithOutConds = array_column($studyListWithOutConds, 'uid');
// 未读数据
return [$uids_all, $uids_read, $uids_unread, count(array_diff($uids_all, $studyListWithOutConds))];
}
}