ExportReadListController.class.php
5.7 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 18/7/5
* Time: 11:22
*/
namespace Apicp\Controller\News;
use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Common\NewsHelper;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\User;
use Common\Service\ArticleService;
use Common\Service\RightService;
use Common\Service\ReadService;
class ExportReadListController extends \Apicp\Controller\AbstractController
{
/**
* ExportReadList
* @desc 导出阅读列表
* @param Int article_id:true 新闻ID
* @param Int read_type:true:1 阅读类型(1=未读,2=已读)
* @param String username 姓名
* @param Array dp_ids 部门ID
* @param Array job_ids 职位ID
* @param Array role_ids 角色ID
* @return mixed
*/
public function Index_post()
{
// 验证规则
$rules = [
'article_id' => 'require|integer',
'read_type' => 'require|integer',
'username' => 'max:64',
'dp_ids' => 'array',
'job_ids' => 'array',
'role_ids' => 'array',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 取新闻信息
$articleServ = new ArticleService();
$article = $articleServ->get($postData['article_id']);
if (empty($article)) {
E('_ERR_ARTICLE_NOT_FOUND');
}
// UC筛选uid
$uidCond = $this->formatConds($postData);
// 获取人员ID
$newsHelper = &NewsHelper::instance();
list(, $uids_study, $uids_unstudy) = $newsHelper->getReadData($article['article_id'], $uidCond);
// Excel列名
$columns = ['姓名', '组织', '岗位', '角色', '手机号'];
// 实例化对象
$userServ = &User::instance();
$rows = [];
if ($postData['read_type'] == Constant::READ_STATUS_IS_YES) {
$titleSuffix = '_已学人员';
$columns[] = '阅读时间';
// 已读人员列表
$readConds = [
'uid' => $uids_study,
'article_id' => $postData['article_id'],
];
$readServ = new ReadService();
$readList = $readServ->list_by_conds($readConds);
// 人员详情列表
$userList = $userServ->listByUid(array_column($readList, 'uid'));
$userList = array_combine_by_key($userList, 'memUid');
foreach ($readList as $item) {
$uid = $item['uid'];
$user = $userList[$uid];
// Excel行数据
$rows[] = [
$user['memUsername'],
$newsHelper->getDpPath($user),
$user['memJob'],
$user['memRole'],
$user['memMobile'],
rgmdate($item['created'], 'Y/m/d H:i'),
];
}
} else {
$titleSuffix = '_未学人员';
if ($uids_unstudy) {
$userServ = &User::instance();
$userList = $userServ->listUsersAll(['memUids' => $uids_unstudy]);
if ($userList) {
foreach ($userList as $user) {
$rows[] = [
$user['memUsername'],
$newsHelper->getDpPath($user),
$user['memJob'],
$user['memRole'],
$user['memMobile'],
];
}
}
}
}
$title = $article['title'] . $titleSuffix . rgmdate(MILLI_TIME, '_ymdHis');
$filename = ExportDownload::get_down_dir($this->_login->user['eaId']) . $title . '.xls';
$result = PythonExcel::instance()->write($filename, $columns, $rows);
if ($result === true) {
// 写入数据到下载中心
$conditon = [
'title' => $title,
'ea_id' => $this->_login->user['eaId'],
'type' => ExportDownload::EXCEL_TYPE,
'size' => filesize($filename),
'username' => $this->_login->user['eaRealname'],
'url' => $filename,
'app_dir' => APP_DIR
];
ExportDownload::insert_down_load($conditon);
}
return true;
}
/**
* 将搜索条件转化为用户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)) {
$rightServ = new RightService();
$rights = $rightServ->formatPostData($right);
$unitUids = $rightServ->getUidsByRight($rights);
if (!empty($condUids)) {
// 筛选已经有值,取交集
$condUids = array_intersect($condUids, $unitUids);
} else {
$condUids = $unitUids;
}
}
// 如果有查询 并且 结果为空
if ((!empty($right) || !empty($postData['username'])) && empty($condUids)) {
return false;
}
return $condUids;
}
}